#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ *getcwd(char *, size_t);
char *malloc(int);
int /*<<< orphan*/ *realloc(char *, int);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ stderr;
char *get_current_dir(void) {
  size_t i = 50;
  char *p = malloc(sizeof(char) * i);
  while (NULL == getcwd(p, i)) {
    i += 10;
    if (NULL == realloc(p, sizeof(char) * i)) {
      fprintf(stderr, "Warning : unable to set current directory !\n");
      return p;
    }
  }
  setenv("PWD", p, 1);
  return p;
}


x86 assembly (clang, -O0):
<get_current_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x32,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <getcwd@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11e2 <get_current_dir+0x92>
mov    -0x10(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11dd <get_current_dir+0x8d>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ff <get_current_dir+0xaf>
jmp    1173 <get_current_dir+0x23>
mov    -0x18(%rbp),%rsi
lea    0xe40(%rip),%rdi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_current_dir>:
push   %r14
push   %rbx
push   %rax
mov    $0x32,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0x32,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    11aa <get_current_dir+0x5a>
add    $0xa,%rbx
mov    %r14,%rdi
mov    %ebx,%esi
call   1070 <realloc@plt>
test   %rax,%rax
jne    1170 <get_current_dir+0x20>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11be <get_current_dir+0x6e>
lea    0xe7c(%rip),%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [45 x i8] c"Warning : unable to set current directory !\0A\00"
@.str.1 = [4 x i8] c"PWD\00"
define i8* @get_current_dir() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i8*
store i64 50, i64* %2
%4 = load i64, i64* %2
%5 = mul i64 1, %4
%6 = trunc i64 %5 to i32
%7 = call i8* @malloc(i32 %6)
store i8* %7, i8** %3
br label %8
8: ; preds = %26, %0
%9 = load i8*, i8** %3
%10 = load i64, i64* %2
%11 = call i32* @getcwd(i8* %9, i64 %10)
%12 = icmp eq i32* null, %11
br i1 %12, label %13, label %27
13: ; preds = %8
%14 = load i64, i64* %2
%15 = add i64 %14, 10
store i64 %15, i64* %2
%16 = load i8*, i8** %3
%17 = load i64, i64* %2
%18 = mul i64 1, %17
%19 = trunc i64 %18 to i32
%20 = call i32* @realloc(i8* %16, i32 %19)
%21 = icmp eq i32* null, %20
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%25 = load i8*, i8** %3
store i8* %25, i8** %1
br label %31
26: ; preds = %13
br label %8
27: ; preds = %8
%28 = load i8*, i8** %3
%29 = call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %28, i32 1)
%30 = load i8*, i8** %3
store i8* %30, i8** %1
br label %31
31: ; preds = %27, %22
%32 = load i8*, i8** %1
ret i8* %32
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [45 x i8] c"Warning : unable to set current directory !\0A\00"
@.str.1 = [4 x i8] c"PWD\00"
define i8* @get_current_dir() {
%1 = tail call dereferenceable_or_null(50) i8* @malloc(i32 50)
br label %2
2: ; preds = %6, %0
%3 = phi i64 [ 50, %0 ], [ %7, %6 ]
%4 = tail call i32* @getcwd(i8* %1, i64 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %14
6: ; preds = %2
%7 = add i64 %3, 10
%8 = trunc i64 %7 to i32
%9 = tail call i32* @realloc(i8* %1, i32 %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %2
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %16
14: ; preds = %2
%15 = tail call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 1)
br label %16
16: ; preds = %14, %11
ret i8* %1
}