#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_REC;
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ SYS_pivot_root;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
int maybe_mkdir(char *, int);
int mount(char *, char *, char *, int, char *);
int /*<<< orphan*/ realpath(char const *, char *);
int /*<<< orphan*/ rmdir(char *);
int /*<<< orphan*/ snprintf(char *, int /*<<< orphan*/, char *, char *);
int syscall(int /*<<< orphan*/, char *, char *);
int umount2(char *, int /*<<< orphan*/);
int switchroot(const char *root) {
  int r;
  char rootpath[PATH_MAX + 1];
  char pivot[PATH_MAX + 1];
  realpath(root, rootpath);
  snprintf(pivot, PATH_MAX, "%s/tmp/.pivot", rootpath);
  r = maybe_mkdir(pivot, 0700);
  if (r < 0)
    error(1, errno, "can't mkdir %s", pivot);
  r = mount(rootpath, rootpath, "bind", MS_BIND | MS_REC, "");
  if (r < 0)
    error(1, errno, "can't bind mount %s", rootpath);
  r = syscall(SYS_pivot_root, rootpath, pivot);
  if (r < 0)
    error(1, errno, "can't pivot root to %s", pivot);
  chdir("/");
  r = umount2("/tmp/.pivot", MNT_DETACH);
  if (r < 0)
    error(1, errno, "can't umount /.pivot");
  rmdir("/tmp/.pivot");
  return 0;
}


