#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 */
scalar_t__ RTLD_GLOBAL;
scalar_t__ RTLD_LAZY;
char *dlerror();
void *dlopen(char *, scalar_t__);
int /*<<< orphan*/ donothing(char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, char *);
int LoadDll(char *name) {
  void *handle;
  donothing("loading: %s\n", name);
  if ((handle = dlopen(name, RTLD_LAZY + RTLD_GLOBAL)) == NULL) {
    printf("LoadDll: failed to load lib %s\n", name);
    printf(" - dlerror: %s\n", dlerror());
    exit(-1);
  }
  return handle != NULL;
}


x86 assembly (clang, -O0):
<LoadDll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe99(%rip),%rdi
call   1060 <donothing@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%rsi
mov    0x2e67(%rip),%rax
add    (%rax),%rsi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11c7 <LoadDll+0x77>
mov    -0x8(%rbp),%rsi
lea    0xe6b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe75(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LoadDll>:
push   %rbx
mov    %rdi,%rbx
lea    0xea5(%rip),%rdi
mov    %rbx,%rsi
call   1060 <donothing@plt>
mov    0x2e6e(%rip),%rax
mov    0x2e77(%rip),%rcx
mov    (%rcx),%rsi
add    (%rax),%rsi
mov    %rbx,%rdi
call   1050 <dlopen@plt>
test   %rax,%rax
je     118b <LoadDll+0x3b>
mov    $0x1,%eax
pop    %rbx
ret
lea    0xe7b(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe85(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"loading: %s\0A\00"
@RTLD_LAZY = global i64 0
@RTLD_GLOBAL = global i64 0
@.str.1 = [32 x i8] c"LoadDll: failed to load lib %s\0A\00"
@.str.2 = [16 x i8] c" - dlerror: %s\0A\00"
define i32 @LoadDll(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @donothing(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = load i8*, i8** %2
%7 = load i64, i64* @RTLD_LAZY
%8 = load i64, i64* @RTLD_GLOBAL
%9 = add nsw i64 %7, %8
%10 = call i8* @dlopen(i8* %6, i64 %9)
store i8* %10, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i8* (...) @dlerror()
%16 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = icmp ne i8* %19, null
%21 = zext i1 %20 to i32
ret i32 %21
}

LLVM IR (-O2):
@.str = [13 x i8] c"loading: %s\0A\00"
@RTLD_LAZY = global i64 0
@RTLD_GLOBAL = global i64 0
@.str.1 = [32 x i8] c"LoadDll: failed to load lib %s\0A\00"
@.str.2 = [16 x i8] c" - dlerror: %s\0A\00"
define i32 @LoadDll(i8* %0) {
%2 = tail call i32 @donothing(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = load i64, i64* @RTLD_LAZY
%4 = load i64, i64* @RTLD_GLOBAL
%5 = add nsw i64 %4, %3
%6 = tail call i8* @dlopen(i8* %0, i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%10 = tail call i8* (...) @dlerror()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %1
ret i32 1
}