#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 PATH_MAX;
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;
  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    117d <getcwd+0x5d>
mov    -0x38(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e7a(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0xb7,%edi
call   1030 <syscall@plt>
cmp    $0x0,%rax
jge    11ad <getcwd+0x8d>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11e4 <getcwd+0xc4>
mov    -0x38(%rbp),%rax
cmp    %rax,-0x10(%rbp)
jne    11cd <getcwd+0xad>
mov    -0x10(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x40(%rbp)
jmp    11d5 <getcwd+0xb5>
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    %rdi,%r14
mov    0x2ebf(%rip),%rax
movslq (%rax),%rdx
mov    %edx,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
cmovne %rsi,%rdx
cmove  %rbx,%r14
mov    $0xb7,%edi
mov    %r14,%rsi
call   1030 <syscall@plt>
test   %rax,%rax
js     1176 <getcwd+0x56>
cmp    %rbx,%r14
jne    1179 <getcwd+0x59>
mov    %rbx,%rdi
call   1040 <strdup@plt>
mov    %rax,%r14
jmp    1179 <getcwd+0x59>
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
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 %18
18: ; preds = %15, %2
%19 = load i8*, i8** %4
%20 = load i64, i64* %5
%21 = call i64 @syscall(i32 183, i8* %19, i64 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i8* null, i8** %3
store i32 1, i32* %8
br label %34
24: ; preds = %18
%25 = load i8*, i8** %4
%26 = icmp eq i8* %25, %12
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %4
%29 = call i8* @strdup(i8* %28)
br label %32
30: ; preds = %24
%31 = load i8*, i8** %4
br label %32
32: ; preds = %30, %27
%33 = phi i8* [ %29, %27 ], [ %31, %30 ]
store i8* %33, i8** %3
store i32 1, i32* %8
br label %34
34: ; preds = %32, %23
%35 = load i8*, i8** %6
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
@PATH_MAX = 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
%7 = sext i32 %3 to i64
%8 = select i1 %6, i64 %7, i64 %1
%9 = select i1 %6, i8* %5, i8* %0
%10 = call i64 @syscall(i32 183, i8* %9, i64 %8)
%11 = icmp slt i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = icmp eq i8* %9, %5
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = call i8* @strdup(i8* %5)
br label %16
16: ; preds = %14, %12, %2
%17 = phi i8* [ null, %2 ], [ %15, %14 ], [ %9, %12 ]
ret i8* %17
}