#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 errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
char *getcwd(char *, size_t);
int handle_ptr(char *);
int /*<<< orphan*/ printf(char *, ...);
char *getcwd_test(char *buf, size_t size) {
  char *ret;
  int save_errno;
  int passed;
  printf("getcwd(): buf=0x%p, size=%zu\n", buf, size);
  fflush(NULL);
  passed = handle_ptr(ret = getcwd(buf, size));
  save_errno = errno;
  switch (passed) {
  case -1:
  case 0:
    printf("\t\tret = %s\n", (((ret) == NULL) ? "(null)" : (ret)));
    printf("\tSucceeded Phase 1\n");
    fflush(NULL);
    break;
  case -2:
    printf("\tFailed Phase 1: returned undefined value! "
           "ret = %p\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):
<getcwd_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xea1(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x28(%rbp)
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <getcwd@plt>
mov    %rax,%rdi
mov    %rdi,-0x18(%rbp)
call   1050 <handle_ptr@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0xfffffffe,%eax
je     1214 <getcwd_test+0xd4>
jmp    11af <getcwd_test+0x6f>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     1234 <getcwd_test+0xf4>
jmp    11c3 <getcwd_test+0x83>
cmpq   $0x0,-0x18(%rbp)
jne    11de <getcwd_test+0x9e>
lea    0xe55(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11e6 <getcwd_test+0xa6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    124e <getcwd_test+0x10e>
mov    -0x18(%rbp),%rsi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
jmp    124e <getcwd_test+0x10e>
mov    -0x20(%rbp),%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2d88(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd_test>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe9f(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <getcwd@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <handle_ptr@plt>
mov    0x2e4e(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11c7 <getcwd_test+0x77>
test   %rbx,%rbx
lea    0xe83(%rip),%rsi
cmovne %rbx,%rsi
lea    0xe6c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed1(%rip),%rdi
call   1030 <puts@plt>
jmp    11ef <getcwd_test+0x9f>
cmp    $0xfffffffe,%eax
jne    11df <getcwd_test+0x8f>
lea    0xe5e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11ef <getcwd_test+0x9f>
lea    0xe80(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <fflush@plt>
mov    %ebp,(%r14)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"getcwd(): buf=0x%p, size=%zu\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"\09\09ret = %s\0A\00"
@.str.2 = [7 x i8] c"(null)\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 = %p\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i8* @getcwd_test(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = load i8*, i8** %3
%9 = load i64, i64* %4
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %8, i64 %9)
%11 = call i32 @fflush(i32* null)
%12 = load i8*, i8** %3
%13 = load i64, i64* %4
%14 = call i8* @getcwd(i8* %12, i64 %13)
store i8* %14, i8** %5
%15 = call i32 @handle_ptr(i8* %14)
store i32 %15, i32* %7
%16 = load i32, i32* @errno
store i32 %16, i32* %6
%17 = load i32, i32* %7
switch i32 %17, label %33 [
i32 -1, label %18
i32 0, label %18
i32 -2, label %29
]
18: ; preds = %2, %2
%19 = load i8*, i8** %5
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %18
br label %24
22: ; preds = %18
%23 = load i8*, i8** %5
br label %24
24: ; preds = %22, %21
%25 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %21 ], [ %23, %22 ]
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @fflush(i32* null)
br label %37
29: ; preds = %2
%30 = load i8*, i8** %5
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%32 = call i32 @fflush(i32* null)
br label %37
33: ; preds = %2
%34 = load i32, i32* %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %34)
%36 = call i32 @fflush(i32* null)
br label %37
37: ; preds = %33, %29, %24
%38 = load i32, i32* %6
store i32 %38, i32* @errno
%39 = load i8*, i8** %5
ret i8* %39
}

LLVM IR (-O2):
@.str = [30 x i8] c"getcwd(): buf=0x%p, size=%zu\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"\09\09ret = %s\0A\00"
@.str.2 = [7 x i8] c"(null)\00"
@.str.4 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %p\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 i8* @getcwd_test(i8* %0, i64 %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %1)
%4 = tail call i32 @fflush(i32* null)
%5 = tail call i8* @getcwd(i8* %0, i64 %1)
%6 = tail call i32 @handle_ptr(i8* %5)
%7 = load i32, i32* @errno
switch i32 %6, label %15 [
i32 -1, label %8
i32 0, label %8
i32 -2, label %13
]
8: ; preds = %2, %2
%9 = icmp eq i8* %5, null
%10 = select i1 %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %5
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %17
13: ; preds = %2
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i8* %5)
br label %17
15: ; preds = %2
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %6)
br label %17
17: ; preds = %15, %13, %8
%18 = tail call i32 @fflush(i32* null)
store i32 %7, i32* @errno
ret i8* %5
}