#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_LAZY;
int /*<<< orphan*/ dlclose(void *);
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
void Load(char *name, int expect) {
  int val;
  void *handle;
  int (*sym)();
  handle = dlopen(name, RTLD_LAZY);
  if (handle == 0) {
    fprintf(stderr, "Load of %s failed\n", name);
    exit(1);
  }
  sym = (int (*)())dlsym(handle, "one");
  fprintf(stderr, "Address of sym is %p\n", sym);
  if (sym == 0) {
    fprintf(stderr, "Dlsym of %s failed\n", name);
    exit(1);
  }
  val = sym();
  if (val != expect)
    exit(1);
  dlclose(handle);
}


x86 assembly (clang, -O0):
<Load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1030 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a5 <Load+0x55>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1050 <dlsym@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1204 <Load+0xb4>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    $0x0,%al
call   *-0x20(%rbp)
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1222 <Load+0xd2>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1070 <dlclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Load>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
call   1030 <dlopen@plt>
test   %rax,%rax
je     11c0 <Load+0x70>
mov    %rax,%rbx
lea    0xe98(%rip),%rsi
mov    %rax,%rdi
call   1050 <dlsym@plt>
mov    %rax,%rbp
mov    0x2e53(%rip),%r12
mov    (%r12),%edi
lea    0xe7f(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %rbp,%rbp
je     11d2 <Load+0x82>
xor    %eax,%eax
call   *%rbp
cmp    %r15d,%eax
jne    11e7 <Load+0x97>
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <dlclose@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
jmp    11dd <Load+0x8d>
mov    (%r12),%edi
lea    0xe50(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"Load of %s failed\0A\00"
@.str.1 = [4 x i8] c"one\00"
@.str.2 = [22 x i8] c"Address of sym is %p\0A\00"
@.str.3 = [20 x i8] c"Dlsym of %s failed\0A\00"
define void @Load(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32 (...)*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = load i32, i32* @RTLD_LAZY
%10 = call i8* @dlopen(i8* %8, i32 %9)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
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 i32, i32* @stderr
%23 = load i32 (...)*, i32 (...)** %7
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 (...)* %23)
%25 = load i32 (...)*, i32 (...)** %7
%26 = icmp eq i32 (...)* %25, null
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %3
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = load i32 (...)*, i32 (...)** %7
%34 = call i32 (...) %33()
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = load i32, i32* %4
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %32
%41 = load i8*, i8** %6
%42 = call i32 @dlclose(i8* %41)
ret void
}

LLVM IR (-O2):
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"Load of %s failed\0A\00"
@.str.1 = [4 x i8] c"one\00"
@.str.2 = [22 x i8] c"Address of sym is %p\0A\00"
@.str.3 = [20 x i8] c"Dlsym of %s failed\0A\00"
define void @Load(i8* %0, i32 %1) {
%3 = load i32, i32* @RTLD_LAZY
%4 = tail call i8* @dlopen(i8* %0, i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i64 @dlsym(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = inttoptr i64 %11 to i32 (...)*
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 (...)* %12)
%15 = icmp eq i64 %11, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = tail call i32 (...) %12()
%22 = icmp eq i32 %21, %1
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %20
%26 = tail call i32 @dlclose(i8* %4)
ret void
}