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

    <tr>
        <th>Summary</th>
        <td>
            [BUG] Broken loops with malloc-dependent conditions
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    **Minimal reproducing code:**
```c
#include <stdio.h>
#include <stdlib.h>

int main() {
        for (int i = 0; i < 100; ++i) {
                printf("%i\n", i);
        }
        
        void *ret = malloc(1);
        
        while(ret);
        
        return 0;
}
```
### **Expected behaviour:**
Prints integers from 0-99 inclusive, then gets stuck in the loop (because the malloc returned a nonzero pointer, since it is impossibly unlikely that we couldn't allocate that one byte).
This is always what happens when compiling using `gcc`.

**Observed behaviour:**
With `-O0`, as expected
With `-O1`: prints integers starting at 0 but never stops. Disassembly of produced object file shows that there is no loop exit condition for the `for` loop.

Using `-opt-bisect-limit`, it seems that when the following is enabled:
`BISECT: running pass (51) LoopDeletionPass on Parallel Loop at depth 1 containing: %7<header><latch>`
the behaviour changes from as expected to printing 0-99 then segfaulting, and then enabling
`BISECT: running pass (58) SimplifyCFGPass on main`
causes the behaviour to change again to simply never stop printing.
I'm not sure how to enable 58 without 51.

**System:**
Manjaro Linux, `5.16.11-2-MANJARO` kernel
Output of `clang -v`:
```
clang version 13.0.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/10.2.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/11.2.0
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.2.0
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.2.0
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.2.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
```
Clang/LLVM installed via `pacman` from Manjaro repos
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9VsuO2zYU_Rp5Q1jQw_LYCy_8iIMUM52gk6TLgpJoixmKFEjKHvfrey5lTxw3SbtoCggcmrzPc8-9nNLUp0WULfE9SC1brpgVnTV1X0m9Z5WpRZQvB4Eo2UTJMpomw1edf2e51JXqa8GifO18LU3cRPmb79wqWV5fh1Vqz1oudZTNomzOorvV5Xa-M5bhmCQkLGxYEuWrsF2zNAk_omyFT95q4uss9HbBahZlhYyKtQ7bNSNxKH-RvttcaZ43ByNrmF9a4YNvoKNMBXvprfZlc2ykEhCAxvdEcNVbHfI4n726viD7Ct3wsQH-Ny-dqLyoWSkafpCmtzeVeU_pOoZF7IV1bGdNy5LxfM5CCZw8CErdN0KzvYCk8331jEs6YsqYjqAuRcV7J8LZkDAbQoZnzrTRfwprWGfIjSV7DtYFkygQfLedcU6W6sR6reSzwMY33LOjAJd6VQP_O8-CWe7FcGe0YOXJI7h5PGTyoSFbDnJHfnLsSFIN7zqh6QfCr0zbSUUMRVpYAdq-qrDG17wawHksnbCHH-D2u_QNWRg_BvCREndMnNG-FUlJJF-y7gZs57n1FApCTVjZe6bFQVicm87FbCMdd060hIzZsaHDEJMpP8MN24E2zDXm6AZIAL4VhIA2Q2HECwCujK6ll0YzagsqEILBFmuQ-ir5jxdgxqbz41I6-Bkr2Up_ThIGnRDt2WOAlUzuDIpzJF24F5qXCiDkX5p_9e7pzfoDQWB7rUmuQ2bEnILagt0jkI1QguJ8TzcI9z23KLlQ4ZIQqkUHQFPKyKPxYYUMokfv0NiN4DWohRmRrxX3VRgXl66gEF8LyaqG6704U_2qasyboUIUX2iBwHon9jveKzoNZdb1cB7SpMN_kWQYUU8gupK703r79pJjGGCXKEMLOfZ1sIhpiJfxPaeuM-gd2DldUeU16n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