#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__ ENOENT;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ errno;
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ t_error(char *, char const *);
int /*<<< orphan*/ xclose(int);
int file_exists(const char *fn) {
  int fd;
  fd = open(fn, O_RDONLY);
  if (fd != -1) {
    xclose(fd);
    return 1;
  }
  if (errno != ENOENT) {
    t_error("unable to open file for reading: %s", fn);
  }
  return 0;
}


x86 assembly (clang, -O0):
<file_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     116f <file_exists+0x3f>
mov    -0x14(%rbp),%edi
call   1030 <xclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    11a0 <file_exists+0x70>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
je     1199 <file_exists+0x69>
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdi
call   1050 <t_error@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_exists>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1157 <file_exists+0x27>
mov    %eax,%edi
call   1030 <xclose@plt>
mov    $0x1,%ebp
jmp    117e <file_exists+0x4e>
mov    0x2e7a(%rip),%rax
mov    (%rax),%rax
mov    0x2e60(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
je     117e <file_exists+0x4e>
lea    0xe8a(%rip),%rdi
mov    %rbx,%rsi
call   1050 <t_error@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [36 x i8] c"unable to open file for reading: %s\00"
define i32 @file_exists(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* %4
%12 = call i32 @xclose(i32 %11)
store i32 1, i32* %2
br label %21
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = call i32 @t_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %13
store i32 0, i32* %2
br label %21
21: ; preds = %20, %10
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [36 x i8] c"unable to open file for reading: %s\00"
define i32 @file_exists(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %7, label %5
5: ; preds = %1
%6 = tail call i32 @xclose(i32 %3)
br label %13
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @t_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %13
13: ; preds = %7, %11, %5
%14 = phi i32 [ 1, %5 ], [ 0, %11 ], [ 0, %7 ]
ret i32 %14
}