#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 RTLD_LAZY;
int RTLD_NOW;
int /*<<< orphan*/ __thinthin_dlinstantiate(long, char *);
long __thinthin_dlload(void *, size_t, unsigned long *);
long __thinthin_dlreaddep(long, char *, long);
char *alloca(scalar_t__);
int /*<<< orphan*/ asprintf(char **, char *, char const *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(unsigned long);
char *realloc(char *, long);
void *slurp(char *, size_t *);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ system(char *);
void *dlopen(const char *path, int flags) {
  long modi;
  unsigned long mem;
  char *buf;
  long off;
  long len;
  char *p;
  char *realpath = alloca(strlen(path) + 8);
  sprintf(realpath, "%s.wasm", path);
  if (!(flags & RTLD_LAZY) && !(flags & RTLD_NOW))
    return NULL;
  if (path) {
    size_t modlen;
    void *data;
    while ((data = slurp(realpath, &modlen)) == NULL) {
      char *command;
      asprintf(&command, "wasmify-wasm32 %s > %s", path, realpath);
      system(command);
      free(command);
    };
    modi = __thinthin_dlload(data, modlen, &mem);
    free(data);
    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);
    }
    __thinthin_dlinstantiate(modi, mem ? malloc(mem) : NULL);
    for (p = buf; p < buf + off; p += strlen(p) + 1)
      dlopen(p, flags);
    free(buf);
  } else {
    modi = 1;
  }
  return (void *)modi;
}


