<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/86685>86685</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Clang-18 Miscompilation] clang miscompiles loop with break
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          wierton
      </td>
    </tr>
</table>

<pre>
    Compiler Explorer: https://gcc.godbolt.org/z/3s1ezjzW4
I sanitized this program with ubsan,msan and asan, they reports nothing as shown in the CompilerExplorer, so I think this is likely a true miscompilation bug.

```
#include <stdio.h>
#include <stdlib.h>

void foo(int *s, float *f, long n)
{
  for (long i = 0; i < n; ++i) {
 *f = 2;
    if (i == 1) break;
    *s = 4;
  }
}

int main(void)
{
  union {int s; float f;} u = {0};
  foo(&u.s, &u.f, 2);
  if (u.f != 2) abort ();
  return 0;
}
```

In Compiler Explorer, it is observed that the program compiled with `clang -O0` outputs `2`, whereas the same program compiled with `clang -O1` outputs `0`. Given that the third argument `n` of the function `foo` is 2, the expected execution trace within `foo`'s loop would be `*f = 2`, followed by `*s = 4`, and then `*f = 2` again. This sequence should  leave `u.f` with the value `2` instead of `0`. 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVMtu6zYQ_Rp6M4ggUbYkL7SI7eviLopuCnRNiSOJNxTp8mEn-fpiKNtxkhYoYFgk53XO8HCE92o0iC3b7NjmsBIxTNa1F4UuWLPqrHxr93Y-KY0OfryetHXoWPkMUwgnz8pnxo-MH8e-z0YrO6tDZt3I-PGd8WPpC3z_9f7XmuUHlj__BC-MCuodJYRJeTg5Ozoxw0WFCWLnhWF8P3thQBgJYtlDmPANHJ6sCx6MDZMyIwgPfrIXA8qQA9ww3iHyPXgLP6mQeVnKKQ9avaB-AwHBRYRZ-T7FiaCsgS6O2YL0-l_l19-y5aUyvY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