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


x86 assembly (clang, -O0):
<bar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a5 <bar+0x75>
lea    0xeaa(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a0 <bar+0x70>
lea    0xe9a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   *-0x18(%rbp)
jmp    11b1 <bar+0x81>
lea    0xe92(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bar>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
lea    0xebf(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1185 <bar+0x55>
mov    %rax,%rbx
lea    0xedd(%rip),%rdi
call   1030 <puts@plt>
lea    0xeac(%rip),%rsi
mov    %rbx,%rdi
call   1050 <dlsym@plt>
test   %rax,%rax
je     1192 <bar+0x62>
lea    0xed3(%rip),%rdi
mov    %rax,%rbx
call   1030 <puts@plt>
xor    %eax,%eax
mov    %rbx,%rcx
pop    %rbx
jmp    *%rcx
lea    0xe85(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>
pop    %rbx
ret

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

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 = [33 x i8] c"libbar: Couldn't find libfoo. :(\00"
@str.5 = [22 x i8] c"libbar: Found libfoo!\00"
@str.6 = [21 x i8] c"libbar: Found foo()!\00"
define void @bar() {
%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 ([22 x i8], [22 x i8]* @str.5, 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 ([21 x i8], [21 x i8]* @str.6, 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 ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %4, %8, %11
ret void
}