#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 chroot(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *mkdtemp(char *);
int /*<<< orphan*/ perror(char *);
int rmdir(char *);
int syscall(int /*<<< orphan*/, char *, char *);
int umount2(char *, int /*<<< orphan*/);
void ChangeRoot() {
  char old_root[16] = "old-root-XXXXXX";
  if (mkdtemp(old_root) == NULL) {
    perror("mkdtemp(old_root)");
    exit(1);
  };
  if ((syscall(SYS_pivot_root, ".", old_root)) == -1) {
    perror("syscall(SYS_pivot_root, \".\", old_root)");
    exit(1);
  };
  if ((chroot(".")) == -1) {
    perror("chroot(\".\")");
    exit(1);
  };
  if ((umount2(old_root, MNT_DETACH)) == -1) {
    perror("umount2(old_root, MNT_DETACH)");
    exit(1);
  };
  if ((rmdir(old_root)) == -1) {
    perror("rmdir(old_root)");
    exit(1);
  };
}


x86 assembly (clang, -O0):
<ChangeRoot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0xe81(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe7e(%rip),%rax
mov    %rax,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1070 <mkdtemp@plt>
cmp    $0x0,%rax
jne    11b7 <ChangeRoot+0x47>
lea    0xe68(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rdx
lea    0xe57(%rip),%rsi
call   1060 <syscall@plt>
cmp    $0xffffffff,%eax
jne    11ef <ChangeRoot+0x7f>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe2c(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
jne    121a <ChangeRoot+0xaa>
lea    0xe40(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x10(%rbp),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   1040 <umount2@plt>
cmp    $0xffffffff,%eax
jne    124b <ChangeRoot+0xdb>
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x10(%rbp),%rdi
call   1050 <rmdir@plt>
cmp    $0xffffffff,%eax
jne    1273 <ChangeRoot+0x103>
lea    0xe11(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ChangeRoot>:
sub    $0x18,%rsp
movaps 0xe85(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
call   1070 <mkdtemp@plt>
test   %rax,%rax
je     11e2 <ChangeRoot+0x72>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
mov    %rsp,%rdx
call   1060 <syscall@plt>
cmp    $0xffffffff,%eax
je     11eb <ChangeRoot+0x7b>
lea    0xe72(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
je     11f4 <ChangeRoot+0x84>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1040 <umount2@plt>
cmp    $0xffffffff,%eax
je     11fd <ChangeRoot+0x8d>
mov    %rsp,%rdi
call   1050 <rmdir@plt>
cmp    $0xffffffff,%eax
je     1206 <ChangeRoot+0x96>
add    $0x18,%rsp
ret
lea    0xe27(%rip),%rdi
jmp    120d <ChangeRoot+0x9d>
lea    0xe32(%rip),%rdi
jmp    120d <ChangeRoot+0x9d>
lea    0xe50(%rip),%rdi
jmp    120d <ChangeRoot+0x9d>
lea    0xe53(%rip),%rdi
jmp    120d <ChangeRoot+0x9d>
lea    0xe68(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@__const.ChangeRoot.old_root = [16 x i8] c"old-root-XXXXXX\00"
@.str = [18 x i8] c"mkdtemp(old_root)\00"
@SYS_pivot_root = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [39 x i8] c"syscall(SYS_pivot_root, \22.\22, old_root)\00"
@.str.3 = [12 x i8] c"chroot(\22.\22)\00"
@MNT_DETACH = global i32 0
@.str.4 = [30 x i8] c"umount2(old_root, MNT_DETACH)\00"
@.str.5 = [16 x i8] c"rmdir(old_root)\00"
define void @ChangeRoot() {
%1 = alloca [16 x i8]
%2 = bitcast [16 x i8]* %1 to i8*
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%4 = call i32* @mkdtemp(i8* %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @SYS_pivot_root
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%12 = call i32 @syscall(i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = call i32 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%25 = load i32, i32* @MNT_DETACH
%26 = call i32 @umount2(i8* %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%33 = call i32 @rmdir(i8* %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31
ret void
}

LLVM IR (-O2):
@__const.ChangeRoot.old_root = [16 x i8] c"old-root-XXXXXX\00"
@.str = [18 x i8] c"mkdtemp(old_root)\00"
@SYS_pivot_root = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [39 x i8] c"syscall(SYS_pivot_root, \22.\22, old_root)\00"
@.str.3 = [12 x i8] c"chroot(\22.\22)\00"
@MNT_DETACH = global i32 0
@.str.4 = [30 x i8] c"umount2(old_root, MNT_DETACH)\00"
@.str.5 = [16 x i8] c"rmdir(old_root)\00"
define void @ChangeRoot() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = call i32* @mkdtemp(i8* %2)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @SYS_pivot_root
%10 = call i32 @syscall(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %8
%16 = call i32 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = load i32, i32* @MNT_DETACH
%23 = call i32 @umount2(i8* %2, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %21
%29 = call i32 @rmdir(i8* %2)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %28
ret void
}