<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/114185>114185</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Incorrect assembly code generated with -Os
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mushenoy
</td>
</tr>
</table>
<pre>
For the below sample source file - the assembly code generated is incorrect.
Sample.c:
```
struct sample {
struct sample *next;
char a;
};
char function (char);
void test(void);
void* test_function (const struct sample *c)
{
char type;
if (c) {
type = c->a;
test();
}
function(type);
return (void *)0;
}
```
Compilation command:
```
clang --target=aarch64-linux-gnu -save-temps -c sample.c -o sample.o -Os
```
In the assembly:
- No conditional branch is generated to check if `c` is non-null.
- test function call is done unconditionally
Assembly code:
```
test_function:
stp x29, x30, [sp, #-32]!
str x19, [sp, #16]
mov x29, sp
ldrb w19, [x0, #8]
bl test
mov w0, w19
bl function
mov x0, xzr
ldr x19, [sp, #16]
ldp x29, x30, [sp], #32
ret
```
x86_64:
```
test_function:
push rbx
movsx ebx, byte ptr [rdi + 8]
call test@PLT
mov edi, ebx
call function@PLT
xor eax, eax
pop rbx
ret
```
Observations:
- The issue is observed when -Os is used
- Code gets generated correctly when `type` variable is initialized with some value.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVV9v66gT_TTkZeQIY8dNHvzQNr9IV_ppu9Le9yuMpzG7GCzAqXM__QrsOHb_aaMKUzhzOHMYgDsnzxqxJLsnsjtueO8bY8u2dw1qc91Upr6WJ2PBNwgVKvMGjredQnCmtwLhVSqEBOI8dw7bSl1BmBrhjBot91iDdCC1MNai8FtCj4Q-ju1fkWorSDYNkIJOf_Ff520v_G1F8vA0DgMAvJtijxoHT7IFQjTcAp-HyMPx3o9tBLz2WnhpNBC2DwOEHWbYxcgaPDpP2D70l3N3BGGPEfRrRWW08x9FikAxyXkv1V87X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