#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*/ working_directory_path;

/* Variables and functions */
int /*<<< orphan*/ *getcwd(char *, int);
int /*<<< orphan*/ perror(char *);
char *strchr(char *, char);
char *strdup(char *);
char *get_working_directory() {
  char working_directory_path[2048];
  if (getcwd(working_directory_path, sizeof(working_directory_path)) != NULL) {
    char *file_name = working_directory_path;
    while (strchr(file_name, '/') != NULL) {
      file_name = strchr(file_name, '/') + sizeof(char);
    }
    return strdup(file_name);
  }
  perror("getcwd");
  return NULL;
}


x86 assembly (clang, -O0):
<get_working_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
lea    -0x810(%rbp),%rdi
mov    $0x800,%esi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
je     11c5 <get_working_directory+0x85>
lea    -0x810(%rbp),%rax
mov    %rax,-0x818(%rbp)
mov    -0x818(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11b0 <get_working_directory+0x70>
mov    -0x818(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
add    $0x1,%rax
mov    %rax,-0x818(%rbp)
jmp    1174 <get_working_directory+0x34>
mov    -0x818(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11d9 <get_working_directory+0x99>
lea    0xe34(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_working_directory>:
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x800,%esi
call   1040 <getcwd@plt>
test   %rax,%rax
je     118c <get_working_directory+0x4c>
nop
mov    %r14,%rbx
mov    %r14,%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
lea    0x1(%rax),%r14
test   %rax,%rax
jne    1160 <get_working_directory+0x20>
mov    %rbx,%rdi
call   1060 <strdup@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    1181 <get_working_directory+0x41>

LLVM IR (-O0):
@.str = [7 x i8] c"getcwd\00"
define i8* @get_working_directory() {
%1 = alloca i8*
%2 = alloca [2048 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = call i32* @getcwd(i8* %4, i32 2048)
%6 = icmp ne i32* %5, null
br i1 %6, label %7, label %20
7: ; preds = %0
%8 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
store i8* %8, i8** %3
br label %9
9: ; preds = %13, %7
%10 = load i8*, i8** %3
%11 = call i8* @strchr(i8* %10, i8 signext 47)
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = call i8* @strchr(i8* %14, i8 signext 47)
%16 = getelementptr inbounds i8, i8* %15, i64 1
store i8* %16, i8** %3
br label %9
17: ; preds = %9
%18 = load i8*, i8** %3
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %1
br label %22
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %1
br label %22
22: ; preds = %20, %17
%23 = load i8*, i8** %1
ret i8* %23
}

LLVM IR (-O2):
@.str = [7 x i8] c"getcwd\00"
define noalias i8* @get_working_directory() {
%1 = alloca [2048 x i8]
%2 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%3 = call i32* @getcwd(i8* %2, i32 2048)
%4 = icmp eq i32* %3, null
br i1 %4, label %12, label %5
5: ; preds = %0, %5
%6 = phi i8* [ %9, %5 ], [ %2, %0 ]
%7 = call i8* @strchr(i8* dereferenceable(1) %6, i8 signext 47)
%8 = icmp eq i8* %7, null
%9 = getelementptr inbounds i8, i8* %7, i64 1
br i1 %8, label %10, label %5
10: ; preds = %5
%11 = call i8* @strdup(i8* %6)
br label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %10
%15 = phi i8* [ %11, %10 ], [ null, %12 ]
ret i8* %15
}