#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 /*<<< orphan*/ SYS_pivot_root;
int /*<<< orphan*/ bind_mount(char *, char *);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ check(int /*<<< orphan*/, char *);
int /*<<< orphan*/ mount(char *, char *, char *, int /*<<< orphan*/, char *);
int /*<<< orphan*/ mount_proc(char *);
int /*<<< orphan*/ syscall(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ umount2(char *, int /*<<< orphan*/);
void setup_chroot() {
  check(mount("", "/tmp", "tmpfs", 0, ""), "mount(/tmp)");
  bind_mount("/lib", "/tmp/lib");
  bind_mount("/lib64", "/tmp/lib64");
  mount_proc("/tmp/proc");
  check(syscall(SYS_pivot_root, "/tmp", "/tmp"), "pivot_root");
  check(umount2("/", MNT_DETACH), "umount(/)");
  check(chdir("/"), "chdir(/)");
}


x86 assembly (clang, -O0):
<setup_chroot>:
push   %rbp
mov    %rsp,%rbp
lea    0xed9(%rip),%r8
lea    0xe7e(%rip),%rsi
lea    0xe7c(%rip),%rdx
xor    %ecx,%ecx
mov    %r8,%rdi
call   1060 <mount@plt>
mov    %eax,%edi
lea    0xe6f(%rip),%rsi
call   1090 <check@plt>
lea    0xe73(%rip),%rdi
lea    0xe68(%rip),%rsi
call   1030 <bind_mount@plt>
lea    0xe69(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1030 <bind_mount@plt>
lea    0xe5d(%rip),%rdi
call   1050 <mount_proc@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rdx
mov    %rdx,%rsi
call   1080 <syscall@plt>
mov    %eax,%edi
lea    0xe41(%rip),%rsi
call   1090 <check@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdi
call   1040 <umount2@plt>
mov    %eax,%edi
lea    0xe2b(%rip),%rsi
call   1090 <check@plt>
lea    0xe1d(%rip),%rdi
call   1070 <chdir@plt>
mov    %eax,%edi
lea    0xe1b(%rip),%rsi
call   1090 <check@plt>
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_chroot>:
push   %rbx
lea    0xe88(%rip),%rbx
lea    0xe86(%rip),%rdx
lea    0xece(%rip),%rdi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    %rdi,%r8
call   1060 <mount@plt>
lea    0xe71(%rip),%rsi
mov    %eax,%edi
call   1090 <check@plt>
lea    0xe73(%rip),%rdi
lea    0xe68(%rip),%rsi
call   1030 <bind_mount@plt>
lea    0xe69(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1030 <bind_mount@plt>
lea    0xe5d(%rip),%rdi
call   1050 <mount_proc@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %rbx,%rdx
call   1080 <syscall@plt>
lea    0xe47(%rip),%rsi
mov    %eax,%edi
call   1090 <check@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rbx
mov    %rbx,%rdi
call   1040 <umount2@plt>
lea    0xe2e(%rip),%rsi
mov    %eax,%edi
call   1090 <check@plt>
mov    %rbx,%rdi
call   1070 <chdir@plt>
lea    0xe22(%rip),%rsi
mov    %eax,%edi
pop    %rbx
jmp    1090 <check@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [6 x i8] c"tmpfs\00"
@.str.3 = [12 x i8] c"mount(/tmp)\00"
@.str.4 = [5 x i8] c"/lib\00"
@.str.5 = [9 x i8] c"/tmp/lib\00"
@.str.6 = [7 x i8] c"/lib64\00"
@.str.7 = [11 x i8] c"/tmp/lib64\00"
@.str.8 = [10 x i8] c"/tmp/proc\00"
@SYS_pivot_root = global i32 0
@.str.9 = [11 x i8] c"pivot_root\00"
@.str.10 = [2 x i8] c"/\00"
@MNT_DETACH = global i32 0
@.str.11 = [10 x i8] c"umount(/)\00"
@.str.12 = [9 x i8] c"chdir(/)\00"
define void @setup_chroot() {
%1 = call i32 @mount(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%2 = call i32 @check(i32 %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%3 = call i32 @bind_mount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%4 = call i32 @bind_mount(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%5 = call i32 @mount_proc(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%6 = load i32, i32* @SYS_pivot_root
%7 = call i32 @syscall(i32 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @check(i32 %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
%9 = load i32, i32* @MNT_DETACH
%10 = call i32 @umount2(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0), i32 %9)
%11 = call i32 @check(i32 %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0))
%12 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
%13 = call i32 @check(i32 %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [6 x i8] c"tmpfs\00"
@.str.3 = [12 x i8] c"mount(/tmp)\00"
@.str.4 = [5 x i8] c"/lib\00"
@.str.5 = [9 x i8] c"/tmp/lib\00"
@.str.6 = [7 x i8] c"/lib64\00"
@.str.7 = [11 x i8] c"/tmp/lib64\00"
@.str.8 = [10 x i8] c"/tmp/proc\00"
@SYS_pivot_root = global i32 0
@.str.9 = [11 x i8] c"pivot_root\00"
@.str.10 = [2 x i8] c"/\00"
@MNT_DETACH = global i32 0
@.str.11 = [10 x i8] c"umount(/)\00"
@.str.12 = [9 x i8] c"chdir(/)\00"
define void @setup_chroot() {
%1 = tail call i32 @mount(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @check(i32 %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%3 = tail call i32 @bind_mount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%4 = tail call i32 @bind_mount(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%5 = tail call i32 @mount_proc(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%6 = load i32, i32* @SYS_pivot_root
%7 = tail call i32 @syscall(i32 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @check(i32 %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
%9 = load i32, i32* @MNT_DETACH
%10 = tail call i32 @umount2(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0), i32 %9)
%11 = tail call i32 @check(i32 %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0))
%12 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
%13 = tail call i32 @check(i32 %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0))
ret void
}