#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 */
typedef void (*BarProc)();

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ FAIL(char *, int /*<<< orphan*/);
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
void stub1();
int main() {
  void *h1 = dlopen("./libstuff_better.dylib", RTLD_LAZY);
  if (h1 == NULL) {
    FAIL("rpath-install-name: %s", dlerror());
    return EXIT_SUCCESS;
  }
  void *handle = dlopen("libbar.dylib", RTLD_LAZY);
  if (handle == NULL) {
    FAIL("rpath-install-name: %s", dlerror());
    return EXIT_SUCCESS;
  }
  typedef void (*BarProc)(void);
  BarProc pBar = (BarProc)dlsym(handle, "bar");
  if (pBar == NULL) {
    FAIL("rpath-install-name: %s", dlerror());
    return EXIT_SUCCESS;
  }
  (*pBar)();
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1199 <main+0x59>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe95(%rip),%rdi
call   1060 <FAIL@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <main+0xf7>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e3 <main+0xa3>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe4b(%rip),%rdi
call   1060 <FAIL@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <main+0xf7>
mov    -0x18(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1228 <main+0xe8>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%esi
lea    0xe06(%rip),%rdi
call   1060 <FAIL@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <main+0xf7>
call   *-0x20(%rbp)
mov    0x2dae(%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   %rbx
mov    0x2e90(%rip),%rbx
mov    (%rbx),%esi
lea    0xeaf(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1186 <main+0x46>
mov    (%rbx),%esi
lea    0xecb(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1186 <main+0x46>
lea    0xec7(%rip),%rsi
mov    %rax,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
je     1186 <main+0x46>
call   *%rax
jmp    119b <main+0x5b>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe84(%rip),%rdi
mov    %eax,%esi
call   1060 <FAIL@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"./libstuff_better.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [23 x i8] c"rpath-install-name: %s\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [13 x i8] c"libbar.dylib\00"
@.str.3 = [4 x i8] c"bar\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca void ()*
store i32 0, i32* %1
%5 = load i32, i32* @RTLD_LAZY
%6 = call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %5)
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 (...) @dlerror()
%11 = call i32 @FAIL(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @EXIT_SUCCESS
store i32 %12, i32* %1
br label %35
13: ; preds = %0
%14 = load i32, i32* @RTLD_LAZY
%15 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %14)
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = call i32 (...) @dlerror()
%20 = call i32 @FAIL(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* @EXIT_SUCCESS
store i32 %21, i32* %1
br label %35
22: ; preds = %13
%23 = load i8*, i8** %3
%24 = call i64 @dlsym(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%25 = inttoptr i64 %24 to void ()*
store void ()* %25, void ()** %4
%26 = load void ()*, void ()** %4
%27 = icmp eq void ()* %26, null
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = call i32 (...) @dlerror()
%30 = call i32 @FAIL(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @EXIT_SUCCESS
store i32 %31, i32* %1
br label %35
32: ; preds = %22
%33 = load void ()*, void ()** %4
%34 = load i32, i32* @EXIT_SUCCESS
store i32 %34, i32* %1
br label %35
35: ; preds = %32, %28, %18, %9
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [24 x i8] c"./libstuff_better.dylib\00"
@RTLD_LAZY = global i32 0
@.str.1 = [23 x i8] c"rpath-install-name: %s\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [13 x i8] c"libbar.dylib\00"
@.str.3 = [4 x i8] c"bar\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 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 ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %22
7: ; preds = %0
%8 = load i32, i32* @RTLD_LAZY
%9 = tail call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = tail call i32 (...) @dlerror()
%13 = tail call i32 @FAIL(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %22
14: ; preds = %7
%15 = tail call i64 @dlsym(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 (...) @dlerror()
%19 = tail call i32 @FAIL(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %22
20: ; preds = %14
%21 = inttoptr i64 %15 to void ()*
tail call void %21()
br label %22
22: ; preds = %11, %20, %17, %4
%23 = load i32, i32* @EXIT_SUCCESS
ret i32 %23
}