#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 */
int /*<<< orphan*/ ENOENT;
int /*<<< orphan*/ errno;
char *getenv(char *);
int stat(char *, struct stat *);
char *getcwd_logical(void) {
  struct stat lg, phy;
  char *pwd;
  if ((pwd = getenv("PWD")) != NULL && *pwd == '/') {
    if (stat(pwd, &lg) == -1 || stat(".", &phy) == -1)
      return (NULL);
    if (lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
      return (pwd);
  }
  errno = ENOENT;
  return (NULL);
}


x86 assembly (clang, -O0):
<getcwd_logical>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xed1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11bc <getcwd_logical+0x9c>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11bc <getcwd_logical+0x9c>
mov    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     1181 <getcwd_logical+0x61>
lea    0xe95(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    118e <getcwd_logical+0x6e>
movq   $0x0,-0x8(%rbp)
jmp    11d6 <getcwd_logical+0xb6>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11b7 <getcwd_logical+0x97>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11b7 <getcwd_logical+0x97>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d6 <getcwd_logical+0xb6>
jmp    11bc <getcwd_logical+0x9c>
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e14(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd_logical>:
push   %rbx
sub    $0x20,%rsp
lea    0xed4(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     117b <getcwd_logical+0x5b>
mov    %rax,%rbx
cmpb   $0x2f,(%rax)
jne    117b <getcwd_logical+0x5b>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     118d <getcwd_logical+0x6d>
lea    0xead(%rip),%rdi
mov    %rsp,%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     118d <getcwd_logical+0x6d>
mov    0x10(%rsp),%rax
cmp    (%rsp),%rax
jne    117b <getcwd_logical+0x5b>
mov    0x18(%rsp),%rax
cmp    0x8(%rsp),%rax
je     118f <getcwd_logical+0x6f>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    0x2e55(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
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"
@ENOENT = global i32 0
@errno = global i32 0
define i8* @getcwd_logical() {
%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 = icmp ne i8* %5, null
br i1 %6, label %7, label %35
7: ; preds = %0
%8 = load i8*, i8** %4
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 47
br i1 %11, label %12, label %35
12: ; preds = %7
%13 = load i8*, i8** %4
%14 = call i32 @stat(i8* %13, %struct.stat* %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = call i32 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %16, %12
store i8* null, i8** %1
br label %37
20: ; preds = %16
%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 %34
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 %34
32: ; preds = %26
%33 = load i8*, i8** %4
store i8* %33, i8** %1
br label %37
34: ; preds = %26, %20
br label %35
35: ; preds = %34, %7, %0
%36 = load i32, i32* @ENOENT
store i32 %36, i32* @errno
store i8* null, i8** %1
br label %37
37: ; preds = %35, %32, %19
%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"
@ENOENT = global i32 0
@errno = global i32 0
define i8* @getcwd_logical() {
%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 %28, label %7
7: ; preds = %0
%8 = load i8, i8* %5
%9 = icmp eq i8 %8, 47
br i1 %9, label %10, label %28
10: ; preds = %7
%11 = call i32 @stat(i8* %5, %struct.stat* %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %30, label %13
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, -1
br i1 %15, label %30, label %16
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 %28
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 %30, label %28
28: ; preds = %16, %22, %7, %0
%29 = load i32, i32* @ENOENT
store i32 %29, i32* @errno
br label %30
30: ; preds = %22, %10, %13, %28
%31 = phi i8* [ null, %28 ], [ null, %13 ], [ null, %10 ], [ %5, %22 ]
ret i8* %31
}