G++ ignores comparison at -O2
<p>This code here runs fine on -O but fails to exit on -O2 and -Os. </p>

<pre><code>#include &lt;iostream&gt;

int main() {
    int ctr = 2000000000;
    while (ctr++ &gt; 0) {
        if (ctr % 100000000 == 0) {
             std::cout &lt;&lt; ctr &lt;&lt; '\n';
        }
    }
    return 0;
}
</code></pre>

<p>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.</p>

<p>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?"</p>