<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/112187>112187</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Different Behavior in Floating Point Division by Zero
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
iamanonymouscs
</td>
</tr>
</table>
<pre>
I am writing to report a potential bug in the Clang compiler regarding the behavior of floating point division by zero. The issue arises when compiling the following C code with different optimization levels:
```c
#include <stdio.h>
int f (int i, int k)
{
if (k == 0)
printf ("k = 0\n");
return i/k;
}
int main (void)
{
return f (1, 0);
}
```
Clang version:
```sh
# clang -vUbuntu clang version 19.0.0 (++20240301064251+dd426fa5f931-1~exp1~20240301184412.1845)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6.4
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
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Candidate multilib: .;@m64
Selected multilib: .;@m64
```
When compiled with clang -O0 or gcc -O1, the program results in a floating point exception (core dumped). However, when compiled with clang -O1, -O2, -O3, or -Os, the program prints "k = 0" as expected.
This inconsistency in behavior across different optimization levels is concerning and may indicate a bug in the optimization process or the handling of floating point operations.
Here is the command line:
```sh
gcc bugreport_0_1.c -O0 ; ./a.out
```
k = 0
Floating point exception (core dumped)
https://godbolt.org/z/qE9E7eahz
```sh
clang bugreport_0_1.c -O0 ; ./a.out
```
k = 0
Floating point exception (core dumped)
https://godbolt.org/z/os956srn7
```sh
clang bugreport_0_1.c -O1 ; ./a.out
```
k = 0
https://godbolt.org/z/354nKzTG7
I would appreciate it if you could look into this issue and provide feedback on whether this is a known issue or if further investigation is needed.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVlFv2zYQ_jX0y8GCRMmW9eCH2K7aYg8ZsAwD9lJQ5FniQpEaSdlJHvrbB1K246Zp1wENMCCwpPC7jx_vjnfHnJOtRlyTxYYsdjM2-s7YtWQ900Y_9mZ03M0aIx7XH4H1cLTSS92CN2BxMNYDg8F41F4yBc3YgtTgO4StYroFbvpBKrRgsWVWRMsOocGOHaSxYPawV4ZFysFI7UHIg3TSaGge4QmtSeCuQ5DOjQjMSocOjh3qE_OZcG-UMsfwtQVuBMJR-g6E3O_RovZgBi97-cR8YFZ4QOVIfkPSHUlvyDKd_vjpm-ZSczUKBJJvnRfSJB3J351W429QugdCV-FFErqF8HJPaHVClZvpBWSE3QPJdyTfQXqBAAAMVmofAYTSiIGULLaaUBpw-ZkELPrR6rBTfX_5Nyl3LzX1TOpAdzBSvCLmzBO3zILs9HqfZ8KzS675p4ge0IbwfO09113cBzxC54ffm1H78fR5soSsStIknQ69IXRDU1qkeZqly4IuMkI3QhR0uWeLfZVn8-wzPgzZ5zMoWxVFRpNsVSwu57tjtkVP8ht4WC0_LYv5wOdK6vFh3urxBOksMgG9EagCcDBOPkxLH7XzTCkUO2nDEqH16CyhtZJN-FWHfp5VhNaN1JNFbUYtgDMtpGAe4f02hD-yxAR7haXlnND6JO9ZG62LpHgT0qR8C9rlm5C-hQve4PhlskjSn0-7mih_Q4Xco_gZp95ehPWj8jIY5TeQhHtepP2yeLHj90CvVoI_ngswiqnUnm78bQrGQss5zG9jfQnFebCmtawHi25U3oUOwV6WfXzgOMTqTOiKG4sgxn7AUMQS-GCOeEAb-I7f3jruN7-l0yMPD2Nhfute6ohV18FVyaUUmAN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