#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 /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
bool test_dlopen() {
  void *handle = dlopen("@rpath/libbar.dylib", RTLD_LAZY);
  if (handle == NULL) {
    printf("[FAIL] dlopen-rpath-from-dylib: dlopen(@rpath/libbar.dylib) "
           "failed: %s\n",
           dlerror());
    return false;
  }
  dlclose(handle);
  return true;
}


x86 assembly (clang, -O0):
<test_dlopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118e <test_dlopen+0x4e>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe97(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    119e <test_dlopen+0x5e>
mov    -0x10(%rbp),%rdi
call   1060 <dlclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_dlopen>:
push   %rbx
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     116a <test_dlopen+0x2a>
mov    %rax,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebx
jmp    1182 <test_dlopen+0x42>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe9a(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [20 x i8] c"@rpath/libbar.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [72 x i8] c"[FAIL] dlopen-rpath-from-dylib: dlopen(@rpath/libbar.dylib) failed: %s\0A\00"
define i32 @test_dlopen() {
%1 = alloca i32
%2 = alloca i8*
%3 = load i32, i32* @RTLD_LAZY
%4 = call i8* @dlopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %3)
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i8* (...) @dlerror()
%9 = call i32 @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i8* %8)
store i32 0, i32* %1
br label %13
10: ; preds = %0
%11 = load i8*, i8** %2
%12 = call i32 @dlclose(i8* %11)
store i32 1, i32* %1
br label %13
13: ; preds = %10, %7
%14 = load i32, i32* %1
ret i32 %14
}

LLVM IR (-O2):
@.str = [20 x i8] c"@rpath/libbar.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [72 x i8] c"[FAIL] dlopen-rpath-from-dylib: dlopen(@rpath/libbar.dylib) failed: %s\0A\00"
define i32 @test_dlopen() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i8* (...) @dlerror()
%6 = tail call i32 @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %9
7: ; preds = %0
%8 = tail call i32 @dlclose(i8* %2)
br label %9
9: ; preds = %7, %4
%10 = phi i32 [ 0, %4 ], [ 1, %7 ]
ret i32 %10
}