#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 *);
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int handle_zng(int);
int /*<<< orphan*/ printf(char *, ...);
int chdir_test(char *dir) {
  int ret, save_errno;
  int passed;
  printf("chdir(): dir=%s\n", (((dir) == NULL) ? "(null)" : (dir)));
  fflush(NULL);
  passed = handle_zng(ret = chdir(dir));
  save_errno = errno;
  switch (passed) {
  case -1:
  case 0:
    printf("\t\tret = %d\n", ret);
    printf("\tSucceeded Phase 1\n");
    fflush(NULL);
    break;
  case -2:
    printf("\tFailed Phase 1: returned undefined value! "
           "ret = %d\n",
           ret);
    fflush(NULL);
    break;
  default:
    printf("Internal syscalltester error: passed = %d\n", passed);
    fflush(NULL);
    break;
  }
  errno = save_errno;
  return ret;
}


x86 assembly (clang, -O0):
<chdir_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1167 <chdir_test+0x27>
lea    0xeb3(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    116f <chdir_test+0x2f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe86(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x28(%rbp)
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x8(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,%edi
mov    %edi,-0xc(%rbp)
call   1030 <handle_zng@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0xfffffffe,%eax
je     1205 <chdir_test+0xc5>
jmp    11c4 <chdir_test+0x84>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     1224 <chdir_test+0xe4>
jmp    11d8 <chdir_test+0x98>
mov    -0xc(%rbp),%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    123e <chdir_test+0xfe>
mov    -0xc(%rbp),%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    123e <chdir_test+0xfe>
mov    -0x14(%rbp),%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x10(%rbp),%ecx
mov    0x2d98(%rip),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chdir_test>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %rdi,%rdi
lea    0xeb0(%rip),%rsi
cmovne %rdi,%rsi
lea    0xe94(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %rbx,%rdi
call   1050 <chdir@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <handle_zng@plt>
mov    0x2e4e(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11bb <chdir_test+0x6b>
lea    0xe74(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xed0(%rip),%rdi
call   1030 <puts@plt>
jmp    11db <chdir_test+0x8b>
cmp    $0xfffffffe,%eax
jne    11cb <chdir_test+0x7b>
lea    0xe5d(%rip),%rdi
mov    %ebx,%esi
jmp    11d4 <chdir_test+0x84>
lea    0xe87(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %ebp,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"chdir(): dir=%s\0A\00"
@.str.1 = [7 x i8] c"(null)\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"\09\09ret = %d\0A\00"
@.str.3 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.4 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i32 @chdir_test(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
br label %11
9: ; preds = %1
%10 = load i8*, i8** %2
br label %11
11: ; preds = %9, %8
%12 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %8 ], [ %10, %9 ]
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = call i32 @fflush(i32* null)
%15 = load i8*, i8** %2
%16 = call i32 @chdir(i8* %15)
store i32 %16, i32* %3
%17 = call i32 @handle_zng(i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* @errno
store i32 %18, i32* %4
%19 = load i32, i32* %5
switch i32 %19, label %29 [
i32 -1, label %20
i32 0, label %20
i32 -2, label %25
]
20: ; preds = %11, %11
%21 = load i32, i32* %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @fflush(i32* null)
br label %33
25: ; preds = %11
%26 = load i32, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = call i32 @fflush(i32* null)
br label %33
29: ; preds = %11
%30 = load i32, i32* %5
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = call i32 @fflush(i32* null)
br label %33
33: ; preds = %29, %25, %20
%34 = load i32, i32* %4
store i32 %34, i32* @errno
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [17 x i8] c"chdir(): dir=%s\0A\00"
@.str.1 = [7 x i8] c"(null)\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"\09\09ret = %d\0A\00"
@.str.4 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
@str = [19 x i8] c"\09Succeeded Phase 1\00"
define i32 @chdir_test(i8* %0) {
%2 = icmp eq i8* %0, null
%3 = select i1 %2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %3)
%5 = tail call i32 @fflush(i32* null)
%6 = tail call i32 @chdir(i8* %0)
%7 = tail call i32 @handle_zng(i32 %6)
%8 = load i32, i32* @errno
switch i32 %7, label %14 [
i32 -1, label %9
i32 0, label %9
i32 -2, label %12
]
9: ; preds = %1, %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %16
12: ; preds = %1
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %6)
br label %16
14: ; preds = %1
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %7)
br label %16
16: ; preds = %14, %12, %9
%17 = tail call i32 @fflush(i32* null)
store i32 %8, i32* @errno
ret i32 %6
}