x86 assembly (clang, -O0):
<switchroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rsi
sub    %rdx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rcx,-0x20(%rbp)
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <realpath@plt>
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%rdi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    $0x1c0,%esi
call   10a0 <maybe_mkdir@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    124d <switchroot+0xbd>
mov    -0x30(%rbp),%rcx
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdc8(%rip),%rdx
mov    $0x0,%al
call   10b0 <error@plt>
mov    -0x38(%rbp),%rsi
mov    0x2d88(%rip),%rax
mov    (%rax),%ecx
mov    0x2d77(%rip),%rax
or     (%rax),%ecx
lea    0xdb3(%rip),%rdx
lea    0xddd(%rip),%r8
mov    %rsi,%rdi
call   1050 <mount@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12a6 <switchroot+0x116>
mov    -0x38(%rbp),%rcx
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xd83(%rip),%rdx
mov    $0x0,%al
call   10b0 <error@plt>
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rsi
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   1080 <syscall@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12e9 <switchroot+0x159>
mov    -0x30(%rbp),%rcx
mov    0x2cf4(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xd54(%rip),%rdx
mov    $0x0,%al
call   10b0 <error@plt>
lea    0xd5d(%rip),%rdi
call   1060 <chdir@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%esi
lea    0xcfd(%rip),%rdi
call   1030 <umount2@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1333 <switchroot+0x1a3>
mov    0x2caa(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xd23(%rip),%rdx
mov    $0x0,%al
call   10b0 <error@plt>
lea    0xcc8(%rip),%rdi
call   1040 <rmdir@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switchroot>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e4f(%rip),%r15
mov    (%r15),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
mov    %rbx,%rsi
call   1090 <realpath@plt>
mov    (%r15),%esi
lea    0xe2d(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rcx
call   1070 <snprintf@plt>
mov    %r14,%rdi
mov    $0x1c0,%esi
call   10a0 <maybe_mkdir@plt>
test   %eax,%eax
jns    120e <switchroot+0x7e>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0xe0f(%rip),%rdx
mov    $0x1,%edi
mov    %r14,%rcx
xor    %eax,%eax
call   10b0 <error@plt>
mov    0x2dcb(%rip),%rax
mov    0x2dbc(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
lea    0xdf6(%rip),%rdx
lea    0xe20(%rip),%r8
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1050 <mount@plt>
test   %eax,%eax
jns    125c <switchroot+0xcc>
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
lea    0xdd5(%rip),%rdx
mov    $0x1,%edi
mov    %rbx,%rcx
xor    %eax,%eax
call   10b0 <error@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <syscall@plt>
test   %eax,%eax
jns    1293 <switchroot+0x103>
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
lea    0xdb2(%rip),%rdx
mov    $0x1,%edi
mov    %r14,%rcx
xor    %eax,%eax
call   10b0 <error@plt>
lea    0xdb3(%rip),%rdi
call   1060 <chdir@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
lea    0xd53(%rip),%rdi
call   1030 <umount2@plt>
test   %eax,%eax
jns    12d4 <switchroot+0x144>
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
lea    0xd87(%rip),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   10b0 <error@plt>
lea    0xd27(%rip),%rdi
call   1040 <rmdir@plt>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [14 x i8] c"%s/tmp/.pivot\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"can't mkdir %s\00"
@.str.2 = [5 x i8] c"bind\00"
@MS_BIND = global i32 0
@MS_REC = global i32 0
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [20 x i8] c"can't bind mount %s\00"
@SYS_pivot_root = global i32 0
@.str.5 = [23 x i8] c"can't pivot root to %s\00"
@.str.6 = [2 x i8] c"/\00"
@.str.7 = [12 x i8] c"/tmp/.pivot\00"
@MNT_DETACH = global i32 0
@.str.8 = [21 x i8] c"can't umount /.pivot\00"
define i32 @switchroot(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i32, i32* @PATH_MAX
%8 = add nsw i32 %7, 1
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = load i32, i32* @PATH_MAX
%13 = add nsw i32 %12, 1
%14 = zext i32 %13 to i64
%15 = alloca i8, i64 %14
store i64 %14, i64* %6
%16 = load i8*, i8** %2
%17 = call i32 @realpath(i8* %16, i8* %11)
%18 = load i32, i32* @PATH_MAX
%19 = call i32 @snprintf(i8* %15, i32 %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %11)
%20 = call i32 @maybe_mkdir(i8* %15, i32 448)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %1
%24 = load i32, i32* @errno
%25 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %26
26: ; preds = %23, %1
%27 = load i32, i32* @MS_BIND
%28 = load i32, i32* @MS_REC
%29 = or i32 %27, %28
%30 = call i32 @mount(i8* %11, i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %29, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
store i32 %30, i32* %3
%31 = load i32, i32* %3
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i32, i32* @errno
%35 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %34, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %11)
br label %36
36: ; preds = %33, %26
%37 = load i32, i32* @SYS_pivot_root
%38 = call i32 @syscall(i32 %37, i8* %11, i8* %15)
store i32 %38, i32* %3
%39 = load i32, i32* %3
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @errno
%43 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %42, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %15)
br label %44
44: ; preds = %41, %36
%45 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%46 = load i32, i32* @MNT_DETACH
%47 = call i32 @umount2(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %46)
store i32 %47, i32* %3
%48 = load i32, i32* %3
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = load i32, i32* @errno
%52 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %51, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0))
br label %53
53: ; preds = %50, %44
%54 = call i32 @rmdir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%55 = load i8*, i8** %4
ret i32 0
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [14 x i8] c"%s/tmp/.pivot\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"can't mkdir %s\00"
@.str.2 = [5 x i8] c"bind\00"
@MS_BIND = global i32 0
@MS_REC = global i32 0
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [20 x i8] c"can't bind mount %s\00"
@SYS_pivot_root = global i32 0
@.str.5 = [23 x i8] c"can't pivot root to %s\00"
@.str.6 = [2 x i8] c"/\00"
@.str.7 = [12 x i8] c"/tmp/.pivot\00"
@MNT_DETACH = global i32 0
@.str.8 = [21 x i8] c"can't umount /.pivot\00"
define i32 @switchroot(i8* nocapture %0) {
%2 = load i32, i32* @PATH_MAX
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = alloca i8, i64 %4
%7 = call i32 @realpath(i8* %0, i8* %5)
%8 = load i32, i32* @PATH_MAX
%9 = call i32 @snprintf(i8* %6, i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %5)
%10 = call i32 @maybe_mkdir(i8* %6, i32 448)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %15
15: ; preds = %12, %1
%16 = load i32, i32* @MS_BIND
%17 = load i32, i32* @MS_REC
%18 = or i32 %17, %16
%19 = call i32 @mount(i8* %5, i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %18, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i32, i32* @errno
%23 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %5)
br label %24
24: ; preds = %21, %15
%25 = load i32, i32* @SYS_pivot_root
%26 = call i32 @syscall(i32 %25, i8* %5, i8* %6)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %29, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %6)
br label %31
31: ; preds = %28, %24
%32 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%33 = load i32, i32* @MNT_DETACH
%34 = call i32 @umount2(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* @errno
%38 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0))
br label %39
39: ; preds = %36, %31
%40 = call i32 @rmdir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}