#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int ERANGE;
int errno;
int /*<<< orphan*/ free(char *);
char *getcwd(char *, unsigned int);
scalar_t__ malloc(unsigned int);
char *strdup(char *);
char *mu_getcwd() {
  char *ret;
  unsigned path_max;
  char buf[128];
  errno = 0;
  ret = getcwd(buf, sizeof(buf));
  if (ret != NULL)
    return strdup(buf);
  if (errno != ERANGE)
    return NULL;
  path_max = 128;
  path_max += 2;
  for (;;) {
    char *cwd = (char *)malloc(path_max);
    errno = 0;
    ret = getcwd(cwd, path_max);
    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;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<mu_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    0x2e86(%rip),%rax
movl   $0x0,(%rax)
lea    -0xa0(%rbp),%rdi
mov    $0x80,%esi
call   1040 <getcwd@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118d <mu_getcwd+0x4d>
lea    -0xa0(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    127e <mu_getcwd+0x13e>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%eax
je     11b2 <mu_getcwd+0x72>
movq   $0x0,-0x8(%rbp)
jmp    127e <mu_getcwd+0x13e>
movl   $0x80,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0xa8(%rbp)
mov    0x2e00(%rip),%rax
movl   $0x0,(%rax)
mov    -0xa8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <getcwd@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1209 <mu_getcwd+0xc9>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    127e <mu_getcwd+0x13e>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%eax
je     1258 <mu_getcwd+0x118>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xac(%rbp)
mov    -0xa8(%rbp),%rdi
call   1030 <free@plt>
mov    -0xac(%rbp),%ecx
mov    0x2d8f(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    127e <mu_getcwd+0x13e>
mov    -0xa8(%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    11c2 <mu_getcwd+0x82>
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mu_getcwd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    0x2e82(%rip),%r15
movl   $0x0,(%r15)
mov    %rsp,%rdi
mov    $0x80,%esi
call   1040 <getcwd@plt>
test   %rax,%rax
je     117c <mu_getcwd+0x3c>
mov    %rsp,%rdi
call   1060 <strdup@plt>
jmp    1208 <mu_getcwd+0xc8>
mov    (%r15),%eax
mov    0x2e62(%rip),%r12
cmp    (%r12),%eax
jne    1206 <mu_getcwd+0xc6>
mov    $0x82,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
movl   $0x0,(%r15)
mov    %rax,%rdi
mov    $0x82,%esi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    1208 <mu_getcwd+0xc8>
mov    (%r15),%ebp
cmp    (%r12),%ebp
jne    11fb <mu_getcwd+0xbb>
mov    $0x82,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
shr    $0x4,%eax
add    %eax,%ebx
add    $0x20,%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
movl   $0x0,(%r15)
mov    %rax,%rdi
mov    %ebx,%esi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    1208 <mu_getcwd+0xc8>
mov    (%r15),%ebp
cmp    (%r12),%ebp
je     11c0 <mu_getcwd+0x80>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,(%r15)
xor    %eax,%eax
add    $0x80,%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* @mu_getcwd() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = alloca i8*
%6 = alloca i32
store i32 0, i32* @errno
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%8 = call i8* @getcwd(i8* %7, i32 128)
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 [128 x i8], [128 x i8]* %4, i64 0, i64 0
%13 = call i8* @strdup(i8* %12)
store i8* %13, i8** %1
br label %51
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 %51
19: ; preds = %14
store i32 128, i32* %3
%20 = load i32, i32* %3
%21 = add i32 %20, 2
store i32 %21, i32* %3
br label %22
22: ; preds = %42, %19
%23 = load i32, i32* %3
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %5
store i32 0, i32* @errno
%26 = load i8*, i8** %5
%27 = load i32, i32* %3
%28 = call i8* @getcwd(i8* %26, i32 %27)
store i8* %28, i8** %2
%29 = load i8*, i8** %2
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = load i8*, i8** %2
store i8* %32, i8** %1
br label %51
33: ; preds = %22
%34 = load i32, i32* @errno
%35 = load i32, i32* @ERANGE
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* @errno
store i32 %38, i32* %6
%39 = load i8*, i8** %5
%40 = call i32 @free(i8* %39)
%41 = load i32, i32* %6
store i32 %41, i32* @errno
store i8* null, i8** %1
br label %51
42: ; preds = %33
%43 = load i8*, i8** %5
%44 = call i32 @free(i8* %43)
%45 = load i32, i32* %3
%46 = udiv i32 %45, 16
%47 = load i32, i32* %3
%48 = add i32 %47, %46
store i32 %48, i32* %3
%49 = load i32, i32* %3
%50 = add i32 %49, 32
store i32 %50, i32* %3
br label %22
51: ; preds = %37, %31, %18, %11
%52 = load i8*, i8** %1
ret i8* %52
}

LLVM IR (-O2):
@errno = global i32 0
@ERANGE = global i32 0
define i8* @mu_getcwd() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
store i32 0, i32* @errno
%3 = call i8* @getcwd(i8* %2, i32 128)
%4 = icmp eq i8* %3, null
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = call i8* @strdup(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 @malloc(i32 130)
%13 = inttoptr i64 %12 to i8*
store i32 0, i32* @errno
%14 = call i8* @getcwd(i8* %13, i32 130)
%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 ], [ 130, %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 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i8*
store i32 0, i32* @errno
%38 = call i8* @getcwd(i8* %37, i32 %35)
%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
}