#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_NOW;
char *dlerror();
void *dlopen(char *, int);
int /*<<< orphan*/ printf(char *, char *, ...);
void *load(char *library_name) {
  void *libhandle;
  printf("driver: Calling dlopen on %s from driver\n", library_name);
  libhandle = dlopen(library_name, RTLD_NOW | RTLD_GLOBAL);
  if (libhandle == NULL)
    printf("driver: Failed to load %s: %s\n", library_name, dlerror());
  else
    printf("driver: loaded %s.  Jumping into %s's main\n", library_name,
           library_name);
  return libhandle;
}


x86 assembly (clang, -O0):
<load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    0x2e86(%rip),%rax
or     (%rax),%esi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a1 <load+0x71>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x18(%rbp),%rsi
mov    %rax,%rdx
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b7 <load+0x87>
mov    -0x8(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xec2(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e91(%rip),%rax
mov    0x2e92(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <dlopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     1179 <load+0x49>
lea    0xed8(%rip),%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
jmp    118d <load+0x5d>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xea3(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [42 x i8] c"driver: Calling dlopen on %s from driver\0A\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.1 = [31 x i8] c"driver: Failed to load %s: %s\0A\00"
@.str.2 = [44 x i8] c"driver: loaded %s. Jumping into %s's main\0A\00"
define i8* @load(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = load i8*, i8** %2
%7 = load i32, i32* @RTLD_NOW
%8 = load i32, i32* @RTLD_GLOBAL
%9 = or i32 %7, %8
%10 = call i8* @dlopen(i8* %6, i32 %9)
store i8* %10, i8** %3
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = call i8* (...) @dlerror()
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %15)
br label %21
17: ; preds = %1
%18 = load i8*, i8** %2
%19 = load i8*, i8** %2
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %18, i8* %19)
br label %21
21: ; preds = %17, %13
%22 = load i8*, i8** %3
ret i8* %22
}

LLVM IR (-O2):
@.str = [42 x i8] c"driver: Calling dlopen on %s from driver\0A\00"
@RTLD_NOW = global i32 0
@RTLD_GLOBAL = global i32 0
@.str.1 = [31 x i8] c"driver: Failed to load %s: %s\0A\00"
@.str.2 = [44 x i8] c"driver: loaded %s. Jumping into %s's main\0A\00"
define i8* @load(i8* %0) {
%2 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = load i32, i32* @RTLD_NOW
%4 = load i32, i32* @RTLD_GLOBAL
%5 = or i32 %4, %3
%6 = tail call i8* @dlopen(i8* %0, i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i8* (...) @dlerror()
%10 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %9)
br label %13
11: ; preds = %1
%12 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %0)
br label %13
13: ; preds = %11, %8
ret i8* %6
}