#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 */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
size_t fdsize(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int try_file(const char *path, size_t *size) {
  int fd;
  if (access(path, R_OK) != 0) {
    perror("access");
    return -1;
  }
  if ((fd = open(path, O_RDONLY)) < 0) {
    perror("open");
    return fd;
  }
  if ((*size = fdsize(fd)) == (size_t)-1) {
    perror("fdsize");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<try_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
je     1194 <try_file+0x44>
lea    0xe7d(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <try_file+0xb8>
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11c9 <try_file+0x79>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1208 <try_file+0xb8>
mov    -0x1c(%rbp),%edi
call   1070 <fdsize@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0xffffffffffffffff,%rax
jne    1202 <try_file+0xb2>
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <try_file+0xb8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_file>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
test   %rax,%rax
jne    119e <try_file+0x4e>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ac <try_file+0x5c>
mov    %ebx,%edi
call   1070 <fdsize@plt>
mov    %rax,(%r14)
cmp    $0xffffffffffffffff,%rax
je     11ba <try_file+0x6a>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
jmp    11cd <try_file+0x7d>
lea    0xe54(%rip),%rdi
call   1060 <perror@plt>
jmp    1194 <try_file+0x44>
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
jmp    1194 <try_file+0x44>

LLVM IR (-O0):
@R_OK = global i32 0
@.str = [7 x i8] c"access\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [7 x i8] c"fdsize\00"
define i32 @try_file(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
%7 = load i8*, i8** %4
%8 = load i32, i32* @R_OK
%9 = call i64 @access(i8* %7, i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %32
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %32
21: ; preds = %13
%22 = load i32, i32* %6
%23 = call i64 @fdsize(i32 %22)
%24 = load i64*, i64** %5
store i64 %23, i64* %24
%25 = icmp eq i64 %23, -1
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %3
br label %32
30: ; preds = %21
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %32
32: ; preds = %30, %26, %18, %11
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@R_OK = global i32 0
@.str = [7 x i8] c"access\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [7 x i8] c"fdsize\00"
define i32 @try_file(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = load i32, i32* @R_OK
%4 = tail call i64 @access(i8* %0, i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %20
8: ; preds = %2
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %20
14: ; preds = %8
%15 = tail call i64 @fdsize(i32 %10)
store i64 %15, i64* %1
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @close(i32 %10)
br label %20
20: ; preds = %14, %17, %12, %6
%21 = phi i32 [ -1, %6 ], [ %10, %12 ], [ -1, %17 ], [ %10, %14 ]
ret i32 %21
}