#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 RTLD_LAZY;
int RTLD_LOCAL;
char *dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
void stub1();
int main(int argc, char **argv) {
  void *h, *s;
  if (argc < 2) {
    fprintf(stderr, "usage: loader <module>\n");
    exit(1);
  }
  h = dlopen(argv[1], RTLD_LAZY | RTLD_LOCAL);
  if (h == NULL) {
    fprintf(stderr, "%s\n", dlerror());
    exit(1);
  }
  fprintf(stderr, "Opened %s\n", argv[1]);
  s = dlsym(h, "the_symbol");
  if (s == NULL) {
    fprintf(stderr, "Couldn't find the_symbol\n");
    exit(1);
  }
  fprintf(stderr, "Found the_symbol %p\n", s);
  fprintf(stderr, "Calling the_symbol\n");
  ((void (*)(void))s)();
  fprintf(stderr, "Called the_symbol\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1191 <main+0x41>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f0 <main+0xa0>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    124f <main+0xff>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
call   *-0x20(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    1204 <main+0xb4>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e6a(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1040 <dlopen@plt>
mov    0x2e5b(%rip),%r15
mov    (%r15),%ebp
test   %rax,%rax
je     1216 <main+0xc6>
mov    %rax,%rbx
mov    0x8(%r14),%rdx
lea    0xe79(%rip),%rsi
mov    %ebp,%edi
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe74(%rip),%rsi
mov    %rbx,%rdi
call   1060 <dlsym@plt>
mov    (%r15),%edi
test   %rax,%rax
je     123a <main+0xea>
mov    %rax,%rbx
lea    0xe7f(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%edi
lea    0xe80(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
call   *%rbx
mov    (%r15),%edi
lea    0xe81(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
jmp    1241 <main+0xf1>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xdfb(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xded(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"usage: loader <module>\0A\00"
@RTLD_LAZY = global i32 0
@RTLD_LOCAL = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [11 x i8] c"Opened %s\0A\00"
@.str.3 = [11 x i8] c"the_symbol\00"
@.str.4 = [26 x i8] c"Couldn't find the_symbol\0A\00"
@.str.5 = [21 x i8] c"Found the_symbol %p\0A\00"
@.str.6 = [20 x i8] c"Calling the_symbol\0A\00"
@.str.7 = [19 x i8] c"Called the_symbol\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @RTLD_LAZY
%19 = load i32, i32* @RTLD_LOCAL
%20 = or i32 %18, %19
%21 = call i8* @dlopen(i8* %17, i32 %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %14
%25 = load i32, i32* @stderr
%26 = call i8* (...) @dlerror()
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %14
%30 = load i32, i32* @stderr
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = load i8*, i8** %6
%36 = call i8* @dlsym(i8* %35, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %43
39: ; preds = %29
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %29
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %7
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %45)
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%49 = load i8*, i8** %7
%50 = bitcast i8* %49 to void ()*
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"usage: loader <module>\0A\00"
@RTLD_LAZY = global i32 0
@RTLD_LOCAL = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [11 x i8] c"Opened %s\0A\00"
@.str.3 = [11 x i8] c"the_symbol\00"
@.str.4 = [26 x i8] c"Couldn't find the_symbol\0A\00"
@.str.5 = [21 x i8] c"Found the_symbol %p\0A\00"
@.str.6 = [20 x i8] c"Calling the_symbol\0A\00"
@.str.7 = [19 x i8] c"Called the_symbol\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @RTLD_LAZY
%12 = load i32, i32* @RTLD_LOCAL
%13 = or i32 %12, %11
%14 = tail call i8* @dlopen(i8* %10, i32 %13)
%15 = icmp eq i8* %14, null
%16 = load i32, i32* @stderr
br i1 %15, label %17, label %21
17: ; preds = %8
%18 = tail call i8* (...) @dlerror()
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %8
%22 = load i8*, i8** %9
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = tail call i8* @dlsym(i8* %14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i8* %24, null
%26 = load i32, i32* @stderr
br i1 %25, label %27, label %30
27: ; preds = %21
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %24)
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%34 = bitcast i8* %24 to void ()*
tail call void %34()
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}