#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__ calloc(size_t, size_t);
int /*<<< orphan*/ free(char *);
char *getcwd(char *, int);
char *mus_getcwd(void) {
  int i, path_max = 0;
  char *pwd = NULL, *res = NULL;
  if (path_max < 1024) {
    if (path_max < 1024)
      path_max = 1024;
  }
  for (i = path_max;; i *= 2) {
    if (pwd)
      free(pwd);
    pwd = (char *)calloc((size_t)(i), (size_t)(sizeof(char)));
    res = getcwd(pwd, i);
    if (res)
      break;
  }
  return (pwd);
}


x86 assembly (clang, -O0):
<mus_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x400,-0x8(%rbp)
jge    1175 <mus_getcwd+0x45>
cmpl   $0x400,-0x8(%rbp)
jge    1170 <mus_getcwd+0x40>
movl   $0x400,-0x8(%rbp)
jmp    1175 <mus_getcwd+0x45>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118f <mus_getcwd+0x5f>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movslq -0x4(%rbp),%rdi
mov    $0x1,%esi
call   1050 <calloc@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     11c1 <mus_getcwd+0x91>
jmp    11d4 <mus_getcwd+0xa4>
jmp    11c6 <mus_getcwd+0x96>
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    117b <mus_getcwd+0x4b>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mus_getcwd>:
push   %r14
push   %rbx
push   %rax
mov    $0x400,%ebx
xor    %r14d,%r14d
jmp    1160 <mus_getcwd+0x30>
xchg   %ax,%ax
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
call   1040 <getcwd@plt>
add    %ebx,%ebx
test   %rax,%rax
jne    116f <mus_getcwd+0x3f>
test   %r14,%r14
je     1140 <mus_getcwd+0x10>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1140 <mus_getcwd+0x10>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @mus_getcwd() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i32 0, i32* %2
store i8* null, i8** %3
store i8* null, i8** %4
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 1024
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 1024
br i1 %9, label %10, label %11
10: ; preds = %7
store i32 1024, i32* %2
br label %11
11: ; preds = %10, %7
br label %12
12: ; preds = %11, %0
%13 = load i32, i32* %2
store i32 %13, i32* %1
br label %14
14: ; preds = %32, %12
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = call i32 @free(i8* %18)
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %1
%22 = sext i32 %21 to i64
%23 = call i64 @calloc(i64 %22, i64 1)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %3
%25 = load i8*, i8** %3
%26 = load i32, i32* %1
%27 = call i8* @getcwd(i8* %25, i32 %26)
store i8* %27, i8** %4
%28 = load i8*, i8** %4
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %20
br label %35
31: ; preds = %20
br label %32
32: ; preds = %31
%33 = load i32, i32* %1
%34 = mul nsw i32 %33, 2
store i32 %34, i32* %1
br label %14
35: ; preds = %30
%36 = load i8*, i8** %3
ret i8* %36
}

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