#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int R_OK;
scalar_t__ W_OK;
scalar_t__ access(char *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ stderr;
FILE *fopen2(char *filename, char *mode) {
  int mask;
  mask = R_OK;
  if (mode[0] == 'w')
    mask += W_OK;
  if (access(filename, mask) < 0) {
    fprintf(stderr, "%s: Permission denied (%s)\n", filename, mode);
    exit(EXIT_FAILURE);
  }
  return fopen(filename, mode);
}


x86 assembly (clang, -O0):
<fopen2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    1180 <fopen2+0x40>
mov    0x2e75(%rip),%rax
mov    (%rax),%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <access@plt>
cmp    $0x0,%rax
jge    11c1 <fopen2+0x81>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe52(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <fopen@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen2>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e77(%rip),%rax
xor    %esi,%esi
cmpb   $0x77,(%r14)
jne    1162 <fopen2+0x22>
mov    0x2e88(%rip),%rcx
mov    (%rcx),%esi
add    (%rax),%esi
mov    %rbx,%rdi
call   1040 <access@plt>
test   %rax,%rax
js     1183 <fopen2+0x43>
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <fopen@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <fprintf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@R_OK = global i32 0
@W_OK = global i64 0
@stderr = global i32 0
@.str = [28 x i8] c"%s: Permission denied (%s)\0A\00"
@EXIT_FAILURE = global i32 0
define i32* @fopen2(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i32, i32* @R_OK
store i32 %6, i32* %5
%7 = load i8*, i8** %4
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 119
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i64, i64* @W_OK
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = add nsw i64 %15, %13
%17 = trunc i64 %16 to i32
store i32 %17, i32* %5
br label %18
18: ; preds = %12, %2
%19 = load i8*, i8** %3
%20 = load i32, i32* %5
%21 = call i64 @access(i8* %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %3
%26 = load i8*, i8** %4
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %18
%31 = load i8*, i8** %3
%32 = load i8*, i8** %4
%33 = call i32* @fopen(i8* %31, i8* %32)
ret i32* %33
}

LLVM IR (-O2):
@R_OK = global i32 0
@W_OK = global i64 0
@stderr = global i32 0
@.str = [28 x i8] c"%s: Permission denied (%s)\0A\00"
@EXIT_FAILURE = global i32 0
define noalias i32* @fopen2(i8* %0, i8* %1) {
%3 = load i32, i32* @R_OK
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 119
%6 = load i64, i64* @W_OK
%7 = trunc i64 %6 to i32
%8 = select i1 %5, i32 %7, i32 0
%9 = add i32 %8, %3
%10 = tail call i64 @access(i8* %0, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = tail call i32* @fopen(i8* %0, i8* %1)
ret i32* %18
}