#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*/ ENOMEM;
size_t PATH_MAX;
char *__getcwd_without_malloc(char *, size_t);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
scalar_t__ realloc(char *, scalar_t__);
scalar_t__ strlen(char *);
char *getcwd(char *buffer, size_t len) {
  int allocated = 0;
  int do_realloc = 0;
  if (buffer == NULL) {
    if (len == 0) {
      len = PATH_MAX;
      do_realloc = 1;
    }
    buffer = (char *)malloc(len);
    if (buffer == NULL) {
      errno = ENOMEM;
      return NULL;
    }
    allocated = 1;
  } else if (len == 0) {
    errno = EINVAL;
    return NULL;
  }
  char *rtn = __getcwd_without_malloc(buffer, len);
  if (allocated) {
    if (rtn == NULL) {
      free(buffer);
    } else if (do_realloc) {
      rtn = (char *)realloc(rtn, strlen(rtn) + 1);
    }
  }
  return rtn;
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11dc <getcwd+0x8c>
cmpq   $0x0,-0x18(%rbp)
jne    1199 <getcwd+0x49>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d0 <getcwd+0x80>
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0f(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1280 <getcwd+0x130>
movl   $0x1,-0x1c(%rbp)
jmp    120b <getcwd+0xbb>
cmpq   $0x0,-0x18(%rbp)
jne    1206 <getcwd+0xb6>
mov    0x2dda(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd9(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1280 <getcwd+0x130>
jmp    120b <getcwd+0xbb>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <__getcwd_without_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1278 <getcwd+0x128>
cmpq   $0x0,-0x28(%rbp)
jne    123f <getcwd+0xef>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
jmp    1273 <getcwd+0x123>
cmpl   $0x0,-0x20(%rbp)
je     126e <getcwd+0x11e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x28(%rbp)
jmp    1273 <getcwd+0x123>
jmp    1278 <getcwd+0x128>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
test   %rdi,%rdi
je     116f <getcwd+0x1f>
test   %rbx,%rbx
je     11c2 <getcwd+0x72>
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <__getcwd_without_malloc@plt>
mov    %rbx,%r14
test   %rbx,%rbx
jne    1181 <getcwd+0x31>
mov    0x2e72(%rip),%rax
mov    (%rax),%r14
mov    %r14,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11cb <getcwd+0x7b>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <__getcwd_without_malloc@plt>
test   %rax,%rax
je     11e9 <getcwd+0x99>
mov    %rax,%r14
test   %rbx,%rbx
jne    11e0 <getcwd+0x90>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %r14,%rdi
call   1070 <realloc@plt>
mov    %rax,%r14
jmp    11e0 <getcwd+0x90>
mov    0x2dff(%rip),%rax
jmp    11d2 <getcwd+0x82>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    0x2df5(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
call   1030 <free@plt>
jmp    11dd <getcwd+0x8d>

LLVM IR (-O0):
@PATH_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
@EINVAL = 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
%8 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 0, i32* %6
store i32 0, i32* %7
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %25
11: ; preds = %2
%12 = load i64, i64* %5
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = load i64, i64* @PATH_MAX
store i64 %15, i64* %5
store i32 1, i32* %7
br label %16
16: ; preds = %14, %11
%17 = load i64, i64* %5
%18 = call i64 @malloc(i64 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = load i32, i32* @ENOMEM
store i32 %23, i32* @errno
store i8* null, i8** %3
br label %57
24: ; preds = %16
store i32 1, i32* %6
br label %31
25: ; preds = %2
%26 = load i64, i64* %5
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = load i32, i32* @EINVAL
store i32 %29, i32* @errno
store i8* null, i8** %3
br label %57
30: ; preds = %25
br label %31
31: ; preds = %30, %24
%32 = load i8*, i8** %4
%33 = load i64, i64* %5
%34 = call i8* @__getcwd_without_malloc(i8* %32, i64 %33)
store i8* %34, i8** %8
%35 = load i32, i32* %6
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %55
37: ; preds = %31
%38 = load i8*, i8** %8
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i8*, i8** %4
%42 = call i32 @free(i8* %41)
br label %54
43: ; preds = %37
%44 = load i32, i32* %7
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %43
%47 = load i8*, i8** %8
%48 = load i8*, i8** %8
%49 = call i64 @strlen(i8* %48)
%50 = add nsw i64 %49, 1
%51 = call i64 @realloc(i8* %47, i64 %50)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %8
br label %53
53: ; preds = %46, %43
br label %54
54: ; preds = %53, %40
br label %55
55: ; preds = %54, %31
%56 = load i8*, i8** %8
store i8* %56, i8** %3
br label %57
57: ; preds = %55, %28, %22
%58 = load i8*, i8** %3
ret i8* %58
}

LLVM IR (-O2):
@PATH_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
@EINVAL = global i32 0
define i8* @getcwd(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i64 %1, 0
br i1 %3, label %5, label %12
5: ; preds = %2
%6 = load i64, i64* @PATH_MAX
%7 = select i1 %4, i64 %6, i64 %1
%8 = tail call i64 @malloc(i64 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %5
%11 = load i32, i32* @ENOMEM
store i32 %11, i32* @errno
br label %29
12: ; preds = %2
br i1 %4, label %13, label %15
13: ; preds = %12
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
br label %29
15: ; preds = %12
%16 = tail call i8* @__getcwd_without_malloc(i8* %0, i64 %1)
br label %29
17: ; preds = %5
%18 = inttoptr i64 %8 to i8*
%19 = tail call i8* @__getcwd_without_malloc(i8* %18, i64 %7)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @free(i8* %18)
br label %29
23: ; preds = %17
br i1 %4, label %24, label %29
24: ; preds = %23
%25 = tail call i64 @strlen(i8* %19)
%26 = add nsw i64 %25, 1
%27 = tail call i64 @realloc(i8* %19, i64 %26)
%28 = inttoptr i64 %27 to i8*
br label %29
29: ; preds = %15, %23, %24, %21, %13, %10
%30 = phi i8* [ null, %10 ], [ null, %13 ], [ null, %21 ], [ %28, %24 ], [ %19, %23 ], [ %16, %15 ]
ret i8* %30
}