#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 */
typedef int /*<<< orphan*/ cwd;

/* Variables and functions */
int /*<<< orphan*/ erase();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *getcwd(char *, int);
char *malloc(scalar_t__);
int /*<<< orphan*/ printw(char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *currentDirectory(void) {
  char cwd[1024];
  char *returnedDirectory;
  if (getcwd(cwd, sizeof(cwd)) != NULL) {
    returnedDirectory = malloc(strlen(cwd) + 1);
    strcpy(returnedDirectory, cwd);
  } else {
    erase();
    printw("Error, could not find current directory");
    exit(1);
  }
  return returnedDirectory;
}


x86 assembly (clang, -O0):
<currentDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
lea    -0x400(%rbp),%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
cmp    $0x0,%rax
je     11cd <currentDirectory+0x5d>
lea    -0x400(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x408(%rbp)
mov    -0x408(%rbp),%rdi
lea    -0x400(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11ea <currentDirectory+0x7a>
mov    $0x0,%al
call   1050 <erase@plt>
lea    0xe25(%rip),%rdi
call   1070 <printw@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x408(%rbp),%rax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<currentDirectory>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     11bc <currentDirectory+0x4c>
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1050 <erase@plt>
lea    0xe36(%rip),%rdi
call   1070 <printw@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"Error, could not find current directory\00"
define i8* @currentDirectory() {
%1 = alloca [1024 x i8]
%2 = alloca i8*
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = call i32* @getcwd(i8* %3, i32 1024)
%5 = icmp ne i32* %4, null
br i1 %5, label %6, label %14
6: ; preds = %0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%8 = call i64 @strlen(i8* %7)
%9 = add nsw i64 %8, 1
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%13 = call i32 @strcpy(i8* %11, i8* %12)
br label %18
14: ; preds = %0
%15 = call i32 (...) @erase()
%16 = call i32 @printw(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %6
%19 = load i8*, i8** %2
ret i8* %19
}

LLVM IR (-O2):
@.str = [40 x i8] c"Error, could not find current directory\00"
define i8* @currentDirectory() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32* @getcwd(i8* %2, i32 1024)
%4 = icmp eq i32* %3, null
br i1 %4, label %10, label %5
5: ; preds = %0
%6 = call i64 @strlen(i8* %2)
%7 = add nsw i64 %6, 1
%8 = call i8* @malloc(i64 %7)
%9 = call i32 @strcpy(i8* %8, i8* %2)
ret i8* %8
10: ; preds = %0
%11 = call i32 (...) @erase()
%12 = call i32 @printw(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
}