#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 */

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ FAIL(char *);
int /*<<< orphan*/ PASS(char *);
int /*<<< orphan*/ dlopen_preflight(char *);
int getpid();
int /*<<< orphan*/ sprintf(char *, char *, int);
int system(char *);
int main() {
  for (int i = 0; i < 100; ++i) {
    dlopen_preflight("libfoo.dylib");
  }
  char cmd[512];
  sprintf(cmd, "sudo leaks %u > /dev/null\n", getpid());
  int result = system(cmd);
  if (result == EXIT_SUCCESS)
    PASS("dlopen_preflight-leak");
  else
    FAIL("dlopen_preflight-leak");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x64,-0x8(%rbp)
jge    119d <main+0x3d>
lea    0xe76(%rip),%rdi
call   1040 <dlopen_preflight@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1179 <main+0x19>
lea    -0x210(%rbp),%rax
mov    %rax,-0x220(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x220(%rbp),%rdi
mov    %eax,%edx
lea    0xe4b(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x210(%rbp),%rdi
call   1050 <system@plt>
mov    %eax,-0x214(%rbp)
mov    -0x214(%rbp),%eax
mov    0x2dfa(%rip),%rcx
cmp    (%rcx),%eax
jne    11ff <main+0x9f>
lea    0xe33(%rip),%rdi
call   1060 <PASS@plt>
jmp    120b <main+0xab>
lea    0xe22(%rip),%rdi
call   1080 <FAIL@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
mov    $0x64,%ebp
lea    0xe8b(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1040 <dlopen_preflight@plt>
add    $0xffffffff,%ebp
jne    1180 <main+0x20>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe72(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
mov    0x2e29(%rip),%rbx
cmp    (%rbx),%eax
jne    11c9 <main+0x69>
lea    0xe66(%rip),%rdi
call   1060 <PASS@plt>
jmp    11d5 <main+0x75>
lea    0xe58(%rip),%rdi
call   1080 <FAIL@plt>
mov    (%rbx),%eax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"libfoo.dylib\00"
@.str.1 = [27 x i8] c"sudo leaks %u > /dev/null\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [22 x i8] c"dlopen_preflight-leak\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [512 x i8]
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
br label %5
5: ; preds = %10, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 100
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = call i32 @dlopen_preflight(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8
%11 = load i32, i32* %2
%12 = add nsw i32 %11, 1
store i32 %12, i32* %2
br label %5
13: ; preds = %5
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%15 = call i32 (...) @getpid()
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%18 = call i32 @system(i8* %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = load i32, i32* @EXIT_SUCCESS
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = call i32 @PASS(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %26
24: ; preds = %13
%25 = call i32 @FAIL(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %22
%27 = load i32, i32* @EXIT_SUCCESS
ret i32 %27
}

LLVM IR (-O2):
@.str = [13 x i8] c"libfoo.dylib\00"
@.str.1 = [27 x i8] c"sudo leaks %u > /dev/null\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [22 x i8] c"dlopen_preflight-leak\00"
define i32 @main() {
%1 = alloca [512 x i8]
br label %9
2: ; preds = %9
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%4 = tail call i32 (...) @getpid()
%5 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = call i32 @system(i8* %3)
%7 = load i32, i32* @EXIT_SUCCESS
%8 = icmp eq i32 %6, %7
br i1 %8, label %14, label %16
9: ; preds = %0, %9
%10 = phi i32 [ 0, %0 ], [ %12, %9 ]
%11 = tail call i32 @dlopen_preflight(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%12 = add nuw nsw i32 %10, 1
%13 = icmp eq i32 %12, 100
br i1 %13, label %2, label %9
14: ; preds = %2
%15 = tail call i32 @PASS(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %18
16: ; preds = %2
%17 = tail call i32 @FAIL(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
%19 = load i32, i32* @EXIT_SUCCESS
ret i32 %19
}