#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(int /*<<< orphan*/, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ stat(char *, struct stat *);
char *strdup(char *);
char *get_current_dir_name(void) {
  struct stat a, b;
  char *res = getenv("PWD");
  if (res && *res && !stat(res, &a) && !stat(".", &b) &&
      (a.st_dev == b.st_dev) && (a.st_ino == b.st_ino))
    return strdup(res);
  return getcwd(0, 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,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11d0 <get_current_dir_name+0x90>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11d0 <get_current_dir_name+0x90>
mov    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%eax
jne    11d0 <get_current_dir_name+0x90>
lea    0xe74(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%eax
jne    11d0 <get_current_dir_name+0x90>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11d0 <get_current_dir_name+0x90>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11d0 <get_current_dir_name+0x90>
mov    -0x30(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11dd <get_current_dir_name+0x9d>
xor    %esi,%esi
mov    %esi,%edi
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     11a6 <get_current_dir_name+0x66>
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     11a6 <get_current_dir_name+0x66>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %eax,%eax
jne    11a6 <get_current_dir_name+0x66>
lea    0xe8e(%rip),%rdi
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %eax,%eax
jne    11a6 <get_current_dir_name+0x66>
mov    0x10(%rsp),%rax
cmp    (%rsp),%rax
jne    11a6 <get_current_dir_name+0x66>
mov    0x18(%rsp),%rax
cmp    0x8(%rsp),%rax
jne    11a6 <get_current_dir_name+0x66>
mov    %rbx,%rdi
add    $0x20,%rsp
pop    %rbx
jmp    1060 <strdup@plt>
xor    %edi,%edi
xor    %esi,%esi
add    $0x20,%rsp
pop    %rbx
jmp    1040 <getcwd@plt>

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 %struct.stat
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %4
%6 = load i8*, i8** %4
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %35
8: ; preds = %0
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %35
13: ; preds = %8
%14 = load i8*, i8** %4
%15 = call i32 @stat(i8* %14, %struct.stat* %2)
%16 = icmp ne i32 %15, 0
br i1 %16, label %35, label %17
17: ; preds = %13
%18 = call i32 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%19 = icmp ne i32 %18, 0
br i1 %19, label %35, label %20
20: ; preds = %17
%21 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%22 = load i64, i64* %21
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%24 = load i64, i64* %23
%25 = icmp eq i64 %22, %24
br i1 %25, label %26, label %35
26: ; preds = %20
%27 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 1
%28 = load i64, i64* %27
%29 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%30 = load i64, i64* %29
%31 = icmp eq i64 %28, %30
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i8*, i8** %4
%34 = call i8* @strdup(i8* %33)
store i8* %34, i8** %1
br label %37
35: ; preds = %26, %20, %17, %13, %8, %0
%36 = call i8* @getcwd(i32 0, i32 0)
store i8* %36, i8** %1
br label %37
37: ; preds = %35, %32
%38 = load i8*, i8** %1
ret i8* %38
}

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 %30, label %7
7: ; preds = %0
%8 = load i8, i8* %5
%9 = icmp eq i8 %8, 0
br i1 %9, label %30, label %10
10: ; preds = %7
%11 = call i32 @stat(i8* %5, %struct.stat* %1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = call i32 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %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* %5)
br label %32
30: ; preds = %22, %16, %13, %10, %7, %0
%31 = tail call i8* @getcwd(i32 0, i32 0)
br label %32
32: ; preds = %30, %28
%33 = phi i8* [ %31, %30 ], [ %29, %28 ]
ret i8* %33
}