#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 */
int ERANGE;
int errno;
int /*<<< orphan*/ free(char *);
char *getcwd(char *, size_t);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
int get_path_r(char **out, size_t len) {
  char *path_buffer = (char *)malloc(len);
  char *_t = getcwd(path_buffer, len);
  if (_t) {
    *out = path_buffer;
    return 0;
  }
  int err = errno;
  free(path_buffer);
  if (errno == ERANGE) {
    return get_path_r(out, len * 2);
  }
  errno = err;
  perror("getpwd");
  return -1;
}


x86 assembly (clang, -O0):
<get_path_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <getcwd@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11a0 <get_path_r+0x50>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1205 <get_path_r+0xb5>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%eax
jne    11e6 <get_path_r+0x96>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shl    $0x1,%rsi
call   1040 <get_path_r@plt>
mov    %eax,-0x4(%rbp)
jmp    1205 <get_path_r+0xb5>
mov    -0x2c(%rbp),%ecx
mov    0x2de8(%rip),%rax
mov    %ecx,(%rax)
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_path_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    11b4 <get_path_r+0x74>
mov    0x2e65(%rip),%r12
mov    0x2e6e(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    (%r12),%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    (%r12),%eax
cmp    0x0(%r13),%eax
jne    11c8 <get_path_r+0x88>
add    %rbx,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
test   %rax,%rax
je     1180 <get_path_r+0x40>
mov    %r15,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,(%r12)
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11b9 <get_path_r+0x79>

LLVM IR (-O0):
@errno = global i32 0
@ERANGE = global i32 0
@.str = [7 x i8] c"getpwd\00"
define i32 @get_path_r(i8** %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8** %0, i8*** %4
store i64 %1, i64* %5
%9 = load i64, i64* %5
%10 = call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = load i64, i64* %5
%14 = call i8* @getcwd(i8* %12, i64 %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %6
%19 = load i8**, i8*** %4
store i8* %18, i8** %19
store i32 0, i32* %3
br label %35
20: ; preds = %2
%21 = load i32, i32* @errno
store i32 %21, i32* %8
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
%24 = load i32, i32* @errno
%25 = load i32, i32* @ERANGE
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i8**, i8*** %4
%29 = load i64, i64* %5
%30 = mul i64 %29, 2
%31 = call i32 @get_path_r(i8** %28, i64 %30)
store i32 %31, i32* %3
br label %35
32: ; preds = %20
%33 = load i32, i32* %8
store i32 %33, i32* @errno
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %35
35: ; preds = %32, %27, %17
%36 = load i32, i32* %3
ret i32 %36
}

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