Specialize branched loops

Continuation of PR #469 inside the main repo.
Please update CMake build as well.