Since this is obviously a pain to do statically or by hand in a dynamic fashion, I would suggest trying another approach.
I personally would try to do it by writing a small emulator.
Since it is mostly 'stack arithmetic' and junk code, this would be perfect. Take a look at the emulation framework named 'Unicorn'.
With this option, you don't have to re-...