Embedded program paste to IDE has problems.

For some reason I have to declare global statics which can change both in interrupts and are read locally as "volatile". This does not highlight as a keyword, but adding it everywhere fixed the problem. Now I have to try narrowing down which are really needed.

I also needed to not call init() in arduino, so I added it to the c program.

Theoretically it is the same version of the compiler with the same optimization so I don't understand the lack of volatility.

Update:

To simplify things, you can use a minimal sketch, but take any embedded set of code but rename main to void init() and the first thing the Arduino code will do is jump to your routine - Usually "main" never returns, so in that case it would work.

I haven't found anything more about the "volatile" problem, but it is good practice to declare anything modified by multiple threads (or the equivalent in this case) as "volatile", at least if part of the logic is that they would change. In one case it works without the declaration and saves the interrupt from having to reread the variable many times.