#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*/ SIGKILL;
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WUNTRACED;
int execute_helper();
scalar_t__ execute_in_jail(int, int);
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
int load_elf();
int /*<<< orphan*/ puts(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int exe = load_elf();
  if (exe < 0) {
    return -1;
  }
  puts("ok");
  int comms = execute_helper();
  if (comms < 0) {
    return -1;
  }
  pid_t child = execute_in_jail(exe, comms);
  if (child < 0) {
    return -1;
  }
  puts("started");
  int status;
  if (waitpid(child, &status, WUNTRACED) != child) {
    puts("internal error");
    return -1;
  }
  if (WIFEXITED(status)) {
    puts("exited");
    if (WEXITSTATUS(status) != 0) {
      return -1;
    }
  } else {
    kill(child, SIGKILL);
    puts("killed");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <load_elf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b6 <main+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <main+0x132>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    $0x0,%al
call   10a0 <execute_helper@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11e2 <main+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <main+0x132>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <execute_in_jail@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1208 <main+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <main+0x132>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
mov    -0x20(%rbp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rsi
call   1080 <waitpid@plt>
cmp    -0x20(%rbp),%rax
je     124c <main+0xcc>
lea    0xdd0(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <main+0x132>
mov    -0x24(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     128d <main+0x10d>
lea    0xdb5(%rip),%rdi
call   1030 <puts@plt>
mov    -0x24(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     1288 <main+0x108>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <main+0x132>
jmp    12ab <main+0x12b>
mov    -0x20(%rbp),%rdi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
lea    0xd7b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1050 <load_elf@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1256 <main+0xd6>
mov    %eax,%ebp
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <execute_helper@plt>
test   %eax,%eax
js     1256 <main+0xd6>
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <execute_in_jail@plt>
test   %rax,%rax
js     1256 <main+0xd6>
mov    %rax,%r14
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
call   1080 <waitpid@plt>
cmp    %r14,%rax
jne    1224 <main+0xa4>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     1237 <main+0xb7>
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1256 <main+0xd6>
jmp    1254 <main+0xd4>
lea    0xde0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    1256 <main+0xd6>
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <kill@plt>
lea    0xdd2(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"ok\00"
@.str.1 = [8 x i8] c"started\00"
@WUNTRACED = global i32 0
@.str.2 = [15 x i8] c"internal error\00"
@.str.3 = [7 x i8] c"exited\00"
@SIGKILL = global i32 0
@.str.4 = [7 x i8] c"killed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (...) @load_elf()
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %53
14: ; preds = %2
%15 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%16 = call i32 (...) @execute_helper()
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %3
br label %53
20: ; preds = %14
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = call i64 @execute_in_jail(i32 %21, i32 %22)
store i64 %23, i64* %8
%24 = load i64, i64* %8
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 -1, i32* %3
br label %53
27: ; preds = %20
%28 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%29 = load i64, i64* %8
%30 = load i32, i32* @WUNTRACED
%31 = call i64 @waitpid(i64 %29, i32* %9, i32 %30)
%32 = load i64, i64* %8
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
36: ; preds = %27
%37 = load i32, i32* %9
%38 = call i64 @WIFEXITED(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %36
%41 = call i32 @puts(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* %9
%43 = call i64 @WEXITSTATUS(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
store i32 -1, i32* %3
br label %53
46: ; preds = %40
br label %52
47: ; preds = %36
%48 = load i64, i64* %8
%49 = load i32, i32* @SIGKILL
%50 = call i32 @kill(i64 %48, i32 %49)
%51 = call i32 @puts(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %47, %46
store i32 0, i32* %3
br label %53
53: ; preds = %52, %45, %34, %26, %19, %13
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [3 x i8] c"ok\00"
@.str.1 = [8 x i8] c"started\00"
@WUNTRACED = global i32 0
@.str.2 = [15 x i8] c"internal error\00"
@.str.3 = [7 x i8] c"exited\00"
@SIGKILL = global i32 0
@.str.4 = [7 x i8] c"killed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i32 (...) @load_elf()
%5 = icmp slt i32 %4, 0
br i1 %5, label %37, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 (...) @execute_helper()
%9 = icmp slt i32 %8, 0
br i1 %9, label %37, label %10
10: ; preds = %6
%11 = tail call i64 @execute_in_jail(i32 %4, i32 %8)
%12 = icmp slt i64 %11, 0
br i1 %12, label %37, label %13
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%15 = bitcast i32* %3 to i8*
%16 = load i32, i32* @WUNTRACED
%17 = call i64 @waitpid(i64 %11, i32* %3, i32 %16)
%18 = icmp eq i64 %17, %11
br i1 %18, label %21, label %19
19: ; preds = %13
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %35
21: ; preds = %13
%22 = load i32, i32* %3
%23 = call i64 @WIFEXITED(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %21
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* %3
%28 = call i64 @WEXITSTATUS(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %35
30: ; preds = %21
%31 = load i32, i32* @SIGKILL
%32 = call i32 @kill(i64 %11, i32 %31)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %25, %30
br label %35
35: ; preds = %25, %34, %19
%36 = phi i32 [ -1, %19 ], [ 0, %34 ], [ -1, %25 ]
br label %37
37: ; preds = %6, %10, %35, %2
%38 = phi i32 [ -1, %2 ], [ -1, %6 ], [ %36, %35 ], [ -1, %10 ]
ret i32 %38
}