#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*/ execlp(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
main(int argc, char **argv) {
  if (chdir(argv[1])) {
    perror("chdir");
    exit(1);
  }
  if (chroot(".")) {
    perror("chroot");
    exit(1);
  }
  if (argv[2]) {
    execvp(argv[2], &argv[2]);
    perror("exec argv1");
    exit(1);
  }
  execlp("/bin/sh", "/bin/sh", "-i", NULL);
  perror("exec sh");
  exit(1);
}


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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11a3 <main+0x43>
lea    0xe6c(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe5c(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
je     11cf <main+0x6f>
lea    0xe48(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     1209 <main+0xa9>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   1060 <execvp@plt>
lea    0xe15(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe0a(%rip),%rsi
lea    0xe0b(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1080 <execlp@plt>
lea    0xdfb(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    11ad <main+0x4d>
lea    0xe8d(%rip),%rdi
call   1030 <chroot@plt>
test   %rax,%rax
jne    11b6 <main+0x56>
mov    0x10(%rbx),%rdi
test   %rdi,%rdi
jne    11bf <main+0x5f>
lea    0xe87(%rip),%rdi
lea    0xe88(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1080 <execlp@plt>
lea    0xe7a(%rip),%rdi
jmp    11d2 <main+0x72>
lea    0xe4c(%rip),%rdi
jmp    11d2 <main+0x72>
lea    0xe4b(%rip),%rdi
jmp    11d2 <main+0x72>
add    $0x10,%rbx
mov    %rbx,%rsi
call   1060 <execvp@plt>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"chdir\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [7 x i8] c"chroot\00"
@.str.3 = [11 x i8] c"exec argv1\00"
@.str.4 = [8 x i8] c"/bin/sh\00"
@.str.5 = [3 x i8] c"-i\00"
@.str.6 = [8 x i8] c"exec sh\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 i8**, i8*** %5
%7 = getelementptr inbounds i8*, i8** %6, i64 1
%8 = load i8*, i8** %7
%9 = call i64 @chdir(i8* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %34
25: ; preds = %20
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = call i32 @execvp(i8* %28, i8** %30)
%32 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %20
%35 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* null)
%36 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"chdir\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [7 x i8] c"chroot\00"
@.str.3 = [11 x i8] c"exec argv1\00"
@.str.4 = [8 x i8] c"/bin/sh\00"
@.str.5 = [3 x i8] c"-i\00"
@.str.6 = [8 x i8] c"exec sh\00"
define i32 @main(i32 %0, i8** %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i64 @chdir(i8* %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = icmp eq i8* %18, null
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = tail call i32 @execvp(i8* %18, i8** %17)
%22 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %16
%25 = tail call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* null)
%26 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
}