#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;
scalar_t__ ENOENT;
int /*<<< orphan*/ F_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ handle_error(int, char *);
int /*<<< orphan*/ handle_errorf(int, char *, char const *);
scalar_t__ strlen(char const *);
bool file_exists(const char *path) {
  if (!path || strlen(path) == 0) {
    handle_error(0, "Invalid argument.");
    return false;
  }
  if (access(path, F_OK) != 0) {
    if (errno == ENOENT)
      handle_errorf(1, "File '%s' does not exist.", path);
    else if (errno == EACCES)
      handle_errorf(1, "File '%s' is not accessible.", path);
    else
      handle_error(1, "Invalid argument.");
    return false;
  }
  return true;
}


x86 assembly (clang, -O0):
<file_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116a <file_exists+0x2a>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jne    1184 <file_exists+0x44>
xor    %edi,%edi
lea    0xe8d(%rip),%rsi
call   1060 <handle_error@plt>
movl   $0x0,-0x4(%rbp)
jmp    1231 <file_exists+0xf1>
mov    -0x10(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
je     122a <file_exists+0xea>
mov    0x2e31(%rip),%rax
mov    (%rax),%rax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%rax
jne    11d4 <file_exists+0x94>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe48(%rip),%rsi
call   1040 <handle_errorf@plt>
jmp    121e <file_exists+0xde>
mov    0x2dfd(%rip),%rax
mov    (%rax),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
jne    1208 <file_exists+0xc8>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe2e(%rip),%rsi
call   1040 <handle_errorf@plt>
jmp    1219 <file_exists+0xd9>
mov    $0x1,%edi
lea    0xdec(%rip),%rsi
call   1060 <handle_error@plt>
jmp    121e <file_exists+0xde>
movl   $0x0,-0x4(%rbp)
jmp    1231 <file_exists+0xf1>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_exists>:
push   %rbx
test   %rdi,%rdi
je     1173 <file_exists+0x43>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1173 <file_exists+0x43>
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <access@plt>
test   %rax,%rax
je     1185 <file_exists+0x55>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
jne    118c <file_exists+0x5c>
lea    0xea1(%rip),%rsi
jmp    119f <file_exists+0x6f>
lea    0xe86(%rip),%rsi
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <handle_error@plt>
jmp    11c1 <file_exists+0x91>
mov    $0x1,%ebx
jmp    11c1 <file_exists+0x91>
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
jne    11ae <file_exists+0x7e>
lea    0xe8d(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1030 <handle_errorf@plt>
jmp    11bf <file_exists+0x8f>
lea    0xe4b(%rip),%rsi
mov    $0x1,%edi
call   1050 <handle_error@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Invalid argument.\00"
@F_OK = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [26 x i8] c"File '%s' does not exist.\00"
@EACCES = global i64 0
@.str.2 = [29 x i8] c"File '%s' is not accessible.\00"
define i32 @file_exists(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
store i8* %0, i8** %3
%4 = load i8*, i8** %3
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6, %1
%11 = call i32 @handle_error(i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %36
12: ; preds = %6
%13 = load i8*, i8** %3
%14 = load i32, i32* @F_OK
%15 = call i64 @access(i8* %13, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %12
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i8*, i8** %3
%23 = call i32 @handle_errorf(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %34
24: ; preds = %17
%25 = load i64, i64* @errno
%26 = load i64, i64* @EACCES
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i8*, i8** %3
%30 = call i32 @handle_errorf(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %33
31: ; preds = %24
%32 = call i32 @handle_error(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
br label %34
34: ; preds = %33, %21
store i32 0, i32* %2
br label %36
35: ; preds = %12
store i32 1, i32* %2
br label %36
36: ; preds = %35, %34, %10
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [18 x i8] c"Invalid argument.\00"
@F_OK = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [26 x i8] c"File '%s' does not exist.\00"
@EACCES = global i64 0
@.str.2 = [29 x i8] c"File '%s' is not accessible.\00"
define i32 @file_exists(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3, %1
%7 = tail call i32 @handle_error(i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %25
8: ; preds = %3
%9 = load i32, i32* @F_OK
%10 = tail call i64 @access(i8* %0, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %8
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @handle_errorf(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
18: ; preds = %12
%19 = load i64, i64* @EACCES
%20 = icmp eq i64 %13, %19
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @handle_errorf(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %25
23: ; preds = %18
%24 = tail call i32 @handle_error(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %8, %16, %23, %21, %6
%26 = phi i32 [ 0, %6 ], [ 0, %21 ], [ 0, %23 ], [ 0, %16 ], [ 1, %8 ]
ret i32 %26
}