x86 assembly (clang, -O0):
<dlopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x8,%rdi
call   1090 <alloca@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdf6(%rip),%rsi
call   10f0 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    0x2dbf(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1246 <dlopen+0x76>
mov    -0x14(%rbp),%eax
mov    0x2dba(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1246 <dlopen+0x76>
movq   $0x0,-0x8(%rbp)
jmp    13cd <dlopen+0x1fd>
cmpq   $0x0,-0x10(%rbp)
je     13bd <dlopen+0x1ed>
jmp    1256 <dlopen+0x86>
mov    -0x50(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   1050 <slurp@plt>
mov    %rax,-0x60(%rbp)
cmp    $0x0,%rax
jne    12a0 <dlopen+0xd0>
mov    -0x10(%rbp),%rdx
mov    -0x50(%rbp),%rcx
lea    -0x68(%rbp),%rdi
lea    0xd84(%rip),%rsi
call   10e0 <asprintf@plt>
mov    -0x68(%rbp),%rdi
call   1080 <system@plt>
mov    -0x68(%rbp),%rdi
call   1030 <free@plt>
jmp    1256 <dlopen+0x86>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <__thinthin_dlload@plt>
mov    %rax,-0x20(%rbp)
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
movq   $0x1,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
sub    -0x38(%rbp),%rdx
call   10a0 <__thinthin_dlreaddep@plt>
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmp    -0x40(%rbp),%rax
jne    132f <dlopen+0x15f>
mov    -0x40(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   10d0 <realloc@plt>
mov    %rax,-0x30(%rbp)
jmp    12e2 <dlopen+0x112>
mov    -0x20(%rbp),%rax
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1354 <dlopen+0x184>
mov    -0x28(%rbp),%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x78(%rbp)
jmp    135f <dlopen+0x18f>
xor    %eax,%eax
mov    %rax,-0x78(%rbp)
jmp    135f <dlopen+0x18f>
mov    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rsi
call   10c0 <__thinthin_dlinstantiate@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x30(%rbp),%rcx
add    -0x38(%rbp),%rcx
cmp    %rcx,%rax
jae    13af <dlopen+0x1df>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <dlopen@plt>
mov    -0x48(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1374 <dlopen+0x1a4>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    13c5 <dlopen+0x1f5>
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dlopen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r13d
mov    %rdi,%rbx
call   1060 <strlen@plt>
lea    0x8(%rax),%rdi
call   1090 <alloca@plt>
mov    %rax,%rbp
lea    0xe04(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   10f0 <sprintf@plt>
mov    0x2dca(%rip),%rax
test   %r13d,(%rax)
jne    1223 <dlopen+0x53>
mov    0x2dce(%rip),%rax
test   %r13d,(%rax)
je     1389 <dlopen+0x1b9>
test   %rbx,%rbx
je     1323 <dlopen+0x153>
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <slurp@plt>
mov    %rax,%r14
test   %rax,%rax
jne    129b <dlopen+0xcb>
mov    %r13d,0xc(%rsp)
lea    0xdbb(%rip),%r15
mov    %rsp,%r12
lea    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %rbp,%rcx
call   10e0 <asprintf@plt>
mov    (%rsp),%rdi
call   1080 <system@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1050 <slurp@plt>
test   %rax,%rax
je     1260 <dlopen+0x90>
mov    %rax,%r14
mov    0xc(%rsp),%r13d
mov    0x10(%rsp),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   1070 <__thinthin_dlload@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x1,%ebp
mov    $0x1,%edi
call   10b0 <malloc@plt>
mov    %rax,%r12
movb   $0x0,(%rax)
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rax,%rsi
call   10a0 <__thinthin_dlreaddep@plt>
cmp    $0x1,%rax
jne    132b <dlopen+0x15b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%r14
add    %rbp,%r14
mov    %r12,%rdi
mov    %r14,%rsi
call   10d0 <realloc@plt>
mov    %rax,%r12
lea    (%rax,%rbp,1),%rsi
mov    %r15,%rdi
mov    %rbp,%rdx
call   10a0 <__thinthin_dlreaddep@plt>
mov    %rax,%rbx
add    %rbp,%rbx
mov    %r14,%rbp
cmp    %r14,%rbx
je     12f0 <dlopen+0x120>
jmp    132e <dlopen+0x15e>
mov    $0x1,%r15d
jmp    138c <dlopen+0x1bc>
mov    %rax,%rbx
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1341 <dlopen+0x171>
call   10b0 <malloc@plt>
mov    %rax,%rsi
jmp    1343 <dlopen+0x173>
xor    %esi,%esi
mov    %r15,%rdi
call   10c0 <__thinthin_dlinstantiate@plt>
test   %rbx,%rbx
jle    137f <dlopen+0x1af>
add    %r12,%rbx
mov    %r12,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r13d,%esi
call   1040 <dlopen@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
add    %rax,%rbp
add    $0x1,%rbp
cmp    %rbx,%rbp
jb     1360 <dlopen+0x190>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    138c <dlopen+0x1bc>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"%s.wasm\00"
@RTLD_LAZY = global i32 0
@RTLD_NOW = global i32 0
@.str.1 = [23 x i8] c"wasmify-wasm32 %s > %s\00"
define i8* @dlopen(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%16 = load i8*, i8** %4
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 8
%19 = call i8* @alloca(i64 %18)
store i8* %19, i8** %12
%20 = load i8*, i8** %12
%21 = load i8*, i8** %4
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* %5
%24 = load i32, i32* @RTLD_LAZY
%25 = and i32 %23, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %2
%28 = load i32, i32* %5
%29 = load i32, i32* @RTLD_NOW
%30 = and i32 %28, %29
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %27
store i8* null, i8** %3
br label %113
33: ; preds = %27, %2
%34 = load i8*, i8** %4
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %109
36: ; preds = %33
br label %37
37: ; preds = %41, %36
%38 = load i8*, i8** %12
%39 = call i8* @slurp(i8* %38, i64* %13)
store i8* %39, i8** %14
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %49
41: ; preds = %37
%42 = load i8*, i8** %4
%43 = load i8*, i8** %12
%44 = call i32 @asprintf(i8** %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %42, i8* %43)
%45 = load i8*, i8** %15
%46 = call i32 @system(i8* %45)
%47 = load i8*, i8** %15
%48 = call i32 @free(i8* %47)
br label %37
49: ; preds = %37
%50 = load i8*, i8** %14
%51 = load i64, i64* %13
%52 = call i64 @__thinthin_dlload(i8* %50, i64 %51, i64* %7)
store i64 %52, i64* %6
%53 = load i8*, i8** %14
%54 = call i32 @free(i8* %53)
store i64 0, i64* %9
store i64 1, i64* %10
%55 = load i64, i64* %10
%56 = call i8* @malloc(i64 %55)
store i8* %56, i8** %8
%57 = load i8*, i8** %8
%58 = getelementptr inbounds i8, i8* %57, i64 0
store i8 0, i8* %58
br label %59
59: ; preds = %72, %49
%60 = load i64, i64* %6
%61 = load i8*, i8** %8
%62 = load i64, i64* %9
%63 = getelementptr inbounds i8, i8* %61, i64 %62
%64 = load i64, i64* %10
%65 = load i64, i64* %9
%66 = sub nsw i64 %64, %65
%67 = call i64 @__thinthin_dlreaddep(i64 %60, i8* %63, i64 %66)
%68 = load i64, i64* %9
%69 = add nsw i64 %68, %67
store i64 %69, i64* %9
%70 = load i64, i64* %10
%71 = icmp eq i64 %69, %70
br i1 %71, label %72, label %78
72: ; preds = %59
%73 = load i64, i64* %10
%74 = mul nsw i64 %73, 2
store i64 %74, i64* %10
%75 = load i8*, i8** %8
%76 = load i64, i64* %10
%77 = call i8* @realloc(i8* %75, i64 %76)
store i8* %77, i8** %8
br label %59
78: ; preds = %59
%79 = load i64, i64* %6
%80 = load i64, i64* %7
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %78
%83 = load i64, i64* %7
%84 = call i8* @malloc(i64 %83)
br label %86
85: ; preds = %78
br label %86
86: ; preds = %85, %82
%87 = phi i8* [ %84, %82 ], [ null, %85 ]
%88 = call i32 @__thinthin_dlinstantiate(i64 %79, i8* %87)
%89 = load i8*, i8** %8
store i8* %89, i8** %11
br label %90
90: ; preds = %100, %86
%91 = load i8*, i8** %11
%92 = load i8*, i8** %8
%93 = load i64, i64* %9
%94 = getelementptr inbounds i8, i8* %92, i64 %93
%95 = icmp ult i8* %91, %94
br i1 %95, label %96, label %106
96: ; preds = %90
%97 = load i8*, i8** %11
%98 = load i32, i32* %5
%99 = call i8* @dlopen(i8* %97, i32 %98)
br label %100
100: ; preds = %96
%101 = load i8*, i8** %11
%102 = call i64 @strlen(i8* %101)
%103 = add nsw i64 %102, 1
%104 = load i8*, i8** %11
%105 = getelementptr inbounds i8, i8* %104, i64 %103
store i8* %105, i8** %11
br label %90
106: ; preds = %90
%107 = load i8*, i8** %8
%108 = call i32 @free(i8* %107)
br label %110
109: ; preds = %33
store i64 1, i64* %6
br label %110
110: ; preds = %109, %106
%111 = load i64, i64* %6
%112 = inttoptr i64 %111 to i8*
store i8* %112, i8** %3
br label %113
113: ; preds = %110, %32
%114 = load i8*, i8** %3
ret i8* %114
}

LLVM IR (-O2):
@.str = [8 x i8] c"%s.wasm\00"
@RTLD_LAZY = global i32 0
@RTLD_NOW = global i32 0
@.str.1 = [23 x i8] c"wasmify-wasm32 %s > %s\00"
define i8* @dlopen(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = bitcast i64* %3 to i8*
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = add nsw i64 %7, 8
%9 = tail call i8* @alloca(i64 %8)
%10 = tail call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = load i32, i32* @RTLD_LAZY
%12 = and i32 %11, %1
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @RTLD_NOW
%16 = and i32 %15, %1
%17 = icmp eq i32 %16, 0
br i1 %17, label %73, label %18
18: ; preds = %14, %2
%19 = icmp eq i8* %0, null
br i1 %19, label %73, label %20
20: ; preds = %18
%21 = bitcast i64* %4 to i8*
%22 = call i8* @slurp(i8* %9, i64* %4)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %34
24: ; preds = %20
%25 = bitcast i8** %5 to i8*
br label %26
26: ; preds = %24, %26
%27 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %9)
%28 = load i8*, i8** %5
%29 = call i32 @system(i8* %28)
%30 = load i8*, i8** %5
%31 = call i32 @free(i8* %30)
%32 = call i8* @slurp(i8* %9, i64* %4)
%33 = icmp eq i8* %32, null
br i1 %33, label %26, label %34
34: ; preds = %26, %20
%35 = phi i8* [ %22, %20 ], [ %32, %26 ]
%36 = load i64, i64* %4
%37 = call i64 @__thinthin_dlload(i8* %35, i64 %36, i64* %3)
%38 = call i32 @free(i8* %35)
%39 = call dereferenceable_or_null(1) i8* @malloc(i64 1)
store i8 0, i8* %39
%40 = call i64 @__thinthin_dlreaddep(i64 %37, i8* %39, i64 1)
%41 = icmp eq i64 %40, 1
br i1 %41, label %42, label %51
42: ; preds = %34, %42
%43 = phi i64 [ %45, %42 ], [ 1, %34 ]
%44 = phi i8* [ %46, %42 ], [ %39, %34 ]
%45 = shl nsw i64 %43, 1
%46 = call i8* @realloc(i8* %44, i64 %45)
%47 = getelementptr inbounds i8, i8* %46, i64 %43
%48 = call i64 @__thinthin_dlreaddep(i64 %37, i8* %47, i64 %43)
%49 = add nsw i64 %48, %43
%50 = icmp eq i64 %49, %45
br i1 %50, label %42, label %51
51: ; preds = %42, %34
%52 = phi i8* [ %39, %34 ], [ %46, %42 ]
%53 = phi i64 [ %40, %34 ], [ %49, %42 ]
%54 = load i64, i64* %3
%55 = icmp eq i64 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %51
%57 = call i8* @malloc(i64 %54)
br label %58
58: ; preds = %51, %56
%59 = phi i8* [ %57, %56 ], [ null, %51 ]
%60 = call i32 @__thinthin_dlinstantiate(i64 %37, i8* %59)
%61 = getelementptr inbounds i8, i8* %52, i64 %53
%62 = icmp sgt i64 %53, 0
br i1 %62, label %63, label %70
63: ; preds = %58, %63
%64 = phi i8* [ %68, %63 ], [ %52, %58 ]
%65 = call i8* @dlopen(i8* %64, i32 %1)
%66 = call i64 @strlen(i8* dereferenceable(1) %64)
%67 = add nsw i64 %66, 1
%68 = getelementptr inbounds i8, i8* %64, i64 %67
%69 = icmp ult i8* %68, %61
br i1 %69, label %63, label %70
70: ; preds = %63, %58
%71 = call i32 @free(i8* %52)
%72 = inttoptr i64 %37 to i8*
br label %73
73: ; preds = %70, %18, %14
%74 = phi i8* [ null, %14 ], [ %72, %70 ], [ inttoptr (i64 1 to i8*), %18 ]
ret i8* %74
}