#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*/ RB_AUTOBOOT;
int /*<<< orphan*/ TIOCSCTTY;
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*/ 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) {
    run_process("/bin/sh");
  } else {
    pid_t corpse;
    do {
      corpse = waitpid(-1, &status, 0);
    } while (corpse != child);
  }
  reboot(RB_AUTOBOOT);
  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    0xe43(%rip),%rdi
call   1040 <puts@plt>
mov    $0x0,%al
call   1050 <do_mounts@plt>
mov    $0x0,%al
call   1080 <setsid@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1070 <ioctl@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1217 <main+0x77>
mov    -0x18(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1296 <main+0xf6>
cmpq   $0x0,-0x18(%rbp)
jne    1233 <main+0x93>
lea    0xe01(%rip),%rdi
call   1030 <run_process@plt>
jmp    125f <main+0xbf>
jmp    1238 <main+0x98>
mov    $0xffffffff,%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1238 <main+0x98>
jmp    125f <main+0xbf>
jmp    1264 <main+0xc4>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   10a0 <reboot@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xda8(%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    0xe55(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <do_mounts@plt>
xor    %eax,%eax
call   1080 <setsid@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1070 <ioctl@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     1206 <main+0x66>
je     1212 <main+0x72>
lea    0x4(%rsp),%r14
nopl   0x0(%rax)
mov    $0xffffffff,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    %rbx,%rax
jne    11f0 <main+0x50>
jmp    121e <main+0x7e>
lea    0xdff(%rip),%rdi
mov    %rbx,%rsi
jmp    1244 <main+0xa4>
lea    0xe11(%rip),%rdi
call   1030 <run_process@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   10a0 <reboot@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdf1(%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 = [8 x i8] c"/bin/sh\00"
@RB_AUTOBOOT = global i32 0
@.str.3 = [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 %41
21: ; preds = %2
%22 = load i64, i64* %6
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @run_process(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %34
26: ; preds = %21
br label %27
27: ; preds = %29, %26
%28 = call i64 @waitpid(i32 -1, i32* %7, i32 0)
store i64 %28, i64* %8
br label %29
29: ; preds = %27
%30 = load i64, i64* %8
%31 = load i64, i64* %6
%32 = icmp ne i64 %30, %31
br i1 %32, label %27, label %33
33: ; preds = %29
br label %34
34: ; preds = %33, %24
br label %35
35: ; preds = %34
%36 = load i32, i32* @RB_AUTOBOOT
%37 = call i32 @reboot(i32 %36)
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %39)
store i32 0, i32* %3
br label %41
41: ; preds = %35, %17
%42 = load i32, i32* %3
ret i32 %42
}

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 = [8 x i8] c"/bin/sh\00"
@RB_AUTOBOOT = global i32 0
@.str.3 = [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 %28
15: ; preds = %2
%16 = icmp eq i64 %10, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @run_process(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %22
19: ; preds = %15, %19
%20 = call i64 @waitpid(i32 -1, i32* %3, i32 0)
%21 = icmp eq i64 %20, %10
br i1 %21, label %22, label %19
22: ; preds = %19, %17
%23 = load i32, i32* @RB_AUTOBOOT
%24 = call i32 @reboot(i32 %23)
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %22, %12
ret i32 0
}