#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, scalar_t__);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, char *, char const *,
                          char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getpid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
char *compile(const char *path) {
  fprintf(stderr, "[+] Compiling %s\n", path);
  char *name = NULL;
  pid_t pid = fork();
  if (pid) {
    int status = 0;
    waitpid(pid, &status, 0);
    if (status != 0) {
      fprintf(stderr, "failed to compile: %d\n", status);
    }
    asprintf(&name, ".%d.dylib", pid);
  } else {
    asprintf(&name, ".%d.dylib", getpid());
    unlink(name);
    execlp("gcc", "gcc", "-shared", "-arch", "x86_64", path, "-o", name, NULL);
    fprintf(stderr, "failed to exec compiler\n");
    exit(2);
  }
  return name;
}


x86 assembly (clang, -O0):
<compile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1218 <compile+0x98>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmpl   $0x0,-0x1c(%rbp)
je     11ff <compile+0x7f>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   1060 <asprintf@plt>
jmp    12a3 <compile+0x123>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rdx
lea    -0x10(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1060 <asprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%r9
mov    -0x10(%rbp),%rax
lea    0xde9(%rip),%rsi
lea    0xde6(%rip),%rdx
lea    0xde7(%rip),%rcx
lea    0xde6(%rip),%r8
lea    0xde6(%rip),%r10
xor    %edi,%edi
mov    %rsi,%rdi
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
call   1090 <execlp@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compile>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e4d(%rip),%r15
mov    (%r15),%edi
lea    0xe63(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
movq   $0x0,(%rsp)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     120e <compile+0x8e>
mov    %rax,%rbx
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    0xc(%rsp),%edx
test   %edx,%edx
je     11ee <compile+0x6e>
mov    (%r15),%edi
lea    0xe2b(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe34(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1060 <asprintf@plt>
mov    (%rsp),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe0d(%rip),%rsi
mov    %rsp,%rdi
mov    %rax,%rdx
call   1060 <asprintf@plt>
mov    (%rsp),%rdi
call   1030 <unlink@plt>
sub    $0x8,%rsp
lea    0xe11(%rip),%rax
lea    0xdf1(%rip),%rdi
lea    0xdee(%rip),%rdx
lea    0xdef(%rip),%rcx
lea    0xdee(%rip),%r8
mov    %rdi,%rsi
mov    %r14,%r9
push   $0x0
push   0x10(%rsp)
push   %rax
call   1090 <execlp@plt>
add    $0x20,%rsp
mov    (%r15),%edi
lea    0xdd8(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"[+] Compiling %s\0A\00"
@.str.1 = [23 x i8] c"failed to compile: %d\0A\00"
@.str.2 = [10 x i8] c".%d.dylib\00"
@.str.3 = [4 x i8] c"gcc\00"
@.str.4 = [8 x i8] c"-shared\00"
@.str.5 = [6 x i8] c"-arch\00"
@.str.6 = [7 x i8] c"x86_64\00"
@.str.7 = [3 x i8] c"-o\00"
@.str.8 = [25 x i8] c"failed to exec compiler\0A\00"
define i8* @compile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %2
%8 = call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7)
store i8* null, i8** %3
%9 = call i64 (...) @fork()
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %1
store i32 0, i32* %5
%13 = load i64, i64* %4
%14 = call i32 @waitpid(i64 %13, i32* %5, i32 0)
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i32, i32* %5
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %12
%22 = load i64, i64* %4
%23 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %22)
br label %35
24: ; preds = %1
%25 = call i64 (...) @getpid()
%26 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %25)
%27 = load i8*, i8** %3
%28 = call i32 @unlink(i8* %27)
%29 = load i8*, i8** %2
%30 = load i8*, i8** %3
%31 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %30, i32* null)
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%34 = call i32 @exit(i32 2)
unreachable
35: ; preds = %21
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"[+] Compiling %s\0A\00"
@.str.1 = [23 x i8] c"failed to compile: %d\0A\00"
@.str.2 = [10 x i8] c".%d.dylib\00"
@.str.3 = [4 x i8] c"gcc\00"
@.str.4 = [8 x i8] c"-shared\00"
@.str.5 = [6 x i8] c"-arch\00"
@.str.6 = [7 x i8] c"x86_64\00"
@.str.7 = [3 x i8] c"-o\00"
@.str.8 = [25 x i8] c"failed to exec compiler\0A\00"
define i8* @compile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %20, label %9
9: ; preds = %1
%10 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%11 = call i32 @waitpid(i64 %7, i32* %3, i32 0)
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %17
17: ; preds = %14, %9
%18 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %7)
%19 = load i8*, i8** %2
ret i8* %19
20: ; preds = %1
%21 = tail call i64 (...) @getpid()
%22 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %21)
%23 = load i8*, i8** %2
%24 = call i32 @unlink(i8* %23)
%25 = load i8*, i8** %2
%26 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %25, i32* null)
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
%29 = call i32 @exit(i32 2)
unreachable
}