#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*/ cleanup_lupkg(char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ *mkdtemp(char *);
int mount_lupkg(char *, char *);
int /*<<< orphan*/ readlink(char *, char *, int);
int run_lupkg(char *, char **);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char *cmd;
  char lupkg_dir[] = "/tmp/.lupkg_XXXXXX";
  char srcpath[1024] = {0};
  int status = 0;
  pid_t pid;
  readlink("/proc/self/exe", srcpath, 1024);
  if (mkdtemp(lupkg_dir) == NULL) {
    fprintf(stderr, "Could not make package dir\n");
    return 1;
  }
  if ((status = mount_lupkg(srcpath, lupkg_dir))) {
    fprintf(stderr, "Could not mount package\n");
    cleanup_lupkg(lupkg_dir);
    return status;
  }
  pid = fork();
  if (pid == 0) {
    if ((status = run_lupkg(lupkg_dir, argv))) {
      fprintf(stderr, "Could not run package\n");
    }
    return status;
  }
  waitpid(pid, &status, 0);
  cleanup_lupkg(lupkg_dir);
  return status;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe50(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe4d(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe4a(%rip),%ax
mov    %ax,-0x20(%rbp)
mov    0xe42(%rip),%al
mov    %al,-0x1e(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
movl   $0x0,-0x434(%rbp)
lea    -0x430(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x400,%edx
call   1030 <readlink@plt>
lea    -0x30(%rbp),%rdi
call   1080 <mkdtemp@plt>
cmp    $0x0,%rax
jne    123c <main+0xac>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1309 <main+0x179>
lea    -0x430(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <mount_lupkg@plt>
mov    %eax,-0x434(%rbp)
cmp    $0x0,%eax
je     1287 <main+0xf7>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x30(%rbp),%rdi
call   10a0 <cleanup_lupkg@plt>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1309 <main+0x179>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
jne    12e2 <main+0x152>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <run_lupkg@plt>
mov    %eax,-0x434(%rbp)
cmp    $0x0,%eax
je     12d4 <main+0x144>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1309 <main+0x179>
mov    -0x440(%rbp),%rdi
lea    -0x434(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
lea    -0x30(%rbp),%rdi
call   10a0 <cleanup_lupkg@plt>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x438,%rsp
mov    %rsi,%rbx
movaps 0xe5d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x585858,0x1f(%rsp)
lea    0x30(%rsp),%rbp
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
lea    0xe48(%rip),%rdi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
lea    0x10(%rsp),%rdi
call   1080 <mkdtemp@plt>
test   %rax,%rax
je     1221 <main+0x91>
lea    0x30(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1070 <mount_lupkg@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
je     123d <main+0xad>
mov    %eax,%ebp
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
lea    0x10(%rsp),%rdi
call   10a0 <cleanup_lupkg@plt>
jmp    1294 <main+0x104>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%ebp
jmp    1294 <main+0x104>
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     126a <main+0xda>
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1090 <waitpid@plt>
lea    0x10(%rsp),%rdi
call   10a0 <cleanup_lupkg@plt>
mov    0xc(%rsp),%ebp
jmp    1294 <main+0x104>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1060 <run_lupkg@plt>
test   %eax,%eax
je     1294 <main+0x104>
mov    %eax,%ebx
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x438,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.lupkg_dir = [19 x i8] c"/tmp/.lupkg_XXXXXX\00"
@.str = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Could not make package dir\0A\00"
@.str.2 = [25 x i8] c"Could not mount package\0A\00"
@.str.3 = [23 x i8] c"Could not run package\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [19 x i8]
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [19 x i8]* %7 to i8*
%12 = bitcast [1024 x i8]* %8 to i8*
store i32 0, i32* %9
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%14 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %13, i32 1024)
%15 = getelementptr inbounds [19 x i8], [19 x i8]* %7, i64 0, i64 0
%16 = call i32* @mkdtemp(i8* %15)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %52
21: ; preds = %2
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%23 = getelementptr inbounds [19 x i8], [19 x i8]* %7, i64 0, i64 0
%24 = call i32 @mount_lupkg(i8* %22, i8* %23)
store i32 %24, i32* %9
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%29 = getelementptr inbounds [19 x i8], [19 x i8]* %7, i64 0, i64 0
%30 = call i32 @cleanup_lupkg(i8* %29)
%31 = load i32, i32* %9
store i32 %31, i32* %3
br label %52
32: ; preds = %21
%33 = call i64 (...) @fork()
store i64 %33, i64* %10
%34 = load i64, i64* %10
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %32
%37 = getelementptr inbounds [19 x i8], [19 x i8]* %7, i64 0, i64 0
%38 = load i8**, i8*** %5
%39 = call i32 @run_lupkg(i8* %37, i8** %38)
store i32 %39, i32* %9
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %41, %36
%45 = load i32, i32* %9
store i32 %45, i32* %3
br label %52
46: ; preds = %32
%47 = load i64, i64* %10
%48 = call i32 @waitpid(i64 %47, i32* %9, i32 0)
%49 = getelementptr inbounds [19 x i8], [19 x i8]* %7, i64 0, i64 0
%50 = call i32 @cleanup_lupkg(i8* %49)
%51 = load i32, i32* %9
store i32 %51, i32* %3
br label %52
52: ; preds = %46, %44, %26, %18
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@__const.main.lupkg_dir = [19 x i8] c"/tmp/.lupkg_XXXXXX\00"
@.str = [15 x i8] c"/proc/self/exe\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Could not make package dir\0A\00"
@.str.2 = [25 x i8] c"Could not mount package\0A\00"
@.str.3 = [23 x i8] c"Could not run package\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [19 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [19 x i8], [19 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
%9 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %7, i32 1024)
%10 = call i32* @mkdtemp(i8* %6)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %35
15: ; preds = %2
%16 = call i32 @mount_lupkg(i8* %7, i8* %6)
store i32 %16, i32* %5
%17 = icmp eq i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @cleanup_lupkg(i8* %6)
br label %35
22: ; preds = %15
%23 = call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = call i32 @run_lupkg(i8* %6, i8** %1)
%27 = icmp eq i32 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %35
31: ; preds = %22
%32 = call i32 @waitpid(i64 %23, i32* %5, i32 0)
%33 = call i32 @cleanup_lupkg(i8* %6)
%34 = load i32, i32* %5
br label %35
35: ; preds = %25, %28, %31, %18, %12
%36 = phi i32 [ 1, %12 ], [ %16, %18 ], [ %34, %31 ], [ %26, %28 ], [ 0, %25 ]
ret i32 %36
}