#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;
size_t PATH_MAX;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *getcwd(char *, size_t);
void *malloc(size_t);
char *__picotm_libc_get_current_dir_name() {
  size_t siz = PATH_MAX;
  char *buf = NULL;
  do {
    void *mem = malloc(siz);
    if (!mem) {
      free(buf);
    }
    buf = mem;
    char *cwd = getcwd(buf, siz);
    if (cwd) {
      return cwd;
    }
    siz *= 2;
  } while (errno == ERANGE);
  free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<__picotm_libc_get_current_dir_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2eb1(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116f <__picotm_libc_get_current_dir_name+0x3f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <getcwd@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11a0 <__picotm_libc_get_current_dir_name+0x70>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d7 <__picotm_libc_get_current_dir_name+0xa7>
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
je     114e <__picotm_libc_get_current_dir_name+0x1e>
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):
<__picotm_libc_get_current_dir_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2eae(%rip),%rax
mov    (%rax),%rbx
xor    %r15d,%r15d
mov    0x2e81(%rip),%r13
mov    0x2e8a(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1178 <__picotm_libc_get_current_dir_name+0x48>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11a3 <__picotm_libc_get_current_dir_name+0x73>
add    %rbx,%rbx
mov    0x0(%r13),%rax
mov    %r14,%r15
cmp    0x0(%rbp),%rax
je     1160 <__picotm_libc_get_current_dir_name+0x30>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i64 0
@errno = global i64 0
@ERANGE = global i64 0
define i8* @__picotm_libc_get_current_dir_name() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = load i64, i64* @PATH_MAX
store i64 %6, i64* %2
store i8* null, i8** %3
br label %7
7: ; preds = %27, %0
%8 = load i64, i64* %2
%9 = call i8* @malloc(i64 %8)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = load i8*, i8** %3
%14 = call i32 @free(i8* %13)
br label %15
15: ; preds = %12, %7
%16 = load i8*, i8** %4
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = load i64, i64* %2
%19 = call i8* @getcwd(i8* %17, i64 %18)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = load i8*, i8** %5
store i8* %23, i8** %1
br label %34
24: ; preds = %15
%25 = load i64, i64* %2
%26 = mul i64 %25, 2
store i64 %26, i64* %2
br label %27
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @ERANGE
%30 = icmp eq i64 %28, %29
br i1 %30, label %7, label %31
31: ; preds = %27
%32 = load i8*, i8** %3
%33 = call i32 @free(i8* %32)
store i8* null, i8** %1
br label %34
34: ; preds = %31, %22
%35 = load i8*, i8** %1
ret i8* %35
}

LLVM IR (-O2):
@PATH_MAX = global i64 0
@errno = global i64 0
@ERANGE = global i64 0
define i8* @__picotm_libc_get_current_dir_name() {
%1 = load i64, i64* @PATH_MAX
br label %2
2: ; preds = %12, %0
%3 = phi i8* [ null, %0 ], [ %5, %12 ]
%4 = phi i64 [ %1, %0 ], [ %13, %12 ]
%5 = tail call i8* @malloc(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @free(i8* %3)
br label %9
9: ; preds = %7, %2
%10 = tail call i8* @getcwd(i8* %5, i64 %4)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = shl i64 %4, 1
%14 = load i64, i64* @errno
%15 = load i64, i64* @ERANGE
%16 = icmp eq i64 %14, %15
br i1 %16, label %2, label %17
17: ; preds = %12
%18 = tail call i32 @free(i8* %5)
br label %19
19: ; preds = %9, %17
ret i8* %10
}