#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, int);
int open_rom(char *fname, FILE **buf) {
  int flen = 0;
  *buf = fopen(fname, "rb");
  if (*buf == NULL) {
    printf("ERROR: Could not open rom file %s: %d\n", fname, errno);
    return 0;
  }
  fseek(*buf, 0, SEEK_END);
  flen = ftell(*buf);
  fseek(*buf, 0, SEEK_SET);
  return flen;
}


x86 assembly (clang, -O0):
<open_rom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a4 <open_rom+0x64>
mov    -0x10(%rbp),%rsi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
lea    0xe70(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e7 <open_rom+0xa7>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_rom>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xeaf(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     1192 <open_rom+0x52>
mov    0x2e83(%rip),%rcx
mov    (%rcx),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
mov    (%rbx),%rdi
call   1040 <ftell@plt>
mov    %eax,%ebp
mov    (%rbx),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
mov    %ebp,%eax
jmp    11ac <open_rom+0x6c>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
lea    0xe61(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [39 x i8] c"ERROR: Could not open rom file %s: %d\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @open_rom(i8* %0, i32** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32**
%6 = alloca i32
store i8* %0, i8** %4
store i32** %1, i32*** %5
store i32 0, i32* %6
%7 = load i8*, i8** %4
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = load i32**, i32*** %5
store i32* %8, i32** %9
%10 = load i32**, i32*** %5
%11 = load i32*, i32** %10
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i32, i32* @errno
%16 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32 %15)
store i32 0, i32* %3
br label %30
17: ; preds = %2
%18 = load i32**, i32*** %5
%19 = load i32*, i32** %18
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i32**, i32*** %5
%23 = load i32*, i32** %22
%24 = call i32 @ftell(i32* %23)
store i32 %24, i32* %6
%25 = load i32**, i32*** %5
%26 = load i32*, i32** %25
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @fseek(i32* %26, i32 0, i32 %27)
%29 = load i32, i32* %6
store i32 %29, i32* %3
br label %30
30: ; preds = %17, %13
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [39 x i8] c"ERROR: Could not open rom file %s: %d\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @open_rom(i8* %0, i32** nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %3, i32** %1
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %6)
br label %16
8: ; preds = %2
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = load i32*, i32** %1
%12 = tail call i32 @ftell(i32* %11)
%13 = load i32*, i32** %1
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %13, i32 0, i32 %14)
br label %16
16: ; preds = %8, %5
%17 = phi i32 [ 0, %5 ], [ %12, %8 ]
ret i32 %17
}