#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 /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOENT;
int PATH_MAX;
int /*<<< orphan*/ errno;
char *strdup(char *);
long syscall(int, char *, size_t);
char *getcwd(char *buf, size_t size) {
  char tmp[buf ? 1 : PATH_MAX];
  if (!buf) {
    buf = tmp;
    size = sizeof tmp;
  } else if (!size) {
    errno = EINVAL;
    return 0;
  }
  long ret = syscall(183, buf, size);
  if (ret < 0)
    return 0;
  if (ret == 0 || buf[0] != '/') {
    errno = ENOENT;
    return 0;
  }
  return buf == tmp ? strdup(buf) : buf;
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1148 <getcwd+0x28>
mov    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1154 <getcwd+0x34>
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a0 <getcwd+0x80>
mov    -0x50(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,-0x18(%rbp)
jmp    11d6 <getcwd+0xb6>
cmpq   $0x0,-0x18(%rbp)
jne    11d1 <getcwd+0xb1>
mov    0x2e1e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1d(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1283 <getcwd+0x163>
jmp    11d6 <getcwd+0xb6>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0xb7,%edi
call   1030 <syscall@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    120b <getcwd+0xeb>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1283 <getcwd+0x163>
cmpq   $0x0,-0x38(%rbp)
je     1226 <getcwd+0x106>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     124c <getcwd+0x12c>
mov    0x2d93(%rip),%rax
mov    (%rax),%ecx
mov    0x2da2(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1283 <getcwd+0x163>
mov    -0x48(%rbp),%rax
cmp    %rax,-0x10(%rbp)
jne    126c <getcwd+0x14c>
mov    -0x10(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x58(%rbp)
jmp    1274 <getcwd+0x154>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
test   %rdi,%rdi
mov    0x2ebf(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%eax
cmove  %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%r14
test   %rdi,%rdi
je     1160 <getcwd+0x40>
mov    %rsi,%rdx
mov    %rdi,%r14
test   %rsi,%rsi
je     1195 <getcwd+0x75>
mov    $0xb7,%edi
mov    %r14,%rsi
call   1030 <syscall@plt>
test   %rax,%rax
js     11a7 <getcwd+0x87>
je     118c <getcwd+0x6c>
cmpb   $0x2f,(%r14)
jne    118c <getcwd+0x6c>
cmp    %rbx,%r14
jne    11aa <getcwd+0x8a>
mov    %rbx,%rdi
call   1040 <strdup@plt>
mov    %rax,%r14
jmp    11aa <getcwd+0x8a>
mov    0x2e2d(%rip),%rax
jmp    119c <getcwd+0x7c>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    0x2e33(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
mov    %r14,%rax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
define i8* @getcwd(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
br label %15
13: ; preds = %2
%14 = load i32, i32* @PATH_MAX
br label %15
15: ; preds = %13, %12
%16 = phi i32 [ 1, %12 ], [ %14, %13 ]
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %6
%19 = alloca i8, i64 %17
store i64 %17, i64* %7
%20 = load i8*, i8** %4
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %15
store i8* %19, i8** %4
store i64 %17, i64* %5
br label %29
23: ; preds = %15
%24 = load i64, i64* %5
%25 = icmp ne i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = load i32, i32* @EINVAL
store i32 %27, i32* @errno
store i8* null, i8** %3
store i32 1, i32* %8
br label %57
28: ; preds = %23
br label %29
29: ; preds = %28, %22
%30 = load i8*, i8** %4
%31 = load i64, i64* %5
%32 = call i64 @syscall(i32 183, i8* %30, i64 %31)
store i64 %32, i64* %9
%33 = load i64, i64* %9
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
store i8* null, i8** %3
store i32 1, i32* %8
br label %57
36: ; preds = %29
%37 = load i64, i64* %9
%38 = icmp eq i64 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %36
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 47
br i1 %44, label %45, label %47
45: ; preds = %39, %36
%46 = load i32, i32* @ENOENT
store i32 %46, i32* @errno
store i8* null, i8** %3
store i32 1, i32* %8
br label %57
47: ; preds = %39
%48 = load i8*, i8** %4
%49 = icmp eq i8* %48, %19
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i8*, i8** %4
%52 = call i8* @strdup(i8* %51)
br label %55
53: ; preds = %47
%54 = load i8*, i8** %4
br label %55
55: ; preds = %53, %50
%56 = phi i8* [ %52, %50 ], [ %54, %53 ]
store i8* %56, i8** %3
store i32 1, i32* %8
br label %57
57: ; preds = %55, %45, %35, %26
%58 = load i8*, i8** %6
%59 = load i8*, i8** %3
ret i8* %59
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
define i8* @getcwd(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = select i1 %3, i64 %5, i64 1
%7 = alloca i8, i64 %6
br i1 %3, label %12, label %8
8: ; preds = %2
%9 = icmp eq i64 %1, 0
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %28
12: ; preds = %2, %8
%13 = phi i64 [ %1, %8 ], [ %5, %2 ]
%14 = phi i8* [ %0, %8 ], [ %7, %2 ]
%15 = call i64 @syscall(i32 183, i8* %14, i64 %13)
%16 = icmp slt i64 %15, 0
br i1 %16, label %28, label %17
17: ; preds = %12
%18 = icmp eq i64 %15, 0
br i1 %18, label %22, label %19
19: ; preds = %17
%20 = load i8, i8* %14
%21 = icmp eq i8 %20, 47
br i1 %21, label %24, label %22
22: ; preds = %19, %17
%23 = load i32, i32* @ENOENT
store i32 %23, i32* @errno
br label %28
24: ; preds = %19
%25 = icmp eq i8* %14, %7
br i1 %25, label %26, label %28
26: ; preds = %24
%27 = call i8* @strdup(i8* %7)
br label %28
28: ; preds = %22, %12, %24, %26, %10
%29 = phi i8* [ null, %10 ], [ null, %22 ], [ null, %12 ], [ %27, %26 ], [ %14, %24 ]
ret i8* %29
}