#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 */

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ apply_dir_rules();
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ die(char *);
scalar_t__ errno;
scalar_t__ mkdir(char *, int);
scalar_t__ mount(char *, char *, char *, int, char *);
void setup_root(void) {
  if (mkdir("root", 0750) < 0 && errno != EEXIST)
    die("mkdir('root'): %m");
  if (mount(NULL, "/", NULL, (1 << 14) | (1 << 18), NULL) < 0)
    die("Cannot privatize mounts: %m");
  if (mount("none", "root", "tmpfs", 0, "mode=755") < 0)
    die("Cannot mount root ramdisk: %m");
  apply_dir_rules();
  if (chroot("root") < 0)
    die("Chroot failed: %m");
  if (chdir("root/box") < 0)
    die("Cannot change current directory: %m");
}


x86 assembly (clang, -O0):
<setup_root>:
push   %rbp
mov    %rsp,%rbp
lea    0xe95(%rip),%rdi
mov    $0x1e8,%esi
call   1050 <mkdir@plt>
cmp    $0x0,%rax
jge    11a5 <setup_root+0x45>
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
je     11a5 <setup_root+0x45>
lea    0xe65(%rip),%rdi
call   1080 <die@plt>
xor    %eax,%eax
mov    %eax,%r8d
lea    0xe66(%rip),%rsi
mov    $0x44000,%ecx
mov    %r8,%rdi
mov    %r8,%rdx
call   1060 <mount@plt>
cmp    $0x0,%rax
jge    11d7 <setup_root+0x77>
lea    0xe47(%rip),%rdi
call   1080 <die@plt>
lea    0xe57(%rip),%rdi
lea    0xe1b(%rip),%rsi
lea    0xe4e(%rip),%rdx
xor    %ecx,%ecx
lea    0xe4b(%rip),%r8
call   1060 <mount@plt>
cmp    $0x0,%rax
jge    1210 <setup_root+0xb0>
lea    0xe3e(%rip),%rdi
call   1080 <die@plt>
mov    $0x0,%al
call   1030 <apply_dir_rules@plt>
lea    0xde2(%rip),%rdi
call   1040 <chroot@plt>
cmp    $0x0,%rax
jge    1239 <setup_root+0xd9>
lea    0xe33(%rip),%rdi
call   1080 <die@plt>
lea    0xe39(%rip),%rdi
call   1070 <chdir@plt>
cmp    $0x0,%rax
jge    125b <setup_root+0xfb>
lea    0xe2c(%rip),%rdi
call   1080 <die@plt>
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_root>:
push   %rax
lea    0xe98(%rip),%rdi
mov    $0x1e8,%esi
call   1050 <mkdir@plt>
test   %rax,%rax
jns    1199 <setup_root+0x39>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
je     1199 <setup_root+0x39>
lea    0xe71(%rip),%rdi
call   1080 <die@plt>
lea    0xe77(%rip),%rsi
xor    %edi,%edi
xor    %edx,%edx
mov    $0x44000,%ecx
xor    %r8d,%r8d
call   1060 <mount@plt>
test   %rax,%rax
jns    11c2 <setup_root+0x62>
lea    0xe5c(%rip),%rdi
call   1080 <die@plt>
lea    0xe6c(%rip),%rdi
lea    0xe30(%rip),%rsi
lea    0xe63(%rip),%rdx
lea    0xe62(%rip),%r8
xor    %ecx,%ecx
call   1060 <mount@plt>
test   %rax,%rax
jns    11f6 <setup_root+0x96>
lea    0xe58(%rip),%rdi
call   1080 <die@plt>
xor    %eax,%eax
call   1030 <apply_dir_rules@plt>
lea    0xdfc(%rip),%rdi
call   1040 <chroot@plt>
test   %rax,%rax
jns    121a <setup_root+0xba>
lea    0xe52(%rip),%rdi
call   1080 <die@plt>
lea    0xe58(%rip),%rdi
call   1070 <chdir@plt>
test   %rax,%rax
js     122d <setup_root+0xcd>
pop    %rax
ret
lea    0xe4e(%rip),%rdi
pop    %rax
jmp    1080 <die@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"root\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [18 x i8] c"mkdir('root'): %m\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [28 x i8] c"Cannot privatize mounts: %m\00"
@.str.4 = [5 x i8] c"none\00"
@.str.5 = [6 x i8] c"tmpfs\00"
@.str.6 = [9 x i8] c"mode=755\00"
@.str.7 = [30 x i8] c"Cannot mount root ramdisk: %m\00"
@.str.8 = [18 x i8] c"Chroot failed: %m\00"
@.str.9 = [9 x i8] c"root/box\00"
@.str.10 = [36 x i8] c"Cannot change current directory: %m\00"
define void @setup_root() {
%1 = call i64 @mkdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 488)
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %9
3: ; preds = %0
%4 = load i64, i64* @errno
%5 = load i64, i64* @EEXIST
%6 = icmp ne i64 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %3, %0
%10 = call i64 @mount(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* null, i32 278528, i8* null)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = call i64 @mount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = call i32 (...) @apply_dir_rules()
%21 = call i64 @chroot(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = call i64 @chdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.10, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"root\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [18 x i8] c"mkdir('root'): %m\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [28 x i8] c"Cannot privatize mounts: %m\00"
@.str.4 = [5 x i8] c"none\00"
@.str.5 = [6 x i8] c"tmpfs\00"
@.str.6 = [9 x i8] c"mode=755\00"
@.str.7 = [30 x i8] c"Cannot mount root ramdisk: %m\00"
@.str.8 = [18 x i8] c"Chroot failed: %m\00"
@.str.9 = [9 x i8] c"root/box\00"
@.str.10 = [36 x i8] c"Cannot change current directory: %m\00"
define void @setup_root() {
%1 = tail call i64 @mkdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 488)
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %9
3: ; preds = %0
%4 = load i64, i64* @errno
%5 = load i64, i64* @EEXIST
%6 = icmp eq i64 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %3, %0
%10 = tail call i64 @mount(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* null, i32 278528, i8* null)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = tail call i64 @mount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = tail call i32 (...) @apply_dir_rules()
%21 = tail call i64 @chroot(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = tail call i64 @chdir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = tail call i32 @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.10, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
ret void
}