#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 *);
int /*<<< orphan*/ close(int);
scalar_t__ drop_capabilities(char const *);
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 *console,
                     const char *drop_caps, 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(".") || chdir("/"))
    return "chroot";
  if (drop_capabilities(drop_caps) < 0)
    return "dropping capabilities";
  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     11ef <run_init+0x3f>
lea    0xe1a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13d3 <run_init+0x223>
lea    0xe5f(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1223 <run_init+0x73>
lea    0xe02(%rip),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     1233 <run_init+0x83>
lea    0xdea(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13d3 <run_init+0x223>
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jne    1251 <run_init+0xa1>
lea    0xdd1(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13d3 <run_init+0x223>
lea    0xdfd(%rip),%rdi
lea    -0x48(%rbp),%rsi
call   1090 <statfs@plt>
cmp    $0x0,%rax
je     127b <run_init+0xcb>
lea    0xddc(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13d3 <run_init+0x223>
cmpl   $0x858458f6,-0x48(%rbp)
je     12a5 <run_init+0xf5>
cmpl   $0x1021994,-0x48(%rbp)
je     12a5 <run_init+0xf5>
lea    0xdbb(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13d3 <run_init+0x223>
lea    0xda9(%rip),%rdi
call   10c0 <nuke_dir@plt>
cmp    $0x0,%rax
je     12cb <run_init+0x11b>
lea    0xdb1(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13d3 <run_init+0x223>
lea    0xd40(%rip),%rdi
lea    0xd7c(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x2000,%ecx
mov    %r8,%rdx
call   1040 <mount@plt>
cmp    $0x0,%rax
je     1305 <run_init+0x155>
lea    0xd91(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13d3 <run_init+0x223>
lea    0xd06(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
jne    1331 <run_init+0x181>
lea    0xd33(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     1341 <run_init+0x191>
lea    0xd67(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13d3 <run_init+0x223>
mov    -0x20(%rbp),%rdi
call   10a0 <drop_capabilities@plt>
cmp    $0x0,%rax
jge    1364 <run_init+0x1b4>
lea    0xd4b(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13d3 <run_init+0x223>
mov    -0x18(%rbp),%rdi
mov    0x2c79(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
jge    1392 <run_init+0x1e2>
lea    0xd33(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13d3 <run_init+0x223>
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   10d0 <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,%r13
mov    %rsi,%r12
call   1050 <chdir@plt>
test   %rax,%rax
je     11dc <run_init+0x2c>
lea    0xe26(%rip),%rbx
jmp    1241 <run_init+0x91>
lea    0xe72(%rip),%rdi
lea    0x18(%rsp),%rsi
call   1080 <stat@plt>
lea    0xe20(%rip),%rbx
test   %rax,%rax
jne    1241 <run_init+0x91>
lea    0xe12(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    1241 <run_init+0x91>
mov    0x18(%rsp),%rax
cmp    0x10(%rsp),%rax
jne    1224 <run_init+0x74>
lea    0xdf7(%rip),%rbx
jmp    1241 <run_init+0x91>
lea    0xe2a(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1090 <statfs@plt>
test   %rax,%rax
je     1252 <run_init+0xa2>
lea    0xe0d(%rip),%rbx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%eax
cmp    $0x1021994,%eax
je     1264 <run_init+0xb4>
cmp    $0x858458f6,%eax
jne    127e <run_init+0xce>
lea    0xdea(%rip),%rdi
call   10c0 <nuke_dir@plt>
test   %rax,%rax
je     1287 <run_init+0xd7>
lea    0xdf7(%rip),%rbx
jmp    1241 <run_init+0x91>
lea    0xdd2(%rip),%rbx
jmp    1241 <run_init+0x91>
lea    0xd84(%rip),%rdi
lea    0xdc0(%rip),%rsi
xor    %edx,%edx
mov    $0x2000,%ecx
xor    %r8d,%r8d
call   1040 <mount@plt>
test   %rax,%rax
je     12b2 <run_init+0x102>
lea    0xddd(%rip),%rbx
jmp    1241 <run_init+0x91>
lea    0xd59(%rip),%rdi
call   1030 <chroot@plt>
lea    0xdda(%rip),%rbx
test   %rax,%rax
jne    1241 <run_init+0x91>
lea    0xd80(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    1241 <run_init+0x91>
mov    %r13,%rdi
call   10a0 <drop_capabilities@plt>
test   %rax,%rax
js     1342 <run_init+0x192>
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     134e <run_init+0x19e>
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   10d0 <execv@plt>
mov    %r14,%rbx
jmp    1241 <run_init+0x91>
lea    0xd5d(%rip),%rbx
jmp    1241 <run_init+0x91>
lea    0xd67(%rip),%rbx
jmp    1241 <run_init+0x91>

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"
@.str.10 = [22 x i8] c"dropping capabilities\00"
@O_RDWR = global i32 0
@.str.11 = [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 %86
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 %86
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 %86
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 %86
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 %86
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 %86
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 %86
55: ; preds = %51
%56 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%57 = icmp ne i64 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %55
%59 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %58, %55
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0), i8** %6
br label %86
62: ; preds = %58
%63 = load i8*, i8** %9
%64 = call i64 @drop_capabilities(i8* %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %62
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0), i8** %6
br label %86
67: ; preds = %62
%68 = load i8*, i8** %8
%69 = load i32, i32* @O_RDWR
%70 = call i32 @open(i8* %68, i32 %69)
store i32 %70, i32* %15
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %67
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.11, i64 0, i64 0), i8** %6
br label %86
73: ; preds = %67
%74 = load i32, i32* %15
%75 = call i32 @dup2(i32 %74, i32 0)
%76 = load i32, i32* %15
%77 = call i32 @dup2(i32 %76, i32 1)
%78 = load i32, i32* %15
%79 = call i32 @dup2(i32 %78, i32 2)
%80 = load i32, i32* %15
%81 = call i32 @close(i32 %80)
%82 = load i8*, i8** %10
%83 = load i8**, i8*** %11
%84 = call i32 @execv(i8* %82, i8** %83)
%85 = load i8*, i8** %10
store i8* %85, i8** %6
br label %86
86: ; preds = %73, %72, %66, %61, %54, %50, %46, %37, %33, %26, %19
%87 = load i8*, i8** %6
ret i8* %87
}

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"
@.str.10 = [22 x i8] c"dropping capabilities\00"
@O_RDWR = global i32 0
@.str.11 = [16 x i8] c"opening console\00"
define i8* @run_init(i8* %0, i8* nocapture %1, i8* %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 %57
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 %57
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 %57
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 %57, 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 %57
29: ; preds = %26
%30 = getelementptr inbounds %struct.statfs, %struct.statfs* %8, i64 0, i32 0
%31 = load i32, i32* %30
switch i32 %31, label %57 [
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 %57
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 %57
38: ; preds = %35
%39 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %57
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 %57
44: ; preds = %41
%45 = call i64 @drop_capabilities(i8* %2)
%46 = icmp slt i64 %45, 0
br i1 %46, label %57, label %47
47: ; preds = %44
%48 = load i32, i32* @O_RDWR
%49 = call i32 @open(i8* %1, i32 %48)
%50 = icmp slt i32 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %47
%52 = call i32 @dup2(i32 %49, i32 0)
%53 = call i32 @dup2(i32 %49, i32 1)
%54 = call i32 @dup2(i32 %49, i32 2)
%55 = call i32 @close(i32 %49)
%56 = call i32 @execv(i8* %3, i8** %4)
br label %57
57: ; preds = %47, %44, %38, %41, %35, %32, %29, %26, %20, %14, %17, %5, %51
%58 = phi i8* [ %3, %51 ], [ 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 ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0), %44 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.11, i64 0, i64 0), %47 ]
ret i8* %58
}