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


x86 assembly (clang, -O0):
<gnu_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x64,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1040 <getcwd@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1170 <gnu_getcwd+0x40>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
jmp    114b <gnu_getcwd+0x1b>

x86 assembly (clang, -O2):
<gnu_getcwd>:
push   %r14
push   %rbx
push   %rax
mov    $0x64,%ebx
mov    $0x64,%edi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x64,%esi
xchg   %ax,%ax
call   1040 <getcwd@plt>
test   %rax,%rax
jne    1175 <gnu_getcwd+0x45>
add    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
jmp    1150 <gnu_getcwd+0x20>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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