#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_NOW;
int /*<<< orphan*/ dlclose(void *);
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int fib(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ shared_hello();
void stub1();
void stub2();
void stub3();
void stub4();
int main() {
  void *freston;
  void *girlfriend;
  shared_hello();
  printf("%d %d %d %d %d %d\n", fib(0), fib(1), fib(2), fib(3), fib(4), fib(5));
  printf("\nloading girlfriend.so ...\n");
  freston = dlopen("/boot/lib/girlfriend.so", RTLD_NOW);
  girlfriend = dlsym(freston, "girlfriend");
  ((void (*)(void))girlfriend)();
  dlclose(freston);
  printf("unloaded girlfriend.so ...\n");
  printf("\nloading girlfriend.so ...\n");
  freston = dlopen("/boot/lib/girlfriend.so", RTLD_NOW);
  freston = dlopen("/boot/lib/girlfriend.so", RTLD_NOW);
  girlfriend = dlsym(freston, "girlfriend");
  ((void (*)(void))girlfriend)();
  dlclose(freston);
  ((void (*)(void))girlfriend)();
  dlclose(freston);
  printf("unloaded girlfriend.so ...\n");
  printf("\ncalling girlfriend again (must page fault)...\n");
  ((void (*)(void))girlfriend)();
  printf("If you are reading this... something went really wrong\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <shared_hello@plt>
xor    %edi,%edi
call   1060 <fib@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x1,%edi
call   1060 <fib@plt>
mov    %eax,-0x28(%rbp)
mov    $0x2,%edi
call   1060 <fib@plt>
mov    %eax,-0x24(%rbp)
mov    $0x3,%edi
call   1060 <fib@plt>
mov    %eax,-0x20(%rbp)
mov    $0x4,%edi
call   1060 <fib@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x5,%edi
call   1060 <fib@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
lea    0xe2a(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x18(%rbp)
call   *-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
lea    0xddc(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
lea    0xdc3(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdc7(%rip),%rsi
call   1070 <dlsym@plt>
mov    %rax,-0x18(%rbp)
call   *-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
call   *-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
call   *-0x18(%rbp)
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <shared_hello@plt>
xor    %edi,%edi
call   1070 <fib@plt>
mov    %eax,%r14d
mov    $0x1,%edi
call   1070 <fib@plt>
mov    %eax,%r15d
mov    $0x2,%edi
call   1070 <fib@plt>
mov    %eax,%r12d
mov    $0x3,%edi
call   1070 <fib@plt>
mov    %eax,%ebp
mov    $0x4,%edi
call   1070 <fib@plt>
mov    %eax,%ebx
mov    $0x5,%edi
call   1070 <fib@plt>
mov    %eax,(%rsp)
lea    0xe2e(%rip),%rdi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %r12d,%ecx
mov    %ebp,%r8d
mov    %ebx,%r9d
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe47(%rip),%r12
mov    %r12,%rdi
call   1040 <puts@plt>
mov    0x2dea(%rip),%r13
mov    0x0(%r13),%esi
lea    0xe0a(%rip),%rbx
mov    %rbx,%rdi
call   1060 <dlopen@plt>
mov    %rax,%rbp
lea    0xe10(%rip),%r15
mov    %rax,%rdi
mov    %r15,%rsi
call   1080 <dlsym@plt>
call   *%rax
mov    %rbp,%rdi
call   1090 <dlclose@plt>
lea    0xe1a(%rip),%r14
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
call   1040 <puts@plt>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1060 <dlopen@plt>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1060 <dlopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1080 <dlsym@plt>
mov    %rax,%rbp
call   *%rax
mov    %rbx,%rdi
call   1090 <dlclose@plt>
call   *%rbp
mov    %rbx,%rdi
call   1090 <dlclose@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
lea    0xdd9(%rip),%rdi
call   1040 <puts@plt>
call   *%rbp
lea    0xdfa(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"%d %d %d %d %d %d\0A\00"
@.str.1 = [28 x i8] c"\0Aloading girlfriend.so ...\0A\00"
@.str.2 = [24 x i8] c"/boot/lib/girlfriend.so\00"
@RTLD_NOW = global i32 0
@.str.3 = [11 x i8] c"girlfriend\00"
@.str.4 = [28 x i8] c"unloaded girlfriend.so ...\0A\00"
@.str.5 = [48 x i8] c"\0Acalling girlfriend again (must page fault)...\0A\00"
@.str.6 = [56 x i8] c"If you are reading this... something went really wrong\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i32 (...) @shared_hello()
%5 = call i32 @fib(i32 0)
%6 = call i32 @fib(i32 1)
%7 = call i32 @fib(i32 2)
%8 = call i32 @fib(i32 3)
%9 = call i32 @fib(i32 4)
%10 = call i32 @fib(i32 5)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6, i32 %7, i32 %8, i32 %9, i32 %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @RTLD_NOW
%14 = call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %13)
store i8* %14, i8** %2
%15 = load i8*, i8** %2
%16 = call i8* @dlsym(i8* %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = bitcast i8* %17 to void ()*
%19 = load i8*, i8** %2
%20 = call i32 @dlclose(i8* %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @RTLD_NOW
%24 = call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i8* %24, i8** %2
%25 = load i32, i32* @RTLD_NOW
%26 = call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %25)
store i8* %26, i8** %2
%27 = load i8*, i8** %2
%28 = call i8* @dlsym(i8* %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i8* %28, i8** %3
%29 = load i8*, i8** %3
%30 = bitcast i8* %29 to void ()*
%31 = load i8*, i8** %2
%32 = call i32 @dlclose(i8* %31)
%33 = load i8*, i8** %3
%34 = bitcast i8* %33 to void ()*
%35 = load i8*, i8** %2
%36 = call i32 @dlclose(i8* %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
%39 = load i8*, i8** %3
%40 = bitcast i8* %39 to void ()*
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"%d %d %d %d %d %d\0A\00"
@.str.2 = [24 x i8] c"/boot/lib/girlfriend.so\00"
@RTLD_NOW = global i32 0
@.str.3 = [11 x i8] c"girlfriend\00"
@str.8 = [27 x i8] c"\0Aloading girlfriend.so ...\00"
@str.9 = [27 x i8] c"unloaded girlfriend.so ...\00"
@str.10 = [47 x i8] c"\0Acalling girlfriend again (must page fault)...\00"
@str.11 = [55 x i8] c"If you are reading this... something went really wrong\00"
define i32 @main() {
%1 = tail call i32 (...) @shared_hello()
%2 = tail call i32 @fib(i32 0)
%3 = tail call i32 @fib(i32 1)
%4 = tail call i32 @fib(i32 2)
%5 = tail call i32 @fib(i32 3)
%6 = tail call i32 @fib(i32 4)
%7 = tail call i32 @fib(i32 5)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3, i32 %4, i32 %5, i32 %6, i32 %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
%10 = load i32, i32* @RTLD_NOW
%11 = tail call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = tail call i8* @dlsym(i8* %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%13 = bitcast i8* %12 to void ()*
tail call void %13()
%14 = tail call i32 @dlclose(i8* %11)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.9, i64 0, i64 0))
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
%17 = load i32, i32* @RTLD_NOW
%18 = tail call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = load i32, i32* @RTLD_NOW
%20 = tail call i8* @dlopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = tail call i8* @dlsym(i8* %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%22 = bitcast i8* %21 to void ()*
tail call void %22()
%23 = tail call i32 @dlclose(i8* %20)
tail call void %22()
%24 = tail call i32 @dlclose(i8* %20)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.9, i64 0, i64 0))
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.10, i64 0, i64 0))
tail call void %22()
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.11, i64 0, i64 0))
ret i32 0
}