How does the generated quantities block iterate over sample draws?

sounds like an infinite loop - I recommend adding “print” statements within your loops to check this.

I’m not sure I understand what you’re asking here - wasn’t this already answered in the previous topic Specifying the number of samples for rng - see last comment?

how Stan generates the generated quantities

see the Stan langauge manual section “Program Blocks”