#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*/ tmp;
struct stat {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
};

/* Variables and functions */
int PATH_MAX;
scalar_t__ getcwd(char *, int);
char *getenv(char *);
int /*<<< orphan*/ stat(char *, struct stat *);
char *strdup(char *);
char *get_current_dir_name(void) {
  char *pwd = getenv("PWD");
  char tmp[PATH_MAX];
  struct stat a, b;
  if (pwd && !stat(".", &a) && !stat(pwd, &b) && a.st_dev == b.st_dev &&
      a.st_ino == b.st_ino)
    return strdup(pwd);
  if (getcwd(tmp, sizeof(tmp)))
    return strdup(tmp);
  return 0;
}


x86 assembly (clang, -O0):
<get_current_dir_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f7 <get_current_dir_name+0xb7>
lea    0xe6a(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%eax
jne    11f7 <get_current_dir_name+0xb7>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%eax
jne    11f7 <get_current_dir_name+0xb7>
mov    -0x30(%rbp),%rax
cmp    -0x40(%rbp),%rax
jne    11f7 <get_current_dir_name+0xb7>
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jne    11f7 <get_current_dir_name+0xb7>
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1238 <get_current_dir_name+0xf8>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
je     1229 <get_current_dir_name+0xe9>
mov    -0x50(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1238 <get_current_dir_name+0xf8>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_current_dir_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xeac(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
mov    0x2e8d(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %r15,%r15
je     11c6 <get_current_dir_name+0x86>
lea    0xe81(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   1050 <stat@plt>
test   %eax,%eax
jne    11c6 <get_current_dir_name+0x86>
lea    -0x28(%rbp),%rsi
mov    %r15,%rdi
call   1050 <stat@plt>
test   %eax,%eax
jne    11c6 <get_current_dir_name+0x86>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11c6 <get_current_dir_name+0x86>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11c6 <get_current_dir_name+0x86>
mov    %r15,%rdi
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <strdup@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <getcwd@plt>
test   %rax,%rax
je     11e0 <get_current_dir_name+0xa0>
mov    %rbx,%rdi
call   1060 <strdup@plt>
jmp    11e2 <get_current_dir_name+0xa2>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@.str = [4 x i8] c"PWD\00"
@PATH_MAX = global i32 0
@.str.1 = [2 x i8] c".\00"
define i8* @get_current_dir_name() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.stat
%6 = alloca %struct.stat
%7 = alloca i32
%8 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %2
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
%13 = load i8*, i8** %2
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %37
15: ; preds = %0
%16 = call i32 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %5)
%17 = icmp ne i32 %16, 0
br i1 %17, label %37, label %18
18: ; preds = %15
%19 = load i8*, i8** %2
%20 = call i32 @stat(i8* %19, %struct.stat* %6)
%21 = icmp ne i32 %20, 0
br i1 %21, label %37, label %22
22: ; preds = %18
%23 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i64, i64* %25
%27 = icmp eq i64 %24, %26
br i1 %27, label %28, label %37
28: ; preds = %22
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%30 = load i64, i64* %29
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%32 = load i64, i64* %31
%33 = icmp eq i64 %30, %32
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i8*, i8** %2
%36 = call i8* @strdup(i8* %35)
store i8* %36, i8** %1
store i32 1, i32* %7
br label %44
37: ; preds = %28, %22, %18, %15, %0
%38 = trunc i64 %10 to i32
%39 = call i64 @getcwd(i8* %12, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i8* @strdup(i8* %12)
store i8* %42, i8** %1
store i32 1, i32* %7
br label %44
43: ; preds = %37
store i8* null, i8** %1
store i32 1, i32* %7
br label %44
44: ; preds = %43, %41, %34
%45 = load i8*, i8** %3
%46 = load i8*, i8** %1
ret i8* %46
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@.str = [4 x i8] c"PWD\00"
@PATH_MAX = global i32 0
@.str.1 = [2 x i8] c".\00"
define noalias i8* @get_current_dir_name() {
%1 = alloca %struct.stat
%2 = alloca %struct.stat
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %1 to i8*
%8 = bitcast %struct.stat* %2 to i8*
%9 = icmp eq i8* %3, null
br i1 %9, label %30, label %10
10: ; preds = %0
%11 = call i32 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = call i32 @stat(i8* %3, %struct.stat* %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%18 = load i64, i64* %17
%19 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%20 = load i64, i64* %19
%21 = icmp eq i64 %18, %20
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%26 = load i64, i64* %25
%27 = icmp eq i64 %24, %26
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = tail call i8* @strdup(i8* %3)
br label %35
30: ; preds = %22, %16, %13, %10, %0
%31 = call i64 @getcwd(i8* %6, i32 %4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i8* @strdup(i8* %6)
br label %35
35: ; preds = %30, %33, %28
%36 = phi i8* [ %34, %33 ], [ %29, %28 ], [ null, %30 ]
ret i8* %36
}