#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;
int /*<<< orphan*/ _PC_PATH_MAX;
scalar_t__ errno;
char *getcwd(char *, size_t);
char *malloc(int);
size_t pathconf(char *, int /*<<< orphan*/);
char *realloc(char *, size_t);
char *lbs_posix_getcwd(void) {
  char *cwd, *result;
  size_t size = pathconf(".", _PC_PATH_MAX);
  size = size > 0 ? size : 256;
  size = size > 8192 ? 8192 : size;
  cwd = malloc(sizeof(char) * size);
  while ((result = getcwd(cwd, size)) == NULL && errno == ERANGE) {
    size *= 2;
    cwd = realloc(cwd, size);
  }
  return cwd;
}


x86 assembly (clang, -O0):
<lbs_posix_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1030 <pathconf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    1179 <lbs_posix_getcwd+0x39>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1187 <lbs_posix_getcwd+0x47>
mov    $0x100,%eax
mov    %rax,-0x20(%rbp)
jmp    1187 <lbs_posix_getcwd+0x47>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x2000,-0x18(%rbp)
jbe    11ab <lbs_posix_getcwd+0x6b>
mov    $0x2000,%eax
mov    %rax,-0x28(%rbp)
jmp    11b3 <lbs_posix_getcwd+0x73>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <getcwd@plt>
mov    %rax,%rcx
mov    %rcx,-0x10(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
jne    120b <lbs_posix_getcwd+0xcb>
mov    0x2dd8(%rip),%rax
mov    (%rax),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    121b <lbs_posix_getcwd+0xdb>
jmp    123d <lbs_posix_getcwd+0xfd>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11ce <lbs_posix_getcwd+0x8e>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lbs_posix_getcwd>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1030 <pathconf@plt>
test   %rax,%rax
mov    $0x100,%ecx
cmovne %rax,%rcx
cmp    $0x2000,%rcx
mov    $0x2000,%ebx
cmovb  %rcx,%rbx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    11da <lbs_posix_getcwd+0x9a>
mov    0x2e36(%rip),%r15
mov    0x2e47(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12),%rax
cmp    %rax,(%r15)
jne    11da <lbs_posix_getcwd+0x9a>
add    %rbx,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
test   %rax,%rax
je     11b0 <lbs_posix_getcwd+0x70>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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