#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*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
FILE *k_fopen(char *fname, char *mode, int exit_on_error) {
  FILE *f;
  if (fname == NULL || fname[0] == 0)
    return NULL;
  f = fopen(fname, mode);
  if (f == NULL) {
    fprintf(stderr, "Can't open file %s for %s (%s)\n", fname,
            (mode[0] == 'r') ? "input" : "output", strerror(errno));
    if (exit_on_error == (1))
      exit(-1);
  }
  return f;
}


x86 assembly (clang, -O0):
<k_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116e <k_fopen+0x2e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    117b <k_fopen+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    1214 <k_fopen+0xd4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120c <k_fopen+0xcc>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edx
lea    0xe6d(%rip),%rax
lea    0xe60(%rip),%rcx
cmp    $0x72,%edx
cmove  %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    %rax,%r8
lea    0xe12(%rip),%rsi
call   1030 <fprintf@plt>
cmpl   $0x1,-0x1c(%rbp)
jne    1207 <k_fopen+0xc7>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
jmp    120c <k_fopen+0xcc>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<k_fopen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11b3 <k_fopen+0x73>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     11b3 <k_fopen+0x73>
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rbx,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
jne    11b5 <k_fopen+0x75>
mov    0x2e72(%rip),%rax
mov    (%rax),%r15d
cmpb   $0x72,0x0(%rbp)
lea    0xea4(%rip),%rax
lea    0xea3(%rip),%rbp
cmove  %rax,%rbp
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe64(%rip),%rsi
mov    %r15d,%edi
mov    %rbx,%rdx
mov    %rbp,%rcx
mov    %rax,%r8
call   1030 <fprintf@plt>
cmp    $0x1,%r14d
je     11c0 <k_fopen+0x80>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Can't open file %s for %s (%s)\0A\00"
@.str.1 = [6 x i8] c"input\00"
@.str.2 = [7 x i8] c"output\00"
@errno = global i32 0
define i32* @k_fopen(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %11, %3
store i32* null, i32** %4
br label %44
18: ; preds = %11
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = call i32* @fopen(i8* %19, i8* %20)
store i32* %21, i32** %8
%22 = load i32*, i32** %8
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %42
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %5
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 114
%32 = zext i1 %31 to i64
%33 = select i1 %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0)
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %33, i8* %35)
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %41
39: ; preds = %24
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %24
br label %42
42: ; preds = %41, %18
%43 = load i32*, i32** %8
store i32* %43, i32** %4
br label %44
44: ; preds = %42, %17
%45 = load i32*, i32** %4
ret i32* %45
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Can't open file %s for %s (%s)\0A\00"
@.str.1 = [6 x i8] c"input\00"
@.str.2 = [7 x i8] c"output\00"
@errno = global i32 0
define noalias i32* @k_fopen(i8* %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %22, label %5
5: ; preds = %3
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %5
%9 = tail call i32* @fopen(i8* %0, i8* %1)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %22
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = load i8, i8* %1
%14 = icmp eq i8 %13, 114
%15 = select i1 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0)
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15, i8* %17)
%19 = icmp eq i32 %2, 1
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = tail call i32 @exit(i32 -1)
unreachable
22: ; preds = %8, %11, %3, %5
%23 = phi i32* [ null, %5 ], [ null, %3 ], [ null, %11 ], [ %9, %8 ]
ret i32* %23
}