#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_ino;
  scalar_t__ st_dev;
};

/* Variables and functions */
int errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getcwd(char *, size_t);
char *getenv(char *);
scalar_t__ stat(char *, struct stat *);
char *xmalloc(size_t);
char *getpwd() {
  static char *pwd;
  static int failure_errno;
  char *p = pwd;
  size_t s;
  struct stat dotstat, pwdstat;
  if (!p && !(errno = failure_errno)) {
    if (!((p = getenv("PWD")) != 0 && *p == '/' && stat(p, &pwdstat) == 0 &&
          stat(".", &dotstat) == 0 && dotstat.st_ino == pwdstat.st_ino &&
          dotstat.st_dev == pwdstat.st_dev))
      for (s = 100; !getcwd(p = xmalloc(s), s); s *= 2) {
        int e = errno;
        free(p);
        {
          errno = failure_errno = e;
          p = 0;
          break;
        }
      }
    pwd = p;
  }
  return p;
}


x86 assembly (clang, -O0):
<getpwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2ef1(%rip),%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1272 <getpwd+0x122>
mov    0x2ee4(%rip),%eax
mov    0x2e65(%rip),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jne    1272 <getpwd+0x122>
lea    0xe73(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     11fd <getpwd+0xad>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11fd <getpwd+0xad>
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    11fd <getpwd+0xad>
lea    0xe36(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    11fd <getpwd+0xad>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    11fd <getpwd+0xad>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1267 <getpwd+0x117>
movq   $0x64,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <xmalloc@plt>
mov    %rax,%rdi
mov    %rdi,-0x8(%rbp)
mov    -0x10(%rbp),%rsi
call   1050 <getcwd@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1233 <getpwd+0xe3>
jmp    1262 <getpwd+0x112>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <free@plt>
mov    -0x34(%rbp),%ecx
mov    %ecx,0x2e07(%rip)
mov    0x2d88(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1267 <getpwd+0x117>
mov    -0x8(%rbp),%rax
mov    %rax,0x2dde(%rip)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpwd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2ef1(%rip),%rbx
test   %rbx,%rbx
jne    1214 <getpwd+0xc4>
mov    0x2eea(%rip),%eax
mov    0x2e6b(%rip),%r14
mov    %eax,(%r14)
test   %eax,%eax
je     1183 <getpwd+0x33>
xor    %ebx,%ebx
jmp    1214 <getpwd+0xc4>
lea    0xe76(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d9 <getpwd+0x89>
mov    %rax,%rbx
cmpb   $0x2f,(%rax)
jne    11d9 <getpwd+0x89>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
jne    11d9 <getpwd+0x89>
lea    0xe51(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    11d9 <getpwd+0x89>
mov    0x10(%rsp),%rax
cmp    (%rsp),%rax
jne    11d9 <getpwd+0x89>
mov    0x18(%rsp),%rax
cmp    0x8(%rsp),%rax
je     120d <getpwd+0xbd>
mov    $0x64,%edi
call   1060 <xmalloc@plt>
mov    %rax,%rbx
mov    $0x64,%esi
mov    %rax,%rdi
call   1050 <getcwd@plt>
test   %eax,%eax
jne    120d <getpwd+0xbd>
mov    (%r14),%ebp
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %ebp,0x2e50(%rip)
mov    %ebp,(%r14)
xor    %ebx,%ebx
mov    %rbx,0x2e3c(%rip)
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@getpwd.pwd = internal global i8* null
@getpwd.failure_errno = internal global i32 0
@errno = global i32 0
@.str = [4 x i8] c"PWD\00"
@.str.1 = [2 x i8] c".\00"
define i8* @getpwd() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca i32
%6 = load i8*, i8** @getpwd.pwd
store i8* %6, i8** %1
%7 = load i8*, i8** %1
%8 = icmp ne i8* %7, null
br i1 %8, label %58, label %9
9: ; preds = %0
%10 = load i32, i32* @getpwd.failure_errno
store i32 %10, i32* @errno
%11 = icmp ne i32 %10, 0
br i1 %11, label %58, label %12
12: ; preds = %9
%13 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %1
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %39
15: ; preds = %12
%16 = load i8*, i8** %1
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 47
br i1 %19, label %20, label %39
20: ; preds = %15
%21 = load i8*, i8** %1
%22 = call i64 @stat(i8* %21, %struct.stat* %4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %39
24: ; preds = %20
%25 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %39
27: ; preds = %24
%28 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%29 = load i64, i64* %28
%30 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%31 = load i64, i64* %30
%32 = icmp eq i64 %29, %31
br i1 %32, label %33, label %39
33: ; preds = %27
%34 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%35 = load i64, i64* %34
%36 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%37 = load i64, i64* %36
%38 = icmp eq i64 %35, %37
br i1 %38, label %56, label %39
39: ; preds = %33, %27, %24, %20, %15, %12
store i64 100, i64* %2
br label %40
40: ; preds = %52, %39
%41 = load i64, i64* %2
%42 = call i8* @xmalloc(i64 %41)
store i8* %42, i8** %1
%43 = load i64, i64* %2
%44 = call i32 @getcwd(i8* %42, i64 %43)
%45 = icmp ne i32 %44, 0
%46 = xor i1 %45, true
br i1 %46, label %47, label %55
47: ; preds = %40
%48 = load i32, i32* @errno
store i32 %48, i32* %5
%49 = load i8*, i8** %1
%50 = call i32 @free(i8* %49)
%51 = load i32, i32* %5
store i32 %51, i32* @getpwd.failure_errno
store i32 %51, i32* @errno
store i8* null, i8** %1
br label %55
52: ; No predecessors!
%53 = load i64, i64* %2
%54 = mul i64 %53, 2
store i64 %54, i64* %2
br label %40
55: ; preds = %47, %40
br label %56
56: ; preds = %55, %33
%57 = load i8*, i8** %1
store i8* %57, i8** @getpwd.pwd
br label %58
58: ; preds = %56, %9, %0
%59 = load i8*, i8** %1
ret i8* %59
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@getpwd.pwd = internal unnamed_addr global i8* null
@getpwd.failure_errno = internal unnamed_addr global i32 0
@errno = global i32 0
@.str = [4 x i8] c"PWD\00"
@.str.1 = [2 x i8] c".\00"
define i8* @getpwd() {
%1 = alloca %struct.stat
%2 = alloca %struct.stat
%3 = load i8*, i8** @getpwd.pwd
%4 = bitcast %struct.stat* %1 to i8*
%5 = bitcast %struct.stat* %2 to i8*
%6 = icmp eq i8* %3, null
br i1 %6, label %7, label %43
7: ; preds = %0
%8 = load i32, i32* @getpwd.failure_errno
store i32 %8, i32* @errno
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %43
10: ; preds = %7
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %34, label %13
13: ; preds = %10
%14 = load i8, i8* %11
%15 = icmp eq i8 %14, 47
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = call i64 @stat(i8* %11, %struct.stat* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %34
19: ; preds = %16
%20 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %34
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 %34
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
br i1 %33, label %41, label %34
34: ; preds = %28, %22, %19, %16, %13, %10
%35 = call i8* @xmalloc(i64 100)
%36 = call i32 @getcwd(i8* %35, i64 100)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @errno
%40 = call i32 @free(i8* %35)
store i32 %39, i32* @getpwd.failure_errno
store i32 %39, i32* @errno
br label %41
41: ; preds = %34, %38, %28
%42 = phi i8* [ %11, %28 ], [ null, %38 ], [ %35, %34 ]
store i8* %42, i8** @getpwd.pwd
br label %43
43: ; preds = %41, %7, %0
%44 = phi i8* [ %3, %0 ], [ null, %7 ], [ %42, %41 ]
ret i8* %44
}