#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_GLOBAL;
int RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int);
void *dlsym(void *, char *);
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "USAGE:\t%s <soname>\n", argv[0]);
    fprintf(stderr, "\tTest a shared library\n");
    return 1;
  };
  void *lib = dlopen(argv[1], RTLD_GLOBAL | RTLD_LAZY);
  if (lib == NULL) {
    fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1], dlerror());
    return 1;
  };
  while (1) {
    char linebuf[1024];
    printf("symbol: ");
    char *line = fgets(linebuf, 1024, stdin);
    if (line == NULL)
      break;
    char *newline = strchr(line, '\n');
    if (newline != NULL)
      *newline = 0;
    void *sym = dlsym(lib, line);
    printf("Symbol %s is at %p\n", line, sym);
  };
  dlclose(lib);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e4 <main+0x64>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1335 <main+0x1b5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2dcc(%rip),%rax
or     (%rax),%esi
call   1070 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1276 <main+0xf6>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x448(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x440(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x44c(%rbp),%edi
mov    -0x448(%rbp),%rdx
mov    -0x440(%rbp),%rcx
mov    %rax,%r8
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1335 <main+0x1b5>
jmp    127b <main+0xfb>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x420(%rbp),%rdi
mov    0x2d51(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    12bd <main+0x13d>
jmp    1325 <main+0x1a5>
mov    -0x428(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
je     12ed <main+0x16d>
mov    -0x430(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1090 <dlsym@plt>
mov    %rax,-0x438(%rbp)
mov    -0x428(%rbp),%rsi
mov    -0x438(%rbp),%rdx
lea    0xd28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    127b <main+0xfb>
mov    -0x18(%rbp),%rdi
call   10a0 <dlclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
jne    1264 <main+0xe4>
mov    0x8(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    0x2e19(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <dlopen@plt>
test   %rax,%rax
je     129d <main+0x11d>
mov    %rax,%rbx
lea    0xe6d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e0f(%rip),%r14
mov    (%r14),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     1291 <main+0x111>
mov    %rax,%rbp
lea    0xe45(%rip),%r15
lea    0xe35(%rip),%r12
mov    %rsp,%r13
jmp    124d <main+0xcd>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <dlsym@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r14),%edx
mov    %r13,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1291 <main+0x111>
mov    %rbp,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1210 <main+0x90>
movb   $0x0,(%rax)
jmp    1210 <main+0x90>
mov    0x2d65(%rip),%rbx
mov    (%rbx),%edi
mov    0x0(%rbp),%rdx
lea    0xd88(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd8c(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12ce <main+0x14e>
mov    %rbx,%rdi
call   10a0 <dlclose@plt>
xor    %eax,%eax
jmp    12d3 <main+0x153>
mov    0x2d2c(%rip),%rax
mov    (%rax),%ebx
mov    0x0(%rbp),%r14
mov    0x8(%rbp),%rbp
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xd70(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rbp,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"USAGE:\09%s <soname>\0A\00"
@.str.1 = [24 x i8] c"\09Test a shared library\0A\00"
@RTLD_GLOBAL = global i32 0
@RTLD_LAZY = global i32 0
@.str.2 = [12 x i8] c"%s: %s: %s\0A\00"
@.str.3 = [9 x i8] c"symbol: \00"
@stdin = global i32 0
@.str.4 = [20 x i8] c"Symbol %s is at %p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %67
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @RTLD_GLOBAL
%26 = load i32, i32* @RTLD_LAZY
%27 = or i32 %25, %26
%28 = call i8* @dlopen(i8* %24, i32 %27)
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %41
31: ; preds = %21
%32 = load i32, i32* @stderr
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i8* (...) @dlerror()
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %35, i8* %38, i8* %39)
store i32 1, i32* %3
br label %67
41: ; preds = %21
br label %42
42: ; preds = %41, %57
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%45 = load i32, i32* @stdin
%46 = call i8* @fgets(i8* %44, i32 1024, i32 %45)
store i8* %46, i8** %8
%47 = load i8*, i8** %8
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %50
49: ; preds = %42
br label %64
50: ; preds = %42
%51 = load i8*, i8** %8
%52 = call i8* @strchr(i8* %51, i8 signext 10)
store i8* %52, i8** %9
%53 = load i8*, i8** %9
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = load i8*, i8** %9
store i8 0, i8* %56
br label %57
57: ; preds = %55, %50
%58 = load i8*, i8** %6
%59 = load i8*, i8** %8
%60 = call i8* @dlsym(i8* %58, i8* %59)
store i8* %60, i8** %10
%61 = load i8*, i8** %8
%62 = load i8*, i8** %10
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %61, i8* %62)
br label %42
64: ; preds = %49
%65 = load i8*, i8** %6
%66 = call i32 @dlclose(i8* %65)
store i32 0, i32* %3
br label %67
67: ; preds = %64, %31, %13
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"USAGE:\09%s <soname>\0A\00"
@.str.1 = [24 x i8] c"\09Test a shared library\0A\00"
@RTLD_GLOBAL = global i32 0
@RTLD_LAZY = global i32 0
@.str.2 = [12 x i8] c"%s: %s: %s\0A\00"
@.str.3 = [9 x i8] c"symbol: \00"
@stdin = global i32 0
@.str.4 = [20 x i8] c"Symbol %s is at %p\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = icmp eq i32 %0, 2
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %45
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @RTLD_GLOBAL
%15 = load i32, i32* @RTLD_LAZY
%16 = or i32 %15, %14
%17 = tail call i8* @dlopen(i8* %13, i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %25, label %19
19: ; preds = %11
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* @stdin
%23 = call i8* @fgets(i8* %20, i32 1024, i32 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %43, label %31
25: ; preds = %11
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %1
%28 = load i8*, i8** %12
%29 = tail call i8* (...) @dlerror()
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %27, i8* %28, i8* %29)
br label %45
31: ; preds = %19, %36
%32 = phi i8* [ %41, %36 ], [ %23, %19 ]
%33 = call i8* @strchr(i8* %32, i8 signext 10)
%34 = icmp eq i8* %33, null
br i1 %34, label %36, label %35
35: ; preds = %31
store i8 0, i8* %33
br label %36
36: ; preds = %31, %35
%37 = call i8* @dlsym(i8* %17, i8* %32)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %32, i8* %37)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%40 = load i32, i32* @stdin
%41 = call i8* @fgets(i8* %20, i32 1024, i32 %40)
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %31
43: ; preds = %36, %19
%44 = call i32 @dlclose(i8* %17)
br label %45
45: ; preds = %25, %43, %5
%46 = phi i32 [ 1, %5 ], [ 1, %25 ], [ 0, %43 ]
ret i32 %46
}