#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 /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *getcwd(char *, int);
char *malloc(int);
char *realloc(char *, size_t);
size_t strlen(char *);
char *dctrl_get_current_dir_name() {
  char *buf;
  char *path;
  size_t len;
  buf = malloc(4096);
  if (NULL == buf)
    return NULL;
  if (NULL == getcwd(buf, 4096)) {
    free(buf);
    return NULL;
  }
  len = strlen(buf);
  path = realloc(buf, len + 1);
  if (NULL == path)
    ;
  path = buf;
  return path;
}


x86 assembly (clang, -O0):
<dctrl_get_current_dir_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    117f <dctrl_get_current_dir_name+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11f4 <dctrl_get_current_dir_name+0xa4>
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <getcwd@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11b1 <dctrl_get_current_dir_name+0x61>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f4 <dctrl_get_current_dir_name+0xa4>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11e4 <dctrl_get_current_dir_name+0x94>
jmp    11e4 <dctrl_get_current_dir_name+0x94>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dctrl_get_current_dir_name>:
push   %rbx
mov    $0x1000,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1193 <dctrl_get_current_dir_name+0x43>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1000,%esi
call   1050 <getcwd@plt>
test   %rax,%rax
je     118b <dctrl_get_current_dir_name+0x3b>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %rbx,%rdi
call   1070 <realloc@plt>
jmp    1195 <dctrl_get_current_dir_name+0x45>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @dctrl_get_current_dir_name() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = call i8* @malloc(i32 4096)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp eq i8* null, %6
br i1 %7, label %8, label %9
8: ; preds = %0
store i8* null, i8** %1
br label %29
9: ; preds = %0
%10 = load i8*, i8** %2
%11 = call i32* @getcwd(i8* %10, i32 4096)
%12 = icmp eq i32* null, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i8*, i8** %2
%15 = call i32 @free(i8* %14)
store i8* null, i8** %1
br label %29
16: ; preds = %9
%17 = load i8*, i8** %2
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %4
%19 = load i8*, i8** %2
%20 = load i64, i64* %4
%21 = add i64 %20, 1
%22 = call i8* @realloc(i8* %19, i64 %21)
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = icmp eq i8* null, %23
br i1 %24, label %25, label %26
25: ; preds = %16
br label %26
26: ; preds = %25, %16
%27 = load i8*, i8** %2
store i8* %27, i8** %3
%28 = load i8*, i8** %3
store i8* %28, i8** %1
br label %29
29: ; preds = %26, %13, %8
%30 = load i8*, i8** %1
ret i8* %30
}

LLVM IR (-O2):
define i8* @dctrl_get_current_dir_name() {
%1 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%2 = icmp eq i8* %1, null
br i1 %2, label %12, label %3
3: ; preds = %0
%4 = tail call i32* @getcwd(i8* %1, i32 4096)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @free(i8* %1)
br label %12
8: ; preds = %3
%9 = tail call i64 @strlen(i8* %1)
%10 = add i64 %9, 1
%11 = tail call i8* @realloc(i8* %1, i64 %10)
br label %12
12: ; preds = %0, %8, %6
%13 = phi i8* [ null, %6 ], [ %1, %8 ], [ null, %0 ]
ret i8* %13
}