This code here runs fine on -O but fails to exit on -O2 and -Os.
#include <iostream>
int main() {
int ctr = 2000000000;
while (ctr++ > 0) {
if (ctr % 100000000 == 0) {
std::cout << ctr << '\n';
}
}
return 0;
}
I know that it has something to do with integer overflow, but I thought that was defined behavior. In case it may be relevant, I'm compiling on a Linux virtual machine on a Windows 64-bit computer.
EDIT: Integer overflow is not defined behavior. So then what optimization or combination of optimizations causes the problem? The question is: "Why does the code work fine on -O but fail on -O2 and -Os?"