#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 */
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ execvp(char *, char **);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: chroot NEWROOT [COMMAND...] \n");
    return 1;
  }
  printf("newroot = %s\n", argv[1]);
  if (chroot(argv[1])) {
    perror("chroot");
    return 1;
  }
  if (chdir("/")) {
    perror("chdir");
    return 1;
  }
  printf("argc %d\n", argc);
  if (argc == 2) {
    argv[0] = getenv("SHELL");
    if (!argv[0])
      argv[0] = (char *)"/bin/sh";
    argv[1] = (char *)"-i";
    argv[2] = NULL;
  } else {
    argv += 2;
  }
  execvp(argv[0], argv);
  printf("chroot: cannot run command `%s`\n", *argv);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119a <main+0x3a>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x152>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chroot@plt>
cmp    $0x0,%rax
je     11df <main+0x7f>
lea    0xe65(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x152>
lea    0xe54(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     120d <main+0xad>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x152>
mov    -0x8(%rbp),%esi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jne    127a <main+0x11a>
lea    0xe1c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    125a <main+0xfa>
mov    -0x10(%rbp),%rax
lea    0xdfa(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
lea    0xdf4(%rip),%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,0x10(%rax)
jmp    1286 <main+0x126>
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <execvp@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     118e <main+0x1e>
lea    0xed9(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%ebx
jmp    1232 <main+0xc2>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rsi
lea    0xe62(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rbx),%rdi
call   1040 <chroot@plt>
test   %rax,%rax
jne    123b <main+0xcb>
lea    0xe57(%rip),%rdi
call   1060 <chdir@plt>
test   %rax,%rax
jne    1244 <main+0xd4>
lea    0xe4e(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x2,%ebp
jne    120f <main+0x9f>
lea    0xe42(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe39(%rip),%rdi
cmovne %rax,%rdi
mov    %rdi,(%rbx)
lea    0xe33(%rip),%rax
mov    %rax,0x8(%rbx)
movq   $0x0,0x10(%rbx)
jmp    1217 <main+0xa7>
mov    0x10(%rbx),%rdi
add    $0x10,%rbx
mov    %rbx,%rsi
call   1090 <execvp@plt>
mov    (%rbx),%rsi
lea    0xe0e(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdcc(%rip),%rdi
jmp    124b <main+0xdb>
lea    0xdcc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    1232 <main+0xc2>

LLVM IR (-O0):
@.str = [37 x i8] c"Usage: chroot NEWROOT [COMMAND...] \0A\00"
@.str.1 = [14 x i8] c"newroot = %s\0A\00"
@.str.2 = [7 x i8] c"chroot\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [6 x i8] c"chdir\00"
@.str.5 = [9 x i8] c"argc %d\0A\00"
@.str.6 = [6 x i8] c"SHELL\00"
@.str.7 = [8 x i8] c"/bin/sh\00"
@.str.8 = [3 x i8] c"-i\00"
@.str.9 = [33 x i8] c"chroot: cannot run command `%s`\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 2
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %60
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @chroot(i8* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %60
22: ; preds = %10
%23 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %60
27: ; preds = %22
%28 = load i32, i32* %4
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 2
br i1 %31, label %32, label %48
32: ; preds = %27
%33 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 0
store i8* %33, i8** %35
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 0
%38 = load i8*, i8** %37
%39 = icmp ne i8* %38, null
br i1 %39, label %43, label %40
40: ; preds = %32
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8** %42
br label %43
43: ; preds = %40, %32
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8** %45
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 2
store i8* null, i8** %47
br label %51
48: ; preds = %27
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 2
store i8** %50, i8*** %5
br label %51
51: ; preds = %48, %43
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 0
%54 = load i8*, i8** %53
%55 = load i8**, i8*** %5
%56 = call i32 @execvp(i8* %54, i8** %55)
%57 = load i8**, i8*** %5
%58 = load i8*, i8** %57
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), i8* %58)
store i32 0, i32* %3
br label %60
60: ; preds = %51, %25, %20, %8
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str.1 = [14 x i8] c"newroot = %s\0A\00"
@.str.2 = [7 x i8] c"chroot\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [6 x i8] c"chdir\00"
@.str.5 = [9 x i8] c"argc %d\0A\00"
@.str.6 = [6 x i8] c"SHELL\00"
@.str.7 = [8 x i8] c"/bin/sh\00"
@.str.8 = [3 x i8] c"-i\00"
@.str.9 = [33 x i8] c"chroot: cannot run command `%s`\0A\00"
@str = [36 x i8] c"Usage: chroot NEWROOT [COMMAND...] \00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %37
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %7
%11 = tail call i64 @chroot(i8* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %6
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %37
15: ; preds = %6
%16 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %37
20: ; preds = %15
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %0)
%22 = icmp eq i32 %0, 2
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%25 = icmp eq i8* %24, null
%26 = select i1 %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* %24
store i8* %26, i8** %1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8** %7
%27 = getelementptr inbounds i8*, i8** %1, i64 2
store i8* null, i8** %27
br label %31
28: ; preds = %20
%29 = getelementptr inbounds i8*, i8** %1, i64 2
%30 = load i8*, i8** %29
br label %31
31: ; preds = %28, %23
%32 = phi i8* [ %26, %23 ], [ %30, %28 ]
%33 = phi i8** [ %1, %23 ], [ %29, %28 ]
%34 = tail call i32 @execvp(i8* %32, i8** %33)
%35 = load i8*, i8** %33
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %31, %18, %13, %4
%38 = phi i32 [ 1, %4 ], [ 1, %13 ], [ 1, %18 ], [ 0, %31 ]
ret i32 %38
}