#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_NOW;
int /*<<< orphan*/ bar();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ printf(char *);
int main(void) {
  void *libfoo = dlopen("libfoo.dylib", RTLD_NOW);
  if (libfoo != NULL) {
    printf("main: Found libfoo!\n");
    void *foo = dlsym(libfoo, "foo");
    if (foo != NULL) {
      printf("main: Found foo()!\n");
      void (*foo_fcn)() = foo;
      foo_fcn();
    }
  } else {
    printf("main: Couldn't find libfoo. :(\n");
  }
  printf("main: pre-bar!\n");
  bar();
  printf("main: post-bar!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11bc <main+0x7c>
lea    0xe93(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe98(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b7 <main+0x77>
lea    0xe81(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   *-0x20(%rbp)
jmp    11c8 <main+0x88>
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
lea    0xe8b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <bar@plt>
lea    0xe88(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     1193 <main+0x53>
mov    %rax,%rbx
lea    0xeea(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9c(%rip),%rsi
mov    %rbx,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     119f <main+0x5f>
mov    %rax,%rbx
lea    0xedb(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   *%rbx
jmp    119f <main+0x5f>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <bar@plt>
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_NOW = global i32 0
@.str.1 = [21 x i8] c"main: Found libfoo!\0A\00"
@.str.2 = [4 x i8] c"foo\00"
@.str.3 = [20 x i8] c"main: Found foo()!\0A\00"
@.str.4 = [32 x i8] c"main: Couldn't find libfoo. :(\0A\00"
@.str.5 = [16 x i8] c"main: pre-bar!\0A\00"
@.str.6 = [17 x i8] c"main: post-bar!\0A\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_NOW
%6 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %21
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%11 = load i8*, i8** %2
%12 = call i8* @dlsym(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i8* %12, i8** %3
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %9
%16 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%17 = load i8*, i8** %3
%18 = bitcast i8* %17 to void (...)*
store void (...)* %18, void (...)** %4
%19 = load void (...)*, void (...)** %4
br label %20
20: ; preds = %15, %9
br label %23
21: ; preds = %0
%22 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %23
23: ; preds = %21, %20
%24 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%25 = call i32 (...) @bar()
%26 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"libfoo.dylib\00"
@RTLD_NOW = global i32 0
@.str.2 = [4 x i8] c"foo\00"
@str = [31 x i8] c"main: Couldn't find libfoo. :(\00"
@str.7 = [15 x i8] c"main: pre-bar!\00"
@str.8 = [16 x i8] c"main: post-bar!\00"
@str.9 = [20 x i8] c"main: Found libfoo!\00"
@str.10 = [19 x i8] c"main: Found foo()!\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_NOW
%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 %11, label %4
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
%6 = tail call i8* @dlsym(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
%10 = bitcast i8* %6 to void (...)*
tail call void (...) %10()
br label %13
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %4, %8, %11
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.7, i64 0, i64 0))
%15 = tail call i32 (...) @bar()
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
ret i32 0
}