#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*/ EXIT_FAILURE;
scalar_t__ chdir(char const *);
scalar_t__ chroot(char const *);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
void switch_root(const char *root, const char *dir) {
  if (chroot(root))
    err(EXIT_FAILURE, "chroot");
  if (chdir("/"))
    err(EXIT_FAILURE, "chdir to /");
  if (chdir(dir))
    err(EXIT_FAILURE, "chdir to home (%s)", dir);
}


x86 assembly (clang, -O0):
<switch_root>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
je     116a <switch_root+0x3a>
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
lea    0xe9d(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
lea    0xe9f(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     1197 <switch_root+0x67>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11c5 <switch_root+0x95>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switch_root>:
push   %rbx
mov    %rsi,%rbx
call   1030 <chroot@plt>
test   %rax,%rax
je     1155 <switch_root+0x25>
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
lea    0xeb2(%rip),%rsi
xor    %eax,%eax
call   1050 <err@plt>
lea    0xeb4(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
je     117d <switch_root+0x4d>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
lea    0xe91(%rip),%rsi
xor    %eax,%eax
call   1050 <err@plt>
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
je     11a5 <switch_root+0x75>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
pop    %rbx
jmp    1050 <err@plt>
pop    %rbx
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [7 x i8] c"chroot\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [11 x i8] c"chdir to /\00"
@.str.3 = [19 x i8] c"chdir to home (%s)\00"
define void @switch_root(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%5 = load i8*, i8** %3
%6 = call i64 @chroot(i8* %5)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 (i32, i8*, ...) @err(i32 %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %8, %2
%12 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 (i32, i8*, ...) @err(i32 %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %14, %11
%18 = load i8*, i8** %4
%19 = call i64 @chdir(i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* @EXIT_FAILURE
%23 = load i8*, i8** %4
%24 = call i32 (i32, i8*, ...) @err(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %21, %17
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [7 x i8] c"chroot\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [11 x i8] c"chdir to /\00"
@.str.3 = [19 x i8] c"chdir to home (%s)\00"
define void @switch_root(i8* %0, i8* %1) {
%3 = tail call i64 @chroot(i8* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 (i32, i8*, ...) @err(i32 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %5, %2
%9 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 (i32, i8*, ...) @err(i32 %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %11, %8
%15 = tail call i64 @chdir(i8* %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 (i32, i8*, ...) @err(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %20
20: ; preds = %17, %14
ret void
}