#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;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char **argv) {
  void *Handle = dlopen(argv[1], RTLD_NOW);
  int (*Foo)(void);
  if (Handle == NULL) {
    printf("dlopen() failed: %s\n", dlerror());
    return 1;
  }
  Foo = (int (*)(void))dlsym(Handle, "foo");
  if (Handle == NULL) {
    printf("dlsym() failed: %s\n", dlerror());
    return 1;
  }
  return Foo();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119d <main+0x5d>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e4 <main+0xa4>
mov    -0x18(%rbp),%rdi
lea    0xe6d(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11de <main+0x9e>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e4 <main+0xa4>
call   *-0x20(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x8(%rsi),%rdi
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
call   1050 <dlopen@plt>
test   %rax,%rax
je     116a <main+0x2a>
lea    0xeb6(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
pop    %rcx
jmp    *%rax
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe88(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
@RTLD_NOW = global i32 0
@.str = [21 x i8] c"dlopen() failed: %s\0A\00"
@.str.1 = [4 x i8] c"foo\00"
@.str.2 = [20 x i8] c"dlsym() failed: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32 ()*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @RTLD_NOW
%12 = call i8* @dlopen(i8* %10, i32 %11)
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i8* (...) @dlerror()
%17 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 1, i32* %3
br label %30
18: ; preds = %2
%19 = load i8*, i8** %6
%20 = call i64 @dlsym(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%21 = inttoptr i64 %20 to i32 ()*
store i32 ()* %21, i32 ()** %7
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i8* (...) @dlerror()
%26 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %25)
store i32 1, i32* %3
br label %30
27: ; preds = %18
%28 = load i32 ()*, i32 ()** %7
%29 = call i32 %28()
store i32 %29, i32* %3
br label %30
30: ; preds = %27, %24, %15
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@RTLD_NOW = global i32 0
@.str = [21 x i8] c"dlopen() failed: %s\0A\00"
@.str.1 = [4 x i8] c"foo\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i32, i32* @RTLD_NOW
%6 = tail call i8* @dlopen(i8* %4, i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i8* (...) @dlerror()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %15
11: ; preds = %2
%12 = tail call i64 @dlsym(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%13 = inttoptr i64 %12 to i32 ()*
%14 = tail call i32 %13()
br label %15
15: ; preds = %11, %8
%16 = phi i32 [ 1, %8 ], [ %14, %11 ]
ret i32 %16
}