#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 statfs {
  int f_type;
};
struct stat {
  scalar_t__ st_dev;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
scalar_t__ chdir(char const *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
scalar_t__ mount(char *, char *, int /*<<< orphan*/ *, int,
                 int /*<<< orphan*/ *);
scalar_t__ nuke_dir(char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
scalar_t__ statfs(char *, struct statfs *);
const char *run_init(const char *realroot) {
  struct stat rst, cst, ist;
  struct statfs sfs;
  int confd;
  if (chdir(realroot))
    return "chdir to new root";
  if (stat("/", &rst) || stat(".", &cst))
    return "stat";
  if (rst.st_dev == cst.st_dev)
    return "current directory on the same filesystem as the root";
  if (stat("/init", &ist) || !S_ISREG(ist.st_mode))
    return "can't find /init on initramfs";
  if (statfs("/", &sfs))
    return "statfs /";
  if (sfs.f_type != 0x858458f6 && sfs.f_type != 0x01021994)
    return "rootfs not a ramfs or tmpfs";
  if (nuke_dir("/"))
    return "nuking initramfs contents";
  if (mount(".", "/", NULL, 8192, NULL))
    return "overmounting root";
  if (chroot(".") || chdir("/"))
    return "chroot";
  if ((confd = open("/dev/console", O_RDWR)) < 0)
    return "opening console";
  dup2(confd, 0);
  dup2(confd, 1);
  dup2(confd, 2);
  close(confd);
  return "ok";
}


x86 assembly (clang, -O0):
<run_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     11cf <run_init+0x2f>
lea    0xe3a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <run_init+0x224>
lea    0xea3(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1203 <run_init+0x63>
lea    0xe22(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     1213 <run_init+0x73>
lea    0xe0a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <run_init+0x224>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    1231 <run_init+0x91>
lea    0xdf1(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <run_init+0x224>
lea    0xe16(%rip),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    125c <run_init+0xbc>
mov    -0x38(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%eax
jne    126c <run_init+0xcc>
lea    0xdf1(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <run_init+0x224>
lea    0xe06(%rip),%rdi
lea    -0x48(%rbp),%rsi
call   10a0 <statfs@plt>
cmp    $0x0,%rax
je     1296 <run_init+0xf6>
lea    0xde5(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <run_init+0x224>
cmpl   $0x858458f6,-0x48(%rbp)
je     12c0 <run_init+0x120>
cmpl   $0x1021994,-0x48(%rbp)
je     12c0 <run_init+0x120>
lea    0xdc4(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <run_init+0x224>
lea    0xdb2(%rip),%rdi
call   10c0 <nuke_dir@plt>
cmp    $0x0,%rax
je     12e6 <run_init+0x146>
lea    0xdba(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <run_init+0x224>
lea    0xd25(%rip),%rdi
lea    0xd85(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x2000,%ecx
mov    %r8,%rdx
call   1040 <mount@plt>
cmp    $0x0,%rax
je     1320 <run_init+0x180>
lea    0xd9a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <run_init+0x224>
lea    0xceb(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
jne    134c <run_init+0x1ac>
lea    0xd3c(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     135c <run_init+0x1bc>
lea    0xd70(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <run_init+0x224>
mov    0x2c85(%rip),%rax
mov    (%rax),%esi
lea    0xd5e(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
jge    138d <run_init+0x1ed>
lea    0xd53(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13c4 <run_init+0x224>
mov    -0x4c(%rbp),%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    -0x4c(%rbp),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    -0x4c(%rbp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    -0x4c(%rbp),%edi
call   1070 <close@plt>
lea    0xd27(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_init>:
push   %rbx
sub    $0x40,%rsp
call   1050 <chdir@plt>
test   %rax,%rax
je     11b8 <run_init+0x18>
lea    0xe4a(%rip),%rbx
jmp    121d <run_init+0x7d>
lea    0xeba(%rip),%rdi
lea    0x30(%rsp),%rsi
call   1080 <stat@plt>
lea    0xe44(%rip),%rbx
test   %rax,%rax
jne    121d <run_init+0x7d>
lea    0xe36(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    121d <run_init+0x7d>
mov    0x30(%rsp),%rax
cmp    0x20(%rsp),%rax
jne    1200 <run_init+0x60>
lea    0xe1b(%rip),%rbx
jmp    121d <run_init+0x7d>
lea    0xe47(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1080 <stat@plt>
lea    0xe3c(%rip),%rbx
test   %rax,%rax
je     1226 <run_init+0x86>
mov    %rbx,%rax
add    $0x40,%rsp
pop    %rbx
ret
mov    0x18(%rsp),%edi
call   1090 <S_ISREG@plt>
test   %eax,%eax
je     121d <run_init+0x7d>
lea    0xe3f(%rip),%rdi
lea    0x8(%rsp),%rsi
call   10a0 <statfs@plt>
test   %rax,%rax
je     1252 <run_init+0xb2>
lea    0xe22(%rip),%rbx
jmp    121d <run_init+0x7d>
mov    0x8(%rsp),%eax
cmp    $0x1021994,%eax
je     1264 <run_init+0xc4>
cmp    $0x858458f6,%eax
jne    127e <run_init+0xde>
lea    0xe0e(%rip),%rdi
call   10c0 <nuke_dir@plt>
test   %rax,%rax
je     1287 <run_init+0xe7>
lea    0xe1b(%rip),%rbx
jmp    121d <run_init+0x7d>
lea    0xdf6(%rip),%rbx
jmp    121d <run_init+0x7d>
lea    0xd84(%rip),%rdi
lea    0xde4(%rip),%rsi
xor    %edx,%edx
mov    $0x2000,%ecx
xor    %r8d,%r8d
call   1040 <mount@plt>
test   %rax,%rax
je     12b5 <run_init+0x115>
lea    0xe01(%rip),%rbx
jmp    121d <run_init+0x7d>
lea    0xd56(%rip),%rdi
call   1030 <chroot@plt>
lea    0xdfb(%rip),%rbx
test   %rax,%rax
jne    121d <run_init+0x7d>
lea    0xda1(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    121d <run_init+0x7d>
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
lea    0xdd4(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1335 <run_init+0x195>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xdb7(%rip),%rbx
jmp    121d <run_init+0x7d>
lea    0xd9b(%rip),%rbx
jmp    121d <run_init+0x7d>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
%struct.statfs = type { i32 }
@.str = [18 x i8] c"chdir to new root\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [53 x i8] c"current directory on the same filesystem as the root\00"
@.str.5 = [6 x i8] c"/init\00"
@.str.6 = [30 x i8] c"can't find /init on initramfs\00"
@.str.7 = [9 x i8] c"statfs /\00"
@.str.8 = [28 x i8] c"rootfs not a ramfs or tmpfs\00"
@.str.9 = [26 x i8] c"nuking initramfs contents\00"
@.str.10 = [18 x i8] c"overmounting root\00"
@.str.11 = [7 x i8] c"chroot\00"
@.str.12 = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@.str.13 = [16 x i8] c"opening console\00"
@.str.14 = [3 x i8] c"ok\00"
define i8* @run_init(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca %struct.stat
%6 = alloca %struct.stat
%7 = alloca %struct.statfs
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i64 @chdir(i8* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %2
br label %78
13: ; preds = %1
%14 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %4)
%15 = icmp ne i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %5)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16, %13
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %78
20: ; preds = %16
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%22 = load i64, i64* %21
%23 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%24 = load i64, i64* %23
%25 = icmp eq i64 %22, %24
br i1 %25, label %26, label %27
26: ; preds = %20
store i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %78
27: ; preds = %20
%28 = call i64 @stat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), %struct.stat* %6)
%29 = icmp ne i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %27
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @S_ISREG(i32 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %30, %27
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8** %2
br label %78
36: ; preds = %30
%37 = call i64 @statfs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.statfs* %7)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8** %2
br label %78
40: ; preds = %36
%41 = getelementptr inbounds %struct.statfs, %struct.statfs* %7, i32 0, i32 0
%42 = load i32, i32* %41
%43 = icmp ne i32 %42, -2054924042
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = getelementptr inbounds %struct.statfs, %struct.statfs* %7, i32 0, i32 0
%46 = load i32, i32* %45
%47 = icmp ne i32 %46, 16914836
br i1 %47, label %48, label %49
48: ; preds = %44
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8** %2
br label %78
49: ; preds = %44, %40
%50 = call i64 @nuke_dir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %49
store i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i8** %2
br label %78
53: ; preds = %49
%54 = call i64 @mount(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* null, i32 8192, i32* null)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i8** %2
br label %78
57: ; preds = %53
%58 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%59 = icmp ne i64 %58, 0
br i1 %59, label %63, label %60
60: ; preds = %57
%61 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %60, %57
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0), i8** %2
br label %78
64: ; preds = %60
%65 = load i32, i32* @O_RDWR
%66 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.12, i64 0, i64 0), i32 %65)
store i32 %66, i32* %8
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %64
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.13, i64 0, i64 0), i8** %2
br label %78
69: ; preds = %64
%70 = load i32, i32* %8
%71 = call i32 @dup2(i32 %70, i32 0)
%72 = load i32, i32* %8
%73 = call i32 @dup2(i32 %72, i32 1)
%74 = load i32, i32* %8
%75 = call i32 @dup2(i32 %74, i32 2)
%76 = load i32, i32* %8
%77 = call i32 @close(i32 %76)
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i64 0, i64 0), i8** %2
br label %78
78: ; preds = %69, %68, %63, %56, %52, %48, %39, %35, %26, %19, %12
%79 = load i8*, i8** %2
ret i8* %79
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
%struct.statfs = type { i32 }
@.str = [18 x i8] c"chdir to new root\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [53 x i8] c"current directory on the same filesystem as the root\00"
@.str.5 = [6 x i8] c"/init\00"
@.str.6 = [30 x i8] c"can't find /init on initramfs\00"
@.str.7 = [9 x i8] c"statfs /\00"
@.str.8 = [28 x i8] c"rootfs not a ramfs or tmpfs\00"
@.str.9 = [26 x i8] c"nuking initramfs contents\00"
@.str.10 = [18 x i8] c"overmounting root\00"
@.str.11 = [7 x i8] c"chroot\00"
@.str.12 = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@.str.13 = [16 x i8] c"opening console\00"
@.str.14 = [3 x i8] c"ok\00"
define i8* @run_init(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca %struct.statfs
%6 = bitcast %struct.stat* %2 to i8*
%7 = bitcast %struct.stat* %3 to i8*
%8 = bitcast %struct.stat* %4 to i8*
%9 = bitcast %struct.statfs* %5 to i8*
%10 = tail call i64 @chdir(i8* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %59
12: ; preds = %1
%13 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %2)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %59
15: ; preds = %12
%16 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %59
18: ; preds = %15
%19 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%22 = load i64, i64* %21
%23 = icmp eq i64 %20, %22
br i1 %23, label %59, label %24
24: ; preds = %18
%25 = call i64 @stat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), %struct.stat* %4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %59
27: ; preds = %24
%28 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%29 = load i32, i32* %28
%30 = call i32 @S_ISREG(i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %59, label %32
32: ; preds = %27
%33 = call i64 @statfs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.statfs* %5)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %59
35: ; preds = %32
%36 = getelementptr inbounds %struct.statfs, %struct.statfs* %5, i64 0, i32 0
%37 = load i32, i32* %36
switch i32 %37, label %59 [
i32 -2054924042, label %38
i32 16914836, label %38
]
38: ; preds = %35, %35
%39 = call i64 @nuke_dir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %59
41: ; preds = %38
%42 = call i64 @mount(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* null, i32 8192, i32* null)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %59
44: ; preds = %41
%45 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %59
47: ; preds = %44
%48 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %47
%51 = load i32, i32* @O_RDWR
%52 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.12, i64 0, i64 0), i32 %51)
%53 = icmp slt i32 %52, 0
br i1 %53, label %59, label %54
54: ; preds = %50
%55 = call i32 @dup2(i32 %52, i32 0)
%56 = call i32 @dup2(i32 %52, i32 1)
%57 = call i32 @dup2(i32 %52, i32 2)
%58 = call i32 @close(i32 %52)
br label %59
59: ; preds = %50, %44, %47, %41, %38, %35, %32, %24, %27, %18, %12, %15, %1, %54
%60 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i64 0, i64 0), %54 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %15 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %12 ], [ getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), %18 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), %27 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), %24 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), %32 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), %35 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), %38 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), %41 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0), %47 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0), %44 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.13, i64 0, i64 0), %50 ]
ret i8* %60
}