Specialize branched loops

Continuation of PR #469 inside the main repo.
How about if (!stmt.defined())? That's the pattern we use elsewhere, and it's slightly cleaner (no redundant state).