#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 */
typedef int uint64_t;

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int get_kernel_symbol_addr(char *);
int /*<<< orphan*/ kAFL_hypercall(int, int);
int /*<<< orphan*/ load_program(void *);
int /*<<< orphan*/ memset(void *, int, int);
void *mmap(void *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  uint64_t panic_handler = 0x0;
  uint64_t panic_handler64 = 0x0;
  void *program_buffer;
  printf("<< kAFL Usermode Loader for macOS x86-64 >>\n");
  panic_handler = get_kernel_symbol_addr("T _panic");
  panic_handler64 = get_kernel_symbol_addr("T _panic_64");
  printf("panic_handler\t%llx\n", panic_handler);
  printf("panic_handler64\t%llx\n", panic_handler64);
  program_buffer = mmap((void *)0xabcd0000, (16 << 20), PROT_READ | PROT_WRITE,
                        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  memset(program_buffer, 0xff, (16 << 20));
  kAFL_hypercall(10, 0);
  kAFL_hypercall(0, 0);
  kAFL_hypercall(4, 0);
  kAFL_hypercall(6, panic_handler);
  kAFL_hypercall(6, panic_handler64);
  kAFL_hypercall(2, (uint64_t)program_buffer);
  load_program(program_buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe94(%rip),%rdi
call   1030 <get_kernel_symbol_addr@plt>
mov    %eax,-0x14(%rbp)
lea    0xe8e(%rip),%rdi
call   1030 <get_kernel_symbol_addr@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
mov    0x2dee(%rip),%rax
or     (%rax),%edx
mov    0x2ddd(%rip),%rax
mov    (%rax),%ecx
mov    0x2dec(%rip),%rax
or     (%rax),%ecx
mov    $0xabcd0000,%edi
mov    $0x1000000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0xff,%esi
mov    $0x1000000,%edx
call   1060 <memset@plt>
mov    $0xa,%edi
xor    %esi,%esi
call   1070 <kAFL_hypercall@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1070 <kAFL_hypercall@plt>
mov    $0x4,%edi
xor    %esi,%esi
call   1070 <kAFL_hypercall@plt>
mov    -0x14(%rbp),%esi
mov    $0x6,%edi
call   1070 <kAFL_hypercall@plt>
mov    -0x18(%rbp),%esi
mov    $0x6,%edi
call   1070 <kAFL_hypercall@plt>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    $0x2,%edi
call   1070 <kAFL_hypercall@plt>
mov    -0x20(%rbp),%rdi
call   1080 <load_program@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
lea    0xe79(%rip),%rdi
call   1040 <get_kernel_symbol_addr@plt>
mov    %eax,%r14d
lea    0xe73(%rip),%rdi
call   1040 <get_kernel_symbol_addr@plt>
mov    %eax,%ebp
lea    0xe71(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe74(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e33(%rip),%rax
mov    0x2e04(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df1(%rip),%rax
mov    0x2e02(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0xabcd0000,%edi
mov    $0x1000000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0xff,%esi
mov    $0x1000000,%edx
call   1070 <memset@plt>
mov    $0xa,%edi
xor    %esi,%esi
call   1080 <kAFL_hypercall@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1080 <kAFL_hypercall@plt>
mov    $0x4,%edi
xor    %esi,%esi
call   1080 <kAFL_hypercall@plt>
mov    $0x6,%edi
mov    %r14d,%esi
call   1080 <kAFL_hypercall@plt>
mov    $0x6,%edi
mov    %ebp,%esi
call   1080 <kAFL_hypercall@plt>
mov    $0x2,%edi
mov    %ebx,%esi
call   1080 <kAFL_hypercall@plt>
mov    %rbx,%rdi
call   1090 <load_program@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [45 x i8] c"<< kAFL Usermode Loader for macOS x86-64 >>\0A\00"
@.str.1 = [9 x i8] c"T _panic\00"
@.str.2 = [12 x i8] c"T _panic_64\00"
@.str.3 = [20 x i8] c"panic_handler\09%llx\0A\00"
@.str.4 = [22 x i8] c"panic_handler64\09%llx\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @get_kernel_symbol_addr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 %10, i32* %6
%11 = call i32 @get_kernel_symbol_addr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 %11, i32* %7
%12 = load i32, i32* %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %7
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %16, %17
%19 = load i32, i32* @MAP_PRIVATE
%20 = load i32, i32* @MAP_ANONYMOUS
%21 = or i32 %19, %20
%22 = call i8* @mmap(i8* inttoptr (i64 2882338816 to i8*), i32 16777216, i32 %18, i32 %21, i32 -1, i32 0)
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = call i32 @memset(i8* %23, i32 255, i32 16777216)
%25 = call i32 @kAFL_hypercall(i32 10, i32 0)
%26 = call i32 @kAFL_hypercall(i32 0, i32 0)
%27 = call i32 @kAFL_hypercall(i32 4, i32 0)
%28 = load i32, i32* %6
%29 = call i32 @kAFL_hypercall(i32 6, i32 %28)
%30 = load i32, i32* %7
%31 = call i32 @kAFL_hypercall(i32 6, i32 %30)
%32 = load i8*, i8** %8
%33 = ptrtoint i8* %32 to i32
%34 = call i32 @kAFL_hypercall(i32 2, i32 %33)
%35 = load i8*, i8** %8
%36 = call i32 @load_program(i8* %35)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"T _panic\00"
@.str.2 = [12 x i8] c"T _panic_64\00"
@.str.3 = [20 x i8] c"panic_handler\09%llx\0A\00"
@.str.4 = [22 x i8] c"panic_handler64\09%llx\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@str = [44 x i8] c"<< kAFL Usermode Loader for macOS x86-64 >>\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @get_kernel_symbol_addr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @get_kernel_symbol_addr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %4)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %5)
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_PRIVATE
%12 = load i32, i32* @MAP_ANONYMOUS
%13 = or i32 %12, %11
%14 = tail call i8* @mmap(i8* inttoptr (i64 2882338816 to i8*), i32 16777216, i32 %10, i32 %13, i32 -1, i32 0)
%15 = tail call i32 @memset(i8* %14, i32 255, i32 16777216)
%16 = tail call i32 @kAFL_hypercall(i32 10, i32 0)
%17 = tail call i32 @kAFL_hypercall(i32 0, i32 0)
%18 = tail call i32 @kAFL_hypercall(i32 4, i32 0)
%19 = tail call i32 @kAFL_hypercall(i32 6, i32 %4)
%20 = tail call i32 @kAFL_hypercall(i32 6, i32 %5)
%21 = ptrtoint i8* %14 to i64
%22 = trunc i64 %21 to i32
%23 = tail call i32 @kAFL_hypercall(i32 2, i32 %22)
%24 = tail call i32 @load_program(i8* %14)
ret i32 0
}