<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/89112>89112</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang -O0 miscompilation on aarch64-darwin
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
TerrorJack
</td>
</tr>
</table>
<pre>
Reproducer:
```c
#include <stdio.h>
#include <stdint.h>
#include <stdatomic.h>
#include <stdbool.h>
int main (void) {
_Atomic uint32_t x = 42;
uint32_t y = 42;
bool success = atomic_compare_exchange_strong(&x, &y, 43);
printf("# CAS\n");
printf("success=%u\n", (int) success);
printf("old=%u\n", y);
printf("x=%u\n", x);
printf("# Swap\n");
y = atomic_exchange(&x, 2);
printf("x=%u\n", x);
printf("y=%u\n", y);
printf("# Fetch-Add\n");
y = atomic_fetch_add(&x, 2);
printf("x=%u\n", x);
printf("y=%u\n", y);
fflush(stdout);
}
```
The correct runtime output should be:
```
# CAS
success=1
old=42
x=43
# Swap
x=2
y=43
# Fetch-Add
x=4
y=2
```
On a macos-14 m1 machine, using `cc` from apple sdk compiles the given reproducer fine. However, when using `clang` compiled from `llvmorg-18.1.4`, the runtime output of `clang -O0 test.c -o test` is:
```
# CAS
success=1
old=42
x=536870912
# Swap
x=536870912
y=43
# Fetch-Add
x=536870912
y=2
```
Problematic `clang` version:
```
clang version 18.1.4 (git@github.com:llvm/llvm-project.git e6c3289804a67ea0bb6a86fadbe454dd93b8d855)
Target: arm64-apple-darwin23.4.0
Thread model: posix
InstalledDir: /Users/terrorjack/workspace/llvm-project/build/bin
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVl2P4jYU_TXm5YoosZ0PHniYHYravmzV3T4jx75JvJPYyHZm4N9XThhgmYVupUqVUBJzj4_PPdfXifBetwZxTfJPJN8sxBg669Zf0TnrfhfyZVFbdVz_iXtn1SjRE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