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

    <tr>
        <th>Summary</th>
        <td>
            Missing optimization: @malloc call not conditionally placed inside if branch
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          0xdeafbeef
      </td>
    </tr>
</table>

<pre>
    [Alive2 proof](https://alive2.llvm.org/ce/z/c3yvcs)

## Description
The LLVM optimizer does not move the `malloc` call into the conditional branch for a function that conditionally allocates memory based on an input parameter. This optimization is expected to reduce unnecessary allocations when the condition is not met.

## Steps to Reproduce
Consider the following C function `alloc_escape`:

```c
char* alloc_escape(int alloc) {
 char* alloced = malloc(10);
    if (alloc) {
        return alloced;
    } else {
        free(alloced);
        return NULL;
    }
}
```
or more opaque rust version:
```rust
pub fn alloc_escape(alloc: bool) -> Option<Vec<u8>> {
    let alloced = vec![0; 10];
    if alloc {
        Some(alloced)
    } else {
 None
    }
}
```

## Expected Behavior

The memory allocation should be moved inside the conditional branch to ensure that it only occurs when necessary, based on the condition.
## Actual Behavior

The memory allocation is not moved into the conditional branch, leading to a pair of unnecessary allocations and deallocations.

Llvm version -  17.0.6



</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU2PozgQ_TXOpdSRMQGSA4d8dE6ZWWlndq4rYxfBK2OztklP5tePDKQTMh_StFoEqsrPVc_1ytx7dTaIJcl2JDsseB8a60r6VSKvK8R6UVl5jd6tVhdk0Dlra5IdCFs3IXSepFvCjoQd-eBfan1pl9adCTsKJOz4Lb6k14vwhG0IPRC6nZ4sJSyFA3rhVBeUNaP9c4NwOn35ALYLqlXf0IG06MHYAK29IIQGgeS05VpbQXIKgmsNygQ7uIQ1UkU4rqFy3IgGauuAQ90bEe0QGh4ew_QVBiwe0EOLrXVXqLhH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