#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*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
void *dlopen(char *, int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ sprintf(char *, char *, int);
int system(char *);
int main() {
  for (int i = 0; i < 100; ++i) {
    void *handle = dlopen("libfoo.dylib", RTLD_LAZY);
    if (handle != NULL)
      dlclose(handle);
    dlopen("libnotthere.dylib", RTLD_LAZY);
  }
  char cmd[512];
  sprintf(cmd, "sudo leaks %u > /dev/null\n", getpid());
  int result = system(cmd);
  if (result == EXIT_SUCCESS)
    PASS("dlopen-leak");
  else
    FAIL("dlopen-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    11e3 <main+0x73>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
lea    0xe5d(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c0 <main+0x50>
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0xe3d(%rip),%rdi
call   1050 <dlopen@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1189 <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    0xe17(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x210(%rbp),%rdi
call   1040 <system@plt>
mov    %eax,-0x214(%rbp)
mov    -0x214(%rbp),%eax
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%eax
jne    1245 <main+0xd5>
lea    0xdff(%rip),%rdi
call   1060 <PASS@plt>
jmp    1251 <main+0xe1>
lea    0xdee(%rip),%rdi
call   1090 <FAIL@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    $0x64,%ebp
mov    0x2e4f(%rip),%rbx
lea    0xe70(%rip),%r14
lea    0xe76(%rip),%r15
jmp    11af <main+0x3f>
nopl   0x0(%rax)
mov    (%rbx),%esi
mov    %r15,%rdi
call   1050 <dlopen@plt>
add    $0xffffffff,%ebp
je     11c8 <main+0x58>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     11a0 <main+0x30>
mov    %rax,%rdi
call   1080 <dlclose@plt>
jmp    11a0 <main+0x30>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe49(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
mov    0x2dee(%rip),%rbx
cmp    (%rbx),%eax
jne    1204 <main+0x94>
lea    0xe3d(%rip),%rdi
call   1060 <PASS@plt>
jmp    1210 <main+0xa0>
lea    0xe2f(%rip),%rdi
call   1090 <FAIL@plt>
mov    (%rbx),%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [18 x i8] c"libnotthere.dylib\00"
@.str.2 = [27 x i8] c"sudo leaks %u > /dev/null\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [12 x i8] c"dlopen-leak\00"
define i32 @main() {
%1 = alloca [512 x i8]
br label %9
2: ; preds = %16
%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.2, 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 %21, label %23
9: ; preds = %0, %16
%10 = phi i32 [ 0, %0 ], [ %19, %16 ]
%11 = load i32, i32* @RTLD_LAZY
%12 = tail call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %9
%15 = tail call i32 @dlclose(i8* %12)
br label %16
16: ; preds = %14, %9
%17 = load i32, i32* @RTLD_LAZY
%18 = tail call i8* @dlopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = add nuw nsw i32 %10, 1
%20 = icmp eq i32 %19, 100
br i1 %20, label %2, label %9
21: ; preds = %2
%22 = tail call i32 @PASS(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %25
23: ; preds = %2
%24 = tail call i32 @FAIL(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %21
%26 = load i32, i32* @EXIT_SUCCESS
ret i32 %26
}