#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(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
FILE *check_fopen(char *filename, char *mode) {
  FILE *res = fopen(filename, mode);
  if (res == NULL) {
    if (mode[0] == 'w')
      fprintf(stderr, "[Error] Failed to open file %s for writing!\n",
              filename);
    else if (mode[0] == 'a')
      fprintf(stderr, "[Error] Failed to open file %s for appending!\n",
              filename);
    else
      fprintf(stderr, "[Error] Failed to open file %s for reading!\n",
              filename);
    exit(1);
  }
  return res;
}


x86 assembly (clang, -O0):
<check_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e0 <check_fopen+0xb0>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    118a <check_fopen+0x5a>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe80(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11d6 <check_fopen+0xa6>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x61,%eax
jne    11b8 <check_fopen+0x88>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe7f(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11d1 <check_fopen+0xa1>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe90(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11d6 <check_fopen+0xa6>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_fopen>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <fopen@plt>
test   %rax,%rax
je     114c <check_fopen+0x1c>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rbx),%al
cmp    $0x61,%al
lea    0xed6(%rip),%rcx
lea    0xefe(%rip),%rdx
cmove  %rcx,%rdx
cmp    $0x77,%al
lea    0xe95(%rip),%rsi
cmovne %rdx,%rsi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rdx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [45 x i8] c"[Error] Failed to open file %s for writing!\0A\00"
@.str.1 = [47 x i8] c"[Error] Failed to open file %s for appending!\0A\00"
@.str.2 = [45 x i8] c"[Error] Failed to open file %s for reading!\0A\00"
define i32* @check_fopen(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 i8*, i8** %3
%7 = load i8*, i8** %4
%8 = call i32* @fopen(i8* %6, i8* %7)
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %38
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 119
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %3
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %36
21: ; preds = %11
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 97
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %3
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %35
31: ; preds = %21
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %3
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %31, %27
br label %36
36: ; preds = %35, %17
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %2
%39 = load i32*, i32** %5
ret i32* %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [45 x i8] c"[Error] Failed to open file %s for writing!\0A\00"
@.str.1 = [47 x i8] c"[Error] Failed to open file %s for appending!\0A\00"
@.str.2 = [45 x i8] c"[Error] Failed to open file %s for reading!\0A\00"
define noalias i32* @check_fopen(i8* %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* %1)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %14
5: ; preds = %2
%6 = load i8, i8* %1
%7 = icmp eq i8 %6, 97
%8 = select i1 %7, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0)
%9 = icmp eq i8 %6, 119
%10 = select i1 %9, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %8
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* %10, i8* %0)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
ret i32* %3
}