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


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1182 <getcwd+0x62>
mov    -0x38(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e7a(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11b8 <getcwd+0x98>
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <getcwd+0x93>
mov    0x2e3c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3b(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    121f <getcwd+0xff>
jmp    11b8 <getcwd+0x98>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0xb7,%edi
call   1030 <syscall@plt>
cmp    $0x0,%rax
jge    11e8 <getcwd+0xc8>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    121f <getcwd+0xff>
mov    -0x38(%rbp),%rax
cmp    %rax,-0x10(%rbp)
jne    1208 <getcwd+0xe8>
mov    -0x10(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x40(%rbp)
jmp    1210 <getcwd+0xf0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%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
mov    0x2ec2(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
lea    0xf(%rax),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     1165 <getcwd+0x45>
mov    %rsi,%rdx
mov    %rdi,%r14
test   %rsi,%rsi
jne    116b <getcwd+0x4b>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    0x2e77(%rip),%rcx
mov    %eax,(%rcx)
jmp    118f <getcwd+0x6f>
movslq %eax,%rdx
mov    %rbx,%r14
mov    $0xb7,%edi
mov    %r14,%rsi
call   1030 <syscall@plt>
test   %rax,%rax
js     118f <getcwd+0x6f>
cmp    %rbx,%r14
jne    1192 <getcwd+0x72>
mov    %rbx,%rdi
call   1040 <strdup@plt>
mov    %rax,%r14
jmp    1192 <getcwd+0x72>
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
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
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %2
store i8* %12, i8** %4
%16 = load i32, i32* @PATH_MAX
%17 = sext i32 %16 to i64
store i64 %17, i64* %5
br label %24
18: ; preds = %2
%19 = load i64, i64* %5
%20 = icmp ne i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = load i32, i32* @EINVAL
store i32 %22, i32* @errno
store i8* null, i8** %3
store i32 1, i32* %8
br label %40
23: ; preds = %18
br label %24
24: ; preds = %23, %15
%25 = load i8*, i8** %4
%26 = load i64, i64* %5
%27 = call i64 @syscall(i32 183, i8* %25, i64 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
store i8* null, i8** %3
store i32 1, i32* %8
br label %40
30: ; preds = %24
%31 = load i8*, i8** %4
%32 = icmp eq i8* %31, %12
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i8*, i8** %4
%35 = call i8* @strdup(i8* %34)
br label %38
36: ; preds = %30
%37 = load i8*, i8** %4
br label %38
38: ; preds = %36, %33
%39 = phi i8* [ %35, %33 ], [ %37, %36 ]
store i8* %39, i8** %3
store i32 1, i32* %8
br label %40
40: ; preds = %38, %29, %21
%41 = load i8*, i8** %6
%42 = load i8*, i8** %3
ret i8* %42
}

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