<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </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 --- - Most negative integer greater than zero"
   href="http://llvm.org/bugs/show_bug.cgi?id=19757">19757</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Most negative integer greater than zero
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.4
          </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>akeep@cisco.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=12516" name="attach_12516" title="C program that demonstrates the bug when compiled with -O2">attachment 12516</a> <a href="attachment.cgi?id=12516&action=edit" title="C program that demonstrates the bug when compiled with -O2">[details]</a></span>
C program that demonstrates the bug when compiled with -O2

Clang reports that the most negative 32-bit integer (0x80000000) is a positive
integer when the -O2 flag is specified.  (The same problem occurs for the most
negative 64-bit number when compiling to a 64-bit target.)

Here is the example program that demonstrates the problem (also added as an
attachment):

#include <stdio.h>

unsigned int bogus(unsigned int b) {
  return b;
}

int main(int argc, char * argv[]) {
  unsigned int n = bogus(0x80000000);
  signed int final = -(signed int) n;

  if (final > 0) {
    printf("clang failed final (%d) > 0 where n is (%u)\n", final, n);
  } else {
    printf("clang worked! final (%d) < 0 where n is (%u)\n", final, n);
  }
}

(Note: bogus here is the identity function and is used simply to interfere with
constant folding that would normally make this go away---this is a simplified
demonstration of the bug from the actual case that led us to the problem.)

Compiled on Mac OS X Mavericks with the default Xcode clang:

% clang -m32 -Wextra -Werror -Wno-unused-parameter -O2 -msse2
-mmacosx-version-min=10.4 -o break_clang break_clang.c
% ./break_clang
clang failed final (-2147483648) > 0 where n is (2147483648)
% clang --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

The same problem exists with a 64-bit build:

% clang -m64 -Wextra -Werror -Wno-unused-parameter -O2 -msse2
-mmacosx-version-min=10.4 -o break_clang break_clang.c
% ./break_clang
clang failed final (-2147483648) > 0 where n is (2147483648)
% clang --version                                                              
                     Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM
3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

The program works correctly when not running at -O2:

% clang -m32 -Wextra -Werror -Wno-unused-parameter -msse2
-mmacosx-version-min=10.4 -o break_clang break_clang.c
% ./break_clang
clang worked! final (-2147483648) < 0 where n is (2147483648)</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>