#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 ERANGE;
int /*<<< orphan*/ dieErrno(char *);
int errno;
int /*<<< orphan*/ free(char *);
char *getcwd(char *, size_t);
char *sMalloc(size_t);
char *sRealloc(char *, size_t);
size_t sSizeMul(size_t, int);
char *sGetCwd(void) {
  size_t capacity = 4;
  char *buffer = sMalloc(capacity);
  int old_errno = errno;
  char *result = NULL;
  do {
    errno = 0;
    result = getcwd(buffer, capacity);
    if (result == NULL) {
      if (errno != ERANGE) {
        free(buffer);
        dieErrno("failed to determine current working directory");
      }
      capacity = sSizeMul(capacity, 2);
      buffer = sRealloc(buffer, capacity);
    }
  } while (result == NULL);
  errno = old_errno;
  return buffer;
}


x86 assembly (clang, -O0):
<sGetCwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x4,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <sMalloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e40(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <getcwd@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120a <sGetCwd+0xaa>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%eax
je     11e7 <sGetCwd+0x87>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
lea    0xe1e(%rip),%rdi
call   1070 <dieErrno@plt>
mov    -0x8(%rbp),%rdi
mov    $0x2,%esi
call   1060 <sSizeMul@plt>
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <sRealloc@plt>
mov    %rax,-0x10(%rbp)
jmp    120f <sGetCwd+0xaf>
cmpq   $0x0,-0x20(%rbp)
je     1191 <sGetCwd+0x31>
mov    -0x14(%rbp),%ecx
mov    0x2db4(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sGetCwd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x4,%ebx
mov    $0x4,%edi
call   1030 <sMalloc@plt>
mov    %rax,%r14
mov    0x2e54(%rip),%r12
mov    (%r12),%r13d
movl   $0x0,(%r12)
mov    $0x4,%esi
mov    %rax,%rdi
call   1080 <getcwd@plt>
test   %rax,%rax
je     11b8 <sGetCwd+0x58>
mov    %r13d,(%r12)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e29(%rip),%rbp
lea    0xe3a(%rip),%r15
jmp    1206 <sGetCwd+0xa6>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x2,%esi
call   1060 <sSizeMul@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <sRealloc@plt>
mov    %rax,%r14
movl   $0x0,(%r12)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1080 <getcwd@plt>
test   %rax,%rax
jne    11a2 <sGetCwd+0x42>
mov    (%r12),%eax
cmp    0x0(%rbp),%eax
je     11d0 <sGetCwd+0x70>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
call   1070 <dieErrno@plt>
jmp    11d0 <sGetCwd+0x70>

LLVM IR (-O0):
@errno = global i32 0
@ERANGE = global i32 0
@.str = [46 x i8] c"failed to determine current working directory\00"
define i8* @sGetCwd() {
%1 = alloca i64
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i64 4, i64* %1
%5 = load i64, i64* %1
%6 = call i8* @sMalloc(i64 %5)
store i8* %6, i8** %2
%7 = load i32, i32* @errno
store i32 %7, i32* %3
store i8* null, i8** %4
br label %8
8: ; preds = %29, %0
store i32 0, i32* @errno
%9 = load i8*, i8** %2
%10 = load i64, i64* %1
%11 = call i8* @getcwd(i8* %9, i64 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %28
14: ; preds = %8
%15 = load i32, i32* @errno
%16 = load i32, i32* @ERANGE
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i8*, i8** %2
%20 = call i32 @free(i8* %19)
%21 = call i32 @dieErrno(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %18, %14
%23 = load i64, i64* %1
%24 = call i64 @sSizeMul(i64 %23, i32 2)
store i64 %24, i64* %1
%25 = load i8*, i8** %2
%26 = load i64, i64* %1
%27 = call i8* @sRealloc(i8* %25, i64 %26)
store i8* %27, i8** %2
br label %28
28: ; preds = %22, %8
br label %29
29: ; preds = %28
%30 = load i8*, i8** %4
%31 = icmp eq i8* %30, null
br i1 %31, label %8, label %32
32: ; preds = %29
%33 = load i32, i32* %3
store i32 %33, i32* @errno
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@errno = global i32 0
@ERANGE = global i32 0
@.str = [46 x i8] c"failed to determine current working directory\00"
define i8* @sGetCwd() {
%1 = tail call i8* @sMalloc(i64 4)
%2 = load i32, i32* @errno
store i32 0, i32* @errno
%3 = tail call i8* @getcwd(i8* %1, i64 4)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %19
5: ; preds = %0, %14
%6 = phi i64 [ %15, %14 ], [ 4, %0 ]
%7 = phi i8* [ %16, %14 ], [ %1, %0 ]
%8 = load i32, i32* @errno
%9 = load i32, i32* @ERANGE
%10 = icmp eq i32 %8, %9
br i1 %10, label %14, label %11
11: ; preds = %5
%12 = tail call i32 @free(i8* %7)
%13 = tail call i32 @dieErrno(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %5, %11
%15 = tail call i64 @sSizeMul(i64 %6, i32 2)
%16 = tail call i8* @sRealloc(i8* %7, i64 %15)
store i32 0, i32* @errno
%17 = tail call i8* @getcwd(i8* %16, i64 %15)
%18 = icmp eq i8* %17, null
br i1 %18, label %5, label %19
19: ; preds = %14, %0
%20 = phi i8* [ %1, %0 ], [ %16, %14 ]
store i32 %2, i32* @errno
ret i8* %20
}