title: Error in code during gcc optimization
body: <p>I wrote some code in coding coding competition. I use gcc-4.6.3 Linaro.
  When i use command:</p>
  
  <pre><code> g++ main.cpp -x c++ -O2 -o binary
  </code></pre>
  
  <p>Output of program - "NO"</p>
  
  <pre><code>g++ main.cpp -x c++ -o binary
  </code></pre>
  
  <p>Without optimization - "YES"</p>
  
  <p>I test this code on online compilers: 
  1) <a href="http://ideone.com/lv8OZs" rel="nofollow">http://ideone.com/lv8OZs</a>
  2) <a href="http://coliru.stacked-crooked.com/" rel="nofollow">http://coliru.stacked-crooked.com/</a>
  These compilers are not too old, but situation not changed.
  On <a href="http://ideone.com/qqHbLO" rel="nofollow">http://ideone.com/qqHbLO</a> i uncomment one output-stream string - and
  after compilation with optimization result was changed to "YES".</p>
  
  <p>Is it a bug of gcc? May be i don't understand something about optimization.
  Can anybody explain me?</p>
  
  <p>Code /* it's useless example code */</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;math.h&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  
  using namespace std;
  int main() {
          int x1=0;
          int y1=0;
          int x2=1;
          int y2=1;
          int x3=0;
          int y3=1;
          int x4=1;
          int y4=0;
  
          std::vector&lt;double&gt; vec;
          vec.push_back(sqrt((double)((x2- x1)*(x2-x1) + (y2-y1)*(y2-y1))));
          vec.push_back(sqrt((double)((x2- x3)*(x2-x3) + (y2-y3)*(y2-y3))));
          vec.push_back(sqrt((double)((x4- x3)*(x4-x3) + (y4-y3)*(y4-y3))));
          vec.push_back(sqrt((double)((x4- x1)*(x4-x1) + (y4-y1)*(y4-y1))));
          vec.push_back(sqrt((double)((x4- x2)*(x4-x2) + (y4-y2)*(y4-y2))));
          vec.push_back(sqrt((double)((x3- x1)*(x3-x1) + (y3-y1)*(y3-y1))));
  
          std::sort(vec.begin(),vec.end());
  
  
          if(vec[0]==vec[3])
          {   
              std::cout&lt;&lt;"Test"&lt;&lt;std::endl;
              double sqrt1 = sqrt(vec[0]*vec[0]+vec[1]*vec[1]);
              double sqrt2 = sqrt(vec[2]*vec[2]+vec[3]*vec[3]);
              /*
              std::cout&lt;&lt;"vec[4] ="&lt;&lt;vec[4]&lt;&lt;std::endl;
              std::cout&lt;&lt;"sqrt1 = "&lt;&lt;sqrt1&lt;&lt;std::endl;
              */
              if(vec[4]==sqrt1 &amp;&amp; vec[5] == sqrt2)
              {   
                  std::cout&lt;&lt;"YES"&lt;&lt;std::endl;
                  return 0;
              }   
          }   
  
          std::cout&lt;&lt;"NO";
          return 0;
  }
  </code></pre>