#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;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  printf("[BEGIN] dlopen-prebuilt-dlopen-closure\n");
  void *handle = dlopen("/usr/lib/libobjc-trampolines.dylib", RTLD_LAZY);
  if (handle == NULL) {
    printf("dlerror(): %s\n", dlerror());
    printf("[FAIL] dlopen-prebuilt-dlopen-closure\n");
    return 0;
  }
  printf("[PASS] dlopen-prebuilt-dlopen-closure\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xecb(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a3 <main+0x73>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xec9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11b8 <main+0x88>
lea    0xed7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xeea(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xea3(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     1170 <main+0x30>
lea    0xeeb(%rip),%rdi
jmp    118f <main+0x4f>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xea5(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xef0(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [40 x i8] c"[BEGIN] dlopen-prebuilt-dlopen-closure\0A\00"
@.str.1 = [35 x i8] c"/usr/lib/libobjc-trampolines.dylib\00"
@RTLD_LAZY = global i32 0
@.str.2 = [15 x i8] c"dlerror(): %s\0A\00"
@.str.3 = [39 x i8] c"[FAIL] dlopen-prebuilt-dlopen-closure\0A\00"
@.str.4 = [39 x i8] c"[PASS] dlopen-prebuilt-dlopen-closure\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
store i32 0, i32* %1
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @RTLD_LAZY
%5 = call i8* @dlopen(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %4)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i8* (...) @dlerror()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %14
14: ; preds = %12, %8
%15 = load i32, i32* %1
ret i32 %15
}

LLVM IR (-O2):
@.str.1 = [35 x i8] c"/usr/lib/libobjc-trampolines.dylib\00"
@RTLD_LAZY = global i32 0
@.str.2 = [15 x i8] c"dlerror(): %s\0A\00"
@str = [39 x i8] c"[BEGIN] dlopen-prebuilt-dlopen-closure\00"
@str.5 = [38 x i8] c"[PASS] dlopen-prebuilt-dlopen-closure\00"
@str.6 = [38 x i8] c"[FAIL] dlopen-prebuilt-dlopen-closure\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @RTLD_LAZY
%3 = tail call i8* @dlopen(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i8* (...) @dlerror()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %8
8: ; preds = %0, %5
%9 = phi i8* [ getelementptr inbounds ([38 x i8], [38 x i8]* @str.6, i64 0, i64 0), %5 ], [ getelementptr inbounds ([38 x i8], [38 x i8]* @str.5, i64 0, i64 0), %0 ]
%10 = tail call i32 @puts(i8* dereferenceable(1) %9)
ret i32 0
}