Specialize branched loops

Continuation of PR #469 inside the main repo.
This is a minor nit: I'd prefer to read this code that uses swap immediately after the branch_point call that sets swap. It doesn't look like 'swap' is used between here and there, it would make it easier to read IMO.