#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 *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
FILE *fileopen(char *filepath) {
  FILE *fp;
  if (filepath == NULL) {
    fp = stdin;
  } else {
    fp = fopen(filepath, "rb");
    if (fp == NULL) {
      fprintf(stderr, "ERR: file open failed\n");
      exit(1);
    }
  }
  return fp;
}


x86 assembly (clang, -O0):
<fileopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    115a <fileopen+0x2a>
mov    0x2e9a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    119d <fileopen+0x6d>
mov    -0x8(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1198 <fileopen+0x68>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    119d <fileopen+0x6d>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileopen>:
push   %rax
test   %rdi,%rdi
je     1149 <fileopen+0x19>
lea    0xec3(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1155 <fileopen+0x25>
pop    %rcx
ret
mov    0x2e98(%rip),%rax
mov    (%rax),%rax
pop    %rcx
ret
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    0xe9e(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stdin = global i32* null
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"ERR: file open failed\0A\00"
define i32* @fileopen(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = load i32*, i32** @stdin
store i32* %7, i32** %3
br label %18
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %8
br label %18
18: ; preds = %17, %6
%19 = load i32*, i32** %3
ret i32* %19
}

LLVM IR (-O2):
@stdin = global i32* null
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"ERR: file open failed\0A\00"
define i32* @fileopen(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = load i32*, i32** @stdin
br label %12
5: ; preds = %1
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %5, %3
%13 = phi i32* [ %4, %3 ], [ %6, %5 ]
ret i32* %13
}