#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 *getenv(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ *strstr(char *, char *);
char *getcwd_logical(void) {
  char *pwd;
  struct stat s_pwd, s_dot;
  pwd = getenv("PWD");
  if (pwd == NULL)
    return NULL;
  if (pwd[0] != '/')
    return NULL;
  if (strstr(pwd, "/./") != NULL)
    return NULL;
  if (strstr(pwd, "/../") != NULL)
    return NULL;
  if (stat(pwd, &s_pwd) == -1 || stat(".", &s_dot) == -1)
    return NULL;
  if (s_pwd.st_dev != s_dot.st_dev || s_pwd.st_ino != s_dot.st_ino)
    return NULL;
  return pwd;
}


x86 assembly (clang, -O0):
<getcwd_logical>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xec1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1160 <getcwd_logical+0x30>
movq   $0x0,-0x8(%rbp)
jmp    1238 <getcwd_logical+0x108>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     117d <getcwd_logical+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    1238 <getcwd_logical+0x108>
mov    -0x10(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1050 <strstr@plt>
cmp    $0x0,%rax
je     11a4 <getcwd_logical+0x74>
movq   $0x0,-0x8(%rbp)
jmp    1238 <getcwd_logical+0x108>
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1050 <strstr@plt>
cmp    $0x0,%rax
je     11cb <getcwd_logical+0x9b>
movq   $0x0,-0x8(%rbp)
jmp    1238 <getcwd_logical+0x108>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     11fa <getcwd_logical+0xca>
lea    0xe25(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    1207 <getcwd_logical+0xd7>
movq   $0x0,-0x8(%rbp)
jmp    1238 <getcwd_logical+0x108>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    1223 <getcwd_logical+0xf3>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1230 <getcwd_logical+0x100>
movq   $0x0,-0x8(%rbp)
jmp    1238 <getcwd_logical+0x108>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd_logical>:
push   %rbx
sub    $0x20,%rsp
lea    0xec4(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1176 <getcwd_logical+0x46>
mov    %rax,%rbx
cmpb   $0x2f,(%rax)
jne    1176 <getcwd_logical+0x46>
lea    0xeaf(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strstr@plt>
test   %rax,%rax
jne    1176 <getcwd_logical+0x46>
lea    0xe9f(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strstr@plt>
test   %rax,%rax
je     117e <getcwd_logical+0x4e>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     1176 <getcwd_logical+0x46>
lea    0xe76(%rip),%rdi
mov    %rsp,%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     1176 <getcwd_logical+0x46>
mov    0x10(%rsp),%rax
cmp    (%rsp),%rax
jne    1176 <getcwd_logical+0x46>
mov    0x18(%rsp),%rcx
xor    %eax,%eax
cmp    0x8(%rsp),%rcx
cmove  %rbx,%rax
jmp    1178 <getcwd_logical+0x48>

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

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@.str = [4 x i8] c"PWD\00"
@.str.1 = [4 x i8] c"/./\00"
@.str.2 = [5 x i8] c"/../\00"
@.str.3 = [2 x i8] c".\00"
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 %35, label %7
7: ; preds = %0
%8 = load i8, i8* %5
%9 = icmp eq i8 %8, 47
br i1 %9, label %10, label %35
10: ; preds = %7
%11 = tail call i32* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %35
13: ; preds = %10
%14 = tail call i32* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %35
16: ; preds = %13
%17 = call i32 @stat(i8* %5, %struct.stat* %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %35, label %19
19: ; preds = %16
%20 = call i32 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), %struct.stat* %2)
%21 = icmp eq i32 %20, -1
br i1 %21, label %35, label %22
22: ; preds = %19
%23 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%26 = load i64, i64* %25
%27 = icmp eq i64 %24, %26
br i1 %27, label %28, label %35
28: ; preds = %22
%29 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%30 = load i64, i64* %29
%31 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%32 = load i64, i64* %31
%33 = icmp eq i64 %30, %32
%34 = select i1 %33, i8* %5, i8* null
br label %35
35: ; preds = %28, %22, %16, %19, %13, %10, %7, %0
%36 = phi i8* [ null, %0 ], [ null, %7 ], [ null, %10 ], [ null, %13 ], [ null, %19 ], [ null, %16 ], [ null, %22 ], [ %34, %28 ]
ret i8* %36
}