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

    <tr>
        <th>Summary</th>
        <td>
            [clang] Main Function Missing Due to Incorrect Infinite Loop Optimization
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang,
            new issue
      </td>
    </tr>

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

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

<pre>
    We are submitting this report to bring to your attention an issue that I have encountered while using clang compiler, specifically related to compiler optimization. While working with clang compiler, I have observed an error that seems to be related to optimization levels `O1`, `O2`, and `O3`. Although I had expected an infinite loop to occur when applying these optimization options, I have instead encountered an unexpected termination of the main function.

I have conducted extensive research and testing to pinpoint the root cause of this problem, but unfortunately, my efforts have not yielded a conclusive solution. Given the complexity of LLVM and its extensive code base, I believe that your expertise and resources can shed light on this issue and help clarify its underlying causes.

# Extended Description
### **Proof of Concept to trigger the bug**
```C
#include <stdint.h>

uint8_t uint8_sub(uint8_t ui1, uint8_t ui2) {
  return ui1 - ui2;
}

int main() {
    int32_t i;
    for(i = 0; i > (-22); i = uint8_sub(i, 1)) { }
    return 0;
}
```
While it was anticipated that an infinite loop would ensue when employing all optimization options, the principal function becomes non-existent, leading to anomalous termination across `O1`, `O2`, and `O3` optimization levels.
### Expected Result
```bash
# version
$ clang-18 --version
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 4706251a3186c34da0ee8fd894f7e6b095da8fdc)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/dongFiles/compiler_trunk/llvm-project/build/bin

# compile with O0 optimization
$ clang-18 target.c -o clang_O0 -O0 

# correct result: infinite loop
$ ./clang_O0 # Infinite loop 
^C
```
### Clang Result (incorrect)
- **clang x64 (incorrect)**
```bash
$ clang-18 --version
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 4706251a3186c34da0ee8fd894f7e6b095da8fdc)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/dongFiles/compiler_trunk/llvm-project/build/bin

$ clang-18 target.c -o clang_O1 -O1
$ clang-18 target.c -o clang_O2 -O2
$ clang-18 target.c -o clang_O3 -O3

$ ./clang_O1 
$ echo $?   # Abnormal termination
64
$ ./clang_O2 
$ echo $?   # Abnormal termination
64
$ ./clang_O3 
$ echo $?   # Abnormal termination
64
```
- **clang aarch64 (incorrect)**
```bash
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O0_aarch64 -O0
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O1_aarch64 -O1
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O2_aarch64 -O2
$ clang-18 --target=aarch64-linux-gnu target.c -o clang_O3_aarch64 -O3

$ ./clang_O1_aarch64 
$ echo $?    # Abnormal termination
1
$ ./clang_O2_aarch64 
$ echo $?    # Abnormal termination
1
$ ./clang_O3_aarch64 
$ echo $?    # Abnormal termination
1
```
- **clang x64 O0, O1, O2, O3 : https://godbolt.org/z/sKsh6Tdv1**
- **clang aarch64 O0, O1, O2, O3 : https://godbolt.org/z/PTG47q6v9**
### Comparison to GCC Result (correct)
```bash
$ gcc-trunk --version 
gcc-trunk (GCC) 14.0.0 20230913 (experimental)
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc-trunk target.c -o gcc_O0 -O0 
$ gcc-trunk target.c -o gcc_O1 -O1
$ gcc-trunk target.c -o gcc_O2 -O2
$ gcc-trunk target.c -o gcc_O3 -O3 
$ ./gcc_O0   # Infinite loop   
^C
$ ./gcc_O1   # Infinite loop   
^C
$ ./gcc_O2   # Infinite loop   
^C
$ ./gcc_O3 # Infinite loop
^C
```
- **gcc x64 O0, O1, O2, O3 : https://godbolt.org/z/5YM9r56EM**
- **gcc aarch64 O0, O1, O2, O3 : https://godbolt.org/z/TGdvGP9Ws**
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWFtz4joS_jXKSxeULYNxHnhgIGRTOxlSmZydOk8p2W6wdoTklWQI59dvtWzCJUzO3PZtqygucvfXre6vWy2Ec3KlEcds-IENZ1ei8ZWx41Ve6lUe86vclLvxFwRhEVyTr6X3Uq_AV9KBxdpYD95AbsOigZ1pLAjvUXtpNAgN0rkGwVfCwx1UYoOAujCN9mixhG0lFULjSL1Qgt7NupYKLeNTcDUWcikLodQOLCrhsSQrexkwtZdr-ZcgY334EsC2xn4luK301QXMzgmTO7QbLMlFtNbY1kWHuHZhR3hs8NgOKNygcsDSaBGzNCJQ-s6770KX4XfC0qgPE-Ur06yqYLcEfKmx8K1ZqZdSS4-gjKmDlaJoLGwr1CDqWu3aQKPDU_v0w2h3tBmpnUdCP4qs0NDoV3Me7VrqTn9JqLAWUsOy0UUIHotmLJq07x1qYXTZBG188aid3FBMHApbVGGXHp3v8l5LXRupfUC2xngoREOOL1uq1NbkCtfkc954aPTSWN9o4VHtaHG9A1zSmmtta-NhJ1GVtBPypFBNcMAZ1bTZvpUb1MEepVfhi_Q7svfx47_ug3vSuyPPC1Mi5MJhG7cclcRNR8zAWoqV9dJhULboTGMLdFAIDa7CEpRcVR6MbnfU8ppEK1Q1Ec3K5S4YbXSJtk1fiII7iS7jCdyQW7S3GbrCypDQ16ftCxifMD55sMYsaVtTowusQ7V5K1crtGHvebNqBTv1NGpf01c4SbErEVgydb6U2vcrltwce9RI7bNnD-2na3LGs8NaTAE7_OSMXwMbfWhVASz6xmqSg154nHSP2Gh2bITIQZRjPDtDAJDaJ_zZg3xVptWlsYxnElgyg4glH4C-3gDjWY-TF_ul2YnjktyN6XFrBV79IMzO2-iCl_vItT_bXiI9bIUDob0sZN12AyLMm-rdmkZR_REnQgHjulYmUEAo9c36pQzWVmoCV6_FCDkWZo0OtNE9fJGO2imJKxRlV3BCm7VQpnEnpS0Ka9z3taZLPa1_zsGbff94RN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