#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 PATH_MAX;
int /*<<< orphan*/ abort();
scalar_t__ chown(char *, int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execv(char *, char **);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
pid_t execute_in_jail(int exe, int comms) {
  char path[PATH_MAX];
  sprintf(path, "/proc/%d/fd/%d", getpid(), exe);
  pid_t pid = fork();
  if (pid == 0) {
    if (chown("/tmp/sboxchroot", 1339, 1339) != 0) {
      perror("chown");
      abort();
    }
    dup2(comms, 37);
    char *args[] = {
        "/root/nsjail",
        "-Q",
        "--pass_fd",
        "37",
        "-u",
        "1000:1339",
        "-g",
        "1000:1339",
        "--rw",
        "--chroot",
        "/tmp/sboxchroot",
        "--proc_rw",
        "--execute_fd",
        "--disable_rlimits",
        "--",
        path,
        NULL,
    };
    execv("/root/nsjail", args);
    abort();
  }
  close(comms);
  return pid;
}


x86 assembly (clang, -O0):
<execute_in_jail>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xb8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0xb8(%rbp),%rdi
mov    %eax,%edx
mov    -0x4(%rbp),%ecx
lea    0xe16(%rip),%rsi
call   1090 <sprintf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1326 <execute_in_jail+0x196>
lea    0xe03(%rip),%rdi
mov    $0x53b,%edx
mov    %edx,%esi
call   1070 <chown@plt>
cmp    $0x0,%rax
je     1235 <execute_in_jail+0xa5>
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%edi
mov    $0x25,%esi
call   1050 <dup2@plt>
mov    -0xb8(%rbp),%rax
lea    0xdd5(%rip),%rcx
mov    %rcx,-0xb0(%rbp)
lea    0xdd4(%rip),%rcx
mov    %rcx,-0xa8(%rbp)
lea    0xdc9(%rip),%rcx
mov    %rcx,-0xa0(%rbp)
lea    0xdc5(%rip),%rcx
mov    %rcx,-0x98(%rbp)
lea    0xdba(%rip),%rcx
mov    %rcx,-0x90(%rbp)
lea    0xdaf(%rip),%rcx
mov    %rcx,-0x88(%rbp)
lea    0xdab(%rip),%rcx
mov    %rcx,-0x80(%rbp)
lea    0xd96(%rip),%rcx
mov    %rcx,-0x78(%rbp)
lea    0xd98(%rip),%rcx
mov    %rcx,-0x70(%rbp)
lea    0xd92(%rip),%rcx
mov    %rcx,-0x68(%rbp)
lea    0xd3f(%rip),%rcx
mov    %rcx,-0x60(%rbp)
lea    0xd85(%rip),%rcx
mov    %rcx,-0x58(%rbp)
lea    0xd84(%rip),%rcx
mov    %rcx,-0x50(%rbp)
lea    0xd86(%rip),%rcx
mov    %rcx,-0x48(%rbp)
lea    0xd8d(%rip),%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0xb0(%rbp),%rsi
lea    0xd0b(%rip),%rdi
call   10a0 <execv@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_in_jail>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe31(%rip),%rsi
mov    %r12,%rdi
mov    %eax,%edx
mov    %ebx,%ecx
call   1090 <sprintf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1202 <execute_in_jail+0x72>
mov    %rax,%rbx
mov    %r14d,%edi
call   1060 <close@plt>
mov    %rbx,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe06(%rip),%r15
mov    %r15,%rdi
mov    $0x53b,%esi
mov    $0x53b,%edx
call   1070 <chown@plt>
test   %rax,%rax
jne    12f9 <execute_in_jail+0x169>
mov    %r14d,%edi
mov    $0x25,%esi
call   1050 <dup2@plt>
lea    0xded(%rip),%rdi
mov    %rdi,-0xb0(%rbp)
lea    0xdec(%rip),%rax
mov    %rax,-0xa8(%rbp)
lea    0xde1(%rip),%rax
mov    %rax,-0xa0(%rbp)
lea    0xddd(%rip),%rax
mov    %rax,-0x98(%rbp)
lea    0xdd2(%rip),%rax
mov    %rax,-0x90(%rbp)
lea    0xdc7(%rip),%rax
mov    %rax,-0x88(%rbp)
lea    0xdc3(%rip),%rcx
mov    %rcx,-0x80(%rbp)
mov    %rax,-0x78(%rbp)
lea    0xdb7(%rip),%rax
mov    %rax,-0x70(%rbp)
lea    0xdb1(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    %r15,-0x60(%rbp)
lea    0xdab(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    0xdaa(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xdac(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    0xdb3(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    %r12,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0xb0(%rbp),%rsi
call   10a0 <execv@plt>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xd1f(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/%d/fd/%d\00"
@.str.1 = [16 x i8] c"/tmp/sboxchroot\00"
@.str.2 = [6 x i8] c"chown\00"
@.str.3 = [13 x i8] c"/root/nsjail\00"
@.str.4 = [3 x i8] c"-Q\00"
@.str.5 = [10 x i8] c"--pass_fd\00"
@.str.6 = [3 x i8] c"37\00"
@.str.7 = [3 x i8] c"-u\00"
@.str.8 = [10 x i8] c"1000:1339\00"
@.str.9 = [3 x i8] c"-g\00"
@.str.10 = [5 x i8] c"--rw\00"
@.str.11 = [9 x i8] c"--chroot\00"
@.str.12 = [10 x i8] c"--proc_rw\00"
@.str.13 = [13 x i8] c"--execute_fd\00"
@.str.14 = [18 x i8] c"--disable_rlimits\00"
@.str.15 = [3 x i8] c"--\00"
define i64 @execute_in_jail(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca [17 x i8*]
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = call i32 (...) @getpid()
%14 = load i32, i32* %3
%15 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
%16 = call i64 (...) @fork()
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %48
19: ; preds = %2
%20 = call i64 @chown(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 1339, i32 1339)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 (...) @abort()
unreachable
25: ; preds = %19
%26 = load i32, i32* %4
%27 = call i32 @dup2(i32 %26, i32 37)
%28 = getelementptr inbounds [17 x i8*], [17 x i8*]* %8, i64 0, i64 0
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %28
%29 = getelementptr inbounds i8*, i8** %28, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %29
%30 = getelementptr inbounds i8*, i8** %29, i64 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8** %30
%31 = getelementptr inbounds i8*, i8** %30, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8** %31
%32 = getelementptr inbounds i8*, i8** %31, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8** %32
%33 = getelementptr inbounds i8*, i8** %32, i64 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8** %33
%34 = getelementptr inbounds i8*, i8** %33, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8** %34
%35 = getelementptr inbounds i8*, i8** %34, i64 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8** %35
%36 = getelementptr inbounds i8*, i8** %35, i64 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i8** %36
%37 = getelementptr inbounds i8*, i8** %36, i64 1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0), i8** %37
%38 = getelementptr inbounds i8*, i8** %37, i64 1
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8** %38
%39 = getelementptr inbounds i8*, i8** %38, i64 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.12, i64 0, i64 0), i8** %39
%40 = getelementptr inbounds i8*, i8** %39, i64 1
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.13, i64 0, i64 0), i8** %40
%41 = getelementptr inbounds i8*, i8** %40, i64 1
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.14, i64 0, i64 0), i8** %41
%42 = getelementptr inbounds i8*, i8** %41, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.15, i64 0, i64 0), i8** %42
%43 = getelementptr inbounds i8*, i8** %42, i64 1
store i8* %12, i8** %43
%44 = getelementptr inbounds i8*, i8** %43, i64 1
store i8* null, i8** %44
%45 = getelementptr inbounds [17 x i8*], [17 x i8*]* %8, i64 0, i64 0
%46 = call i32 @execv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %45)
%47 = call i32 (...) @abort()
unreachable
48: ; preds = %2
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
%51 = load i64, i64* %7
%52 = load i8*, i8** %5
ret i64 %51
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/%d/fd/%d\00"
@.str.1 = [16 x i8] c"/tmp/sboxchroot\00"
@.str.2 = [6 x i8] c"chown\00"
@.str.3 = [13 x i8] c"/root/nsjail\00"
@.str.4 = [3 x i8] c"-Q\00"
@.str.5 = [10 x i8] c"--pass_fd\00"
@.str.6 = [3 x i8] c"37\00"
@.str.7 = [3 x i8] c"-u\00"
@.str.8 = [10 x i8] c"1000:1339\00"
@.str.9 = [3 x i8] c"-g\00"
@.str.10 = [5 x i8] c"--rw\00"
@.str.11 = [9 x i8] c"--chroot\00"
@.str.12 = [10 x i8] c"--proc_rw\00"
@.str.13 = [13 x i8] c"--execute_fd\00"
@.str.14 = [18 x i8] c"--disable_rlimits\00"
@.str.15 = [3 x i8] c"--\00"
define i64 @execute_in_jail(i32 %0, i32 %1) {
%3 = alloca [17 x i8*]
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i32 (...) @getpid()
%8 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %0)
%9 = tail call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %39
11: ; preds = %2
%12 = tail call i64 @chown(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 1339, i32 1339)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 (...) @abort()
unreachable
17: ; preds = %11
%18 = tail call i32 @dup2(i32 %1, i32 37)
%19 = bitcast [17 x i8*]* %3 to i8*
%20 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %20
%21 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %21
%22 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8** %22
%23 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8** %23
%24 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 4
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8** %24
%25 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8** %25
%26 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 6
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8** %26
%27 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 7
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8** %27
%28 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 8
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0), i8** %28
%29 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 9
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0), i8** %29
%30 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 10
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8** %30
%31 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 11
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.12, i64 0, i64 0), i8** %31
%32 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 12
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.13, i64 0, i64 0), i8** %32
%33 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 13
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.14, i64 0, i64 0), i8** %33
%34 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 14
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.15, i64 0, i64 0), i8** %34
%35 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 15
store i8* %6, i8** %35
%36 = getelementptr inbounds [17 x i8*], [17 x i8*]* %3, i64 0, i64 16
store i8* null, i8** %36
%37 = call i32 @execv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %20)
%38 = call i32 (...) @abort()
unreachable
39: ; preds = %2
%40 = tail call i32 @close(i32 %1)
ret i64 %9
}