#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 /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int main(int argc, char *argv[]) {
  void *dl, *dl2;
  void *fn, *fn2;
  void *(*fninit)(int);
  void *(*fncall)();
  int (*addfn)(int, ...);
  int res;
  dl = dlopen("./vfnbank.so", RTLD_NOW);
  if (dl == NULL) {
    perror("dl NULL");
    return -1;
  }
  fn = dlsym(dl, "vfn0001");
  if (fn == NULL) {
    perror("fn NULL");
    dlclose(dl);
    return -1;
  }
  printf("dl = %u\nfn = %u\n", dl, fn);
  dl2 = dlopen("./vfnbank2.so", RTLD_NOW);
  if (dl2 == NULL) {
    perror("dl2 NULL");
    return -1;
  }
  fn2 = dlsym(dl2, "vfn0001");
  if (fn2 == NULL) {
    perror("fn2 NULL");
    dlclose(dl2);
    return -1;
  }
  printf("dl2 = %u\nfn2 = %u\n", dl2, fn2);
  fninit = fn;
  fninit(2);
  fncall = fn;
  fncall();
  addfn = fn;
  res = addfn(1, 2);
  printf("addfn returned: %d\n", res);
  dlclose(dl2);
  dlclose(dl);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a2 <main+0x52>
lea    0xe7c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <main+0x19f>
mov    -0x18(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e2 <main+0x92>
lea    0xe55(%rip),%rdi
call   1050 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1070 <dlclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <main+0x19f>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    -0x28(%rbp),%rdx
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0xe2c(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1236 <main+0xe6>
lea    0xe1f(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <main+0x19f>
mov    -0x20(%rbp),%rdi
lea    0xdd4(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1276 <main+0x126>
lea    0xdf1(%rip),%rdi
call   1050 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1070 <dlclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <main+0x19f>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rdx
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x2,%edi
call   *-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   *-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x1,%edi
mov    $0x2,%esi
mov    $0x0,%al
call   *-0x48(%rbp)
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%esi
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <dlclose@plt>
mov    -0x18(%rbp),%rdi
call   1070 <dlclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8a(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe98(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1237 <main+0xe7>
mov    %rax,%r14
lea    0xe95(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     1240 <main+0xf0>
mov    %rax,%rbx
lea    0xe8a(%rip),%rdi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x0(%rbp),%esi
lea    0xe84(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1251 <main+0x101>
mov    %rax,%r15
lea    0xe4b(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     1264 <main+0x114>
lea    0xe74(%rip),%rdi
xor    %ebp,%ebp
mov    %r15d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x2,%edi
call   *%rbx
xor    %eax,%eax
call   *%rbx
mov    $0x1,%edi
mov    $0x2,%esi
xor    %eax,%eax
call   *%rbx
lea    0xe58(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r15,%rdi
call   1070 <dlclose@plt>
mov    %r14,%rdi
call   1070 <dlclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcf(%rip),%rdi
jmp    1258 <main+0x108>
lea    0xdd6(%rip),%rdi
call   1050 <perror@plt>
mov    %r14,%rdi
jmp    1273 <main+0x123>
lea    0xdec(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebp
jmp    122a <main+0xda>
lea    0xde2(%rip),%rdi
call   1050 <perror@plt>
mov    %r15,%rdi
call   1070 <dlclose@plt>
mov    $0xffffffff,%ebp
jmp    122a <main+0xda>

LLVM IR (-O0):
@.str = [13 x i8] c"./vfnbank.so\00"
@RTLD_NOW = global i32 0
@.str.1 = [8 x i8] c"dl NULL\00"
@.str.2 = [8 x i8] c"vfn0001\00"
@.str.3 = [8 x i8] c"fn NULL\00"
@.str.4 = [17 x i8] c"dl = %u\0Afn = %u\0A\00"
@.str.5 = [14 x i8] c"./vfnbank2.so\00"
@.str.6 = [9 x i8] c"dl2 NULL\00"
@.str.7 = [9 x i8] c"fn2 NULL\00"
@.str.8 = [19 x i8] c"dl2 = %u\0Afn2 = %u\0A\00"
@.str.9 = [20 x i8] c"addfn returned: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8* (i32)*
%11 = alloca i8* (...)*
%12 = alloca i32 (i32, ...)*
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @RTLD_NOW
%15 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
20: ; preds = %2
%21 = load i8*, i8** %6
%22 = call i8* @dlsym(i8* %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%27 = load i8*, i8** %6
%28 = call i32 @dlclose(i8* %27)
store i32 -1, i32* %3
br label %72
29: ; preds = %20
%30 = load i8*, i8** %6
%31 = ptrtoint i8* %30 to i32
%32 = load i8*, i8** %8
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %31, i8* %32)
%34 = load i32, i32* @RTLD_NOW
%35 = call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %34)
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %29
%39 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
40: ; preds = %29
%41 = load i8*, i8** %7
%42 = call i8* @dlsym(i8* %41, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i8* %42, i8** %9
%43 = load i8*, i8** %9
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%47 = load i8*, i8** %7
%48 = call i32 @dlclose(i8* %47)
store i32 -1, i32* %3
br label %72
49: ; preds = %40
%50 = load i8*, i8** %7
%51 = ptrtoint i8* %50 to i32
%52 = load i8*, i8** %9
%53 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i32 %51, i8* %52)
%54 = load i8*, i8** %8
%55 = bitcast i8* %54 to i8* (i32)*
store i8* (i32)* %55, i8* (i32)** %10
%56 = load i8* (i32)*, i8* (i32)** %10
%57 = call i8* %56(i32 2)
%58 = load i8*, i8** %8
%59 = bitcast i8* %58 to i8* (...)*
store i8* (...)* %59, i8* (...)** %11
%60 = load i8* (...)*, i8* (...)** %11
%61 = call i8* (...) %60()
%62 = load i8*, i8** %8
%63 = bitcast i8* %62 to i32 (i32, ...)*
store i32 (i32, ...)* %63, i32 (i32, ...)** %12
%64 = load i32 (i32, ...)*, i32 (i32, ...)** %12
%65 = call i32 (i32, ...) %64(i32 1, i32 2)
store i32 %65, i32* %13
%66 = load i32, i32* %13
%67 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %66)
%68 = load i8*, i8** %7
%69 = call i32 @dlclose(i8* %68)
%70 = load i8*, i8** %6
%71 = call i32 @dlclose(i8* %70)
store i32 0, i32* %3
br label %72
72: ; preds = %49, %45, %38, %25, %18
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [13 x i8] c"./vfnbank.so\00"
@RTLD_NOW = global i32 0
@.str.1 = [8 x i8] c"dl NULL\00"
@.str.2 = [8 x i8] c"vfn0001\00"
@.str.3 = [8 x i8] c"fn NULL\00"
@.str.4 = [17 x i8] c"dl = %u\0Afn = %u\0A\00"
@.str.5 = [14 x i8] c"./vfnbank2.so\00"
@.str.6 = [9 x i8] c"dl2 NULL\00"
@.str.7 = [9 x i8] c"fn2 NULL\00"
@.str.8 = [19 x i8] c"dl2 = %u\0Afn2 = %u\0A\00"
@.str.9 = [20 x i8] c"addfn returned: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @RTLD_NOW
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %42
8: ; preds = %2
%9 = tail call i8* @dlsym(i8* %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i32 @dlclose(i8* %4)
br label %42
14: ; preds = %8
%15 = ptrtoint i8* %4 to i64
%16 = trunc i64 %15 to i32
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %16, i8* %9)
%18 = load i32, i32* @RTLD_NOW
%19 = tail call i8* @dlopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %42
23: ; preds = %14
%24 = tail call i8* @dlsym(i8* %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%28 = tail call i32 @dlclose(i8* %19)
br label %42
29: ; preds = %23
%30 = ptrtoint i8* %19 to i64
%31 = trunc i64 %30 to i32
%32 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i32 %31, i8* %24)
%33 = bitcast i8* %9 to i8* (i32)*
%34 = tail call i8* %33(i32 2)
%35 = bitcast i8* %9 to i8* (...)*
%36 = tail call i8* (...) %35()
%37 = bitcast i8* %9 to i32 (i32, ...)*
%38 = tail call i32 (i32, ...) %37(i32 1, i32 2)
%39 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %38)
%40 = tail call i32 @dlclose(i8* %19)
%41 = tail call i32 @dlclose(i8* %4)
br label %42
42: ; preds = %29, %26, %21, %11, %6
%43 = phi i32 [ -1, %6 ], [ -1, %11 ], [ -1, %21 ], [ -1, %26 ], [ 0, %29 ]
ret i32 %43
}