#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 const *, unsigned long *);
long __thinthin_dlreaddep(long, char *, long);
char *malloc(unsigned long);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, long);
scalar_t__ strlen(char *);
void *my_dlopen(const char *path) {
  long modi;
  unsigned long mem;
  char *buf;
  long off;
  long len;
  char *p;
  printf("starting\n");
  modi = __thinthin_dlload(path, &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 %s %ld\n", path, __thinthin_dlinstantiate(modi, malloc(mem)));
  for (p = buf; p < buf + off; p += strlen(p) + 1)
    my_dlopen(p);
  return (void *)modi;
}


x86 assembly (clang, -O0):
<my_dlopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <__thinthin_dlload@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x1,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <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   1060 <__thinthin_dlreaddep@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmp    -0x30(%rbp),%rax
jne    1232 <my_dlopen+0xb2>
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11e5 <my_dlopen+0x65>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <malloc@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
call   1080 <__thinthin_dlinstantiate@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x28(%rbp),%rcx
cmp    %rcx,%rax
jae    12c1 <my_dlopen+0x141>
mov    -0x38(%rbp),%rdi
call   10a0 <my_dlopen@plt>
mov    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1289 <my_dlopen+0x109>
mov    -0x10(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_dlopen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1050 <__thinthin_dlload@plt>
mov    %rax,%r15
mov    (%rsp),%rdx
lea    0xe3d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%r13d
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rax,%rsi
call   1070 <__thinthin_dlreaddep@plt>
cmp    $0x1,%rax
jne    1233 <my_dlopen+0xa3>
nopl   0x0(%rax)
mov    %r13,%rbp
add    %r13,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <realloc@plt>
mov    %rax,%rbx
lea    (%rax,%r13,1),%rsi
mov    %r15,%rdi
mov    %r13,%rdx
call   1070 <__thinthin_dlreaddep@plt>
mov    %rax,%r12
add    %r13,%r12
mov    %rbp,%r13
cmp    %rbp,%r12
je     1200 <my_dlopen+0x70>
jmp    1236 <my_dlopen+0xa6>
mov    %rax,%r12
lea    0xdd5(%rip),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%rdi
call   1080 <malloc@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   1090 <__thinthin_dlinstantiate@plt>
lea    0xdb9(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
test   %r12,%r12
jle    129c <my_dlopen+0x10c>
add    %rbx,%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10b0 <my_dlopen@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %rax,%rbx
add    $0x1,%rbx
cmp    %r12,%rbx
jb     1280 <my_dlopen+0xf0>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"starting\0A\00"
@.str.1 = [18 x i8] c"modi %ld mem %lu\0A\00"
@.str.2 = [12 x i8] c"buf %ld %s\0A\00"
@.str.3 = [13 x i8] c"inst %s %ld\0A\00"
define i8* @my_dlopen(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %2
%11 = call i64 @__thinthin_dlload(i8* %10, i64* %4)
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = load i64, i64* %4
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %12, i64 %13)
store i64 0, i64* %6
store i64 1, i64* %7
%15 = load i64, i64* %7
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i64 0
store i8 0, i8* %18
br label %19
19: ; preds = %32, %1
%20 = load i64, i64* %3
%21 = load i8*, i8** %5
%22 = load i64, i64* %6
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i64, i64* %7
%25 = load i64, i64* %6
%26 = sub nsw i64 %24, %25
%27 = call i64 @__thinthin_dlreaddep(i64 %20, i8* %23, i64 %26)
%28 = load i64, i64* %6
%29 = add nsw i64 %28, %27
store i64 %29, i64* %6
%30 = load i64, i64* %7
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %38
32: ; preds = %19
%33 = load i64, i64* %7
%34 = mul nsw i64 %33, 2
store i64 %34, i64* %7
%35 = load i8*, i8** %5
%36 = load i64, i64* %7
%37 = call i8* @realloc(i8* %35, i64 %36)
store i8* %37, i8** %5
br label %19
38: ; preds = %19
%39 = load i64, i64* %6
%40 = load i8*, i8** %5
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %39, i8* %40)
%42 = load i8*, i8** %2
%43 = load i64, i64* %3
%44 = load i64, i64* %4
%45 = call i8* @malloc(i64 %44)
%46 = call i32 @__thinthin_dlinstantiate(i64 %43, i8* %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %42, i32 %46)
%48 = load i8*, i8** %5
store i8* %48, i8** %8
br label %49
49: ; preds = %58, %38
%50 = load i8*, i8** %8
%51 = load i8*, i8** %5
%52 = load i64, i64* %6
%53 = getelementptr inbounds i8, i8* %51, i64 %52
%54 = icmp ult i8* %50, %53
br i1 %54, label %55, label %64
55: ; preds = %49
%56 = load i8*, i8** %8
%57 = call i8* @my_dlopen(i8* %56)
br label %58
58: ; preds = %55
%59 = load i8*, i8** %8
%60 = call i64 @strlen(i8* %59)
%61 = add nsw i64 %60, 1
%62 = load i8*, i8** %8
%63 = getelementptr inbounds i8, i8* %62, i64 %61
store i8* %63, i8** %8
br label %49
64: ; preds = %49
%65 = load i64, i64* %3
%66 = inttoptr i64 %65 to i8*
ret i8* %66
}

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