#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 const *, char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *, char const *);
FILE *openfile(const char *filename, const char *mode, int fatal) {
  FILE *f;
  if ((f = fopen(filename, mode)) == NULL) {
    perror("fopen failed\n");
    printf("Could not open \"%s\" in \"%s\" mode.\n", filename, mode);
    if (fatal) {
      exit(1);
    }
  }
  return f;
}


x86 assembly (clang, -O0):
<openfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11a7 <openfile+0x67>
lea    0xe8b(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe85(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
je     11a2 <openfile+0x62>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11a7 <openfile+0x67>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
call   1040 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     116b <openfile+0x2b>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe8e(%rip),%rdi
call   1050 <perror@plt>
lea    0xe90(%rip),%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1030 <printf@plt>
test   %r14d,%r14d
je     115d <openfile+0x1d>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"fopen failed\0A\00"
@.str.1 = [35 x i8] c"Could not open \22%s\22 in \22%s\22 mode.\0A\00"
define i32* @openfile(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %8, i8* %9)
store i32* %10, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %22
12: ; preds = %3
%13 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%14 = load i8*, i8** %4
%15 = load i8*, i8** %5
%16 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %15)
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
br label %22
22: ; preds = %21, %3
%23 = load i32*, i32** %7
ret i32* %23
}

LLVM IR (-O2):
@.str = [14 x i8] c"fopen failed\0A\00"
@.str.1 = [35 x i8] c"Could not open \22%s\22 in \22%s\22 mode.\0A\00"
define noalias i32* @openfile(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* %1)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
%9 = icmp eq i32 %2, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6, %3
ret i32* %4
}