Specialize branched loops

Continuation of PR #469 inside the main repo.
I have a small hunch that simplifying the max expression here might save the simplifier from doing it redundantly. I'm not exactly sure how many times though.