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


x86 assembly (clang, -O0):
<bar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    119f <bar+0x4f>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe81(%rip),%rdi
call   1070 <FAIL@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e1 <bar+0x91>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe3f(%rip),%rdi
call   1070 <FAIL@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bar>:
push   %rax
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1181 <bar+0x31>
lea    0xeb5(%rip),%rsi
mov    %rax,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
je     1181 <bar+0x31>
pop    %rax
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe7e(%rip),%rdi
mov    %eax,%esi
call   1070 <FAIL@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [26 x i8] c"rpath-dlopen-indirect: %s\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [4 x i8] c"foo\00"
define void @bar() {
%1 = alloca i8*
%2 = alloca i8*
%3 = load i32, i32* @RTLD_LAZY
%4 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = call i32 (...) @dlerror()
%9 = call i32 @FAIL(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @EXIT_SUCCESS
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i8*, i8** %1
%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** %2
%15 = load i8*, i8** %2
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = call i32 (...) @dlerror()
%19 = call i32 @FAIL(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @EXIT_SUCCESS
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
ret void
}

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