#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 */
scalar_t__ ERANGE;
size_t PATH_MAX;
int /*<<< orphan*/ cctools_fatal(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ *getcwd(char *, size_t);
int /*<<< orphan*/ strerror(scalar_t__);
char *xxrealloc(char *, size_t);
char *path_getcwd(void) {
  char *result = NULL;
  size_t size = PATH_MAX;
  result = xxrealloc(result, size);
  while (getcwd(result, size) == NULL) {
    if (errno == ERANGE) {
      size *= 2;
      result = xxrealloc(result, size);
    } else {
      cctools_fatal("couldn't getcwd: %s", strerror(errno));
      return NULL;
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<path_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <xxrealloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
jne    11f1 <path_getcwd+0xb1>
mov    0x2e43(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
jne    11c2 <path_getcwd+0x82>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <xxrealloc@plt>
mov    %rax,-0x10(%rbp)
jmp    11ec <path_getcwd+0xac>
mov    0x2e07(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %eax,%esi
lea    0xe26(%rip),%rdi
call   1050 <cctools_fatal@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <path_getcwd+0xb9>
jmp    116f <path_getcwd+0x2f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_getcwd>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2ea1(%rip),%rax
mov    (%rax),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1030 <xxrealloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    11c2 <path_getcwd+0x82>
mov    0x2e5a(%rip),%r15
mov    0x2e63(%rip),%r12
nopl   (%rax)
mov    (%r15),%rdi
cmp    (%r12),%rdi
jne    11ac <path_getcwd+0x6c>
add    %rbx,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <xxrealloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
test   %rax,%rax
je     1180 <path_getcwd+0x40>
jmp    11c2 <path_getcwd+0x82>
call   1060 <strerror@plt>
lea    0xe48(%rip),%rdi
mov    %eax,%esi
call   1050 <cctools_fatal@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PATH_MAX = global i64 0
@errno = global i64 0
@ERANGE = global i64 0
@.str = [20 x i8] c"couldn't getcwd: %s\00"
define i8* @path_getcwd() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
store i8* null, i8** %2
%4 = load i64, i64* @PATH_MAX
store i64 %4, i64* %3
%5 = load i8*, i8** %2
%6 = load i64, i64* %3
%7 = call i8* @xxrealloc(i8* %5, i64 %6)
store i8* %7, i8** %2
br label %8
8: ; preds = %27, %0
%9 = load i8*, i8** %2
%10 = load i64, i64* %3
%11 = call i32* @getcwd(i8* %9, i64 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %28
13: ; preds = %8
%14 = load i64, i64* @errno
%15 = load i64, i64* @ERANGE
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i64, i64* %3
%19 = mul i64 %18, 2
store i64 %19, i64* %3
%20 = load i8*, i8** %2
%21 = load i64, i64* %3
%22 = call i8* @xxrealloc(i8* %20, i64 %21)
store i8* %22, i8** %2
br label %27
23: ; preds = %13
%24 = load i64, i64* @errno
%25 = call i32 @strerror(i64 %24)
%26 = call i32 @cctools_fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %25)
store i8* null, i8** %1
br label %30
27: ; preds = %17
br label %8
28: ; preds = %8
%29 = load i8*, i8** %2
store i8* %29, i8** %1
br label %30
30: ; preds = %28, %23
%31 = load i8*, i8** %1
ret i8* %31
}

LLVM IR (-O2):
@PATH_MAX = global i64 0
@errno = global i64 0
@ERANGE = global i64 0
@.str = [20 x i8] c"couldn't getcwd: %s\00"
define i8* @path_getcwd() {
%1 = load i64, i64* @PATH_MAX
%2 = tail call i8* @xxrealloc(i8* null, i64 %1)
%3 = tail call i32* @getcwd(i8* %2, i64 %1)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %19
5: ; preds = %0, %11
%6 = phi i64 [ %12, %11 ], [ %1, %0 ]
%7 = phi i8* [ %13, %11 ], [ %2, %0 ]
%8 = load i64, i64* @errno
%9 = load i64, i64* @ERANGE
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %16
11: ; preds = %5
%12 = shl i64 %6, 1
%13 = tail call i8* @xxrealloc(i8* %7, i64 %12)
%14 = tail call i32* @getcwd(i8* %13, i64 %12)
%15 = icmp eq i32* %14, null
br i1 %15, label %5, label %19
16: ; preds = %5
%17 = tail call i32 @strerror(i64 %8)
%18 = tail call i32 @cctools_fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %11, %0, %16
%20 = phi i8* [ null, %16 ], [ %2, %0 ], [ %13, %11 ]
ret i8* %20
}