#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int rand();
int return_res_noleak(void) {
  int err = 0;
  uint8_t *foo_p = (uint8_t *)malloc(sizeof(uint8_t));
  if (foo_p == NULL) {
    err = 1;
    goto failure;
  };
  int bar = rand();
  if (bar == 0) {
    err = 2;
    goto failure;
  };
  int baz = rand();
  if (baz == 0) {
    err = 3;
    goto failure;
  };
failure:
  if (foo_p != NULL)
    free(foo_p);
  return err;
}


x86 assembly (clang, -O0):
<return_res_noleak>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <return_res_noleak+0x34>
movl   $0x1,-0x4(%rbp)
jmp    11a9 <return_res_noleak+0x79>
mov    $0x0,%al
call   1050 <rand@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1184 <return_res_noleak+0x54>
movl   $0x2,-0x4(%rbp)
jmp    11a9 <return_res_noleak+0x79>
mov    $0x0,%al
call   1050 <rand@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11a4 <return_res_noleak+0x74>
movl   $0x3,-0x4(%rbp)
jmp    11a9 <return_res_noleak+0x79>
jmp    11a9 <return_res_noleak+0x79>
cmpq   $0x0,-0x10(%rbp)
je     11bd <return_res_noleak+0x8d>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<return_res_noleak>:
push   %rbp
push   %rbx
push   %rax
mov    $0x4,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1163 <return_res_noleak+0x33>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <rand@plt>
test   %eax,%eax
je     116a <return_res_noleak+0x3a>
xor    %eax,%eax
call   1050 <rand@plt>
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
lea    (%rcx,%rcx,2),%ebp
jmp    116f <return_res_noleak+0x3f>
mov    $0x1,%ebp
jmp    1177 <return_res_noleak+0x47>
mov    $0x2,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @return_res_noleak() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 @malloc(i32 4)
%6 = inttoptr i64 %5 to i32*
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 1, i32* %1
br label %21
10: ; preds = %0
%11 = call i32 (...) @rand()
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 2, i32* %1
br label %21
15: ; preds = %10
%16 = call i32 (...) @rand()
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 3, i32* %1
br label %21
20: ; preds = %15
br label %21
21: ; preds = %20, %19, %14, %9
%22 = load i32*, i32** %2
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32*, i32** %2
%26 = call i32 @free(i32* %25)
br label %27
27: ; preds = %24, %21
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
define i32 @return_res_noleak() {
%1 = tail call i64 @malloc(i32 4)
%2 = inttoptr i64 %1 to i32*
%3 = icmp eq i64 %1, 0
br i1 %3, label %14, label %4
4: ; preds = %0
%5 = tail call i32 (...) @rand()
%6 = icmp eq i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %4
%8 = tail call i32 (...) @rand()
%9 = icmp eq i32 %8, 0
%10 = select i1 %9, i32 3, i32 0
br label %11
11: ; preds = %7, %4
%12 = phi i32 [ %10, %7 ], [ 2, %4 ]
%13 = tail call i32 @free(i32* %2)
br label %14
14: ; preds = %0, %11
%15 = phi i32 [ %12, %11 ], [ 1, %0 ]
ret i32 %15
}