#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 *);
scalar_t__ xmalloc(unsigned int);
char *xstrdup(char *);
char *xgetcwd() {
  char *ret;
  unsigned path_max;
  char buf[1024];
  errno = 0;
  ret = getwd(buf);
  if (ret != NULL)
    return xstrdup(buf);
  if (errno != ERANGE)
    return NULL;
  path_max = (unsigned)255;
  path_max += 2;
  for (;;) {
    char *cwd = (char *)xmalloc(path_max);
    errno = 0;
    ret = getwd(cwd);
    if (ret != NULL)
      return ret;
    if (errno != ERANGE) {
      int save_errno = errno;
      free(cwd);
      errno = save_errno;
      return NULL;
    }
    free(cwd);
    path_max += path_max / 16;
    path_max += 32;
  }
}


x86 assembly (clang, -O0):
<xgetcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    0x2e86(%rip),%rax
movl   $0x0,(%rax)
lea    -0x420(%rbp),%rdi
call   1060 <getwd@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1188 <xgetcwd+0x48>
lea    -0x420(%rbp),%rdi
call   1050 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1276 <xgetcwd+0x136>
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%eax
je     11ad <xgetcwd+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    1276 <xgetcwd+0x136>
movl   $0xff,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x428(%rbp)
mov    0x2e05(%rip),%rax
movl   $0x0,(%rax)
mov    -0x428(%rbp),%rdi
call   1060 <getwd@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1201 <xgetcwd+0xc1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1276 <xgetcwd+0x136>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%eax
je     1250 <xgetcwd+0x110>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x428(%rbp),%rdi
call   1030 <free@plt>
mov    -0x42c(%rbp),%ecx
mov    0x2d97(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1276 <xgetcwd+0x136>
mov    -0x428(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
shr    $0x4,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%eax
mov    %eax,-0x14(%rbp)
jmp    11bd <xgetcwd+0x7d>
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xgetcwd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    0x2e82(%rip),%r15
movl   $0x0,(%r15)
mov    %rsp,%rdi
call   1060 <getwd@plt>
test   %rax,%rax
je     1177 <xgetcwd+0x37>
mov    %rsp,%rdi
call   1050 <xstrdup@plt>
jmp    1206 <xgetcwd+0xc6>
mov    (%r15),%eax
mov    0x2e67(%rip),%r12
cmp    (%r12),%eax
jne    1204 <xgetcwd+0xc4>
mov    $0x101,%edi
call   1040 <xmalloc@plt>
mov    %rax,%r14
movl   $0x0,(%r15)
mov    %rax,%rdi
call   1060 <getwd@plt>
test   %rax,%rax
jne    1206 <xgetcwd+0xc6>
mov    (%r15),%ebp
cmp    (%r12),%ebp
jne    11f9 <xgetcwd+0xb9>
mov    $0x101,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
shr    $0x4,%eax
add    %eax,%ebx
add    $0x20,%ebx
mov    %ebx,%edi
call   1040 <xmalloc@plt>
mov    %rax,%r14
movl   $0x0,(%r15)
mov    %rax,%rdi
call   1060 <getwd@plt>
test   %rax,%rax
jne    1206 <xgetcwd+0xc6>
mov    (%r15),%ebp
cmp    (%r12),%ebp
je     11c0 <xgetcwd+0x80>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,(%r15)
xor    %eax,%eax
add    $0x400,%rsp
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 i32
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i32
store i32 0, i32* @errno
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i8* @getwd(i8* %7)
store i8* %8, i8** %2
%9 = load i8*, i8** %2
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = call i8* @xstrdup(i8* %12)
store i8* %13, i8** %1
br label %50
14: ; preds = %0
%15 = load i32, i32* @errno
%16 = load i32, i32* @ERANGE
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
store i8* null, i8** %1
br label %50
19: ; preds = %14
store i32 255, i32* %3
%20 = load i32, i32* %3
%21 = add i32 %20, 2
store i32 %21, i32* %3
br label %22
22: ; preds = %41, %19
%23 = load i32, i32* %3
%24 = call i64 @xmalloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %5
store i32 0, i32* @errno
%26 = load i8*, i8** %5
%27 = call i8* @getwd(i8* %26)
store i8* %27, i8** %2
%28 = load i8*, i8** %2
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = load i8*, i8** %2
store i8* %31, i8** %1
br label %50
32: ; preds = %22
%33 = load i32, i32* @errno
%34 = load i32, i32* @ERANGE
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* @errno
store i32 %37, i32* %6
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
%40 = load i32, i32* %6
store i32 %40, i32* @errno
store i8* null, i8** %1
br label %50
41: ; preds = %32
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
%44 = load i32, i32* %3
%45 = udiv i32 %44, 16
%46 = load i32, i32* %3
%47 = add i32 %46, %45
store i32 %47, i32* %3
%48 = load i32, i32* %3
%49 = add i32 %48, 32
store i32 %49, i32* %3
br label %22
50: ; preds = %36, %30, %18, %11
%51 = load i8*, i8** %1
ret i8* %51
}

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