#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int MS_BIND;
int MS_REC;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int chdir(char *);
int checked_mount(char *, char *);
int chroot(char *);
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ mount(char *, char *, int /*<<< orphan*/, int,
                         int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
int main(int argc, char **argv) {
  if ((chdir(argv[1])) == -1) {
    perror("chdir(argv[1])");
    exit(1);
  };
  if ((checked_mount("/dev", "./dev")) == -1) {
    perror("checked_mount(\"/dev\", \"./dev\")");
    exit(1);
  };
  if ((checked_mount("/proc", "./proc")) == -1) {
    perror("checked_mount(\"/proc\", \"./proc\")");
    exit(1);
  };
  if ((checked_mount("/sys", "./sys")) == -1) {
    perror("checked_mount(\"/sys\", \"./sys\")");
    exit(1);
  };
  struct stat mount_data;
  if (stat("./parent_root", &mount_data) != -1) {
    if (S_ISDIR(mount_data.st_mode)) {
      mount("/", "./parent_root", 0, MS_BIND | MS_REC, 0);
    }
  }
  if ((chroot(".")) == -1) {
    perror("chroot(\".\")");
    exit(1);
  };
  if ((execvp(argv[2], argv + 2)) == -1) {
    perror("execvp(argv[2], argv+2)");
    exit(1);
  };
}


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    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11d2 <main+0x42>
lea    0xe3d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe37(%rip),%rdi
lea    0xe2f(%rip),%rsi
call   1080 <checked_mount@plt>
cmp    $0xffffffff,%eax
jne    1204 <main+0x74>
lea    0xe20(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe2a(%rip),%rdi
lea    0xe22(%rip),%rsi
call   1080 <checked_mount@plt>
cmp    $0xffffffff,%eax
jne    1236 <main+0xa6>
lea    0xe14(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe20(%rip),%rdi
lea    0xe18(%rip),%rsi
call   1080 <checked_mount@plt>
cmp    $0xffffffff,%eax
jne    1268 <main+0xd8>
lea    0xe09(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe12(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     12c3 <main+0x133>
mov    -0x18(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12be <main+0x12e>
mov    0x2d4e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d3d(%rip),%rax
or     (%rax),%ecx
lea    0xde3(%rip),%rdi
lea    0xdce(%rip),%rsi
xor    %r8d,%r8d
mov    %r8d,%edx
call   1040 <mount@plt>
jmp    12c3 <main+0x133>
lea    0xdc7(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
jne    12ee <main+0x15e>
lea    0xdb4(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   10a0 <execvp@plt>
cmp    $0xffffffff,%eax
jne    1322 <main+0x192>
lea    0xd8c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
je     127a <main+0xea>
lea    0xe5f(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1080 <checked_mount@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0xf3>
lea    0xe68(%rip),%rdi
lea    0xe60(%rip),%rsi
call   1080 <checked_mount@plt>
cmp    $0xffffffff,%eax
je     128c <main+0xfc>
lea    0xe74(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1080 <checked_mount@plt>
cmp    $0xffffffff,%eax
je     1295 <main+0x105>
lea    0xe7c(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     124c <main+0xbc>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     124c <main+0xbc>
mov    0x2dbf(%rip),%rax
mov    0x2db0(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
lea    0xe54(%rip),%rdi
lea    0xe3f(%rip),%rsi
xor    %edx,%edx
xor    %r8d,%r8d
call   1040 <mount@plt>
lea    0xe3e(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
je     129e <main+0x10e>
mov    0x10(%rbx),%rdi
add    $0x10,%rbx
mov    %rbx,%rsi
call   10a0 <execvp@plt>
cmp    $0xffffffff,%eax
je     12a7 <main+0x117>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd7f(%rip),%rdi
jmp    12ae <main+0x11e>
lea    0xd8b(%rip),%rdi
jmp    12ae <main+0x11e>
lea    0xda8(%rip),%rdi
jmp    12ae <main+0x11e>
lea    0xdc6(%rip),%rdi
jmp    12ae <main+0x11e>
lea    0xdee(%rip),%rdi
jmp    12ae <main+0x11e>
lea    0xdf1(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [15 x i8] c"chdir(argv[1])\00"
@.str.1 = [5 x i8] c"/dev\00"
@.str.2 = [6 x i8] c"./dev\00"
@.str.3 = [31 x i8] c"checked_mount(\22/dev\22, \22./dev\22)\00"
@.str.4 = [6 x i8] c"/proc\00"
@.str.5 = [7 x i8] c"./proc\00"
@.str.6 = [33 x i8] c"checked_mount(\22/proc\22, \22./proc\22)\00"
@.str.7 = [5 x i8] c"/sys\00"
@.str.8 = [6 x i8] c"./sys\00"
@.str.9 = [31 x i8] c"checked_mount(\22/sys\22, \22./sys\22)\00"
@.str.10 = [14 x i8] c"./parent_root\00"
@.str.11 = [2 x i8] c"/\00"
@MS_BIND = global i32 0
@MS_REC = global i32 0
@.str.12 = [2 x i8] c".\00"
@.str.13 = [12 x i8] c"chroot(\22.\22)\00"
@.str.14 = [24 x i8] c"execvp(argv[2], argv+2)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i8**, i8*** %5
%8 = getelementptr inbounds i8*, i8** %7, i64 1
%9 = load i8*, i8** %8
%10 = call i32 @chdir(i8* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32 @checked_mount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = call i32 @checked_mount(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = call i32 @checked_mount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = call i32 @stat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.10, i64 0, i64 0), %struct.stat* %6)
%35 = icmp ne i32 %34, -1
br i1 %35, label %36, label %47
36: ; preds = %33
%37 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i64 @S_ISDIR(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = load i32, i32* @MS_BIND
%43 = load i32, i32* @MS_REC
%44 = or i32 %42, %43
%45 = call i32 @mount(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.10, i64 0, i64 0), i32 0, i32 %44, i32 0)
br label %46
46: ; preds = %41, %36
br label %47
47: ; preds = %46, %33
%48 = call i32 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.13, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %47
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 2
%56 = load i8*, i8** %55
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 2
%59 = call i32 @execvp(i8* %56, i8** %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %53
%62 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.14, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %53
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [15 x i8] c"chdir(argv[1])\00"
@.str.1 = [5 x i8] c"/dev\00"
@.str.2 = [6 x i8] c"./dev\00"
@.str.3 = [31 x i8] c"checked_mount(\22/dev\22, \22./dev\22)\00"
@.str.4 = [6 x i8] c"/proc\00"
@.str.5 = [7 x i8] c"./proc\00"
@.str.6 = [33 x i8] c"checked_mount(\22/proc\22, \22./proc\22)\00"
@.str.7 = [5 x i8] c"/sys\00"
@.str.8 = [6 x i8] c"./sys\00"
@.str.9 = [31 x i8] c"checked_mount(\22/sys\22, \22./sys\22)\00"
@.str.10 = [14 x i8] c"./parent_root\00"
@.str.11 = [2 x i8] c"/\00"
@MS_BIND = global i32 0
@MS_REC = global i32 0
@.str.12 = [2 x i8] c".\00"
@.str.13 = [12 x i8] c"chroot(\22.\22)\00"
@.str.14 = [24 x i8] c"execvp(argv[2], argv+2)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.stat
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = tail call i32 @chdir(i8* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i32 @checked_mount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %11
%18 = tail call i32 @checked_mount(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = tail call i32 @checked_mount(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %23
%30 = bitcast %struct.stat* %3 to i8*
%31 = call i32 @stat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.10, i64 0, i64 0), %struct.stat* %3)
%32 = icmp eq i32 %31, -1
br i1 %32, label %43, label %33
33: ; preds = %29
%34 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%35 = load i32, i32* %34
%36 = tail call i64 @S_ISDIR(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %33
%39 = load i32, i32* @MS_BIND
%40 = load i32, i32* @MS_REC
%41 = or i32 %40, %39
%42 = tail call i32 @mount(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.10, i64 0, i64 0), i32 0, i32 %41, i32 0)
br label %43
43: ; preds = %33, %38, %29
%44 = tail call i32 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.13, i64 0, i64 0))
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %43
%50 = getelementptr inbounds i8*, i8** %1, i64 2
%51 = load i8*, i8** %50
%52 = tail call i32 @execvp(i8* %51, i8** %50)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %49
%55 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.14, i64 0, i64 0))
%56 = tail call i32 @exit(i32 1)
unreachable
57: ; preds = %49
ret i32 0
}