#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 */
int /*<<< orphan*/ MNT_DETACH;
int MS_BIND;
int MS_PRIVATE;
int MS_REC;
int /*<<< orphan*/ SYS_pivot_root;
scalar_t__ chdir(char *);
int errno;
scalar_t__ mount(char *, char *, int /*<<< orphan*/ *, int,
                 int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ syscall(int /*<<< orphan*/, char *, char *);
scalar_t__ umount2(char *, int /*<<< orphan*/);
int setup_filesystem(char *root, char *pivot, char *pivot_abs) {
  if (mount("", "/", NULL, MS_PRIVATE | MS_REC, NULL) < 0) {
    printf("setup_filesystem(): Failed to mark root as private\n");
    return -1;
  }
  if (mount(root, root, NULL, MS_BIND | MS_REC, NULL) < 0) {
    printf("setup_filesystem(): Failed to bind pivot root directory\n");
    return -1;
  }
  if (syscall(SYS_pivot_root, root, pivot_abs) < 0) {
    printf("setup_filesystem(): pivot_root failed: %d\n", errno);
    return -1;
  }
  if (chdir("/") < 0) {
    printf("setup_filesystem(): chdir failed\n");
    return -1;
  }
  if (umount2(pivot, MNT_DETACH) < 0) {
    printf("setup_filesystem(): umount2(): %d\n", errno);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_filesystem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6c(%rip),%rax
or     (%rax),%ecx
lea    0xf61(%rip),%rdi
lea    0xe7c(%rip),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
call   1040 <mount@plt>
cmp    $0x0,%rax
jge    11b5 <setup_filesystem+0x65>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <setup_filesystem+0x169>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e24(%rip),%rax
mov    (%rax),%ecx
mov    0x2e13(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
call   1040 <mount@plt>
cmp    $0x0,%rax
jge    1200 <setup_filesystem+0xb0>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <setup_filesystem+0x169>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <syscall@plt>
cmp    $0x0,%rax
jge    1243 <setup_filesystem+0xf3>
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <setup_filesystem+0x169>
lea    0xdb6(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    1273 <setup_filesystem+0x123>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <setup_filesystem+0x169>
mov    -0x18(%rbp),%rdi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
call   1030 <umount2@plt>
cmp    $0x0,%rax
jge    12b2 <setup_filesystem+0x162>
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <setup_filesystem+0x169>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_filesystem>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e58(%rip),%rax
mov    0x2e61(%rip),%r12
mov    (%r12),%ecx
or     (%rax),%ecx
lea    0xec3(%rip),%rdi
lea    0xe6d(%rip),%rsi
xor    %edx,%edx
xor    %r8d,%r8d
call   1050 <mount@plt>
test   %rax,%rax
js     121b <setup_filesystem+0xbb>
mov    0x2e3f(%rip),%rax
mov    (%r12),%ecx
or     (%rax),%ecx
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %r8d,%r8d
call   1050 <mount@plt>
test   %rax,%rax
js     1224 <setup_filesystem+0xc4>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <syscall@plt>
test   %rax,%rax
js     122d <setup_filesystem+0xcd>
lea    0xe1c(%rip),%rdi
call   1060 <chdir@plt>
test   %rax,%rax
js     1246 <setup_filesystem+0xe6>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <umount2@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1257 <setup_filesystem+0xf7>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
lea    0xe14(%rip),%rdi
jmp    123d <setup_filesystem+0xdd>
lea    0xe87(%rip),%rdi
jmp    124d <setup_filesystem+0xed>
lea    0xe46(%rip),%rdi
jmp    124d <setup_filesystem+0xed>
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
lea    0xdc5(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1252 <setup_filesystem+0xf2>
lea    0xe03(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"/\00"
@MS_PRIVATE = global i32 0
@MS_REC = global i32 0
@.str.2 = [52 x i8] c"setup_filesystem(): Failed to mark root as private\0A\00"
@MS_BIND = global i32 0
@.str.3 = [57 x i8] c"setup_filesystem(): Failed to bind pivot root directory\0A\00"
@SYS_pivot_root = global i32 0
@.str.4 = [43 x i8] c"setup_filesystem(): pivot_root failed: %d\0A\00"
@errno = global i32 0
@.str.5 = [34 x i8] c"setup_filesystem(): chdir failed\0A\00"
@MNT_DETACH = global i32 0
@.str.6 = [35 x i8] c"setup_filesystem(): umount2(): %d\0A\00"
define i32 @setup_filesystem(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%8 = load i32, i32* @MS_PRIVATE
%9 = load i32, i32* @MS_REC
%10 = or i32 %8, %9
%11 = call i64 @mount(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* null, i32 %10, i32* null)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i8*, i8** %5
%18 = load i32, i32* @MS_BIND
%19 = load i32, i32* @MS_REC
%20 = or i32 %18, %19
%21 = call i64 @mount(i8* %16, i8* %17, i32* null, i32 %20, i32* null)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
25: ; preds = %15
%26 = load i32, i32* @SYS_pivot_root
%27 = load i8*, i8** %5
%28 = load i8*, i8** %7
%29 = call i64 @syscall(i32 %26, i8* %27, i8* %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i32, i32* @errno
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %32)
store i32 -1, i32* %4
br label %48
34: ; preds = %25
%35 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
39: ; preds = %34
%40 = load i8*, i8** %6
%41 = load i32, i32* @MNT_DETACH
%42 = call i64 @umount2(i8* %40, i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i32, i32* @errno
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %45)
store i32 -1, i32* %4
br label %48
47: ; preds = %39
store i32 0, i32* %4
br label %48
48: ; preds = %47, %44, %37, %31, %23, %13
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"/\00"
@MS_PRIVATE = global i32 0
@MS_REC = global i32 0
@MS_BIND = global i32 0
@SYS_pivot_root = global i32 0
@.str.4 = [43 x i8] c"setup_filesystem(): pivot_root failed: %d\0A\00"
@errno = global i32 0
@MNT_DETACH = global i32 0
@.str.6 = [35 x i8] c"setup_filesystem(): umount2(): %d\0A\00"
@str = [33 x i8] c"setup_filesystem(): chdir failed\00"
@str.7 = [56 x i8] c"setup_filesystem(): Failed to bind pivot root directory\00"
@str.8 = [51 x i8] c"setup_filesystem(): Failed to mark root as private\00"
define i32 @setup_filesystem(i8* %0, i8* %1, i8* %2) {
%4 = load i32, i32* @MS_PRIVATE
%5 = load i32, i32* @MS_REC
%6 = or i32 %5, %4
%7 = tail call i64 @mount(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* null, i32 %6, i32* null)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.8, i64 0, i64 0))
br label %38
11: ; preds = %3
%12 = load i32, i32* @MS_BIND
%13 = load i32, i32* @MS_REC
%14 = or i32 %13, %12
%15 = tail call i64 @mount(i8* %0, i8* %0, i32* null, i32 %14, i32* null)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str.7, i64 0, i64 0))
br label %38
19: ; preds = %11
%20 = load i32, i32* @SYS_pivot_root
%21 = tail call i64 @syscall(i32 %20, i8* %0, i8* %2)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @errno
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %24)
br label %38
26: ; preds = %19
%27 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %38
31: ; preds = %26
%32 = load i32, i32* @MNT_DETACH
%33 = tail call i64 @umount2(i8* %1, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* @errno
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %31, %35, %29, %23, %17, %9
%39 = phi i32 [ -1, %9 ], [ -1, %17 ], [ -1, %23 ], [ -1, %29 ], [ -1, %35 ], [ 0, %31 ]
ret i32 %39
}