#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 */
typedef void (*hello_t)();

/* Variables and functions */
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  printf("C++ dlopen demo\n\n");
  printf("Opening hello.so...\n");
  void *handle = dlopen("./helloworld.so", RTLD_LAZY);
  if (!handle) {
    printf("Cannot open library: %s", dlerror());
    return 1;
  }
  printf("Loading symbol hello...\n");
  typedef void (*hello_t)();
  dlerror();
  hello_t hello = (hello_t)dlsym(handle, "say_hello");
  const char *dlsym_error = dlerror();
  if (dlsym_error) {
    printf("Cannot load symbol 'hello': %s\n", dlerror());
    dlclose(handle);
    return 1;
  }
  printf("Calling hello...\n");
  hello();
  printf("Closing library...\n");
  dlclose(handle);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c3 <main+0x73>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1260 <main+0x110>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     122f <main+0xdf>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <dlclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1260 <main+0x110>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   *-0x18(%rbp)
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <dlclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xee7(%rip),%rdi
call   1040 <puts@plt>
lea    0xeec(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     11f3 <main+0x93>
mov    %rax,%rbx
lea    0xed7(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe73(%rip),%rsi
mov    %rbx,%rdi
call   1070 <dlsym@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
je     1212 <main+0xb2>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe58(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
mov    $0x1,%ebp
jmp    1237 <main+0xd7>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe0f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    1237 <main+0xd7>
lea    0xe76(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   *%r14
lea    0xe76(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"C++ dlopen demo\0A\0A\00"
@.str.1 = [21 x i8] c"Opening hello.so...\0A\00"
@.str.2 = [16 x i8] c"./helloworld.so\00"
@RTLD_LAZY = global i32 0
@.str.3 = [24 x i8] c"Cannot open library: %s\00"
@.str.4 = [25 x i8] c"Loading symbol hello...\0A\00"
@.str.5 = [10 x i8] c"say_hello\00"
@.str.6 = [32 x i8] c"Cannot load symbol 'hello': %s\0A\00"
@.str.7 = [18 x i8] c"Calling hello...\0A\00"
@.str.8 = [20 x i8] c"Closing library...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca void (...)*
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @RTLD_LAZY
%8 = call i8* @dlopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %7)
store i8* %8, i8** %2
%9 = load i8*, i8** %2
%10 = icmp ne i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %0
%12 = call i8* (...) @dlerror()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %12)
store i32 1, i32* %1
br label %34
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%16 = call i8* (...) @dlerror()
%17 = load i8*, i8** %2
%18 = call i64 @dlsym(i8* %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%19 = inttoptr i64 %18 to void (...)*
store void (...)* %19, void (...)** %3
%20 = call i8* (...) @dlerror()
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %28
23: ; preds = %14
%24 = call i8* (...) @dlerror()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %2
%27 = call i32 @dlclose(i8* %26)
store i32 1, i32* %1
br label %34
28: ; preds = %14
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
%30 = load void (...)*, void (...)** %3
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
%32 = load i8*, i8** %2
%33 = call i32 @dlclose(i8* %32)
store i32 0, i32* %1
br label %34
34: ; preds = %28, %23, %11
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str.2 = [16 x i8] c"./helloworld.so\00"
@RTLD_LAZY = global i32 0
@.str.3 = [24 x i8] c"Cannot open library: %s\00"
@.str.5 = [10 x i8] c"say_hello\00"
@.str.6 = [32 x i8] c"Cannot load symbol 'hello': %s\0A\00"
@str = [17 x i8] c"C++ dlopen demo\0A\00"
@str.9 = [20 x i8] c"Opening hello.so...\00"
@str.10 = [24 x i8] c"Loading symbol hello...\00"
@str.11 = [17 x i8] c"Calling hello...\00"
@str.12 = [19 x i8] c"Closing library...\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
%3 = load i32, i32* @RTLD_LAZY
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i8* (...) @dlerror()
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %7)
br label %24
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.10, i64 0, i64 0))
%11 = tail call i8* (...) @dlerror()
%12 = tail call i64 @dlsym(i8* %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%13 = tail call i8* (...) @dlerror()
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = tail call i8* (...) @dlerror()
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %16)
%18 = tail call i32 @dlclose(i8* %4)
br label %24
19: ; preds = %9
%20 = inttoptr i64 %12 to void (...)*
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.11, i64 0, i64 0))
tail call void (...) %20()
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.12, i64 0, i64 0))
%23 = tail call i32 @dlclose(i8* %4)
br label %24
24: ; preds = %15, %19, %6
%25 = phi i32 [ 1, %6 ], [ 1, %15 ], [ 0, %19 ]
ret i32 %25
}