#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*/ LINUX_REBOOT_CMD_RESTART;
int /*<<< orphan*/ R_OK;
int /*<<< orphan*/ TIOCSCTTY;
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ reboot(int /*<<< orphan*/);
int /*<<< orphan*/ run_process(char *);
int /*<<< orphan*/ run_process_sandbox(char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t child;
  int status;
  setsid();
  ioctl(0, TIOCSCTTY, 1);
  child = fork();
  if (child < 0) {
    printf("Fatal: fork() failed with %d\n", child);
    return 0;
  } else if (child == 0) {
    if (access("/virt/sandbox.sh", R_OK) == 0)
      run_process_sandbox("/bin/sh");
    else
      run_process("/bin/sh");
  } else {
    waitpid(child, &status, 0);
  }
  reboot(LINUX_REBOOT_CMD_RESTART);
  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)
mov    $0x0,%al
call   1060 <setsid@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <ioctl@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11f4 <main+0x64>
mov    -0x18(%rbp),%rsi
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1269 <main+0xd9>
cmpq   $0x0,-0x18(%rbp)
jne    1240 <main+0xb0>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0xe0f(%rip),%rdi
call   1090 <access@plt>
cmp    $0x0,%rax
jne    122f <main+0x9f>
lea    0xe0a(%rip),%rdi
call   1070 <run_process_sandbox@plt>
jmp    123b <main+0xab>
lea    0xdf9(%rip),%rdi
call   1030 <run_process@plt>
jmp    124f <main+0xbf>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
jmp    1254 <main+0xc4>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   10a0 <reboot@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <ioctl@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11cc <main+0x3c>
je     11dd <main+0x4d>
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1080 <waitpid@plt>
jmp    120a <main+0x7a>
lea    0xe2d(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
jmp    1218 <main+0x88>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
call   1090 <access@plt>
lea    0xe36(%rip),%rdi
test   %rax,%rax
je     1205 <main+0x75>
call   1030 <run_process@plt>
jmp    120a <main+0x7a>
call   1070 <run_process_sandbox@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   10a0 <reboot@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@TIOCSCTTY = global i32 0
@.str = [30 x i8] c"Fatal: fork() failed with %d\0A\00"
@.str.1 = [17 x i8] c"/virt/sandbox.sh\00"
@R_OK = global i32 0
@.str.2 = [8 x i8] c"/bin/sh\00"
@LINUX_REBOOT_CMD_RESTART = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @setsid()
%9 = load i32, i32* @TIOCSCTTY
%10 = call i32 @ioctl(i32 0, i32 %9, i32 1)
%11 = call i64 (...) @fork()
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i64, i64* %6
%16 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %15)
store i32 0, i32* %3
br label %36
17: ; preds = %2
%18 = load i64, i64* %6
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %17
%21 = load i32, i32* @R_OK
%22 = call i64 @access(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @run_process_sandbox(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %28
26: ; preds = %20
%27 = call i32 @run_process(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %24
br label %32
29: ; preds = %17
%30 = load i64, i64* %6
%31 = call i32 @waitpid(i64 %30, i32* %7, i32 0)
br label %32
32: ; preds = %29, %28
br label %33
33: ; preds = %32
%34 = load i32, i32* @LINUX_REBOOT_CMD_RESTART
%35 = call i32 @reboot(i32 %34)
store i32 0, i32* %3
br label %36
36: ; preds = %33, %14
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@TIOCSCTTY = global i32 0
@.str = [30 x i8] c"Fatal: fork() failed with %d\0A\00"
@.str.1 = [17 x i8] c"/virt/sandbox.sh\00"
@R_OK = global i32 0
@.str.2 = [8 x i8] c"/bin/sh\00"
@LINUX_REBOOT_CMD_RESTART = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @setsid()
%6 = load i32, i32* @TIOCSCTTY
%7 = tail call i32 @ioctl(i32 0, i32 %6, i32 1)
%8 = tail call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %8)
br label %27
12: ; preds = %2
%13 = icmp eq i64 %8, 0
br i1 %13, label %14, label %22
14: ; preds = %12
%15 = load i32, i32* @R_OK
%16 = tail call i64 @access(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @run_process_sandbox(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %24
20: ; preds = %14
%21 = tail call i32 @run_process(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %24
22: ; preds = %12
%23 = call i32 @waitpid(i64 %8, i32* %3, i32 0)
br label %24
24: ; preds = %22, %20, %18
%25 = load i32, i32* @LINUX_REBOOT_CMD_RESTART
%26 = call i32 @reboot(i32 %25)
br label %27
27: ; preds = %24, %10
ret i32 0
}