#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 *);
char *getcwd(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  char *buf = NULL;
  int size = 8;
  char *cwd;
  while ((cwd = getcwd(buf, size)) == NULL) {
    free(buf);
    buf = NULL;
    size *= 2;
    if (errno != ERANGE) {
      perror("pwd");
      return 1;
    }
  }
  printf("%s\n", cwd);
  free(cwd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x8,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <getcwd@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c9 <main+0x89>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <main+0x84>
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e9 <main+0xa9>
jmp    115e <main+0x1e>
mov    -0x20(%rbp),%rsi
lea    0xe30(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x8,%ebx
mov    0x2e84(%rip),%r15
mov    0x2e8d(%rip),%r12
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %ebx,%esi
call   1050 <getcwd@plt>
test   %rax,%rax
jne    1193 <main+0x53>
xor    %edi,%edi
call   1030 <free@plt>
add    %ebx,%ebx
mov    (%r15),%rax
cmp    (%r12),%rax
je     1160 <main+0x20>
lea    0xe79(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
jmp    11af <main+0x6f>
mov    %rax,%r14
lea    0xe67(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [4 x i8] c"pwd\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
store i8* null, i8** %2
store i32 8, i32* %3
br label %5
5: ; preds = %20, %0
%6 = load i8*, i8** %2
%7 = load i32, i32* %3
%8 = call i8* @getcwd(i8* %6, i32 %7)
store i8* %8, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %21
10: ; preds = %5
%11 = load i8*, i8** %2
%12 = call i32 @free(i8* %11)
store i8* null, i8** %2
%13 = load i32, i32* %3
%14 = mul nsw i32 %13, 2
store i32 %14, i32* %3
%15 = load i64, i64* @errno
%16 = load i64, i64* @ERANGE
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %26
20: ; preds = %10
br label %5
21: ; preds = %5
%22 = load i8*, i8** %4
%23 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %4
%25 = call i32 @free(i8* %24)
store i32 0, i32* %1
br label %26
26: ; preds = %21, %18
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [4 x i8] c"pwd\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main() {
br label %1
1: ; preds = %5, %0
%2 = phi i32 [ 8, %0 ], [ %7, %5 ]
%3 = tail call i8* @getcwd(i8* null, i32 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %13
5: ; preds = %1
%6 = tail call i32 @free(i8* null)
%7 = shl nsw i32 %2, 1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ERANGE
%10 = icmp eq i64 %8, %9
br i1 %10, label %1, label %11
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %16
13: ; preds = %1
%14 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%15 = tail call i32 @free(i8* %3)
br label %16
16: ; preds = %13, %11
%17 = phi i32 [ 1, %11 ], [ 0, %13 ]
ret i32 %17
}