#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;
int FILENAME_MAX;
scalar_t__ errno;
char *getcwd(char *, int);
char *malloc(int);
char *realloc(char *, int);
char *get_pwd(void) {
  char *buf, *ret;
  int size = FILENAME_MAX;
  buf = malloc(size);
  while ((ret = getcwd(buf, size)) == NULL && errno == ERANGE) {
    size *= 2;
    buf = realloc(buf, size);
  }
  return buf;
}


x86 assembly (clang, -O0):
<get_pwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <getcwd@plt>
mov    %rax,%rcx
mov    %rcx,-0x10(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x15(%rbp)
jne    118c <get_pwd+0x5c>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    119c <get_pwd+0x6c>
jmp    11ba <get_pwd+0x8a>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
jmp    1150 <get_pwd+0x20>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pwd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2ea2(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <getcwd@plt>
test   %rax,%rax
jne    1196 <get_pwd+0x66>
mov    0x2e70(%rip),%r14
mov    0x2e81(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    %rax,(%r14)
jne    1196 <get_pwd+0x66>
add    %ebp,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <getcwd@plt>
test   %rax,%rax
je     1170 <get_pwd+0x40>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
define i8* @get_pwd() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = load i32, i32* @FILENAME_MAX
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = call i8* @malloc(i32 %5)
store i8* %6, i8** %1
br label %7
7: ; preds = %18, %0
%8 = load i8*, i8** %1
%9 = load i32, i32* %3
%10 = call i8* @getcwd(i8* %8, i32 %9)
store i8* %10, i8** %2
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = load i64, i64* @ERANGE
%15 = icmp eq i64 %13, %14
br label %16
16: ; preds = %12, %7
%17 = phi i1 [ false, %7 ], [ %15, %12 ]
br i1 %17, label %18, label %24
18: ; preds = %16
%19 = load i32, i32* %3
%20 = mul nsw i32 %19, 2
store i32 %20, i32* %3
%21 = load i8*, i8** %1
%22 = load i32, i32* %3
%23 = call i8* @realloc(i8* %21, i32 %22)
store i8* %23, i8** %1
br label %7
24: ; preds = %16
%25 = load i8*, i8** %1
ret i8* %25
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
define i8* @get_pwd() {
%1 = load i32, i32* @FILENAME_MAX
%2 = tail call i8* @malloc(i32 %1)
%3 = tail call i8* @getcwd(i8* %2, i32 %1)
%4 = icmp eq i8* %3, null
%5 = load i64, i64* @errno
%6 = load i64, i64* @ERANGE
%7 = icmp eq i64 %5, %6
%8 = select i1 %4, i1 %7, i1 false
br i1 %8, label %9, label %20
9: ; preds = %0, %9
%10 = phi i32 [ %12, %9 ], [ %1, %0 ]
%11 = phi i8* [ %13, %9 ], [ %2, %0 ]
%12 = shl nsw i32 %10, 1
%13 = tail call i8* @realloc(i8* %11, i32 %12)
%14 = tail call i8* @getcwd(i8* %13, i32 %12)
%15 = icmp eq i8* %14, null
%16 = load i64, i64* @errno
%17 = load i64, i64* @ERANGE
%18 = icmp eq i64 %16, %17
%19 = select i1 %15, i1 %18, i1 false
br i1 %19, label %9, label %20
20: ; preds = %9, %0
%21 = phi i8* [ %2, %0 ], [ %13, %9 ]
ret i8* %21
}