#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 *);
char *getcwd(char *, size_t);
scalar_t__ xmalloc(size_t);
char *gnu_getcwd() {
  size_t size = 100;
  while (1) {
    char *buffer = (char *)xmalloc(size);
    if (getcwd(buffer, size) == buffer) {
      return buffer;
    }
    free(buffer);
    if (errno != ERANGE) {
      return 0;
    }
    size *= 2;
  }
}


x86 assembly (clang, -O0):
<gnu_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x64,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <getcwd@plt>
cmp    -0x18(%rbp),%rax
jne    1171 <gnu_getcwd+0x41>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b2 <gnu_getcwd+0x82>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
je     11a1 <gnu_getcwd+0x71>
movq   $0x0,-0x8(%rbp)
jmp    11b2 <gnu_getcwd+0x82>
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1140 <gnu_getcwd+0x10>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gnu_getcwd>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x64,%ebx
mov    $0x64,%edi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    $0x64,%esi
mov    %rax,%rdi
call   1040 <getcwd@plt>
cmp    %r14,%rax
je     11a4 <gnu_getcwd+0x74>
mov    0x2e75(%rip),%r15
mov    0x2e7e(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <free@plt>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    11a1 <gnu_getcwd+0x71>
add    %rbx,%rbx
mov    %rbx,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
cmp    %r14,%rax
jne    1170 <gnu_getcwd+0x40>
jmp    11a4 <gnu_getcwd+0x74>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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