#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 *);
int /*<<< orphan*/ *getcwd(char *, size_t);
scalar_t__ malloc(size_t);
scalar_t__ realloc(char *, size_t);
char *iupUnixGetCurrentDirectory(void) {
  size_t size = 256;
  char *buffer = (char *)malloc(size);
  for (;;) {
    if (getcwd(buffer, size) != NULL)
      return buffer;
    if (errno != ERANGE) {
      free(buffer);
      return NULL;
    }
    size += size;
    buffer = (char *)realloc(buffer, size);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<iupUnixGetCurrentDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x100,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
je     1181 <iupUnixGetCurrentDirectory+0x41>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d3 <iupUnixGetCurrentDirectory+0x93>
mov    0x2e50(%rip),%rax
mov    (%rax),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
je     11b1 <iupUnixGetCurrentDirectory+0x71>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d3 <iupUnixGetCurrentDirectory+0x93>
mov    -0x10(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
jmp    115d <iupUnixGetCurrentDirectory+0x1d>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iupUnixGetCurrentDirectory>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x100,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    $0x100,%esi
mov    %rax,%rdi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    11c5 <iupUnixGetCurrentDirectory+0x85>
mov    0x2e6a(%rip),%r15
mov    (%r15),%rax
mov    0x2e70(%rip),%r12
cmp    (%r12),%rax
jne    11ba <iupUnixGetCurrentDirectory+0x7a>
mov    $0x100,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %rbx,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    11c5 <iupUnixGetCurrentDirectory+0x85>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1190 <iupUnixGetCurrentDirectory+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
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* @iupUnixGetCurrentDirectory() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i8*
store i64 256, i64* %2
%4 = load i64, i64* %2
%5 = call i64 @malloc(i64 %4)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %3
br label %7
7: ; preds = %21, %0
%8 = load i8*, i8** %3
%9 = load i64, i64* %2
%10 = call i32* @getcwd(i8* %8, i64 %9)
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = load i8*, i8** %3
store i8* %13, i8** %1
br label %29
14: ; preds = %7
%15 = load i64, i64* @errno
%16 = load i64, i64* @ERANGE
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i8*, i8** %3
%20 = call i32 @free(i8* %19)
store i8* null, i8** %1
br label %29
21: ; preds = %14
%22 = load i64, i64* %2
%23 = load i64, i64* %2
%24 = add i64 %23, %22
store i64 %24, i64* %2
%25 = load i8*, i8** %3
%26 = load i64, i64* %2
%27 = call i64 @realloc(i8* %25, i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %3
br label %7
29: ; preds = %18, %12
%30 = load i8*, i8** %1
ret i8* %30
}

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