#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 */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int readlink(char *, char *, size_t);
char *getcwd(char *buf, size_t size) {
  int need_free = 0;
  if (size == 0 && buf != NULL) {
    errno = EINVAL;
    goto err;
  }
  if (buf == NULL) {
    if (size == 0) {
      size = 1024;
    }
    buf = malloc(size);
    need_free = 1;
  }
  ssize_t s = readlink("/proc/self/cwd", buf, size);
  if (s == -1) {
    goto err;
  }
  if (size && size <= (size_t)s) {
    errno = ERANGE;
    goto err;
  }
  buf[s] = '\0';
  return buf;
err:
  if (need_free) {
    free(buf);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1174 <getcwd+0x44>
cmpq   $0x0,-0x10(%rbp)
je     1174 <getcwd+0x44>
mov    0x2e6c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6b(%rip),%rax
mov    %ecx,(%rax)
jmp    1218 <getcwd+0xe8>
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <getcwd+0x76>
cmpq   $0x0,-0x18(%rbp)
jne    1192 <getcwd+0x62>
movq   $0x400,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe4b(%rip),%rdi
call   1040 <readlink@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11cc <getcwd+0x9c>
jmp    1218 <getcwd+0xe8>
cmpq   $0x0,-0x18(%rbp)
je     11ff <getcwd+0xcf>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
cmp    %rcx,%rax
ja     11ff <getcwd+0xcf>
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
mov    0x2de0(%rip),%rax
mov    %ecx,(%rax)
jmp    1218 <getcwd+0xe8>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1233 <getcwd+0x103>
cmpl   $0x0,-0x1c(%rbp)
je     122b <getcwd+0xfb>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r15
test   %rdi,%rdi
je     1159 <getcwd+0x29>
test   %rbx,%rbx
jne    1159 <getcwd+0x29>
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    0x2e83(%rip),%rcx
mov    %eax,(%rcx)
jmp    11bb <getcwd+0x8b>
test   %r15,%r15
je     1163 <getcwd+0x33>
mov    %r15,%r14
jmp    117a <getcwd+0x4a>
test   %rbx,%rbx
mov    $0x400,%eax
cmove  %rax,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xe7f(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11ae <getcwd+0x7e>
cltq
add    $0xffffffffffffffff,%rbx
cmp    %rax,%rbx
jae    11c7 <getcwd+0x97>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    0x2e2c(%rip),%rcx
mov    %eax,(%rcx)
test   %r15,%r15
jne    11bb <getcwd+0x8b>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
movb   $0x0,(%r14,%rax,1)
jmp    11be <getcwd+0x8e>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [15 x i8] c"/proc/self/cwd\00"
@ERANGE = global i32 0
define i8* @getcwd(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 0, i32* %6
%8 = load i64, i64* %5
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
br label %48
15: ; preds = %10, %2
%16 = load i8*, i8** %4
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i64, i64* %5
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i64 1024, i64* %5
br label %22
22: ; preds = %21, %18
%23 = load i64, i64* %5
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %4
store i32 1, i32* %6
br label %25
25: ; preds = %22, %15
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %26, i64 %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %25
br label %48
32: ; preds = %25
%33 = load i64, i64* %5
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %32
%36 = load i64, i64* %5
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = icmp ule i64 %36, %38
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = load i32, i32* @ERANGE
store i32 %41, i32* @errno
br label %48
42: ; preds = %35, %32
%43 = load i8*, i8** %4
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
%47 = load i8*, i8** %4
store i8* %47, i8** %3
br label %55
48: ; preds = %40, %31, %13
%49 = load i32, i32* %6
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i8*, i8** %4
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %51, %48
store i8* null, i8** %3
br label %55
55: ; preds = %54, %42
%56 = load i8*, i8** %3
ret i8* %56
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [15 x i8] c"/proc/self/cwd\00"
@ERANGE = global i32 0
define i8* @getcwd(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
%4 = icmp ne i8* %0, null
%5 = and i1 %4, %3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i32, i32* @EINVAL
store i32 %7, i32* @errno
br label %29
8: ; preds = %2
%9 = icmp eq i8* %0, null
br i1 %9, label %10, label %13
10: ; preds = %8
%11 = select i1 %3, i64 1024, i64 %1
%12 = tail call i8* @malloc(i64 %11)
br label %13
13: ; preds = %10, %8
%14 = phi i8* [ %12, %10 ], [ %0, %8 ]
%15 = phi i64 [ %11, %10 ], [ %1, %8 ]
%16 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %14, i64 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %26, label %18
18: ; preds = %13
%19 = sext i32 %16 to i64
%20 = add i64 %15, -1
%21 = icmp ult i64 %20, %19
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = load i32, i32* @ERANGE
store i32 %23, i32* @errno
br label %26
24: ; preds = %18
%25 = getelementptr inbounds i8, i8* %14, i64 %19
store i8 0, i8* %25
br label %29
26: ; preds = %13, %22
br i1 %9, label %27, label %29
27: ; preds = %26
%28 = tail call i32 @free(i8* %14)
br label %29
29: ; preds = %6, %26, %27, %24
%30 = phi i8* [ %14, %24 ], [ null, %27 ], [ null, %26 ], [ null, %6 ]
ret i8* %30
}