https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81410
--- Comment #3 from Sjors Gielen <gcc at sjor dot sg> --- A trivial workaround is put inside the first loop: std::cout << ""; I suppose this prevents loop optimization by introducing potential side effects.