<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 --- - incorrect loop optimization at O2"
   href="http://llvm.org/bugs/show_bug.cgi?id=16602">16602</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>incorrect loop  optimization at O2
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.2
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>halayli@gmail.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>#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int x = 23;
int y = 29;

void* set_x(void* arg)
{
    (void)arg;
    x = 29;

    return NULL;
}

int main(void)
{

        pthread_t p1;

        pthread_create(&p1, NULL, set_x, NULL);
        pthread_detach(p1);
        while (x < y)
                if (x == y)
                        break;

        return 0;
}


The following code runs forever if compiled with -O2.

clang -Wall -Wextra test.c -pthread -o test && ./test

The assembly code generated causes in infinite loop jumping to LBB1_1.

.Ltmp8:
    .cfi_def_cfa_register %rbp
    subq    $16, %rsp
    leaq    -8(%rbp), %rdi
    xorl    %esi, %esi
    movl    $set_x, %edx
    xorl    %ecx, %ecx
    callq   pthread_create
    movq    -8(%rbp), %rdi
    callq   pthread_detach
    movl    x(%rip), %eax
    cmpl    y(%rip), %eax
    jge .LBB1_2
    .align  16, 0x90
.LBB1_1:                                # %while.cond
                                        # =>This Inner Loop Header: Depth=1
    jmp .LBB1_1</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>