#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;
scalar_t__ getcwd(char *, int);
char *xstrdup(char *);
char *os_GetWorkingDir(void) {
  char buf[2048];
  errno = 0;
  if (getcwd(buf, 2048)) {
    return xstrdup(buf);
  }
  if (errno != ERANGE)
    return 0;
  {
    char buf[16384];
    if (getcwd(buf, 16384)) {
      return xstrdup(buf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<os_GetWorkingDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4810,%rsp
mov    0x2ea6(%rip),%rax
movq   $0x0,(%rax)
lea    -0x810(%rbp),%rdi
mov    $0x800,%esi
call   1030 <getcwd@plt>
cmp    $0x0,%rax
je     1169 <os_GetWorkingDir+0x49>
lea    -0x810(%rbp),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11c8 <os_GetWorkingDir+0xa8>
mov    0x2e68(%rip),%rax
mov    (%rax),%rax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
je     1190 <os_GetWorkingDir+0x70>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <os_GetWorkingDir+0xa8>
lea    -0x4810(%rbp),%rdi
mov    $0x4000,%esi
call   1030 <getcwd@plt>
cmp    $0x0,%rax
je     11c0 <os_GetWorkingDir+0xa0>
lea    -0x4810(%rbp),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11c8 <os_GetWorkingDir+0xa8>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x4810,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_GetWorkingDir>:
push   %rbx
sub    $0x4800,%rsp
mov    0x2ea9(%rip),%rbx
movq   $0x0,(%rbx)
mov    %rsp,%rdi
mov    $0x800,%esi
call   1030 <getcwd@plt>
test   %rax,%rax
je     1152 <os_GetWorkingDir+0x32>
mov    %rsp,%rdi
call   1040 <xstrdup@plt>
jmp    1184 <os_GetWorkingDir+0x64>
mov    (%rbx),%rax
mov    0x2e8c(%rip),%rcx
cmp    (%rcx),%rax
jne    1182 <os_GetWorkingDir+0x62>
lea    0x800(%rsp),%rdi
mov    $0x4000,%esi
call   1030 <getcwd@plt>
test   %rax,%rax
je     1182 <os_GetWorkingDir+0x62>
lea    0x800(%rsp),%rdi
jmp    114b <os_GetWorkingDir+0x2b>
xor    %eax,%eax
add    $0x4800,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
define i8* @os_GetWorkingDir() {
%1 = alloca i8*
%2 = alloca [2048 x i8]
%3 = alloca [16384 x i8]
store i64 0, i64* @errno
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = call i64 @getcwd(i8* %4, i32 2048)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%9 = call i8* @xstrdup(i8* %8)
store i8* %9, i8** %1
br label %23
10: ; preds = %0
%11 = load i64, i64* @errno
%12 = load i64, i64* @ERANGE
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
store i8* null, i8** %1
br label %23
15: ; preds = %10
%16 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%17 = call i64 @getcwd(i8* %16, i32 16384)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%21 = call i8* @xstrdup(i8* %20)
store i8* %21, i8** %1
br label %23
22: ; preds = %15
store i8* null, i8** %1
br label %23
23: ; preds = %22, %19, %14, %7
%24 = load i8*, i8** %1
ret i8* %24
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
define i8* @os_GetWorkingDir() {
%1 = alloca [2048 x i8]
%2 = alloca [16384 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
store i64 0, i64* @errno
%4 = call i64 @getcwd(i8* %3, i32 2048)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = call i8* @xstrdup(i8* %3)
br label %20
8: ; preds = %0
%9 = load i64, i64* @errno
%10 = load i64, i64* @ERANGE
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %20
12: ; preds = %8
%13 = getelementptr inbounds [16384 x i8], [16384 x i8]* %2, i64 0, i64 0
%14 = call i64 @getcwd(i8* %13, i32 16384)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = call i8* @xstrdup(i8* %13)
br label %18
18: ; preds = %12, %16
%19 = phi i8* [ null, %12 ], [ %17, %16 ]
br label %20
20: ; preds = %18, %8, %6
%21 = phi i8* [ %7, %6 ], [ null, %8 ], [ %19, %18 ]
ret i8* %21
}