#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 *);
int /*<<< orphan*/ *getcwd(char *, size_t);
int /*<<< orphan*/ log_real_(int /*<<< orphan*/, char *, int /*<<< orphan*/,
                             char *, int /*<<< orphan*/);
char *malloc(size_t);
char *strdup(char *);
int /*<<< orphan*/ strerror(scalar_t__);
char *get_wd(void) {
  size_t size = 1024;
  char *path;
  for (;;) {
    path = malloc(size);
    if (getcwd(path, size) != NULL)
      return path;
    else {
      if (errno != ERANGE) {
        free(path);
        log_real_(0, "CRITICAL", 0,
                  "getcwd failed: %s"
                  "\n",
                  strerror(errno));
        return strdup("/UNKNOWN");
      }
      free(path);
      size <<= 1;
    }
  }
}


x86 assembly (clang, -O0):
<get_wd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x400,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
je     11a1 <get_wd+0x41>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121c <get_wd+0xbc>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
je     1202 <get_wd+0xa2>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %eax,%r8d
xor    %edx,%edx
lea    0xe21(%rip),%rsi
lea    0xe23(%rip),%rcx
mov    %edx,%edi
call   1040 <log_real_@plt>
lea    0xe28(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    121c <get_wd+0xbc>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1170 <get_wd+0x10>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_wd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x400,%r15d
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0x400,%esi
mov    %rax,%rdi
call   1050 <getcwd@plt>
test   %rax,%rax
jne    11d3 <get_wd+0x73>
mov    0x2e41(%rip),%r12
mov    0x2e4a(%rip),%r13
xchg   %ax,%ax
mov    (%r12),%rbx
mov    0x0(%r13),%rbp
mov    %r14,%rdi
call   1030 <free@plt>
cmp    %rbp,%rbx
jne    11e5 <get_wd+0x85>
add    %r15,%r15
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <getcwd@plt>
test   %rax,%rax
je     11a0 <get_wd+0x40>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1080 <strerror@plt>
lea    0xe0c(%rip),%rsi
lea    0xe0e(%rip),%rcx
xor    %edi,%edi
xor    %edx,%edx
mov    %eax,%r8d
call   1040 <log_real_@plt>
lea    0xe0e(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <strdup@plt>

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [9 x i8] c"CRITICAL\00"
@.str.1 = [19 x i8] c"getcwd failed: %s\0A\00"
@.str.2 = [9 x i8] c"/UNKNOWN\00"
define i8* @get_wd() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i8*
store i64 1024, i64* %2
br label %4
4: ; preds = %29, %0
%5 = load i64, i64* %2
%6 = call i8* @malloc(i64 %5)
store i8* %6, i8** %3
%7 = load i8*, i8** %3
%8 = load i64, i64* %2
%9 = call i32* @getcwd(i8* %7, i64 %8)
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = load i8*, i8** %3
store i8* %12, i8** %1
br label %30
13: ; preds = %4
%14 = load i64, i64* @errno
%15 = load i64, i64* @ERANGE
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %24
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = call i32 @free(i8* %18)
%20 = load i64, i64* @errno
%21 = call i32 @strerror(i64 %20)
%22 = call i32 @log_real_(i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i8* @strdup(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i8* %23, i8** %1
br label %30
24: ; preds = %13
%25 = load i8*, i8** %3
%26 = call i32 @free(i8* %25)
%27 = load i64, i64* %2
%28 = shl i64 %27, 1
store i64 %28, i64* %2
br label %29
29: ; preds = %24
br label %4
30: ; preds = %17, %11
%31 = load i8*, i8** %1
ret i8* %31
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [9 x i8] c"CRITICAL\00"
@.str.1 = [19 x i8] c"getcwd failed: %s\0A\00"
@.str.2 = [9 x i8] c"/UNKNOWN\00"
define i8* @get_wd() {
%1 = tail call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
%2 = tail call i32* @getcwd(i8* %1, i64 1024)
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %20
4: ; preds = %0, %15
%5 = phi i8* [ %17, %15 ], [ %1, %0 ]
%6 = phi i64 [ %16, %15 ], [ 1024, %0 ]
%7 = load i64, i64* @errno
%8 = load i64, i64* @ERANGE
%9 = icmp eq i64 %7, %8
%10 = tail call i32 @free(i8* %5)
br i1 %9, label %15, label %11
11: ; preds = %4
%12 = tail call i32 @strerror(i64 %7)
%13 = tail call i32 @log_real_(i32 0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = tail call dereferenceable_or_null(9) i8* @strdup(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %20
15: ; preds = %4
%16 = shl i64 %6, 1
%17 = tail call i8* @malloc(i64 %16)
%18 = tail call i32* @getcwd(i8* %17, i64 %16)
%19 = icmp eq i32* %18, null
br i1 %19, label %4, label %20
20: ; preds = %15, %0, %11
%21 = phi i8* [ %14, %11 ], [ %1, %0 ], [ %17, %15 ]
ret i8* %21
}