#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 system(char *);
int main() {
  for (int i = 0; i < 100; ++i) {
    void *handle = dlopen("libfoo.dylib", RTLD_LAZY);
    if (handle != NULL)
      dlclose(handle);
  }
  char cmd[512];
  int result = system(cmd);
  if (result == EXIT_SUCCESS)
    PASS("rpath-dlopen-leak");
  else
    FAIL("rpath-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    11b3 <main+0x63>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a0 <main+0x50>
mov    -0x10(%rbp),%rdi
call   1060 <dlclose@plt>
jmp    11a5 <main+0x55>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1169 <main+0x19>
lea    -0x210(%rbp),%rdi
call   1030 <system@plt>
mov    %eax,-0x214(%rbp)
mov    -0x214(%rbp),%eax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%eax
jne    11eb <main+0x9b>
lea    0xe2c(%rip),%rdi
call   1050 <PASS@plt>
jmp    11f7 <main+0xa7>
lea    0xe1b(%rip),%rdi
call   1070 <FAIL@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    $0x64,%ebp
mov    0x2e71(%rip),%r14
lea    0xe92(%rip),%rbx
jmp    1175 <main+0x25>
add    $0xffffffff,%ebp
je     118f <main+0x3f>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1170 <main+0x20>
mov    %rax,%rdi
call   1060 <dlclose@plt>
jmp    1170 <main+0x20>
mov    %rsp,%rdi
call   1030 <system@plt>
mov    0x2e42(%rip),%rbx
cmp    (%rbx),%eax
jne    11b0 <main+0x60>
lea    0xe64(%rip),%rdi
call   1050 <PASS@plt>
jmp    11bc <main+0x6c>
lea    0xe56(%rip),%rdi
call   1070 <FAIL@plt>
mov    (%rbx),%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [18 x i8] c"rpath-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 = %18, %0
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 100
br i1 %8, label %9, label %21
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
br label %18
18: ; preds = %17
%19 = load i32, i32* %2
%20 = add nsw i32 %19, 1
store i32 %20, i32* %2
br label %6
21: ; preds = %6
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%23 = call i32 @system(i8* %22)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = load i32, i32* @EXIT_SUCCESS
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @PASS(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %31
29: ; preds = %21
%30 = call i32 @FAIL(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %27
%32 = load i32, i32* @EXIT_SUCCESS
ret i32 %32
}

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