<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60860>60860</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Wasm] clang produces wrong code at -O targeting wasm32-unknown-emscripten
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Air111
</td>
</tr>
</table>
<pre>
Hi,
I found emcc produce wrong code on this test case:
```c
#include <stdio.h>
#include <stdint.h>
union {
uint64_t c;
int32_t a;
} d;
int32_t a = 0;
int32_t e = 0;
uint64_t *f = &d.c;
static int32_t *g = &d.a;
int main() {
for (; e <= 0; e++) {
*f = 0x3b47c02bffe;
for (; a; a++) {
*g = 2;
}
}
printf("%llx\n", d.c);
}
```
I reported this testcase to emscripten a few days ago(emscripten-core/emscripten#18767), and I think it may be clang bug.
**Version**
```shell
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.31 (e88336121cfe6da4a96c88e46f314552f07dfed0)
clang version 17.0.0 (https://github.com/llvm/llvm-project 1142e6c7c795de7f80774325a07ed49bc95a48c9)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/x/emsdk/upstream/bin
$ /home/x/emsdk/upstream/bin/clang -v
clang version 17.0.0 (https://github.com/llvm/llvm-project 1142e6c7c795de7f80774325a07ed49bc95a48c9)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/x/emsdk/upstream/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.3.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
$ /home/x/emsdk/upstream/bin/wasm-ld -v
LLD 17.0.0
```
**Failing command line in full:**
```shell
$ /home/x/emsdk/upstream/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -DEMSCRIPTEN -Werror=implicit-function-declaration --sysroot=/home/x/emsdk/upstream/emscripten/cache/sysroot -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -Os small.c -c -o small.o
$ /home/x/emsdk/upstream/bin/wasm-ld -o small.wasm small.o -L/home/x/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten /home/x/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/crt1.o -lGL -lal -lhtml5 -lstandalonewasm -lstubs -lc -ldlmalloc -lcompiler_rt -lc++-noexcept -lc++abi-noexcept -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --strip-debug --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export=stackSave --export=stackRestore --export=stackAlloc --export=__errno_location --export-table -z stack-size=65536 --initial-memory=16777216 --max-memory=16777216 --global-base=1024
$ wasmtime small.wasm
3b400000000
//only remove the "-Os" option
$ wasmtime small.wasm
3b47c02bffe
```
If this program contains undefined behaviour, I earnestly ask for your forgiveness.
Hope to get your reply.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEWF9z27gR_zTQyw40JECR0oMebCm680x617lce33TgMBSQgwCGgC0pfv0HZDUHycXO2mT1qORhd3lD_sPu1iKEPTOIi7J7J7M1hPRxb3zyzvt8zyf1E6dlj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