#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*/ ERANGE;
char *canonicalize_file_name(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
size_t strlcpy(char *, char *, size_t);
char *getcwd(char *buffer, size_t size) {
  if (buffer && size == 0) {
    errno = EINVAL;
    return NULL;
  }
  char *result = canonicalize_file_name(".");
  if (!result)
    return NULL;
  if (!buffer) {
    return result;
  }
  if (strlcpy(buffer, result, size) >= size) {
    free(result);
    errno = ERANGE;
    return NULL;
  }
  free(result);
  return buffer;
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1175 <getcwd+0x45>
cmpq   $0x0,-0x18(%rbp)
jne    1175 <getcwd+0x45>
mov    0x2e73(%rip),%rax
mov    (%rax),%ecx
mov    0x2e72(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1209 <getcwd+0xd9>
lea    0xe84(%rip),%rdi
call   1040 <canonicalize_file_name@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119d <getcwd+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    1209 <getcwd+0xd9>
cmpq   $0x0,-0x10(%rbp)
jne    11b5 <getcwd+0x85>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1209 <getcwd+0xd9>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <strlcpy@plt>
cmp    -0x18(%rbp),%rax
jb     11f8 <getcwd+0xc8>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%ecx
mov    0x2def(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1209 <getcwd+0xd9>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
je     1151 <getcwd+0x21>
test   %r14,%r14
jne    1151 <getcwd+0x21>
mov    0x2e81(%rip),%rax
jmp    118f <getcwd+0x5f>
lea    0xea8(%rip),%rdi
call   1040 <canonicalize_file_name@plt>
mov    %rax,%r15
test   %rbx,%rbx
je     119e <getcwd+0x6e>
test   %r15,%r15
je     119e <getcwd+0x6e>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <strlcpy@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1030 <free@plt>
cmp    %r14,%r12
jb     11a1 <getcwd+0x71>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    0x2e40(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
jmp    11a1 <getcwd+0x71>
mov    %r15,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
@ERANGE = global i32 0
define i8* @getcwd(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i64, i64* %5
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
store i8* null, i8** %3
br label %39
14: ; preds = %9, %2
%15 = call i8* @canonicalize_file_name(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %14
store i8* null, i8** %3
br label %39
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = icmp ne i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = load i8*, i8** %6
store i8* %23, i8** %3
br label %39
24: ; preds = %19
%25 = load i8*, i8** %4
%26 = load i8*, i8** %6
%27 = load i64, i64* %5
%28 = call i64 @strlcpy(i8* %25, i8* %26, i64 %27)
%29 = load i64, i64* %5
%30 = icmp uge i64 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i8*, i8** %6
%33 = call i32 @free(i8* %32)
%34 = load i32, i32* @ERANGE
store i32 %34, i32* @errno
store i8* null, i8** %3
br label %39
35: ; preds = %24
%36 = load i8*, i8** %6
%37 = call i32 @free(i8* %36)
%38 = load i8*, i8** %4
store i8* %38, i8** %3
br label %39
39: ; preds = %35, %31, %22, %18, %12
%40 = load i8*, i8** %3
ret i8* %40
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
@ERANGE = global i32 0
define i8* @getcwd(i8* %0, i64 %1) {
%3 = icmp ne i8* %0, null
%4 = icmp eq i64 %1, 0
%5 = and i1 %3, %4
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = tail call i8* @canonicalize_file_name(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp ne i8* %7, null
%9 = and i1 %3, %8
br i1 %9, label %10, label %17
10: ; preds = %6
%11 = tail call i64 @strlcpy(i8* %0, i8* %7, i64 %1)
%12 = icmp ult i64 %11, %1
%13 = tail call i32 @free(i8* %7)
br i1 %12, label %17, label %14
14: ; preds = %10, %2
%15 = phi i32* [ @EINVAL, %2 ], [ @ERANGE, %10 ]
%16 = load i32, i32* %15
store i32 %16, i32* @errno
br label %17
17: ; preds = %14, %10, %6
%18 = phi i8* [ %7, %6 ], [ %0, %10 ], [ null, %14 ]
ret i8* %18
}