#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*/ EXIT_FAILURE;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *getcwd(char *, size_t);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, size_t);
char *dirs_get_cwd() {
  size_t bufsize = 32;
  char *buf = malloc(bufsize * sizeof(char));
  while (getcwd(buf, bufsize) == NULL) {
    int errsv = errno;
    if (errsv == ERANGE) {
      bufsize *= 2;
      buf = realloc(buf, bufsize * sizeof(char));
    } else {
      perror("getcwd");
      exit(EXIT_FAILURE);
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<dirs_get_cwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x20,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
shl    $0x0,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <getcwd@plt>
cmp    $0x0,%rax
jne    11eb <dirs_get_cwd+0x9b>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%eax
jne    11cc <dirs_get_cwd+0x7c>
mov    -0x8(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
shl    $0x0,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
jmp    11e6 <dirs_get_cwd+0x96>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1171 <dirs_get_cwd+0x21>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirs_get_cwd>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x20,%ebx
mov    $0x20,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    $0x20,%esi
mov    %rax,%rdi
call   1030 <getcwd@plt>
test   %rax,%rax
jne    11ba <dirs_get_cwd+0x6a>
mov    0x2e55(%rip),%r15
mov    0x2e5e(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    (%r15),%eax
cmp    (%r12),%eax
jne    11c9 <dirs_get_cwd+0x79>
add    %rbx,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <getcwd@plt>
test   %rax,%rax
je     1190 <dirs_get_cwd+0x40>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe30(%rip),%rdi
call   1060 <perror@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@errno = global i32 0
@ERANGE = global i32 0
@.str = [7 x i8] c"getcwd\00"
@EXIT_FAILURE = global i32 0
define i8* @dirs_get_cwd() {
%1 = tail call dereferenceable_or_null(32) i8* @malloc(i64 32)
%2 = tail call i32* @getcwd(i8* %1, i64 32)
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %19
4: ; preds = %0, %10
%5 = phi i64 [ %11, %10 ], [ 32, %0 ]
%6 = phi i8* [ %12, %10 ], [ %1, %0 ]
%7 = load i32, i32* @errno
%8 = load i32, i32* @ERANGE
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %15
10: ; preds = %4
%11 = shl i64 %5, 1
%12 = tail call i8* @realloc(i8* %6, i64 %11)
%13 = tail call i32* @getcwd(i8* %12, i64 %11)
%14 = icmp eq i32* %13, null
br i1 %14, label %4, label %19
15: ; preds = %4
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %10, %0
%20 = phi i8* [ %1, %0 ], [ %12, %10 ]
ret i8* %20
}