#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_LAZY;
scalar_t__ dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int stub1();
int main() {
  void *handle = NULL;
  int (*_g_echo)();
  int j = 0;
  handle = dlopen("./libecho.so", RTLD_LAZY);
  if (!handle) {
    fprintf(stderr, "dlopen - %s\n", dlerror());
    exit(-1);
  }
  _g_echo = (int (*)())dlsym(handle, "echo");
  if (!_g_echo) {
    fprintf(stderr, "dlsym - %s\n", dlerror());
    exit(-1);
  }
  (*_g_echo)();
  if (0 != dlclose(handle)) {
    fprintf(stderr, "dlsym - %s\n", dlerror());
    exit(-1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d1 <main+0x71>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121f <main+0xbf>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe0f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   *-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <dlclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     126a <main+0x10a>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xdc4(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
jne    1194 <main+0x34>
mov    0x2e5e(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe7b(%rip),%rsi
jmp    11dd <main+0x7d>
mov    %rax,%rbx
lea    0xe7c(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11c6 <main+0x66>
mov    %rax,%rcx
xor    %eax,%eax
call   *%rcx
mov    %rbx,%rdi
call   1080 <dlclose@plt>
test   %rax,%rax
jne    11c6 <main+0x66>
xor    %edi,%edi
call   1070 <exit@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe42(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"./libecho.so\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [13 x i8] c"dlopen - %s\0A\00"
@.str.2 = [5 x i8] c"echo\00"
@.str.3 = [12 x i8] c"dlsym - %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32 (...)*
%4 = alloca i32
store i32 0, i32* %1
store i8* null, i8** %2
store i32 0, i32* %4
%5 = load i32, i32* @RTLD_LAZY
%6 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i8* (...) @dlerror()
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = load i8*, i8** %2
%16 = call i64 @dlsym(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%17 = inttoptr i64 %16 to i32 (...)*
store i32 (...)* %17, i32 (...)** %3
%18 = load i32 (...)*, i32 (...)** %3
%19 = icmp ne i32 (...)* %18, null
br i1 %19, label %25, label %20
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = call i8* (...) @dlerror()
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %14
%26 = load i32 (...)*, i32 (...)** %3
%27 = call i32 (...) %26()
%28 = load i8*, i8** %2
%29 = call i64 @dlclose(i8* %28)
%30 = icmp ne i64 0, %29
br i1 %30, label %31, label %36
31: ; preds = %25
%32 = load i32, i32* @stderr
%33 = call i8* (...) @dlerror()
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %25
%37 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [13 x i8] c"./libecho.so\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [13 x i8] c"dlopen - %s\0A\00"
@.str.2 = [5 x i8] c"echo\00"
@.str.3 = [12 x i8] c"dlsym - %s\0A\00"
define i32 @main() {
%1 = load i32, i32* @RTLD_LAZY
%2 = tail call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %9
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = tail call i8* (...) @dlerror()
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = tail call i64 @dlsym(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i8* (...) @dlerror()
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %9
%18 = inttoptr i64 %10 to i32 (...)*
%19 = tail call i32 (...) %18()
%20 = tail call i64 @dlclose(i8* %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = tail call i8* (...) @dlerror()
%25 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%26 = tail call i32 @exit(i32 -1)
unreachable
27: ; preds = %17
%28 = tail call i32 @exit(i32 0)
unreachable
}