#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 chdir(char *);
int chroot(char *);
int execvp(char *, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[], char *envp[]) {
  if (argc < 3) {
    fprintf(stderr, "Usage: %s newroot command...\n", argv[0]);
    return 1;
  }
  if (chroot(argv[1]) == -1) {
    perror("chroot");
    return 1;
  }
  if (chdir("/") == -1) {
    perror("chdir");
    return 1;
  }
  if (execvp(argv[2], argv + 2) == -1) {
    perror("execvp");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    119c <main+0x4c>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe75(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1234 <main+0xe4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
jne    11ca <main+0x7a>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1234 <main+0xe4>
lea    0xe54(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11f7 <main+0xa7>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1234 <main+0xe4>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   1070 <execvp@plt>
cmp    $0xffffffff,%eax
jne    122d <main+0xdd>
lea    0xe11(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1234 <main+0xe4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     1178 <main+0x28>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe94(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    11b0 <main+0x60>
mov    0x8(%rbx),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
je     11b2 <main+0x62>
lea    0xe98(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0xffffffff,%eax
je     11bb <main+0x6b>
mov    0x10(%rbx),%rdi
add    $0x10,%rbx
mov    %rbx,%rsi
call   1070 <execvp@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     11c4 <main+0x74>
pop    %rbx
ret
lea    0xe65(%rip),%rdi
jmp    11cb <main+0x7b>
lea    0xe65(%rip),%rdi
jmp    11cb <main+0x7b>
lea    0xe62(%rip),%rdi
call   1060 <perror@plt>
jmp    1171 <main+0x21>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: %s newroot command...\0A\00"
@.str.1 = [7 x i8] c"chroot\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@.str.4 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 3
br i1 %9, label %10, label %16
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %6
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 1, i32* %4
br label %40
16: ; preds = %3
%17 = load i8**, i8*** %6
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @chroot(i8* %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %40
24: ; preds = %16
%25 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %4
br label %40
29: ; preds = %24
%30 = load i8**, i8*** %6
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = load i8**, i8*** %6
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = call i32 @execvp(i8* %32, i8** %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %4
br label %40
39: ; preds = %29
store i32 0, i32* %4
br label %40
40: ; preds = %39, %37, %27, %22, %10
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: %s newroot command...\0A\00"
@.str.1 = [7 x i8] c"chroot\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [6 x i8] c"chdir\00"
@.str.4 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1, i8** nocapture readnone %2) {
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %28
9: ; preds = %3
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @chroot(i8* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %28
16: ; preds = %9
%17 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %28
21: ; preds = %16
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = tail call i32 @execvp(i8* %23, i8** %22)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %21, %26, %19, %14, %5
%29 = phi i32 [ 1, %5 ], [ 1, %14 ], [ 1, %19 ], [ 1, %26 ], [ 0, %21 ]
ret i32 %29
}