title: Why does this code behave differently if optimizing (-O2, -O3) is used?
body: <p>I had to code some checking routines and they appear to behave differently if one uses -O0, -O1, -O2 or -O3.</p>
  <p>Below I created a minimal example that works fine for -O0 and -O1. But using -O2 or -O3 the behavior changed. In the -O0 and -O1 case, the for-loop increments the integer and the first time it gets to the maximum, the overflow happens and the check routine triggers. In the other case the for-loop never breaks, although the integer gets negative.</p>
  <h1>Code</h1>
  <pre><code>#include &lt;iostream&gt;
  
  inline bool check(const int i) {
    if (i &lt; 0)
      return false;
    else
      return true;
  }
  
  int main() {
    for (int i = 0;; i += 50000000) {
      std::cout &lt;&lt; i &lt;&lt; std::endl;
      const bool succ = check(i);
      if (succ == false) {
        std::cout &lt;&lt; &quot;Overflow: &quot; &lt;&lt; i &lt;&lt; std::endl;
        break;
      }
    }
    return 0;
  }
  </code></pre>
  <p>Why is the compiler allowed to optimize this away?</p>
  <p>Trying with gcc, clang and icc, only the icc does it correct in all optimization variants the other two did not.</p>