#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 */
#define ENOSYS 129
#define EPERM 128
int _error(char const *, char *, int);
int chroot(char *);
char *dirname(char *);
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char const *);
char *strdup(char const *);
int _chroot(char const *progname) {
  char *p;
  char *dir;
  int res;
  printf("%s: Testing chroot()\n", progname);
  if ((p = strdup(progname)) == NULL)
    return _error(progname, "strdup", 2);
  dir = dirname(p);
  res = chroot(dir);
  free(p);
  if (res != 0)
    switch (errno) {
    case ENOSYS:
    case EPERM:
      break;
    default:
      return _error(progname, "chroot", 3);
    }
  return 0;
}


x86 assembly (clang, -O0):
<_chroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe89(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b0 <_chroot+0x50>
mov    -0x10(%rbp),%rdi
lea    0xe78(%rip),%rsi
mov    $0x2,%edx
call   1070 <_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1224 <_chroot+0xc4>
mov    -0x18(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <chroot@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x24(%rbp)
je     121d <_chroot+0xbd>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     11fb <_chroot+0x9b>
jmp    11f6 <_chroot+0x96>
jmp    1218 <_chroot+0xb8>
mov    -0x10(%rbp),%rdi
lea    0xe17(%rip),%rsi
mov    $0x3,%edx
call   1070 <_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1224 <_chroot+0xc4>
jmp    121d <_chroot+0xbd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_chroot>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe92(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %r14,%rdi
call   1080 <strdup@plt>
test   %rax,%rax
je     11be <_chroot+0x5e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <dirname@plt>
mov    %rax,%rdi
call   1040 <chroot@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
je     11b7 <_chroot+0x57>
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    11cf <_chroot+0x6f>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe51(%rip),%rsi
mov    %r14,%rdi
mov    $0x2,%edx
jmp    11de <_chroot+0x7e>
lea    0xe47(%rip),%rsi
mov    %r14,%rdi
mov    $0x3,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <_error@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"%s: Testing chroot()\0A\00"
@.str.1 = [7 x i8] c"strdup\00"
@errno = global i32 0
@.str.2 = [7 x i8] c"chroot\00"
define i32 @_chroot(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i8*, i8** %3
%10 = call i8* @strdup(i8* %9)
store i8* %10, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @_error(i8* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 2)
store i32 %14, i32* %2
br label %32
15: ; preds = %1
%16 = load i8*, i8** %4
%17 = call i8* @dirname(i8* %16)
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = call i32 @chroot(i8* %18)
store i32 %19, i32* %6
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %15
%25 = load i32, i32* @errno
switch i32 %25, label %27 [
i32 129, label %26
i32 128, label %26
]
26: ; preds = %24, %24
br label %30
27: ; preds = %24
%28 = load i8*, i8** %3
%29 = call i32 @_error(i8* %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 3)
store i32 %29, i32* %2
br label %32
30: ; preds = %26
br label %31
31: ; preds = %30, %15
store i32 0, i32* %2
br label %32
32: ; preds = %31, %27, %12
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@.str = [22 x i8] c"%s: Testing chroot()\0A\00"
@.str.1 = [7 x i8] c"strdup\00"
@errno = global i32 0
@.str.2 = [7 x i8] c"chroot\00"
define i32 @_chroot(i8* %0) {
%2 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = tail call i8* @strdup(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @_error(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 2)
br label %18
7: ; preds = %1
%8 = tail call i8* @dirname(i8* %3)
%9 = tail call i32 @chroot(i8* %8)
%10 = tail call i32 @free(i8* %3)
%11 = icmp eq i32 %9, 0
br i1 %11, label %18, label %12
12: ; preds = %7
%13 = load i32, i32* @errno
%14 = and i32 %13, -2
%15 = icmp eq i32 %14, 128
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @_error(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %18
18: ; preds = %7, %12, %16, %5
%19 = phi i32 [ %6, %5 ], [ %17, %16 ], [ 0, %12 ], [ 0, %7 ]
ret i32 %19
}