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

/* Variables and functions */
char *getcwd(char *, int /*<<< orphan*/);
char *getenv(char *);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
char *get_current_dir_name(void) {
  char *pwd;
  struct stat dotstat, pwdstat;
  pwd = getenv("PWD");
  if (pwd != NULL && stat(".", &dotstat) == 0 && stat(pwd, &pwdstat) == 0 &&
      pwdstat.st_dev == dotstat.st_dev && pwdstat.st_ino == dotstat.st_ino)
    return strdup(pwd);
  return getcwd((char *)NULL, 0);
}


x86 assembly (clang, -O0):
<get_current_dir_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c2 <get_current_dir_name+0x82>
lea    0xe9a(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    11c2 <get_current_dir_name+0x82>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    11c2 <get_current_dir_name+0x82>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11c2 <get_current_dir_name+0x82>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11c2 <get_current_dir_name+0x82>
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11d1 <get_current_dir_name+0x91>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1040 <getcwd@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_current_dir_name>:
push   %rbx
sub    $0x20,%rsp
lea    0xeb4(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11a0 <get_current_dir_name+0x60>
mov    %rax,%rbx
lea    0xea4(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    11a0 <get_current_dir_name+0x60>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    11a0 <get_current_dir_name+0x60>
mov    (%rsp),%rax
cmp    0x10(%rsp),%rax
jne    11a0 <get_current_dir_name+0x60>
mov    0x8(%rsp),%rax
cmp    0x18(%rsp),%rax
jne    11a0 <get_current_dir_name+0x60>
mov    %rbx,%rdi
call   1060 <strdup@plt>
jmp    11a9 <get_current_dir_name+0x69>
xor    %edi,%edi
xor    %esi,%esi
call   1040 <getcwd@plt>
add    $0x20,%rsp
pop    %rbx
ret

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

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