Fix issue with for loops in generators

Turns out that we generated invalid state machine for certain combinations of for loops with empty headers.

This expands on the tests to make sure we actually run the generator a few times.

Fixes #802, #331