I needed to occupy my mind with a puzzle, so I constructed this perl script last week: https://ja.osdn.net/users/reiisi/pastebin/5866 Comoletely brain-dead and probably missing something important. I haven't been able to properly test whether the output code is functional. It's definitely not optimal. But it's there if anyone is curious.