#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;
int /*<<< orphan*/ dlcheck(char *, void *);
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  printf("main-dlopen started with sizeof(void*) = %lu\n", sizeof(void *));
  printf("preloading mangled native lib\n");
  void *preload = dlopen("./mangled-native-lib.dylib", RTLD_LAZY);
  dlcheck("preload dlopen", preload);
  printf("loading fake python module\n");
  void *module = dlopen("./mangled-fake-pymodule.dylib", RTLD_LAZY);
  dlcheck("module dlopen", module);
  printf("fetching entry point\n");
  void *sym = dlsym(module, "pymodule_entrypoint");
  dlcheck("dlsym", sym);
  int (*pymodule_entrypoint)() = sym;
  printf("calling entry point\n");
  return !pymodule_entrypoint();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
mov    $0x8,%esi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xebe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xec6(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xecd(%rip),%rdi
call   1040 <dlcheck@plt>
lea    0xed0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xed5(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xedf(%rip),%rdi
call   1040 <dlcheck@plt>
lea    0xee1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    0xee5(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xee5(%rip),%rdi
call   1040 <dlcheck@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xed7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   *-0x30(%rbp)
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xf33(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e69(%rip),%rbx
mov    (%rbx),%esi
lea    0xeae(%rip),%rdi
call   1060 <dlopen@plt>
lea    0xebd(%rip),%rdi
mov    %rax,%rsi
call   1050 <dlcheck@plt>
lea    0xf21(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%esi
lea    0xeaf(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,%rbx
lea    0xebe(%rip),%rdi
mov    %rax,%rsi
call   1050 <dlcheck@plt>
lea    0xf10(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb1(%rip),%rsi
mov    %rbx,%rdi
call   1070 <dlsym@plt>
mov    %rax,%rbx
lea    0xeb3(%rip),%rdi
mov    %rax,%rsi
call   1050 <dlcheck@plt>
lea    0xef8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   *%rbx
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [46 x i8] c"main-dlopen started with sizeof(void*) = %lu\0A\00"
@.str.1 = [31 x i8] c"preloading mangled native lib\0A\00"
@.str.2 = [27 x i8] c"./mangled-native-lib.dylib\00"
@RTLD_LAZY = global i32 0
@.str.3 = [15 x i8] c"preload dlopen\00"
@.str.4 = [28 x i8] c"loading fake python module\0A\00"
@.str.5 = [30 x i8] c"./mangled-fake-pymodule.dylib\00"
@.str.6 = [14 x i8] c"module dlopen\00"
@.str.7 = [22 x i8] c"fetching entry point\0A\00"
@.str.8 = [20 x i8] c"pymodule_entrypoint\00"
@.str.9 = [6 x i8] c"dlsym\00"
@.str.10 = [21 x i8] c"calling entry point\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32 (...)*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i64 8)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @RTLD_LAZY
%13 = call i8* @dlopen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = call i32 @dlcheck(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%17 = load i32, i32* @RTLD_LAZY
%18 = call i8* @dlopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %17)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = call i32 @dlcheck(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i8* %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%22 = load i8*, i8** %7
%23 = call i8* @dlsym(i8* %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = call i32 @dlcheck(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %8
%27 = bitcast i8* %26 to i32 (...)*
store i32 (...)* %27, i32 (...)** %9
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
%29 = load i32 (...)*, i32 (...)** %9
%30 = call i32 (...) %29()
%31 = icmp ne i32 %30, 0
%32 = xor i1 %31, true
%33 = zext i1 %32 to i32
ret i32 %33
}

LLVM IR (-O2):
@.str = [46 x i8] c"main-dlopen started with sizeof(void*) = %lu\0A\00"
@.str.2 = [27 x i8] c"./mangled-native-lib.dylib\00"
@RTLD_LAZY = global i32 0
@.str.3 = [15 x i8] c"preload dlopen\00"
@.str.5 = [30 x i8] c"./mangled-fake-pymodule.dylib\00"
@.str.6 = [14 x i8] c"module dlopen\00"
@.str.8 = [20 x i8] c"pymodule_entrypoint\00"
@.str.9 = [6 x i8] c"dlsym\00"
@str = [30 x i8] c"preloading mangled native lib\00"
@str.11 = [27 x i8] c"loading fake python module\00"
@str.12 = [21 x i8] c"fetching entry point\00"
@str.13 = [20 x i8] c"calling entry point\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i64 8)
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%5 = load i32, i32* @RTLD_LAZY
%6 = tail call i8* @dlopen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%7 = tail call i32 @dlcheck(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.11, i64 0, i64 0))
%9 = load i32, i32* @RTLD_LAZY
%10 = tail call i8* @dlopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %9)
%11 = tail call i32 @dlcheck(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i8* %10)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.12, i64 0, i64 0))
%13 = tail call i8* @dlsym(i8* %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
%14 = tail call i32 @dlcheck(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i8* %13)
%15 = bitcast i8* %13 to i32 (...)*
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.13, i64 0, i64 0))
%17 = tail call i32 (...) %15()
%18 = icmp eq i32 %17, 0
%19 = zext i1 %18 to i32
ret i32 %19
}