#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 dlclose(void *);
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  printf("[BEGIN] dlopen-signing\n");
  void *handle = dlopen("signed.dylib", RTLD_LAZY);
  if (handle == NULL) {
    printf("dlerror(): %s\n", dlerror());
    printf("[FAIL] dlopen-signing (signed loading signed)\n");
    return 0;
  } else {
    int result = dlclose(handle);
    if (result != 0) {
      printf("dlclose() returned %c\n", result);
      printf("[FAIL] dlopen-signing (signed unloading signed)\n");
      return 0;
    }
  }
  handle = dlopen("unsigned.dylib", RTLD_LAZY);
  if (handle != NULL) {
    printf("dlerror(): %s\n", dlerror());
    printf("[FAIL] dlopen-signing (signed loading unsigned)\n");
    return 0;
  } else {
    int result = dlclose(handle);
    if (result != 0) {
      printf("dlclose() returned %c\n", result);
      printf("[FAIL] dlopen-signing (signed unloading signed)\n");
      return 0;
    }
  }
  printf("[PASS] dlopen-signing\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xf33(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b3 <main+0x73>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12aa <main+0x16a>
mov    -0x10(%rbp),%rdi
call   1060 <dlclose@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11f4 <main+0xb4>
mov    -0x14(%rbp),%esi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12aa <main+0x16a>
jmp    11f9 <main+0xb9>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1050 <dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     124f <main+0x10f>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rsi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12aa <main+0x16a>
mov    -0x10(%rbp),%rdi
call   1060 <dlclose@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1290 <main+0x150>
mov    -0x18(%rbp),%esi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12aa <main+0x16a>
jmp    1295 <main+0x155>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xedd(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e7c(%rip),%rbx
mov    (%rbx),%esi
lea    0xebb(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     119c <main+0x4c>
mov    %rax,%rdi
call   1070 <dlclose@plt>
test   %eax,%eax
je     11bd <main+0x6d>
lea    0xe85(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xef8(%rip),%rdi
jmp    11ef <main+0x9f>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe56(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf07(%rip),%rdi
jmp    11ef <main+0x9f>
mov    (%rbx),%esi
lea    0xe60(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     11f8 <main+0xa8>
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe22(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe73(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
xor    %edi,%edi
call   1070 <dlclose@plt>
test   %eax,%eax
jne    1183 <main+0x33>
lea    0xe42(%rip),%rdi
jmp    11ef <main+0x9f>

LLVM IR (-O0):
@.str = [24 x i8] c"[BEGIN] dlopen-signing\0A\00"
@.str.1 = [13 x i8] c"signed.dylib\00"
@RTLD_LAZY = global i32 0
@.str.2 = [15 x i8] c"dlerror(): %s\0A\00"
@.str.3 = [47 x i8] c"[FAIL] dlopen-signing (signed loading signed)\0A\00"
@.str.4 = [23 x i8] c"dlclose() returned %c\0A\00"
@.str.5 = [49 x i8] c"[FAIL] dlopen-signing (signed unloading signed)\0A\00"
@.str.6 = [15 x i8] c"unsigned.dylib\00"
@.str.7 = [49 x i8] c"[FAIL] dlopen-signing (signed loading unsigned)\0A\00"
@.str.8 = [23 x i8] c"[PASS] dlopen-signing\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @RTLD_LAZY
%7 = call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %6)
store i8* %7, i8** %2
%8 = load i8*, i8** %2
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i8* (...) @dlerror()
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %45
14: ; preds = %0
%15 = load i8*, i8** %2
%16 = call i32 @dlclose(i8* %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %45
23: ; preds = %14
br label %24
24: ; preds = %23
%25 = load i32, i32* @RTLD_LAZY
%26 = call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %25)
store i8* %26, i8** %2
%27 = load i8*, i8** %2
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = call i8* (...) @dlerror()
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %45
33: ; preds = %24
%34 = load i8*, i8** %2
%35 = call i32 @dlclose(i8* %34)
store i32 %35, i32* %4
%36 = load i32, i32* %4
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = load i32, i32* %4
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %45
42: ; preds = %33
br label %43
43: ; preds = %42
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %45
45: ; preds = %43, %38, %29, %19, %10
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"signed.dylib\00"
@RTLD_LAZY = global i32 0
@.str.2 = [15 x i8] c"dlerror(): %s\0A\00"
@.str.4 = [23 x i8] c"dlclose() returned %c\0A\00"
@.str.6 = [15 x i8] c"unsigned.dylib\00"
@str = [23 x i8] c"[BEGIN] dlopen-signing\00"
@str.9 = [22 x i8] c"[PASS] dlopen-signing\00"
@str.11 = [48 x i8] c"[FAIL] dlopen-signing (signed loading unsigned)\00"
@str.12 = [48 x i8] c"[FAIL] dlopen-signing (signed unloading signed)\00"
@str.13 = [46 x i8] c"[FAIL] dlopen-signing (signed loading signed)\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @RTLD_LAZY
%3 = tail call i8* @dlopen(i8* getelementptr inbounds ([13 x i8], [13 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 %25
8: ; preds = %0
%9 = tail call i32 @dlclose(i8* %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %9)
br label %25
13: ; preds = %8
%14 = load i32, i32* @RTLD_LAZY
%15 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = tail call i8* (...) @dlerror()
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %25
20: ; preds = %13
%21 = tail call i32 @dlclose(i8* null)
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %21)
br label %25
25: ; preds = %20, %23, %11, %17, %5
%26 = phi i8* [ getelementptr inbounds ([48 x i8], [48 x i8]* @str.12, i64 0, i64 0), %23 ], [ getelementptr inbounds ([48 x i8], [48 x i8]* @str.12, i64 0, i64 0), %11 ], [ getelementptr inbounds ([48 x i8], [48 x i8]* @str.11, i64 0, i64 0), %17 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @str.13, i64 0, i64 0), %5 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @str.9, i64 0, i64 0), %20 ]
%27 = tail call i32 @puts(i8* dereferenceable(1) %26)
ret i32 0
}