#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*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *getcwd(char *, size_t);
scalar_t__ realloc(void *, int);
int strlen(char *);
char *get_cwd(void) {
  char *cwd = NULL;
  char *buffer = NULL;
  for (size_t size = 128; (buffer = (char *)realloc((void *)buffer, size)) &&
                          !(cwd = getcwd(buffer, size)) && ERANGE == errno;
       size <<= 1)
    ;
  if (cwd) {
    return (char *)realloc((void *)cwd, strlen(cwd) + 1);
  }
  if (buffer) {
    error(0, 0, "Error");
    free(buffer);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<get_cwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x80,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
je     11d2 <get_cwd+0x82>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <getcwd@plt>
mov    %rax,%rcx
mov    %rcx,-0x10(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
jne    11d2 <get_cwd+0x82>
mov    0x2e29(%rip),%rax
mov    (%rax),%rax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11e2 <get_cwd+0x92>
jmp    11f8 <get_cwd+0xa8>
jmp    11e7 <get_cwd+0x97>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1170 <get_cwd+0x20>
cmpq   $0x0,-0x10(%rbp)
je     122b <get_cwd+0xdb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x8(%rbp)
jmp    1257 <get_cwd+0x107>
cmpq   $0x0,-0x18(%rbp)
je     124f <get_cwd+0xff>
xor    %esi,%esi
lea    0xdc1(%rip),%rdx
mov    %esi,%edi
call   1070 <error@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cwd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x80,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     11f7 <get_cwd+0xa7>
mov    %rax,%r15
mov    $0x80,%ebx
mov    0x2e66(%rip),%r13
mov    0x2e4f(%rip),%rbp
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <getcwd@plt>
test   %rax,%rax
jne    11c1 <get_cwd+0x71>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
jne    11dc <get_cwd+0x8c>
add    %rbx,%rbx
mov    %r15,%rdi
mov    %ebx,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1190 <get_cwd+0x40>
jmp    11f7 <get_cwd+0xa7>
mov    %rax,%r12
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%esi
mov    %r12,%rdi
call   1060 <realloc@plt>
mov    %rax,%r14
jmp    11f7 <get_cwd+0xa7>
lea    0xe1d(%rip),%rdx
xor    %r14d,%r14d
xor    %edi,%edi
xor    %esi,%esi
call   1070 <error@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ERANGE = global i64 0
@errno = global i64 0
@.str = [6 x i8] c"Error\00"
define i8* @get_cwd() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* null, i8** %2
store i8* null, i8** %3
store i64 128, i64* %4
br label %5
5: ; preds = %24, %0
%6 = load i8*, i8** %3
%7 = load i64, i64* %4
%8 = trunc i64 %7 to i32
%9 = call i64 @realloc(i8* %6, i32 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %21
12: ; preds = %5
%13 = load i8*, i8** %3
%14 = load i64, i64* %4
%15 = call i8* @getcwd(i8* %13, i64 %14)
store i8* %15, i8** %2
%16 = icmp ne i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %12
%18 = load i64, i64* @ERANGE
%19 = load i64, i64* @errno
%20 = icmp eq i64 %18, %19
br label %21
21: ; preds = %17, %12, %5
%22 = phi i1 [ false, %12 ], [ false, %5 ], [ %20, %17 ]
br i1 %22, label %23, label %27
23: ; preds = %21
br label %24
24: ; preds = %23
%25 = load i64, i64* %4
%26 = shl i64 %25, 1
store i64 %26, i64* %4
br label %5
27: ; preds = %21
%28 = load i8*, i8** %2
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i8*, i8** %2
%32 = load i8*, i8** %2
%33 = call i32 @strlen(i8* %32)
%34 = add nsw i32 %33, 1
%35 = call i64 @realloc(i8* %31, i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %1
br label %45
37: ; preds = %27
%38 = load i8*, i8** %3
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 @error(i32 0, i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%42 = load i8*, i8** %3
%43 = call i32 @free(i8* %42)
br label %44
44: ; preds = %40, %37
store i8* null, i8** %1
br label %45
45: ; preds = %44, %30
%46 = load i8*, i8** %1
ret i8* %46
}

LLVM IR (-O2):
@ERANGE = global i64 0
@errno = global i64 0
@.str = [6 x i8] c"Error\00"
define i8* @get_cwd() {
%1 = tail call i64 @realloc(i8* null, i32 128)
%2 = icmp eq i64 %1, 0
br i1 %2, label %27, label %7
3: ; preds = %13
%4 = trunc i64 %17 to i32
%5 = tail call i64 @realloc(i8* %10, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %0, %3
%8 = phi i64 [ %5, %3 ], [ %1, %0 ]
%9 = phi i64 [ %17, %3 ], [ 128, %0 ]
%10 = inttoptr i64 %8 to i8*
%11 = tail call i8* @getcwd(i8* %10, i64 %9)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = load i64, i64* @ERANGE
%15 = load i64, i64* @errno
%16 = icmp eq i64 %14, %15
%17 = shl i64 %9, 1
br i1 %16, label %3, label %23
18: ; preds = %7
%19 = tail call i32 @strlen(i8* %11)
%20 = add nsw i32 %19, 1
%21 = tail call i64 @realloc(i8* %11, i32 %20)
%22 = inttoptr i64 %21 to i8*
br label %27
23: ; preds = %13
%24 = inttoptr i64 %8 to i8*
%25 = tail call i32 @error(i32 0, i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @free(i8* %24)
br label %27
27: ; preds = %3, %0, %23, %18
%28 = phi i8* [ %22, %18 ], [ null, %23 ], [ null, %0 ], [ null, %3 ]
ret i8* %28
}