#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 EXIT_FAILURE;
int chdir(char *);
int chroot(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execve(char *, char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int status;
  char *rootdir;
  char *executable = "/bin/sh";
  char *newargv[] = {NULL, NULL};
  char *newenviron[] = {NULL};
  if (argc <= 2)
    usage("argument count");
  if (argc >= 3)
    executable = argv[2];
  rootdir = argv[1];
  status = chdir(rootdir);
  if (status) {
    fprintf(stderr, "chroot: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  status = chroot(rootdir);
  if (status) {
    fprintf(stderr, "chroot: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  newargv[0] = executable;
  execve(executable, newargv[0], newenviron);
  fprintf(stderr, "chroot: %s\n", strerror(errno));
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
jg     11d7 <main+0x57>
lea    0xe36(%rip),%rdi
call   1080 <usage@plt>
cmpl   $0x3,-0x8(%rbp)
jl     11ed <main+0x6d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1249 <main+0xc9>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xde1(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1030 <chroot@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1299 <main+0x119>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xd91(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1060 <execve@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xd3e(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
movq   $0x0,0x8(%rsp)
cmp    $0x2,%edi
jg     119f <main+0x2f>
lea    0xe77(%rip),%rdi
call   1070 <usage@plt>
lea    0xe63(%rip),%r15
jmp    11a3 <main+0x33>
mov    0x10(%r14),%r15
mov    0x8(%r14),%rbx
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %eax,%eax
jne    120a <main+0x9a>
mov    %rbx,%rdi
call   1030 <chroot@plt>
test   %eax,%eax
jne    120a <main+0x9a>
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    %r15,%rsi
call   1050 <execve@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%ebx
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe2a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dcf(%rip),%rax
mov    (%rax),%ebx
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdef(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [15 x i8] c"argument count\00"
@stderr = global i32 0
@.str.2 = [12 x i8] c"chroot: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [2 x i8*]
%10 = alloca [1 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %8
%11 = bitcast [2 x i8*]* %9 to i8*
%12 = bitcast [1 x i8*]* %10 to i8*
%13 = load i32, i32* %4
%14 = icmp sle i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @usage(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i32, i32* %4
%19 = icmp sge i32 %18, 3
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
store i8* %23, i8** %8
br label %24
24: ; preds = %20, %17
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = call i32 @chdir(i8* %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %24
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %24
%40 = load i8*, i8** %7
%41 = call i32 @chroot(i8* %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %51
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %39
%52 = load i8*, i8** %8
%53 = getelementptr inbounds [2 x i8*], [2 x i8*]* %9, i64 0, i64 0
store i8* %52, i8** %53
%54 = load i8*, i8** %8
%55 = getelementptr inbounds [2 x i8*], [2 x i8*]* %9, i64 0, i64 0
%56 = load i8*, i8** %55
%57 = getelementptr inbounds [1 x i8*], [1 x i8*]* %10, i64 0, i64 0
%58 = call i32 @execve(i8* %54, i8* %56, i8** %57)
%59 = load i32, i32* @stderr
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %61)
%63 = load i32, i32* @EXIT_FAILURE
ret i32 %63
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [15 x i8] c"argument count\00"
@stderr = global i32 0
@.str.2 = [12 x i8] c"chroot: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1 x i8*]
%4 = bitcast [1 x i8*]* %3 to i8*
%5 = bitcast [1 x i8*]* %3 to i64*
store i64 0, i64* %5
%6 = icmp slt i32 %0, 3
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @usage(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %12
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
br label %12
12: ; preds = %7, %9
%13 = phi i8* [ %11, %9 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %7 ]
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @chdir(i8* %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %12
%26 = tail call i32 @chroot(i8* %15)
%27 = icmp eq i32 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %25
%36 = getelementptr inbounds [1 x i8*], [1 x i8*]* %3, i64 0, i64 0
%37 = call i32 @execve(i8* %13, i8* %13, i8** %36)
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i32, i32* @EXIT_FAILURE
ret i32 %42
}