<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Clang optimizer leads to wrong if-clause result"
   href="https://bugs.llvm.org/show_bug.cgi?id=46710">46710</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang optimizer leads to wrong if-clause result
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>6.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kianoosh.abbasi76@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following code:

#include <iostream>

int main()
{
    int a; // Enter 2000000000 (2*10^9)
    int b; // Enter 1000000000 (10^9)
    std::cin >> a >> b;
    if (a + b >= a)
        std::cout << "Yes" << std::endl;
    else
        std::cout << "No" << std::endl;
}

If you assign 2000000000 and 1000000000 to `a` and `b` from the input
(respectively), `a + b` will overflow and become a negative integer, which will
obviously be less than `a` itself, so if the code is run without any
optimization flags, the result will be `No`.

However, when run with an optimization flags (e.g.: `-O2`), the optimizer
simplifies the `a + b >= a` inequality into `b >= 0` (by deducting `a` from
both sides). Therefore, without any checks for the possible overflow, the
condition will be evaluated to true and the output will be `Yes`.

The code and the results can be fount at <a href="https://rextester.com/OBZ78859">https://rextester.com/OBZ78859</a>


The problem is, both GCC and VC++ compilers take care of the possible overflows
and will output `No`, so it causes a major discrepancy between different C++
compilers whose reason might be very tough to figure out. It also makes the
output different by adding an optimization flag on the same Clang compiler. All
of these may drive programmers' attention to undefined behaviors and they may
spend a lot of time looking at their codes trying to find out any of those,
which actually is not an undefined behavior.

You could find the GCC and VC++ equivalents at <a href="https://rextester.com/CZZY67858">https://rextester.com/CZZY67858</a>
and <a href="https://rextester.com/SJDB96931">https://rextester.com/SJDB96931</a>, respectively. 

Please note that on all of the above links, the default compiler arguments
include the `-O2` flag, but you could see the different by removing the flag
for the Clang code.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>