#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 */

/* Variables and functions */
scalar_t__ EACCES;
int /*<<< orphan*/ O_RDWR;
int close(int);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/);
int isDiskExtant(char *filename) {
  int fd = -1;
  if ((fd = open(filename, O_RDWR)) == -1) {
    if (errno == EACCES) {
      return 0;
    }
    return -1;
  }
  if (close(fd) == -1) {
    return -1;
  }
  return 1;
}


x86 assembly (clang, -O0):
<isDiskExtant>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1183 <isDiskExtant+0x63>
mov    0x2e78(%rip),%rax
mov    (%rax),%rax
mov    0x2e7e(%rip),%rcx
cmp    (%rcx),%rax
jne    1177 <isDiskExtant+0x57>
movl   $0x0,-0x4(%rbp)
jmp    11a7 <isDiskExtant+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <isDiskExtant+0x87>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    11a0 <isDiskExtant+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <isDiskExtant+0x87>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isDiskExtant>:
push   %rax
mov    0x2ec0(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     114b <isDiskExtant+0x2b>
mov    %eax,%edi
call   1030 <close@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
setne  %cl
lea    (%rcx,%rcx,1),%eax
add    $0xffffffff,%eax
pop    %rcx
ret
mov    0x2e7e(%rip),%rax
mov    (%rax),%rcx
mov    0x2e84(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
pop    %rcx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@errno = global i64 0
@EACCES = global i64 0
define i32 @isDiskExtant(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %4
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %4
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @EACCES
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 0, i32* %2
br label %21
14: ; preds = %9
store i32 -1, i32* %2
br label %21
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call i32 @close(i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %2
br label %21
20: ; preds = %15
store i32 1, i32* %2
br label %21
21: ; preds = %20, %19, %14, %13
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@errno = global i64 0
@EACCES = global i64 0
define i32 @isDiskExtant(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = load i64, i64* @errno
%7 = load i64, i64* @EACCES
%8 = icmp ne i64 %6, %7
%9 = sext i1 %8 to i32
br label %14
10: ; preds = %1
%11 = tail call i32 @close(i32 %3)
%12 = icmp eq i32 %11, -1
%13 = select i1 %12, i32 -1, i32 1
br label %14
14: ; preds = %10, %5
%15 = phi i32 [ %9, %5 ], [ %13, %10 ]
ret i32 %15
}