#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 chdir(char const *);
int chroot_fd(int);
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int uio_open(char *, char *, int /*<<< orphan*/);
int chroot_secure_chdir(const char *root, const char *dir) {
  int orig_root, new_root;
  if ((orig_root = uio_open("/", "r", 0)) == -1)
    return -1;
  if (chdir(root) == -1)
    return -1;
  if ((new_root = uio_open(".", "r", 0)) == -1)
    return -1;
  int dirfd;
  int errno_orig;
  if (chroot_fd(new_root) == -1)
    return -1;
  if (chdir(dir) == -1)
    goto err;
  dirfd = uio_open(".", "r", 0);
  if (dirfd == -1)
    goto err;
  chroot_fd(orig_root);
  if (fchdir(dirfd) == -1)
    goto err2;
  close(dirfd);
  return 0;
err2:
  errno_orig = errno;
  close(dirfd);
  errno = errno_orig;
err:
  errno_orig = errno;
  chroot_fd(orig_root);
  errno = errno_orig;
  return -1;
}


x86 assembly (clang, -O0):
<chroot_secure_chdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe99(%rip),%rdi
lea    0xe94(%rip),%rsi
xor    %edx,%edx
call   1060 <uio_open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    118d <chroot_secure_chdir+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <chroot_secure_chdir+0x15c>
mov    -0x10(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    11ab <chroot_secure_chdir+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <chroot_secure_chdir+0x15c>
lea    0xe52(%rip),%rdi
lea    0xe49(%rip),%rsi
xor    %edx,%edx
call   1060 <uio_open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11d8 <chroot_secure_chdir+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <chroot_secure_chdir+0x15c>
mov    -0x20(%rbp),%edi
call   1070 <chroot_fd@plt>
cmp    $0xffffffff,%eax
jne    11f5 <chroot_secure_chdir+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <chroot_secure_chdir+0x15c>
mov    -0x18(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
jne    120c <chroot_secure_chdir+0xbc>
jmp    1285 <chroot_secure_chdir+0x135>
lea    0xdf1(%rip),%rdi
lea    0xde8(%rip),%rsi
xor    %edx,%edx
call   1060 <uio_open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1233 <chroot_secure_chdir+0xe3>
jmp    1285 <chroot_secure_chdir+0x135>
mov    -0x1c(%rbp),%edi
call   1070 <chroot_fd@plt>
mov    -0x24(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    1251 <chroot_secure_chdir+0x101>
jmp    1265 <chroot_secure_chdir+0x115>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ac <chroot_secure_chdir+0x15c>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%ecx
mov    0x2d5d(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <chroot_fd@plt>
mov    -0x28(%rbp),%ecx
mov    0x2d3d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chroot_secure_chdir>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe9b(%rip),%rdi
lea    0xe96(%rip),%rsi
xor    %edx,%edx
call   1060 <uio_open@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     122b <chroot_secure_chdir+0xdb>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
je     122b <chroot_secure_chdir+0xdb>
lea    0xe68(%rip),%rdi
lea    0xe5f(%rip),%rsi
xor    %edx,%edx
call   1060 <uio_open@plt>
cmp    $0xffffffff,%eax
je     122b <chroot_secure_chdir+0xdb>
mov    %eax,%edi
call   1070 <chroot_fd@plt>
cmp    $0xffffffff,%eax
je     122b <chroot_secure_chdir+0xdb>
mov    %r15,%rdi
call   1030 <chdir@plt>
cmp    $0xffffffff,%eax
je     1217 <chroot_secure_chdir+0xc7>
lea    0xe35(%rip),%rdi
lea    0xe2c(%rip),%rsi
xor    %edx,%edx
call   1060 <uio_open@plt>
cmp    $0xffffffff,%eax
je     1217 <chroot_secure_chdir+0xc7>
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <chroot_fd@plt>
mov    %ebx,%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
je     1203 <chroot_secure_chdir+0xb3>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %r14d,%r14d
jmp    122b <chroot_secure_chdir+0xdb>
mov    0x2dd6(%rip),%r15
mov    (%r15),%r12d
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r12d,(%r15)
mov    0x2dc2(%rip),%r15
mov    (%r15),%ebx
mov    %ebp,%edi
call   1070 <chroot_fd@plt>
mov    %ebx,(%r15)
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c".\00"
@errno = global i32 0
define i32 @chroot_secure_chdir(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = call i32 @uio_open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %57
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @chdir(i8* %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 -1, i32* %3
br label %57
18: ; preds = %13
%19 = call i32 @uio_open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %19, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -1, i32* %3
br label %57
22: ; preds = %18
%23 = load i32, i32* %7
%24 = call i32 @chroot_fd(i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %3
br label %57
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = call i32 @chdir(i8* %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %27
br label %52
32: ; preds = %27
%33 = call i32 @uio_open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %32
br label %52
37: ; preds = %32
%38 = load i32, i32* %6
%39 = call i32 @chroot_fd(i32 %38)
%40 = load i32, i32* %8
%41 = call i32 @fchdir(i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %37
br label %47
44: ; preds = %37
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 0, i32* %3
br label %57
47: ; preds = %43
%48 = load i32, i32* @errno
store i32 %48, i32* %9
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %9
store i32 %51, i32* @errno
br label %52
52: ; preds = %47, %36, %31
%53 = load i32, i32* @errno
store i32 %53, i32* %9
%54 = load i32, i32* %6
%55 = call i32 @chroot_fd(i32 %54)
%56 = load i32, i32* %9
store i32 %56, i32* @errno
store i32 -1, i32* %3
br label %57
57: ; preds = %52, %44, %26, %21, %17, %12
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c".\00"
@errno = global i32 0
define i32 @chroot_secure_chdir(i8* %0, i8* %1) {
%3 = tail call i32 @uio_open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %32, label %5
5: ; preds = %2
%6 = tail call i32 @chdir(i8* %0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %32, label %8
8: ; preds = %5
%9 = tail call i32 @uio_open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %32, label %11
11: ; preds = %8
%12 = tail call i32 @chroot_fd(i32 %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %32, label %14
14: ; preds = %11
%15 = tail call i32 @chdir(i8* %1)
%16 = icmp eq i32 %15, -1
br i1 %16, label %29, label %17
17: ; preds = %14
%18 = tail call i32 @uio_open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %29, label %20
20: ; preds = %17
%21 = tail call i32 @chroot_fd(i32 %3)
%22 = tail call i32 @fchdir(i32 %18)
%23 = icmp eq i32 %22, -1
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = tail call i32 @close(i32 %18)
br label %32
26: ; preds = %20
%27 = load i32, i32* @errno
%28 = tail call i32 @close(i32 %18)
store i32 %27, i32* @errno
br label %29
29: ; preds = %17, %14, %26
%30 = load i32, i32* @errno
%31 = tail call i32 @chroot_fd(i32 %3)
store i32 %30, i32* @errno
br label %32
32: ; preds = %24, %29, %11, %8, %5, %2
%33 = phi i32 [ -1, %2 ], [ -1, %5 ], [ -1, %8 ], [ -1, %29 ], [ 0, %24 ], [ -1, %11 ]
ret i32 %33
}