#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;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ getcwd(char *, size_t);
char *malloc(size_t);
char *alloc_getcwd(size_t extra) {
  size_t n = 256;
  while (1) {
    char *buf = malloc(n + extra);
    if (getcwd(buf, n)) {
      return buf;
    }
    free(buf);
    if (errno != ERANGE)
      return NULL;
    n *= 2;
  }
}


x86 assembly (clang, -O0):
<alloc_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x100,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
je     1179 <alloc_getcwd+0x49>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ba <alloc_getcwd+0x8a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
je     11a9 <alloc_getcwd+0x79>
movq   $0x0,-0x8(%rbp)
jmp    11ba <alloc_getcwd+0x8a>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1144 <alloc_getcwd+0x14>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_getcwd>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
add    $0x100,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    $0x100,%ebx
mov    $0x100,%esi
mov    %rax,%rdi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    11a6 <alloc_getcwd+0x76>
mov    0x2e6f(%rip),%r12
mov    0x2e78(%rip),%r13
mov    %r15,%rdi
call   1030 <free@plt>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    11a3 <alloc_getcwd+0x73>
add    %rbx,%rbx
lea    (%rbx,%r14,1),%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
test   %rax,%rax
je     1170 <alloc_getcwd+0x40>
jmp    11a6 <alloc_getcwd+0x76>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
define i8* @alloc_getcwd(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
store i64 %0, i64* %3
store i64 256, i64* %4
br label %6
6: ; preds = %1, %24
%7 = load i64, i64* %4
%8 = load i64, i64* %3
%9 = add i64 %7, %8
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = load i64, i64* %4
%13 = call i64 @getcwd(i8* %11, i64 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %6
%16 = load i8*, i8** %5
store i8* %16, i8** %2
br label %27
17: ; preds = %6
%18 = load i8*, i8** %5
%19 = call i32 @free(i8* %18)
%20 = load i64, i64* @errno
%21 = load i64, i64* @ERANGE
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %17
store i8* null, i8** %2
br label %27
24: ; preds = %17
%25 = load i64, i64* %4
%26 = mul i64 %25, 2
store i64 %26, i64* %4
br label %6
27: ; preds = %23, %15
%28 = load i8*, i8** %2
ret i8* %28
}

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