title: Why does this loop produce "warning: iteration 3u invokes undefined behavior" and output more than 4 lines?
body: <p>Compiling this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  {
      for (int i = 0; i &lt; 4; ++i)
          std::cout &lt;&lt; i*1000000000 &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>and <code>gcc</code> produces the following warning:</p>
  
  <pre><code>warning: iteration 3u invokes undefined behavior [-Waggressive-loop-optimizations]
     std::cout &lt;&lt; i*1000000000 &lt;&lt; std::endl;
                    ^
  </code></pre>
  
  <p>I understand there is a signed integer overflow.</p>
  
  <p>What I cannot get is why <code>i</code> value is broken by that overflow operation?</p>
  
  <p>I've read the answers to <a href="https://stackoverflow.com/questions/7682477/why-does-integer-overflow-on-x86-with-gcc-cause-an-infinite-loop">Why does integer overflow on x86 with GCC cause an infinite loop?</a>, but I'm still not clear on <em>why</em> this happens - I get that "undefined" means "anything can happen", but what's the underlying cause of <em>this specific behavior</em>?</p>
  
  <p>Online: <a href="http://ideone.com/dMrRKR" rel="noreferrer">http://ideone.com/dMrRKR</a></p>
  
  <p>Compiler: <code>gcc (4.8)</code></p>