#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 */
struct stat {
  scalar_t__ st_size;
};
typedef scalar_t__ off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int open_rom_file(const char *name, off_t expected_size, void **file) {
  int fd;
  struct stat st;
  fd = open(name, O_RDONLY);
  if (fd < 0) {
    perror("open()");
    return -1;
  }
  if (fstat(fd, &st) != 0) {
    perror("fstat()");
    close(fd);
    return -1;
  }
  if (st.st_size != expected_size) {
    printf("File size does not match chip size!\n");
    close(fd);
    return -1;
  }
  *file = mmap(NULL, 524288, PROT_READ, MAP_PRIVATE, fd, 0);
  if (*file == MAP_FAILED) {
    perror("mmap()");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_rom_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ab <open_rom_file+0x4b>
lea    0xe66(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <open_rom_file+0x11d>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11e1 <open_rom_file+0x81>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <open_rom_file+0x11d>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     120f <open_rom_file+0xaf>
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <open_rom_file+0x11d>
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
mov    0x2db1(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x80000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%rax
jne    1277 <open_rom_file+0x117>
lea    0xdd6(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <open_rom_file+0x11d>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_rom_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11fb <open_rom_file+0x9b>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
jne    1209 <open_rom_file+0xa9>
cmp    %r15,0x8(%rsp)
jne    11d7 <open_rom_file+0x77>
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x80000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,(%r14)
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
jne    11ef <open_rom_file+0x8f>
lea    0xe3a(%rip),%rdi
jmp    1210 <open_rom_file+0xb0>
lea    0xe38(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
jmp    11ea <open_rom_file+0x8a>
lea    0xdf7(%rip),%rdi
call   1070 <perror@plt>
jmp    11e3 <open_rom_file+0x83>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [7 x i8] c"open()\00"
@.str.1 = [8 x i8] c"fstat()\00"
@.str.2 = [37 x i8] c"File size does not match chip size!\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [7 x i8] c"mmap()\00"
define i32 @open_rom_file(i8* %0, i64 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i32
%9 = alloca %struct.stat
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8** %2, i8*** %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %50
17: ; preds = %3
%18 = load i32, i32* %8
%19 = call i64 @fstat(i32 %18, %struct.stat* %9)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %8
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %4
br label %50
25: ; preds = %17
%26 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* %6
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %4
br label %50
34: ; preds = %25
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @MAP_PRIVATE
%37 = load i32, i32* %8
%38 = call i8* @mmap(i32* null, i32 524288, i32 %35, i32 %36, i32 %37, i32 0)
%39 = load i8**, i8*** %7
store i8* %38, i8** %39
%40 = load i8**, i8*** %7
%41 = load i8*, i8** %40
%42 = load i8*, i8** @MAP_FAILED
%43 = icmp eq i8* %41, %42
br i1 %43, label %44, label %48
44: ; preds = %34
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %4
br label %50
48: ; preds = %34
%49 = load i32, i32* %8
store i32 %49, i32* %4
br label %50
50: ; preds = %48, %44, %30, %21, %15
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [7 x i8] c"open()\00"
@.str.1 = [8 x i8] c"fstat()\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [7 x i8] c"mmap()\00"
@str = [36 x i8] c"File size does not match chip size!\00"
define i32 @open_rom_file(i8* nocapture %0, i64 %1, i8** nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %33
11: ; preds = %3
%12 = call i64 @fstat(i32 %7, %struct.stat* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @close(i32 %7)
br label %33
17: ; preds = %11
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%19 = load i64, i64* %18
%20 = icmp eq i64 %19, %1
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @close(i32 %7)
br label %33
24: ; preds = %17
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_PRIVATE
%27 = tail call i8* @mmap(i32* null, i32 524288, i32 %25, i32 %26, i32 %7, i32 0)
store i8* %27, i8** %2
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%32 = tail call i32 @close(i32 %7)
br label %33
33: ; preds = %24, %30, %21, %14, %9
%34 = phi i32 [ -1, %9 ], [ -1, %14 ], [ -1, %21 ], [ -1, %30 ], [ %7, %24 ]
ret i32 %34
}