#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 */
struct dirent {
  int /*<<< orphan*/ *d_name;
};
typedef int /*<<< orphan*/ sigset_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int O_DIRECTORY;
int O_RDONLY;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_DFL;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WNOHANG;
int WTERMSIG(int);
int /*<<< orphan*/ _SC_OPEN_MAX;
int __WALL;
int /*<<< orphan*/ _exit(int);
int atoi(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int dirfd(int /*<<< orphan*/ *);
int dup2(int, int);
int /*<<< orphan*/ *fdopendir(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getProcessStatus(int, char *, char *);
scalar_t__ hasChildren(int, scalar_t__);
int /*<<< orphan*/ kill(int, int);
int openat(int, char *, int);
int /*<<< orphan*/ readdir_r(int /*<<< orphan*/ *, struct dirent *,
                             struct dirent **);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ signal(int, int /*<<< orphan*/);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ sigwaitinfo(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
scalar_t__ waitpid(int /*<<< orphan*/, int *, int);
scalar_t__ write(int, char *, int);
void SystemInitProcess(int init_fd, int child_pid, int proc_fd, int null_fd) {
  int ret = 0;
  pid_t init_pid = getProcessStatus(proc_fd, "self", "Pid");
  if (init_pid <= 0) {
    fprintf(stderr,
            "Failed to determine real process id of new \"init\" process\n");
    _exit(1);
  }
  if (null_fd < 0 || dup2(null_fd, 0) != 0 || dup2(null_fd, 1) != 1 ||
      dup2(null_fd, 2) != 2) {
    fprintf(stderr, "Failed to point stdio to a safe place\n");
    _exit(1);
  }
  close(null_fd);
  int fds_fd = openat(proc_fd, "self/fd", O_RDONLY | O_DIRECTORY);
  DIR *dir = fds_fd >= 0 ? fdopendir(fds_fd) : NULL;
  if (dir == NULL) {
    for (int fd = sysconf(_SC_OPEN_MAX); --fd > 2;) {
      if (fd != init_fd && fd != proc_fd) {
        close(fd);
      }
    }
  } else {
    struct dirent de, *res;
    while (!readdir_r(dir, &de, &res) && res) {
      if (res->d_name[0] < '0')
        continue;
      int fd = atoi(res->d_name);
      if (fd > 2 && fd != init_fd && fd != proc_fd && fd != dirfd(dir)) {
        close(fd);
      }
    }
    closedir(dir);
  }
  sigset_t mask;
  sigemptyset(&mask);
  sigaddset(&mask, SIGCHLD);
  sigprocmask(SIG_BLOCK, &mask, NULL);
  if (write(init_fd, " ", 1)) {
  }
  close(init_fd);
  for (;;) {
    sigwaitinfo(&mask, NULL);
    bool retry = false;
    do {
      for (;;) {
        int status;
        pid_t pid = waitpid(0, &status, __WALL | WNOHANG);
        if (pid <= 0) {
          break;
        } else {
          retry = false;
        }
        if (pid == child_pid) {
          if (WIFEXITED(status)) {
            ret = WEXITSTATUS(status);
          } else if (WIFSIGNALED(status)) {
            ret = -WTERMSIG(status);
          }
        }
      }
      if (hasChildren(proc_fd, init_pid)) {
        retry = false;
      } else {
        if (retry) {
          if (ret < 0) {
            signal(-ret, SIG_DFL);
            kill(1, -ret);
            ret = 1;
          }
          _exit(ret);
        } else {
          retry = true;
        }
      }
    } while (retry);
  }
}


x86 assembly (clang, -O0):
<SystemInitProcess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%edi
lea    0xd4b(%rip),%rsi
lea    0xd49(%rip),%rdx
call   1060 <getProcessStatus@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jg     12ef <SystemInitProcess+0x5f>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd29(%rip),%rsi
call   1100 <fprintf@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
cmpl   $0x0,-0x10(%rbp)
jl     1338 <SystemInitProcess+0xa8>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1090 <dup2@plt>
cmp    $0x0,%eax
jne    1338 <SystemInitProcess+0xa8>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1090 <dup2@plt>
cmp    $0x1,%eax
jne    1338 <SystemInitProcess+0xa8>
mov    -0x10(%rbp),%edi
mov    $0x2,%esi
call   1090 <dup2@plt>
cmp    $0x2,%eax
je     1357 <SystemInitProcess+0xc7>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
lea    0xcfc(%rip),%rsi
call   1100 <fprintf@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
mov    -0x10(%rbp),%edi
call   10b0 <close@plt>
mov    -0xc(%rbp),%edi
mov    0x2c7f(%rip),%rax
mov    (%rax),%edx
mov    0x2c4e(%rip),%rax
or     (%rax),%edx
lea    0xcf0(%rip),%rsi
call   1080 <openat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     139e <SystemInitProcess+0x10e>
mov    -0x24(%rbp),%edi
call   1180 <fdopendir@plt>
mov    %rax,-0x68(%rbp)
jmp    13a9 <SystemInitProcess+0x119>
xor    %eax,%eax
mov    %rax,-0x68(%rbp)
jmp    13a9 <SystemInitProcess+0x119>
mov    -0x68(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1409 <SystemInitProcess+0x179>
mov    0x2c05(%rip),%rax
mov    (%rax),%edi
call   1170 <sysconf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
cmp    $0x2,%eax
jle    1404 <SystemInitProcess+0x174>
mov    -0x34(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     13ff <SystemInitProcess+0x16f>
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     13ff <SystemInitProcess+0x16f>
mov    -0x34(%rbp),%edi
call   10b0 <close@plt>
jmp    13cd <SystemInitProcess+0x13d>
jmp    14c2 <SystemInitProcess+0x232>
jmp    140e <SystemInitProcess+0x17e>
mov    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1070 <readdir_r@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x69(%rbp)
jne    143a <SystemInitProcess+0x1aa>
cmpq   $0x0,-0x48(%rbp)
setne  %al
mov    %al,-0x69(%rbp)
mov    -0x69(%rbp),%al
test   $0x1,%al
jne    144a <SystemInitProcess+0x1ba>
jmp    14b9 <SystemInitProcess+0x229>
mov    -0x48(%rbp),%rax
mov    (%rax),%rax
cmpl   $0x30,(%rax)
jge    145f <SystemInitProcess+0x1cf>
jmp    140e <SystemInitProcess+0x17e>
mov    -0x48(%rbp),%rax
mov    (%rax),%rdi
call   1190 <atoi@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x2,-0x4c(%rbp)
jle    14b4 <SystemInitProcess+0x224>
mov    -0x4c(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     14b4 <SystemInitProcess+0x224>
mov    -0x4c(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     14b4 <SystemInitProcess+0x224>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x70(%rbp)
mov    -0x30(%rbp),%rdi
call   10f0 <dirfd@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%eax
cmp    %ecx,%eax
je     14b4 <SystemInitProcess+0x224>
mov    -0x4c(%rbp),%edi
call   10b0 <close@plt>
jmp    140e <SystemInitProcess+0x17e>
mov    -0x30(%rbp),%rdi
call   10c0 <closedir@plt>
lea    -0x50(%rbp),%rdi
call   1110 <sigemptyset@plt>
mov    0x2b06(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdi
call   11b0 <sigaddset@plt>
mov    0x2ac4(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x4(%rbp),%edi
lea    0xb76(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0x0,%rax
je     1516 <SystemInitProcess+0x286>
jmp    1516 <SystemInitProcess+0x286>
mov    -0x4(%rbp),%edi
call   10b0 <close@plt>
lea    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1130 <sigwaitinfo@plt>
movl   $0x0,-0x54(%rbp)
jmp    1537 <SystemInitProcess+0x2a7>
mov    0x2a72(%rip),%rax
mov    (%rax),%edx
mov    0x2a99(%rip),%rax
or     (%rax),%edx
xor    %edi,%edi
lea    -0x58(%rbp),%rsi
call   1160 <waitpid@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jg     1568 <SystemInitProcess+0x2d8>
jmp    15d4 <SystemInitProcess+0x344>
movl   $0x0,-0x54(%rbp)
mov    -0x60(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jne    15cf <SystemInitProcess+0x33f>
mov    -0x58(%rbp),%edi
call   11a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     15a2 <SystemInitProcess+0x312>
mov    -0x58(%rbp),%edi
call   1150 <WEXITSTATUS@plt>
mov    %eax,-0x14(%rbp)
jmp    15ca <SystemInitProcess+0x33a>
mov    -0x58(%rbp),%edi
call   10d0 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     15c5 <SystemInitProcess+0x335>
mov    -0x58(%rbp),%edi
call   1140 <WTERMSIG@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    15ca <SystemInitProcess+0x33a>
jmp    15cf <SystemInitProcess+0x33f>
jmp    1537 <SystemInitProcess+0x2a7>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   10a0 <hasChildren@plt>
cmp    $0x0,%rax
je     15f6 <SystemInitProcess+0x366>
movl   $0x0,-0x54(%rbp)
jmp    1647 <SystemInitProcess+0x3b7>
cmpl   $0x0,-0x54(%rbp)
je     163b <SystemInitProcess+0x3ab>
cmpl   $0x0,-0x14(%rbp)
jge    1633 <SystemInitProcess+0x3a3>
xor    %edi,%edi
sub    -0x14(%rbp),%edi
mov    0x298a(%rip),%rax
mov    (%rax),%esi
call   10e0 <signal@plt>
xor    %esi,%esi
sub    -0x14(%rbp),%esi
mov    $0x1,%edi
call   1120 <kill@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <_exit@plt>
movl   $0x1,-0x54(%rbp)
jmp    1647 <SystemInitProcess+0x3b7>
jmp    164c <SystemInitProcess+0x3bc>
cmpl   $0x0,-0x54(%rbp)
jne    1532 <SystemInitProcess+0x2a2>
jmp    151e <SystemInitProcess+0x28e>

x86 assembly (clang, -O2):
<SystemInitProcess>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebx
mov    %edx,%eax
mov    %esi,%r14d
mov    %edi,%r13d
lea    0xd51(%rip),%rsi
lea    0xd4f(%rip),%rdx
mov    %eax,0xc(%rsp)
mov    %eax,%edi
call   1060 <getProcessStatus@plt>
mov    %rax,0x20(%rsp)
test   %rax,%rax
jg     12e0 <SystemInitProcess+0x50>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    0xd2e(%rip),%rsi
jmp    13b5 <SystemInitProcess+0x125>
test   %ebx,%ebx
js     13a5 <SystemInitProcess+0x115>
mov    %ebx,%edi
xor    %esi,%esi
call   1090 <dup2@plt>
test   %eax,%eax
jne    13a5 <SystemInitProcess+0x115>
mov    %ebx,%edi
mov    $0x1,%esi
call   1090 <dup2@plt>
cmp    $0x1,%eax
jne    13a5 <SystemInitProcess+0x115>
mov    %ebx,%edi
mov    $0x2,%esi
call   1090 <dup2@plt>
cmp    $0x2,%eax
jne    13a5 <SystemInitProcess+0x115>
mov    %ebx,%edi
call   10b0 <close@plt>
mov    0x2cb7(%rip),%rax
mov    0x2c88(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xd28(%rip),%rsi
mov    0xc(%rsp),%edi
call   1080 <openat@plt>
test   %eax,%eax
js     135c <SystemInitProcess+0xcc>
mov    %eax,%edi
call   1180 <fdopendir@plt>
test   %rax,%rax
jne    13c4 <SystemInitProcess+0x134>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
call   1170 <sysconf@plt>
cmp    $0x4,%eax
jl     1467 <SystemInitProcess+0x1d7>
mov    %eax,%ebx
add    $0xffffffff,%ebx
jmp    1391 <SystemInitProcess+0x101>
nopw   0x0(%rax,%rax,1)
lea    -0x1(%rbx),%eax
add    $0x1,%ebx
cmp    $0x4,%ebx
mov    %eax,%ebx
jbe    1467 <SystemInitProcess+0x1d7>
cmp    %ebx,%r13d
je     1380 <SystemInitProcess+0xf0>
cmp    %ebx,0xc(%rsp)
je     1380 <SystemInitProcess+0xf0>
mov    %ebx,%edi
call   10b0 <close@plt>
jmp    1380 <SystemInitProcess+0xf0>
mov    0x2c0c(%rip),%rax
mov    (%rax),%edi
lea    0xc8f(%rip),%rsi
call   1100 <fprintf@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
mov    %rax,%rbp
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
call   1070 <readdir_r@plt>
test   %eax,%eax
jne    145f <SystemInitProcess+0x1cf>
mov    0x10(%rsp),%rax
test   %rax,%rax
je     145f <SystemInitProcess+0x1cf>
lea    0x18(%rsp),%r15
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rax),%rdi
cmpl   $0x30,(%rdi)
jge    1430 <SystemInitProcess+0x1a0>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1070 <readdir_r@plt>
test   %eax,%eax
jne    145f <SystemInitProcess+0x1cf>
mov    0x10(%rsp),%rax
test   %rax,%rax
jne    1400 <SystemInitProcess+0x170>
jmp    145f <SystemInitProcess+0x1cf>
cs nopw 0x0(%rax,%rax,1)
call   1190 <atoi@plt>
cmp    0xc(%rsp),%eax
je     1408 <SystemInitProcess+0x178>
mov    %eax,%r12d
cmp    $0x3,%eax
jl     1408 <SystemInitProcess+0x178>
cmp    %r13d,%r12d
je     1408 <SystemInitProcess+0x178>
mov    %rbp,%rdi
call   10f0 <dirfd@plt>
cmp    %eax,%r12d
je     1408 <SystemInitProcess+0x178>
mov    %r12d,%edi
call   10b0 <close@plt>
jmp    1408 <SystemInitProcess+0x178>
mov    %rbp,%rdi
call   10c0 <closedir@plt>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1110 <sigemptyset@plt>
mov    0x2b5d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   11b0 <sigaddset@plt>
mov    0x2b1c(%rip),%rax
mov    (%rax),%edi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
lea    0xbd2(%rip),%rsi
mov    %r13d,%edi
mov    $0x1,%edx
call   1050 <write@plt>
mov    %r13d,%edi
call   10b0 <close@plt>
movslq %r14d,%rbx
mov    0x2af0(%rip),%r12
mov    0x2b19(%rip),%r14
lea    0x10(%rsp),%r13
nopl   0x0(%rax)
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1130 <sigwaitinfo@plt>
xor    %r15d,%r15d
nop
mov    (%r14),%edx
or     (%r12),%edx
jmp    1501 <SystemInitProcess+0x271>
nopl   0x0(%rax)
call   1150 <WEXITSTATUS@plt>
mov    %eax,%ebp
mov    (%r14),%edx
or     (%r12),%edx
xor    %r15d,%r15d
xor    %edi,%edi
mov    %r13,%rsi
call   1160 <waitpid@plt>
test   %rax,%rax
jle    1540 <SystemInitProcess+0x2b0>
cmp    %rbx,%rax
jne    14f7 <SystemInitProcess+0x267>
mov    0x10(%rsp),%edi
call   11a0 <WIFEXITED@plt>
mov    0x10(%rsp),%edi
test   %rax,%rax
jne    14f0 <SystemInitProcess+0x260>
call   10d0 <WIFSIGNALED@plt>
test   %rax,%rax
je     14f7 <SystemInitProcess+0x267>
mov    0x10(%rsp),%edi
call   1140 <WTERMSIG@plt>
mov    %eax,%ebp
neg    %ebp
jmp    14f7 <SystemInitProcess+0x267>
mov    0xc(%rsp),%edi
mov    0x20(%rsp),%rsi
call   10a0 <hasChildren@plt>
test   %rax,%rax
jne    14d0 <SystemInitProcess+0x240>
test   %r15d,%r15d
mov    $0x1,%r15d
je     14e0 <SystemInitProcess+0x250>
test   %ebp,%ebp
jns    158d <SystemInitProcess+0x2fd>
neg    %ebp
mov    0x2a2d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   10e0 <signal@plt>
mov    $0x1,%edi
mov    %ebp,%esi
call   1120 <kill@plt>
mov    $0x1,%ebp
mov    %ebp,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32* }
@.str = [5 x i8] c"self\00"
@.str.1 = [4 x i8] c"Pid\00"
@stderr = global i32 0
@.str.2 = [59 x i8] c"Failed to determine real process id of new \22init\22 process\0A\00"
@.str.3 = [39 x i8] c"Failed to point stdio to a safe place\0A\00"
@.str.4 = [8 x i8] c"self/fd\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@_SC_OPEN_MAX = global i32 0
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@.str.5 = [2 x i8] c" \00"
@__WALL = global i32 0
@WNOHANG = global i32 0
@SIG_DFL = global i32 0
define void @SystemInitProcess(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
%14 = alloca %struct.dirent
%15 = alloca %struct.dirent*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %9
%21 = load i32, i32* %7
%22 = call i64 @getProcessStatus(i32 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i64 %22, i64* %10
%23 = load i64, i64* %10
%24 = icmp sle i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %4
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @_exit(i32 1)
unreachable
29: ; preds = %4
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %44, label %32
32: ; preds = %29
%33 = load i32, i32* %8
%34 = call i32 @dup2(i32 %33, i32 0)
%35 = icmp ne i32 %34, 0
br i1 %35, label %44, label %36
36: ; preds = %32
%37 = load i32, i32* %8
%38 = call i32 @dup2(i32 %37, i32 1)
%39 = icmp ne i32 %38, 1
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i32, i32* %8
%42 = call i32 @dup2(i32 %41, i32 2)
%43 = icmp ne i32 %42, 2
br i1 %43, label %44, label %48
44: ; preds = %40, %36, %32, %29
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @_exit(i32 1)
unreachable
48: ; preds = %40
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %7
%52 = load i32, i32* @O_RDONLY
%53 = load i32, i32* @O_DIRECTORY
%54 = or i32 %52, %53
%55 = call i32 @openat(i32 %51, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %54)
store i32 %55, i32* %11
%56 = load i32, i32* %11
%57 = icmp sge i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %48
%59 = load i32, i32* %11
%60 = call i32* @fdopendir(i32 %59)
br label %62
61: ; preds = %48
br label %62
62: ; preds = %61, %58
%63 = phi i32* [ %60, %58 ], [ null, %61 ]
store i32* %63, i32** %12
%64 = load i32*, i32** %12
%65 = icmp eq i32* %64, null
br i1 %65, label %66, label %86
66: ; preds = %62
%67 = load i32, i32* @_SC_OPEN_MAX
%68 = call i32 @sysconf(i32 %67)
store i32 %68, i32* %13
br label %69
69: ; preds = %84, %66
%70 = load i32, i32* %13
%71 = add nsw i32 %70, -1
store i32 %71, i32* %13
%72 = icmp sgt i32 %71, 2
br i1 %72, label %73, label %85
73: ; preds = %69
%74 = load i32, i32* %13
%75 = load i32, i32* %5
%76 = icmp ne i32 %74, %75
br i1 %76, label %77, label %84
77: ; preds = %73
%78 = load i32, i32* %13
%79 = load i32, i32* %7
%80 = icmp ne i32 %78, %79
br i1 %80, label %81, label %84
81: ; preds = %77
%82 = load i32, i32* %13
%83 = call i32 @close(i32 %82)
br label %84
84: ; preds = %81, %77, %73
br label %69
85: ; preds = %69
br label %131
86: ; preds = %62
br label %87
87: ; preds = %127, %103, %86
%88 = load i32*, i32** %12
%89 = call i32 @readdir_r(i32* %88, %struct.dirent* %14, %struct.dirent** %15)
%90 = icmp ne i32 %89, 0
br i1 %90, label %94, label %91
91: ; preds = %87
%92 = load %struct.dirent*, %struct.dirent** %15
%93 = icmp ne %struct.dirent* %92, null
br label %94
94: ; preds = %91, %87
%95 = phi i1 [ false, %87 ], [ %93, %91 ]
br i1 %95, label %96, label %128
96: ; preds = %94
%97 = load %struct.dirent*, %struct.dirent** %15
%98 = getelementptr inbounds %struct.dirent, %struct.dirent* %97, i32 0, i32 0
%99 = load i32*, i32** %98
%100 = getelementptr inbounds i32, i32* %99, i64 0
%101 = load i32, i32* %100
%102 = icmp slt i32 %101, 48
br i1 %102, label %103, label %104
103: ; preds = %96
br label %87
104: ; preds = %96
%105 = load %struct.dirent*, %struct.dirent** %15
%106 = getelementptr inbounds %struct.dirent, %struct.dirent* %105, i32 0, i32 0
%107 = load i32*, i32** %106
%108 = call i32 @atoi(i32* %107)
store i32 %108, i32* %16
%109 = load i32, i32* %16
%110 = icmp sgt i32 %109, 2
br i1 %110, label %111, label %127
111: ; preds = %104
%112 = load i32, i32* %16
%113 = load i32, i32* %5
%114 = icmp ne i32 %112, %113
br i1 %114, label %115, label %127
115: ; preds = %111
%116 = load i32, i32* %16
%117 = load i32, i32* %7
%118 = icmp ne i32 %116, %117
br i1 %118, label %119, label %127
119: ; preds = %115
%120 = load i32, i32* %16
%121 = load i32*, i32** %12
%122 = call i32 @dirfd(i32* %121)
%123 = icmp ne i32 %120, %122
br i1 %123, label %124, label %127
124: ; preds = %119
%125 = load i32, i32* %16
%126 = call i32 @close(i32 %125)
br label %127
127: ; preds = %124, %119, %115, %111, %104
br label %87
128: ; preds = %94
%129 = load i32*, i32** %12
%130 = call i32 @closedir(i32* %129)
br label %131
131: ; preds = %128, %85
%132 = call i32 @sigemptyset(i32* %17)
%133 = load i32, i32* @SIGCHLD
%134 = call i32 @sigaddset(i32* %17, i32 %133)
%135 = load i32, i32* @SIG_BLOCK
%136 = call i32 @sigprocmask(i32 %135, i32* %17, i32* null)
%137 = load i32, i32* %5
%138 = call i64 @write(i32 %137, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%139 = icmp ne i64 %138, 0
br i1 %139, label %140, label %141
140: ; preds = %131
br label %141
141: ; preds = %140, %131
%142 = load i32, i32* %5
%143 = call i32 @close(i32 %142)
br label %144
144: ; preds = %208, %141
%145 = call i32 @sigwaitinfo(i32* %17, i32* null)
store i32 0, i32* %18
br label %146
146: ; preds = %205, %144
br label %147
147: ; preds = %178, %146
%148 = load i32, i32* @__WALL
%149 = load i32, i32* @WNOHANG
%150 = or i32 %148, %149
%151 = call i64 @waitpid(i32 0, i32* %19, i32 %150)
store i64 %151, i64* %20
%152 = load i64, i64* %20
%153 = icmp sle i64 %152, 0
br i1 %153, label %154, label %155
154: ; preds = %147
br label %179
155: ; preds = %147
store i32 0, i32* %18
br label %156
156: ; preds = %155
%157 = load i64, i64* %20
%158 = load i32, i32* %6
%159 = sext i32 %158 to i64
%160 = icmp eq i64 %157, %159
br i1 %160, label %161, label %178
161: ; preds = %156
%162 = load i32, i32* %19
%163 = call i64 @WIFEXITED(i32 %162)
%164 = icmp ne i64 %163, 0
br i1 %164, label %165, label %168
165: ; preds = %161
%166 = load i32, i32* %19
%167 = call i32 @WEXITSTATUS(i32 %166)
store i32 %167, i32* %9
br label %177
168: ; preds = %161
%169 = load i32, i32* %19
%170 = call i64 @WIFSIGNALED(i32 %169)
%171 = icmp ne i64 %170, 0
br i1 %171, label %172, label %176
172: ; preds = %168
%173 = load i32, i32* %19
%174 = call i32 @WTERMSIG(i32 %173)
%175 = sub nsw i32 0, %174
store i32 %175, i32* %9
br label %176
176: ; preds = %172, %168
br label %177
177: ; preds = %176, %165
br label %178
178: ; preds = %177, %156
br label %147
179: ; preds = %154
%180 = load i32, i32* %7
%181 = load i64, i64* %10
%182 = call i64 @hasChildren(i32 %180, i64 %181)
%183 = icmp ne i64 %182, 0
br i1 %183, label %184, label %185
184: ; preds = %179
store i32 0, i32* %18
br label %204
185: ; preds = %179
%186 = load i32, i32* %18
%187 = icmp ne i32 %186, 0
br i1 %187, label %188, label %202
188: ; preds = %185
%189 = load i32, i32* %9
%190 = icmp slt i32 %189, 0
br i1 %190, label %191, label %199
191: ; preds = %188
%192 = load i32, i32* %9
%193 = sub nsw i32 0, %192
%194 = load i32, i32* @SIG_DFL
%195 = call i32 @signal(i32 %193, i32 %194)
%196 = load i32, i32* %9
%197 = sub nsw i32 0, %196
%198 = call i32 @kill(i32 1, i32 %197)
store i32 1, i32* %9
br label %199
199: ; preds = %191, %188
%200 = load i32, i32* %9
%201 = call i32 @_exit(i32 %200)
unreachable
202: ; preds = %185
store i32 1, i32* %18
br label %203
203: ; preds = %202
br label %204
204: ; preds = %203, %184
br label %205
205: ; preds = %204
%206 = load i32, i32* %18
%207 = icmp ne i32 %206, 0
br i1 %207, label %146, label %208
208: ; preds = %205
br label %144
}

LLVM IR (-O2):
%struct.dirent = type { i32* }
@.str = [5 x i8] c"self\00"
@.str.1 = [4 x i8] c"Pid\00"
@stderr = global i32 0
@.str.2 = [59 x i8] c"Failed to determine real process id of new \22init\22 process\0A\00"
@.str.3 = [39 x i8] c"Failed to point stdio to a safe place\0A\00"
@.str.4 = [8 x i8] c"self/fd\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@_SC_OPEN_MAX = global i32 0
@SIGCHLD = global i32 0
@SIG_BLOCK = global i32 0
@.str.5 = [2 x i8] c" \00"
@__WALL = global i32 0
@WNOHANG = global i32 0
@SIG_DFL = global i32 0
define void @SystemInitProcess(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.dirent
%6 = alloca %struct.dirent*
%7 = alloca i32
%8 = alloca i32
%9 = tail call i64 @getProcessStatus(i32 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp slt i64 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %4
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @_exit(i32 1)
unreachable
15: ; preds = %4
%16 = icmp slt i32 %3, 0
br i1 %16, label %26, label %17
17: ; preds = %15
%18 = tail call i32 @dup2(i32 %3, i32 0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = tail call i32 @dup2(i32 %3, i32 1)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = tail call i32 @dup2(i32 %3, i32 2)
%25 = icmp eq i32 %24, 2
br i1 %25, label %30, label %26
26: ; preds = %23, %20, %17, %15
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%29 = tail call i32 @_exit(i32 1)
unreachable
30: ; preds = %23
%31 = tail call i32 @close(i32 %3)
%32 = load i32, i32* @O_RDONLY
%33 = load i32, i32* @O_DIRECTORY
%34 = or i32 %33, %32
%35 = tail call i32 @openat(i32 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = icmp sgt i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = tail call i32* @fdopendir(i32 %35)
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %54
40: ; preds = %30, %37
%41 = load i32, i32* @_SC_OPEN_MAX
%42 = tail call i32 @sysconf(i32 %41)
%43 = icmp sgt i32 %42, 3
br i1 %43, label %44, label %88
44: ; preds = %40, %52
%45 = phi i32 [ %46, %52 ], [ %42, %40 ]
%46 = add nsw i32 %45, -1
%47 = icmp eq i32 %46, %0
%48 = icmp eq i32 %46, %2
%49 = or i1 %47, %48
br i1 %49, label %52, label %50
50: ; preds = %44
%51 = tail call i32 @close(i32 %46)
br label %52
52: ; preds = %50, %44
%53 = icmp ugt i32 %45, 4
br i1 %53, label %44, label %88
54: ; preds = %37
%55 = bitcast %struct.dirent* %5 to i8*
%56 = bitcast %struct.dirent** %6 to i8*
%57 = call i32 @readdir_r(i32* %38, %struct.dirent* %5, %struct.dirent** %6)
%58 = icmp eq i32 %57, 0
%59 = load %struct.dirent*, %struct.dirent** %6
%60 = icmp ne %struct.dirent* %59, null
%61 = select i1 %58, i1 %60, i1 false
br i1 %61, label %62, label %86
62: ; preds = %54, %80
%63 = phi %struct.dirent* [ %83, %80 ], [ %59, %54 ]
%64 = getelementptr inbounds %struct.dirent, %struct.dirent* %63, i64 0, i32 0
%65 = load i32*, i32** %64
%66 = load i32, i32* %65
%67 = icmp slt i32 %66, 48
br i1 %67, label %80, label %68
68: ; preds = %62
%69 = call i32 @atoi(i32* %65)
%70 = icmp slt i32 %69, 3
%71 = icmp eq i32 %69, %0
%72 = or i1 %70, %71
%73 = icmp eq i32 %69, %2
%74 = or i1 %73, %72
br i1 %74, label %80, label %75
75: ; preds = %68
%76 = call i32 @dirfd(i32* %38)
%77 = icmp eq i32 %69, %76
br i1 %77, label %80, label %78
78: ; preds = %75
%79 = call i32 @close(i32 %69)
br label %80
80: ; preds = %68, %75, %78, %62
%81 = call i32 @readdir_r(i32* %38, %struct.dirent* %5, %struct.dirent** %6)
%82 = icmp eq i32 %81, 0
%83 = load %struct.dirent*, %struct.dirent** %6
%84 = icmp ne %struct.dirent* %83, null
%85 = select i1 %82, i1 %84, i1 false
br i1 %85, label %62, label %86
86: ; preds = %80, %54
%87 = call i32 @closedir(i32* %38)
br label %88
88: ; preds = %52, %40, %86
%89 = bitcast i32* %7 to i8*
%90 = call i32 @sigemptyset(i32* %7)
%91 = load i32, i32* @SIGCHLD
%92 = call i32 @sigaddset(i32* %7, i32 %91)
%93 = load i32, i32* @SIG_BLOCK
%94 = call i32 @sigprocmask(i32 %93, i32* %7, i32* null)
%95 = call i64 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%96 = call i32 @close(i32 %0)
%97 = bitcast i32* %8 to i8*
%98 = sext i32 %1 to i64
br label %99
99: ; preds = %135, %88
%100 = phi i32 [ 0, %88 ], [ %137, %135 ]
%101 = call i32 @sigwaitinfo(i32* %7, i32* null)
br label %102
102: ; preds = %140, %99
%103 = phi i32 [ 0, %99 ], [ 1, %140 ]
%104 = phi i32 [ %100, %99 ], [ %137, %140 ]
%105 = load i32, i32* @__WALL
%106 = load i32, i32* @WNOHANG
%107 = or i32 %106, %105
%108 = call i64 @waitpid(i32 0, i32* %8, i32 %107)
%109 = icmp slt i64 %108, 1
br i1 %109, label %135, label %110
110: ; preds = %102, %128
%111 = phi i64 [ %133, %128 ], [ %108, %102 ]
%112 = phi i32 [ %129, %128 ], [ %104, %102 ]
%113 = icmp eq i64 %111, %98
br i1 %113, label %114, label %128
114: ; preds = %110
%115 = load i32, i32* %8
%116 = call i64 @WIFEXITED(i32 %115)
%117 = icmp eq i64 %116, 0
%118 = load i32, i32* %8
br i1 %117, label %121, label %119
119: ; preds = %114
%120 = call i32 @WEXITSTATUS(i32 %118)
br label %128
121: ; preds = %114
%122 = call i64 @WIFSIGNALED(i32 %118)
%123 = icmp eq i64 %122, 0
br i1 %123, label %128, label %124
124: ; preds = %121
%125 = load i32, i32* %8
%126 = call i32 @WTERMSIG(i32 %125)
%127 = sub nsw i32 0, %126
br label %128
128: ; preds = %110, %121, %124, %119
%129 = phi i32 [ %120, %119 ], [ %127, %124 ], [ %112, %121 ], [ %112, %110 ]
%130 = load i32, i32* @__WALL
%131 = load i32, i32* @WNOHANG
%132 = or i32 %131, %130
%133 = call i64 @waitpid(i32 0, i32* %8, i32 %132)
%134 = icmp slt i64 %133, 1
br i1 %134, label %135, label %110
135: ; preds = %128, %102
%136 = phi i32 [ %103, %102 ], [ 0, %128 ]
%137 = phi i32 [ %104, %102 ], [ %129, %128 ]
%138 = call i64 @hasChildren(i32 %2, i64 %9)
%139 = icmp eq i64 %138, 0
br i1 %139, label %140, label %99
140: ; preds = %135
%141 = icmp eq i32 %136, 0
br i1 %141, label %102, label %142
142: ; preds = %140
%143 = icmp slt i32 %137, 0
br i1 %143, label %144, label %149
144: ; preds = %142
%145 = sub nsw i32 0, %137
%146 = load i32, i32* @SIG_DFL
%147 = call i32 @signal(i32 %145, i32 %146)
%148 = call i32 @kill(i32 1, i32 %145)
br label %149
149: ; preds = %144, %142
%150 = phi i32 [ 1, %144 ], [ %137, %142 ]
%151 = call i32 @_exit(i32 %150)
unreachable
}