#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*/);
int /*<<< orphan*/ do_mounts();
int /*<<< orphan*/ errno;
scalar_t__ fork();
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ reboot(int /*<<< orphan*/);
int /*<<< orphan*/ run_process(char *);
int /*<<< orphan*/ run_process_sandbox(char *);
int /*<<< orphan*/ setsid();
char *strerror(int /*<<< orphan*/);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t child;
  int status;
  puts("Mounting...");
  do_mounts();
  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 {
    pid_t corpse;
    do {
      corpse = waitpid(-1, &status, 0);
    } while (corpse != child);
  }
  reboot(LINUX_REBOOT_CMD_RESTART);
  printf("Init failed: %s\n", strerror(errno));
  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)
lea    0xe23(%rip),%rdi
call   1040 <puts@plt>
mov    $0x0,%al
call   1050 <do_mounts@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1070 <ioctl@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1237 <main+0x77>
mov    -0x18(%rbp),%rsi
lea    0xde6(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <main+0x126>
cmpq   $0x0,-0x18(%rbp)
jne    1283 <main+0xc3>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
call   10b0 <access@plt>
cmp    $0x0,%rax
jne    1272 <main+0xb2>
lea    0xdd3(%rip),%rdi
call   1090 <run_process_sandbox@plt>
jmp    127e <main+0xbe>
lea    0xdc2(%rip),%rdi
call   1030 <run_process@plt>
jmp    12af <main+0xef>
jmp    1288 <main+0xc8>
mov    $0xffffffff,%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1288 <main+0xc8>
jmp    12af <main+0xef>
jmp    12b4 <main+0xf4>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   10c0 <reboot@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd69(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe35(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <do_mounts@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1070 <ioctl@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     1226 <main+0x66>
je     1232 <main+0x72>
lea    0x4(%rsp),%r14
nopl   0x0(%rax)
mov    $0xffffffff,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
cmp    %rbx,%rax
jne    1210 <main+0x50>
jmp    125f <main+0x9f>
lea    0xddf(%rip),%rdi
mov    %rbx,%rsi
jmp    1285 <main+0xc5>
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    0xde8(%rip),%rdi
call   10b0 <access@plt>
lea    0xded(%rip),%rdi
test   %rax,%rax
je     125a <main+0x9a>
call   1030 <run_process@plt>
jmp    125f <main+0x9f>
call   1090 <run_process_sandbox@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   10c0 <reboot@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xdc1(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Mounting...\00"
@TIOCSCTTY = global i32 0
@.str.1 = [30 x i8] c"Fatal: fork() failed with %d\0A\00"
@.str.2 = [17 x i8] c"/virt/sandbox.sh\00"
@R_OK = global i32 0
@.str.3 = [8 x i8] c"/bin/sh\00"
@LINUX_REBOOT_CMD_RESTART = global i32 0
@.str.4 = [17 x i8] c"Init failed: %s\0A\00"
@errno = 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
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (...) @do_mounts()
%11 = call i32 (...) @setsid()
%12 = load i32, i32* @TIOCSCTTY
%13 = call i32 @ioctl(i32 0, i32 %12, i32 1)
%14 = call i64 (...) @fork()
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i64, i64* %6
%19 = inttoptr i64 %18 to i8*
%20 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 0, i32* %3
br label %48
21: ; preds = %2
%22 = load i64, i64* %6
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = load i32, i32* @R_OK
%26 = call i64 @access(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @run_process_sandbox(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %32
30: ; preds = %24
%31 = call i32 @run_process(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %28
br label %41
33: ; preds = %21
br label %34
34: ; preds = %36, %33
%35 = call i64 @waitpid(i32 -1, i32* %7, i32 0)
store i64 %35, i64* %8
br label %36
36: ; preds = %34
%37 = load i64, i64* %8
%38 = load i64, i64* %6
%39 = icmp ne i64 %37, %38
br i1 %39, label %34, label %40
40: ; preds = %36
br label %41
41: ; preds = %40, %32
br label %42
42: ; preds = %41
%43 = load i32, i32* @LINUX_REBOOT_CMD_RESTART
%44 = call i32 @reboot(i32 %43)
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %46)
store i32 0, i32* %3
br label %48
48: ; preds = %42, %17
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [12 x i8] c"Mounting...\00"
@TIOCSCTTY = global i32 0
@.str.1 = [30 x i8] c"Fatal: fork() failed with %d\0A\00"
@.str.2 = [17 x i8] c"/virt/sandbox.sh\00"
@R_OK = global i32 0
@.str.3 = [8 x i8] c"/bin/sh\00"
@LINUX_REBOOT_CMD_RESTART = global i32 0
@.str.4 = [17 x i8] c"Init failed: %s\0A\00"
@errno = 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 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 (...) @do_mounts()
%7 = tail call i32 (...) @setsid()
%8 = load i32, i32* @TIOCSCTTY
%9 = tail call i32 @ioctl(i32 0, i32 %8, i32 1)
%10 = tail call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = inttoptr i64 %10 to i8*
%14 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %34
15: ; preds = %2
%16 = icmp eq i64 %10, 0
br i1 %16, label %17, label %25
17: ; preds = %15
%18 = load i32, i32* @R_OK
%19 = tail call i64 @access(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @run_process_sandbox(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %28
23: ; preds = %17
%24 = tail call i32 @run_process(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %28
25: ; preds = %15, %25
%26 = call i64 @waitpid(i32 -1, i32* %3, i32 0)
%27 = icmp eq i64 %26, %10
br i1 %27, label %28, label %25
28: ; preds = %25, %23, %21
%29 = load i32, i32* @LINUX_REBOOT_CMD_RESTART
%30 = call i32 @reboot(i32 %29)
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %28, %12
ret i32 0
}