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

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EROFS;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ access(char const *, int /*<<< orphan*/);
scalar_t__ chdir(char const *);
int /*<<< orphan*/ die(char *);
scalar_t__ errno;
scalar_t__ getcwd(char *, int);
int /*<<< orphan*/ xchdir(char *);
int exists(const char *dirname, const char *filename) {
  char cwd[4096];
  int does_exist;
  if (getcwd(cwd, sizeof(cwd)) == 0) {
    die("cwd buffer to small");
  }
  does_exist = 1;
  if (chdir(dirname) != 0) {
    does_exist = 0;
  }
  if (does_exist && (access(filename, O_RDONLY) < 0)) {
    if ((errno != EACCES) && (errno != EROFS)) {
      does_exist = 0;
    }
  }
  xchdir(cwd);
  return does_exist;
}


x86 assembly (clang, -O0):
<exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
jne    118a <exists+0x3a>
lea    0xe7b(%rip),%rdi
call   1070 <die@plt>
movl   $0x1,-0x1014(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     11b1 <exists+0x61>
movl   $0x0,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
je     121d <exists+0xcd>
mov    -0x10(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jge    121d <exists+0xcd>
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%rax
je     1218 <exists+0xc8>
mov    0x2dcd(%rip),%rax
mov    (%rax),%rax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%rax
je     1218 <exists+0xc8>
movl   $0x0,-0x1014(%rbp)
jmp    121d <exists+0xcd>
lea    -0x1010(%rbp),%rdi
call   1030 <xchdir@plt>
mov    -0x1014(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exists>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1050 <getcwd@plt>
test   %rax,%rax
jne    117e <exists+0x2e>
lea    0xe87(%rip),%rdi
call   1070 <die@plt>
mov    %rbx,%rdi
call   1040 <chdir@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    11d1 <exists+0x81>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <access@plt>
mov    $0x1,%ebx
test   %rax,%rax
jns    11d1 <exists+0x81>
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
sete   %cl
mov    0x2e22(%rip),%rdx
cmp    (%rdx),%rax
sete   %al
or     %cl,%al
movzbl %al,%ebx
mov    %rsp,%rdi
call   1030 <xchdir@plt>
mov    %ebx,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"cwd buffer to small\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EROFS = global i64 0
define i32 @exists(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [4096 x i8]
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = call i64 @getcwd(i8* %7, i32 4096)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
store i32 1, i32* %6
%13 = load i8*, i8** %3
%14 = call i64 @chdir(i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 0, i32* %6
br label %17
17: ; preds = %16, %12
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %35
20: ; preds = %17
%21 = load i8*, i8** %4
%22 = load i32, i32* @O_RDONLY
%23 = call i64 @access(i8* %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EACCES
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EROFS
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 0, i32* %6
br label %34
34: ; preds = %33, %29, %25
br label %35
35: ; preds = %34, %20, %17
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%37 = call i32 @xchdir(i8* %36)
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
@.str = [20 x i8] c"cwd buffer to small\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EROFS = global i64 0
define i32 @exists(i8* %0, i8* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i64 @getcwd(i8* %4, i32 4096)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = call i64 @chdir(i8* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %9
%13 = load i32, i32* @O_RDONLY
%14 = call i64 @access(i8* %1, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @EACCES
%19 = icmp eq i64 %17, %18
%20 = load i64, i64* @EROFS
%21 = icmp eq i64 %17, %20
%22 = select i1 %19, i1 true, i1 %21
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %16, %12, %9
%25 = phi i32 [ 1, %12 ], [ 0, %9 ], [ %23, %16 ]
%26 = call i32 @xchdir(i8* %4)
ret i32 %25
}