#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*/ eprintf(char *, char const *);
char *getenv(char *);
scalar_t__ stat(char const *, struct stat *);
const char *getpwd(const char *cwd) {
  const char *pwd;
  struct stat cst, pst;
  if (!(pwd = getenv("PWD")) || pwd[0] != '/' || stat(pwd, &pst) < 0)
    return cwd;
  if (stat(cwd, &cst) < 0)
    eprintf("stat %s:", cwd);
  return (pst.st_dev == cst.st_dev && pst.st_ino == cst.st_ino) ? pwd : cwd;
}


x86 assembly (clang, -O0):
<getpwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xebd(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     117d <getpwd+0x4d>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    117d <getpwd+0x4d>
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    118a <getpwd+0x5a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ea <getpwd+0xba>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11b1 <getpwd+0x81>
mov    -0x10(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1050 <eprintf@plt>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11da <getpwd+0xaa>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11da <getpwd+0xaa>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11e2 <getpwd+0xb2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpwd>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
lea    0xebf(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     119e <getpwd+0x6e>
mov    %rax,%rbx
cmpb   $0x2f,(%rax)
jne    119e <getpwd+0x6e>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
js     119e <getpwd+0x6e>
lea    0x18(%rsp),%rsi
mov    %r14,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jns    1186 <getpwd+0x56>
lea    0xe86(%rip),%rdi
mov    %r14,%rsi
call   1050 <eprintf@plt>
mov    0x8(%rsp),%rax
cmp    0x18(%rsp),%rax
jne    119e <getpwd+0x6e>
mov    0x10(%rsp),%rax
cmp    0x20(%rsp),%rax
je     11a1 <getpwd+0x71>
mov    %r14,%rbx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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