#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 ERANGE;
int errno;
int /*<<< orphan*/ free(char *);
char *getwd(char *);
char *xmalloc(unsigned int);
char *xrealloc(char *, unsigned int);
char *xgetcwd(void) {
  char *cwd;
  char *ret;
  unsigned path_max;
  errno = 0;
  path_max = (unsigned)255;
  path_max += 2;
  cwd = xmalloc(path_max);
  errno = 0;
  while ((ret = getwd(cwd)) == NULL && errno == ERANGE) {
    path_max += 32;
    cwd = xrealloc(cwd, path_max);
    errno = 0;
  }
  if (ret == NULL) {
    int save_errno = errno;
    free(cwd);
    errno = save_errno;
    return NULL;
  }
  return cwd;
}


x86 assembly (clang, -O0):
<xgetcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e89(%rip),%rax
movl   $0x0,(%rax)
movl   $0xff,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e60(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1060 <getwd@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
jne    11b5 <xgetcwd+0x75>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11c5 <xgetcwd+0x85>
jmp    11f0 <xgetcwd+0xb0>
mov    -0x1c(%rbp),%eax
add    $0x20,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <xrealloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2df3(%rip),%rax
movl   $0x0,(%rax)
jmp    117e <xgetcwd+0x3e>
cmpq   $0x0,-0x18(%rbp)
jne    1229 <xgetcwd+0xe9>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%ecx
mov    0x2dbe(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1231 <xgetcwd+0xf1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xgetcwd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e89(%rip),%r14
movl   $0x0,(%r14)
mov    $0x101,%edi
call   1050 <xmalloc@plt>
mov    %rax,%r15
movl   $0x0,(%r14)
mov    %rax,%rdi
call   1060 <getwd@plt>
mov    (%r14),%ebx
test   %rax,%rax
sete   %al
jne    11c0 <xgetcwd+0x80>
mov    0x2e64(%rip),%r12
cmp    (%r12),%ebx
jne    11c0 <xgetcwd+0x80>
mov    $0x121,%ebp
nop
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <xrealloc@plt>
mov    %rax,%r15
movl   $0x0,(%r14)
mov    %rax,%rdi
call   1060 <getwd@plt>
mov    (%r14),%ebx
test   %rax,%rax
sete   %al
jne    11c0 <xgetcwd+0x80>
add    $0x20,%ebp
cmp    (%r12),%ebx
je     1190 <xgetcwd+0x50>
test   %al,%al
je     11d2 <xgetcwd+0x92>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,(%r14)
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@ERANGE = global i32 0
define i8* @xgetcwd() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* @errno
store i32 255, i32* %4
%6 = load i32, i32* %4
%7 = add i32 %6, 2
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = call i8* @xmalloc(i32 %8)
store i8* %9, i8** %2
store i32 0, i32* @errno
br label %10
10: ; preds = %20, %0
%11 = load i8*, i8** %2
%12 = call i8* @getwd(i8* %11)
store i8* %12, i8** %3
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @errno
%16 = load i32, i32* @ERANGE
%17 = icmp eq i32 %15, %16
br label %18
18: ; preds = %14, %10
%19 = phi i1 [ false, %10 ], [ %17, %14 ]
br i1 %19, label %20, label %26
20: ; preds = %18
%21 = load i32, i32* %4
%22 = add i32 %21, 32
store i32 %22, i32* %4
%23 = load i8*, i8** %2
%24 = load i32, i32* %4
%25 = call i8* @xrealloc(i8* %23, i32 %24)
store i8* %25, i8** %2
store i32 0, i32* @errno
br label %10
26: ; preds = %18
%27 = load i8*, i8** %3
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* @errno
store i32 %30, i32* %5
%31 = load i8*, i8** %2
%32 = call i32 @free(i8* %31)
%33 = load i32, i32* %5
store i32 %33, i32* @errno
store i8* null, i8** %1
br label %36
34: ; preds = %26
%35 = load i8*, i8** %2
store i8* %35, i8** %1
br label %36
36: ; preds = %34, %29
%37 = load i8*, i8** %1
ret i8* %37
}

LLVM IR (-O2):
@errno = global i32 0
@ERANGE = global i32 0
define i8* @xgetcwd() {
store i32 0, i32* @errno
%1 = tail call i8* @xmalloc(i32 257)
store i32 0, i32* @errno
%2 = tail call i8* @getwd(i8* %1)
%3 = icmp eq i8* %2, null
%4 = load i32, i32* @errno
%5 = load i32, i32* @ERANGE
%6 = icmp eq i32 %4, %5
%7 = select i1 %3, i1 %6, i1 false
br i1 %7, label %8, label %19
8: ; preds = %0, %8
%9 = phi i32 [ %11, %8 ], [ 257, %0 ]
%10 = phi i8* [ %12, %8 ], [ %1, %0 ]
%11 = add i32 %9, 32
%12 = tail call i8* @xrealloc(i8* %10, i32 %11)
store i32 0, i32* @errno
%13 = tail call i8* @getwd(i8* %12)
%14 = icmp eq i8* %13, null
%15 = load i32, i32* @errno
%16 = load i32, i32* @ERANGE
%17 = icmp eq i32 %15, %16
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %8, label %19
19: ; preds = %8, %0
%20 = phi i8* [ %1, %0 ], [ %12, %8 ]
%21 = phi i1 [ %3, %0 ], [ %14, %8 ]
%22 = phi i32 [ %4, %0 ], [ %15, %8 ]
br i1 %21, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @free(i8* %20)
store i32 %22, i32* @errno
br label %25
25: ; preds = %19, %23
%26 = phi i8* [ null, %23 ], [ %20, %19 ]
ret i8* %26
}