#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;
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
int open(char const *, int);
int /*<<< orphan*/ strerror(scalar_t__);
int checked_open(const char *path, int flags) {
  int fd = open(path, flags);
  if (fd == -1 && errno != EACCES && errno != ENOENT) {
    errx(1, "Failed to open a file: %s", strerror(errno));
  }
  return (fd);
}


x86 assembly (clang, -O0):
<checked_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11ae <checked_open+0x7e>
mov    0x2e79(%rip),%rax
mov    (%rax),%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
je     11ae <checked_open+0x7e>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
je     11ae <checked_open+0x7e>
mov    0x2e45(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe57(%rip),%rsi
call   1030 <errx@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checked_open>:
push   %rbx
call   1040 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1177 <checked_open+0x47>
mov    0x2e94(%rip),%rax
mov    (%rax),%rdi
mov    0x2e9a(%rip),%rax
cmp    (%rax),%rdi
je     1177 <checked_open+0x47>
mov    0x2e6e(%rip),%rax
cmp    (%rax),%rdi
je     1177 <checked_open+0x47>
call   1050 <strerror@plt>
lea    0xe95(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1030 <errx@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@EACCES = global i64 0
@ENOENT = global i64 0
@.str = [26 x i8] c"Failed to open a file: %s\00"
define i32 @checked_open(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = load i32, i32* %4
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %23
11: ; preds = %2
%12 = load i64, i64* @errno
%13 = load i64, i64* @EACCES
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOENT
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = call i32 @strerror(i64 %20)
%22 = call i32 @errx(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %19, %15, %11, %2
%24 = load i32, i32* %5
ret i32 %24
}

LLVM IR (-O2):
@errno = global i64 0
@EACCES = global i64 0
@ENOENT = global i64 0
@.str = [26 x i8] c"Failed to open a file: %s\00"
define i32 @checked_open(i8* nocapture %0, i32 %1) {
%3 = tail call i32 @open(i8* %0, i32 %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %15
5: ; preds = %2
%6 = load i64, i64* @errno
%7 = load i64, i64* @EACCES
%8 = icmp eq i64 %6, %7
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %6, %9
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %15, label %12
12: ; preds = %5
%13 = tail call i32 @strerror(i64 %6)
%14 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %5, %2
ret i32 %3
}