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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ getcwd(char *, int);
char *getenv(char *);
int /*<<< orphan*/ stat(char *, struct stat *);
char *strdup(char *);
char *get_current_dir_name(void) {
  struct stat a, b;
  char buf[PATH_MAX];
  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);
  if (!getcwd(buf, sizeof(buf)))
    return NULL;
  return strdup(buf);
}


x86 assembly (clang, -O0):
<get_current_dir_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%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,-0x38(%rbp)
lea    0xe81(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1207 <get_current_dir_name+0xc7>
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1207 <get_current_dir_name+0xc7>
mov    -0x40(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%eax
jne    1207 <get_current_dir_name+0xc7>
lea    0xe44(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%eax
jne    1207 <get_current_dir_name+0xc7>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    1207 <get_current_dir_name+0xc7>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1207 <get_current_dir_name+0xc7>
mov    -0x40(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1247 <get_current_dir_name+0x107>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
call   1040 <getcwd@plt>
cmp    $0x0,%eax
jne    1233 <get_current_dir_name+0xf3>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1247 <get_current_dir_name+0x107>
mov    -0x50(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%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   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsp,%r12
mov    0x2e97(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe8c(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11c3 <get_current_dir_name+0x83>
mov    %rax,%r15
cmpb   $0x0,(%rax)
je     11c3 <get_current_dir_name+0x83>
lea    -0x40(%rbp),%rsi
mov    %r15,%rdi
call   1050 <stat@plt>
test   %eax,%eax
jne    11c3 <get_current_dir_name+0x83>
lea    0xe67(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
test   %eax,%eax
jne    11c3 <get_current_dir_name+0x83>
mov    -0x40(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    11c3 <get_current_dir_name+0x83>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11c3 <get_current_dir_name+0x83>
mov    %r15,%rdi
jmp    11d5 <get_current_dir_name+0x95>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <getcwd@plt>
test   %eax,%eax
je     11dc <get_current_dir_name+0x9c>
mov    %rbx,%rdi
call   1060 <strdup@plt>
jmp    11de <get_current_dir_name+0x9e>
xor    %eax,%eax
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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