#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;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
scalar_t__ chdir(char const *);
scalar_t__ chroot(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execv(char const *, char **);
scalar_t__ mount(char *, char *, int /*<<< orphan*/ *, int,
                 int /*<<< orphan*/ *);
scalar_t__ nuke_dir(char *);
int open(char const *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
scalar_t__ statfs(char *, struct statfs *);
const char *run_init(const char *realroot, const char *chrootdir,
                     const char *console, const char *init, char **initargs) {
  struct stat rst, cst;
  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 (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(chrootdir) || chdir("/"))
    return "chroot";
  if ((confd = open(console, O_RDWR)) < 0)
    return "opening console";
  dup2(confd, 0);
  dup2(confd, 1);
  dup2(confd, 2);
  close(confd);
  execv(init, initargs);
  return init;
}


x86 assembly (clang, -O0):
<run_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     11df <run_init+0x3f>
lea    0xe2a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    139d <run_init+0x1fd>
lea    0xe6f(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1213 <run_init+0x73>
lea    0xe12(%rip),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     1223 <run_init+0x83>
lea    0xdfa(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    139d <run_init+0x1fd>
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jne    1241 <run_init+0xa1>
lea    0xde1(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    139d <run_init+0x1fd>
lea    0xe0d(%rip),%rdi
lea    -0x48(%rbp),%rsi
call   1090 <statfs@plt>
cmp    $0x0,%rax
je     126b <run_init+0xcb>
lea    0xdec(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    139d <run_init+0x1fd>
cmpl   $0x858458f6,-0x48(%rbp)
je     1295 <run_init+0xf5>
cmpl   $0x1021994,-0x48(%rbp)
je     1295 <run_init+0xf5>
lea    0xdcb(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    139d <run_init+0x1fd>
lea    0xdb9(%rip),%rdi
call   10b0 <nuke_dir@plt>
cmp    $0x0,%rax
je     12bb <run_init+0x11b>
lea    0xdc1(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    139d <run_init+0x1fd>
lea    0xd50(%rip),%rdi
lea    0xd8c(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x2000,%ecx
mov    %r8,%rdx
call   1040 <mount@plt>
cmp    $0x0,%rax
je     12f5 <run_init+0x155>
lea    0xda1(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    139d <run_init+0x1fd>
mov    -0x18(%rbp),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
jne    131e <run_init+0x17e>
lea    0xd46(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     132e <run_init+0x18e>
lea    0xd7a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    139d <run_init+0x1fd>
mov    -0x20(%rbp),%rdi
mov    0x2caf(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
jge    135c <run_init+0x1bc>
lea    0xd53(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    139d <run_init+0x1fd>
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>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10c0 <execv@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_init>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8,%r15
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r13
call   1050 <chdir@plt>
test   %rax,%rax
je     11cf <run_init+0x2f>
lea    0xe36(%rip),%rbx
jmp    1325 <run_init+0x185>
lea    0xe7f(%rip),%rdi
lea    0x18(%rsp),%rsi
call   1080 <stat@plt>
lea    0xe2d(%rip),%rbx
test   %rax,%rax
jne    1325 <run_init+0x185>
lea    0xe1b(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    1325 <run_init+0x185>
mov    0x18(%rsp),%rax
cmp    0x10(%rsp),%rax
jne    1222 <run_init+0x82>
lea    0xdfc(%rip),%rbx
jmp    1325 <run_init+0x185>
lea    0xe2c(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1090 <statfs@plt>
test   %rax,%rax
je     1244 <run_init+0xa4>
lea    0xe0f(%rip),%rbx
jmp    1325 <run_init+0x185>
mov    0x8(%rsp),%eax
cmp    $0x1021994,%eax
je     1256 <run_init+0xb6>
cmp    $0x858458f6,%eax
jne    1273 <run_init+0xd3>
lea    0xdf8(%rip),%rdi
call   10b0 <nuke_dir@plt>
test   %rax,%rax
je     127f <run_init+0xdf>
lea    0xe05(%rip),%rbx
jmp    1325 <run_init+0x185>
lea    0xddd(%rip),%rbx
jmp    1325 <run_init+0x185>
lea    0xd8c(%rip),%rdi
lea    0xdc8(%rip),%rsi
xor    %edx,%edx
mov    $0x2000,%ecx
xor    %r8d,%r8d
call   1040 <mount@plt>
test   %rax,%rax
je     12aa <run_init+0x10a>
lea    0xde5(%rip),%rbx
jmp    1325 <run_init+0x185>
mov    %r13,%rdi
call   1030 <chroot@plt>
lea    0xde6(%rip),%rbx
test   %rax,%rax
jne    1325 <run_init+0x185>
lea    0xd90(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    1325 <run_init+0x185>
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     131e <run_init+0x17e>
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>
mov    %r14,%rdi
mov    %r15,%rsi
call   10c0 <execv@plt>
mov    %r14,%rbx
jmp    1325 <run_init+0x185>
lea    0xd81(%rip),%rbx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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