#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*/ __thinthin_dlinstantiate(long, char *);
long __thinthin_dlload(char *, unsigned long *);
long __thinthin_dlreaddep(long, char *, long);
char *malloc(unsigned long);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, long);
int main(void) {
  long modi;
  unsigned long mem;
  char *buf;
  long off;
  long len;
  printf("starting\n");
  modi = __thinthin_dlload("002-x.wasm", &mem);
  printf("modi %ld mem %lu\n", modi, mem);
  off = 0;
  len = 1;
  buf = malloc(len);
  buf[0] = 0;
  while ((off += __thinthin_dlreaddep(modi, buf + off, len - off)) == len) {
    len *= 2;
    buf = realloc(buf, len);
  }
  printf("buf %ld %s\n", off, buf);
  printf("inst %ld\n", __thinthin_dlinstantiate(modi, malloc(mem)));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe86(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <__thinthin_dlload@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x1,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1050 <__thinthin_dlreaddep@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmp    -0x30(%rbp),%rax
jne    1218 <main+0xb8>
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11cb <main+0x6b>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   1070 <__thinthin_dlinstantiate@plt>
mov    %eax,%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
lea    0xe75(%rip),%rdi
mov    %rsp,%rsi
call   1040 <__thinthin_dlload@plt>
mov    %rax,%r14
mov    (%rsp),%rdx
lea    0xe6a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebx
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    $0x1,%edx
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <__thinthin_dlreaddep@plt>
cmp    $0x1,%rax
jne    120c <main+0x9c>
cs nopw 0x0(%rax,%rax,1)
lea    (%rbx,%rbx,1),%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <realloc@plt>
mov    %rax,%r15
lea    (%rax,%rbx,1),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1060 <__thinthin_dlreaddep@plt>
add    %rbx,%rax
mov    %r12,%rbx
cmp    %r12,%rax
je     11e0 <main+0x70>
lea    0xe0a(%rip),%rdi
mov    %rax,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rdi
call   1070 <malloc@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1080 <__thinthin_dlinstantiate@plt>
lea    0xdee(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"starting\0A\00"
@.str.1 = [11 x i8] c"002-x.wasm\00"
@.str.2 = [18 x i8] c"modi %ld mem %lu\0A\00"
@.str.3 = [12 x i8] c"buf %ld %s\0A\00"
@.str.4 = [10 x i8] c"inst %ld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = call i64 @__thinthin_dlload(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64* %3)
store i64 %8, i64* %2
%9 = load i64, i64* %2
%10 = load i64, i64* %3
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 %9, i64 %10)
store i64 0, i64* %5
store i64 1, i64* %6
%12 = load i64, i64* %6
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 0, i8* %15
br label %16
16: ; preds = %29, %0
%17 = load i64, i64* %2
%18 = load i8*, i8** %4
%19 = load i64, i64* %5
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = load i64, i64* %6
%22 = load i64, i64* %5
%23 = sub nsw i64 %21, %22
%24 = call i64 @__thinthin_dlreaddep(i64 %17, i8* %20, i64 %23)
%25 = load i64, i64* %5
%26 = add nsw i64 %25, %24
store i64 %26, i64* %5
%27 = load i64, i64* %6
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %35
29: ; preds = %16
%30 = load i64, i64* %6
%31 = mul nsw i64 %30, 2
store i64 %31, i64* %6
%32 = load i8*, i8** %4
%33 = load i64, i64* %6
%34 = call i8* @realloc(i8* %32, i64 %33)
store i8* %34, i8** %4
br label %16
35: ; preds = %16
%36 = load i64, i64* %5
%37 = load i8*, i8** %4
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i64 %36, i8* %37)
%39 = load i64, i64* %2
%40 = load i64, i64* %3
%41 = call i8* @malloc(i64 %40)
%42 = call i32 @__thinthin_dlinstantiate(i64 %39, i8* %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %42)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"002-x.wasm\00"
@.str.2 = [18 x i8] c"modi %ld mem %lu\0A\00"
@.str.3 = [12 x i8] c"buf %ld %s\0A\00"
@.str.4 = [10 x i8] c"inst %ld\0A\00"
@str = [9 x i8] c"starting\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%4 = call i64 @__thinthin_dlload(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64* %1)
%5 = load i64, i64* %1
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 %4, i64 %5)
%7 = call dereferenceable_or_null(1) i8* @malloc(i64 1)
store i8 0, i8* %7
%8 = call i64 @__thinthin_dlreaddep(i64 %4, i8* %7, i64 1)
%9 = icmp eq i64 %8, 1
br i1 %9, label %10, label %19
10: ; preds = %0, %10
%11 = phi i64 [ %13, %10 ], [ 1, %0 ]
%12 = phi i8* [ %14, %10 ], [ %7, %0 ]
%13 = shl nsw i64 %11, 1
%14 = call i8* @realloc(i8* %12, i64 %13)
%15 = getelementptr inbounds i8, i8* %14, i64 %11
%16 = call i64 @__thinthin_dlreaddep(i64 %4, i8* %15, i64 %11)
%17 = add nsw i64 %16, %11
%18 = icmp eq i64 %17, %13
br i1 %18, label %10, label %19
19: ; preds = %10, %0
%20 = phi i8* [ %7, %0 ], [ %14, %10 ]
%21 = phi i64 [ %8, %0 ], [ %17, %10 ]
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i64 %21, i8* %20)
%23 = load i64, i64* %1
%24 = call i8* @malloc(i64 %23)
%25 = call i32 @__thinthin_dlinstantiate(i64 %4, i8* %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %25)
ret i32 0
}