#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 __thinthin_dlopen(char *);
int __thinthin_dlsym(char *);
int /*<<< orphan*/ printf(char *, int);
int stub1();
int stub2();
void stub3(int);
int stub4();
int stub5();
int main(void) {
  printf("open: %d\n", __thinthin_dlopen("test.wasm"));
  printf("dlsym: %p\n", __thinthin_dlsym("x"));
  printf("x: %d\n", *(int *)__thinthin_dlsym("x"));
  printf("get_x: %p\n", __thinthin_dlsym("get_x"));
  printf("get_x(): %d\n", ((int (*)(void))__thinthin_dlsym("get_x"))());
  ((void (*)(int))__thinthin_dlsym("set_x"))(84);
  printf("get_x(): %d\n", ((int (*)(void))__thinthin_dlsym("get_x"))());
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec4(%rip),%rdi
call   1050 <__thinthin_dlopen@plt>
mov    %eax,%esi
lea    0xeac(%rip),%rdi
call   1030 <printf@plt>
lea    0xed5(%rip),%rdi
call   1040 <__thinthin_dlsym@plt>
mov    %eax,%esi
lea    0xea6(%rip),%rdi
call   1030 <printf@plt>
lea    0xebb(%rip),%rdi
call   1040 <__thinthin_dlsym@plt>
cltq
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1030 <printf@plt>
lea    0xe9b(%rip),%rdi
call   1040 <__thinthin_dlsym@plt>
mov    %eax,%esi
lea    0xe82(%rip),%rdi
call   1030 <printf@plt>
lea    0xe81(%rip),%rdi
call   1040 <__thinthin_dlsym@plt>
cltq
call   *%rax
mov    %eax,%esi
lea    0xe75(%rip),%rdi
call   1030 <printf@plt>
lea    0xe76(%rip),%rdi
call   1040 <__thinthin_dlsym@plt>
cltq
mov    $0x54,%edi
call   *%rax
lea    0xe4e(%rip),%rdi
call   1040 <__thinthin_dlsym@plt>
cltq
call   *%rax
mov    %eax,%esi
lea    0xe42(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xecf(%rip),%rdi
call   1050 <__thinthin_dlopen@plt>
lea    0xeb9(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
lea    0xee0(%rip),%rbx
mov    %rbx,%rdi
call   1040 <__thinthin_dlsym@plt>
lea    0xeb0(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
call   1040 <__thinthin_dlsym@plt>
cltq
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1030 <printf@plt>
lea    0xea7(%rip),%rbx
mov    %rbx,%rdi
call   1040 <__thinthin_dlsym@plt>
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
call   1040 <__thinthin_dlsym@plt>
cltq
call   *%rax
lea    0xe84(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
lea    0xe80(%rip),%rdi
call   1040 <__thinthin_dlsym@plt>
cltq
mov    $0x54,%edi
call   *%rax
mov    %rbx,%rdi
call   1040 <__thinthin_dlsym@plt>
cltq
call   *%rax
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"open: %d\0A\00"
@.str.1 = [10 x i8] c"test.wasm\00"
@.str.2 = [11 x i8] c"dlsym: %p\0A\00"
@.str.3 = [2 x i8] c"x\00"
@.str.4 = [7 x i8] c"x: %d\0A\00"
@.str.5 = [11 x i8] c"get_x: %p\0A\00"
@.str.6 = [6 x i8] c"get_x\00"
@.str.7 = [13 x i8] c"get_x(): %d\0A\00"
@.str.8 = [6 x i8] c"set_x\00"
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = call i32 @__thinthin_dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%3 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%5 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %4)
%6 = call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%7 = sext i32 %6 to i64
%8 = inttoptr i64 %7 to i32*
%9 = load i32, i32* %8
%10 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %9)
%11 = call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%12 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %11)
%13 = call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%14 = sext i32 %13 to i64
%15 = inttoptr i64 %14 to i32 ()*
%16 = call i32 %15()
%17 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %16)
%18 = call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%19 = sext i32 %18 to i64
%20 = inttoptr i64 %19 to void (i32)*
%21 = call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i32 ()*
%24 = call i32 %23()
%25 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %24)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"open: %d\0A\00"
@.str.1 = [10 x i8] c"test.wasm\00"
@.str.2 = [11 x i8] c"dlsym: %p\0A\00"
@.str.3 = [2 x i8] c"x\00"
@.str.4 = [7 x i8] c"x: %d\0A\00"
@.str.5 = [11 x i8] c"get_x: %p\0A\00"
@.str.6 = [6 x i8] c"get_x\00"
@.str.7 = [13 x i8] c"get_x(): %d\0A\00"
@.str.8 = [6 x i8] c"set_x\00"
define i32 @main() {
%1 = tail call i32 @__thinthin_dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%4 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%5 = tail call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%6 = sext i32 %5 to i64
%7 = inttoptr i64 %6 to i32*
%8 = load i32, i32* %7
%9 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %8)
%10 = tail call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%11 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %10)
%12 = tail call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to i32 ()*
%15 = tail call i32 %14()
%16 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %15)
%17 = tail call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to void (i32)*
tail call void %19(i32 84)
%20 = tail call i32 @__thinthin_dlsym(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%21 = sext i32 %20 to i64
%22 = inttoptr i64 %21 to i32 ()*
%23 = tail call i32 %22()
%24 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %23)
ret i32 0
}