#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;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char const *, int /*<<< orphan*/);
int checkFileExist(const char *filename) {
  int fd = open(filename, O_RDONLY);
  if (fd > 0) {
    close(fd);
    return -1;
  } else {
    if (errno == ENOENT) {
      return 0;
    } else {
      return errno;
    }
  }
}


x86 assembly (clang, -O0):
<checkFileExist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2eb1(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    115f <checkFileExist+0x3f>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1192 <checkFileExist+0x72>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
jne    1185 <checkFileExist+0x65>
movl   $0x0,-0x4(%rbp)
jmp    1192 <checkFileExist+0x72>
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkFileExist>:
push   %rax
mov    0x2ec0(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
test   %eax,%eax
jle    1141 <checkFileExist+0x21>
mov    %eax,%edi
call   1030 <close@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
mov    0x2e90(%rip),%rax
mov    (%rax),%rax
mov    0x2e76(%rip),%rcx
xor    %edx,%edx
cmp    (%rcx),%rax
cmove  %edx,%eax
pop    %rcx
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @checkFileExist(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 sgt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* %4
%12 = call i32 @close(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 eq i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 0, i32* %2
br label %21
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = trunc i64 %19 to i32
store i32 %20, i32* %2
br label %21
21: ; preds = %18, %17, %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
define i32 @checkFileExist(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @close(i32 %3)
br label %13
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
%11 = trunc i64 %8 to i32
%12 = select i1 %10, i32 0, i32 %11
br label %13
13: ; preds = %7, %5
%14 = phi i32 [ -1, %5 ], [ %12, %7 ]
ret i32 %14
}