#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*/);
int /*<<< orphan*/ PASS(char *);
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int main() {
  void *handle = dlopen("libfoo.dylib", RTLD_LAZY);
  if (handle == NULL) {
    FAIL("rpath-dlopen: %s", dlerror());
    return EXIT_SUCCESS;
  }
  void *pFoo = dlsym(handle, "foo");
  if (pFoo == NULL) {
    FAIL("rpath-dlopen: %s", dlerror());
    return EXIT_SUCCESS;
  }
  PASS("rpath-dlopen");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a9 <main+0x59>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe7a(%rip),%rdi
call   1070 <FAIL@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <main+0xb6>
mov    -0x10(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ee <main+0x9e>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe35(%rip),%rdi
call   1070 <FAIL@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <main+0xb6>
lea    0xe2d(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     118d <main+0x3d>
lea    0xeac(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     118d <main+0x3d>
lea    0xe9c(%rip),%rdi
call   1050 <PASS@plt>
jmp    11a2 <main+0x52>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe72(%rip),%rdi
mov    %eax,%esi
call   1070 <FAIL@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [17 x i8] c"rpath-dlopen: %s\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [13 x i8] c"rpath-dlopen\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = load i32, i32* @RTLD_LAZY
%5 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 (...) @dlerror()
%10 = call i32 @FAIL(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @EXIT_SUCCESS
store i32 %11, i32* %1
br label %24
12: ; preds = %0
%13 = load i8*, i8** %2
%14 = call i8* @dlsym(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = call i32 (...) @dlerror()
%19 = call i32 @FAIL(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @EXIT_SUCCESS
store i32 %20, i32* %1
br label %24
21: ; preds = %12
%22 = call i32 @PASS(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @EXIT_SUCCESS
store i32 %23, i32* %1
br label %24
24: ; preds = %21, %17, %8
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [17 x i8] c"rpath-dlopen: %s\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [13 x i8] c"rpath-dlopen\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 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 i32 (...) @dlerror()
%6 = tail call i32 @FAIL(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %15
7: ; preds = %0
%8 = tail call i8* @dlsym(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i32 (...) @dlerror()
%12 = tail call i32 @FAIL(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %15
13: ; preds = %7
%14 = tail call i32 @PASS(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %10, %13, %4
%16 = load i32, i32* @EXIT_SUCCESS
ret i32 %16
}