#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ mem_enable_bios();
int /*<<< orphan*/ mem_set_bios(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ set_boot_state();
void dmg_load_bios(char *bios_path) {
  int size;
  FILE *fb;
  uint8_t *bios;
  if (bios_path == NULL) {
    printf("Booting without BIOS ROM\n");
    set_boot_state();
    return;
  }
  fb = fopen(bios_path, "rb");
  if (fb == NULL) {
    printf("Can't open bios file %s", bios_path);
    exit(1);
  }
  fseek(fb, 0, SEEK_END);
  size = ftell(fb);
  if (size > 0x100) {
    printf("Bios file %s too big", bios_path);
    exit(1);
  }
  fseek(fb, 0, SEEK_SET);
  bios = malloc(size);
  fread(bios, 1, size, fb);
  mem_set_bios(bios, size);
  fclose(fb);
  mem_enable_bios();
}


x86 assembly (clang, -O0):
<dmg_load_bios>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11e1 <dmg_load_bios+0x31>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <set_boot_state@plt>
jmp    12b6 <dmg_load_bios+0x106>
mov    -0x8(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121c <dmg_load_bios+0x6c>
mov    -0x8(%rbp),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1090 <ftell@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x100,-0xc(%rbp)
jle    1265 <dmg_load_bios+0xb5>
mov    -0x8(%rbp),%rsi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0xc(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <mem_set_bios@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x0,%al
call   1080 <mem_enable_bios@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dmg_load_bios>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1258 <dmg_load_bios+0x98>
mov    %rdi,%r14
lea    0xe29(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     126f <dmg_load_bios+0xaf>
mov    %rax,%rbx
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
cmp    $0x101,%eax
jge    1278 <dmg_load_bios+0xb8>
mov    %eax,%ebp
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %ebp,%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <mem_set_bios@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1090 <mem_enable_bios@plt>
lea    0xdd1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <set_boot_state@plt>
lea    0xd8d(%rip),%rdi
jmp    127f <dmg_load_bios+0xbf>
lea    0xd9c(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Booting without BIOS ROM\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [24 x i8] c"Can't open bios file %s\00"
@SEEK_END = global i32 0
@.str.3 = [21 x i8] c"Bios file %s too big\00"
@SEEK_SET = global i32 0
define void @dmg_load_bios(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (...) @set_boot_state()
br label %48
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i8*, i8** %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = load i32*, i32** %4
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i32*, i32** %4
%25 = call i32 @ftell(i32* %24)
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = icmp sgt i32 %26, 256
br i1 %27, label %28, label %32
28: ; preds = %20
%29 = load i8*, i8** %2
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %20
%33 = load i32*, i32** %4
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @fseek(i32* %33, i32 0, i32 %34)
%36 = load i32, i32* %3
%37 = call i32* @malloc(i32 %36)
store i32* %37, i32** %5
%38 = load i32*, i32** %5
%39 = load i32, i32* %3
%40 = load i32*, i32** %4
%41 = call i32 @fread(i32* %38, i32 1, i32 %39, i32* %40)
%42 = load i32*, i32** %5
%43 = load i32, i32* %3
%44 = call i32 @mem_set_bios(i32* %42, i32 %43)
%45 = load i32*, i32** %4
%46 = call i32 @fclose(i32* %45)
%47 = call i32 (...) @mem_enable_bios()
br label %48
48: ; preds = %32, %8
ret void
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [24 x i8] c"Can't open bios file %s\00"
@SEEK_END = global i32 0
@.str.3 = [21 x i8] c"Bios file %s too big\00"
@SEEK_SET = global i32 0
@str = [25 x i8] c"Booting without BIOS ROM\00"
define void @dmg_load_bios(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 (...) @set_boot_state()
br label %28
6: ; preds = %1
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6
%13 = load i32, i32* @SEEK_END
%14 = tail call i32 @fseek(i32* %7, i32 0, i32 %13)
%15 = tail call i32 @ftell(i32* %7)
%16 = icmp sgt i32 %15, 256
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %12
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @fseek(i32* %7, i32 0, i32 %21)
%23 = tail call i32* @malloc(i32 %15)
%24 = tail call i32 @fread(i32* %23, i32 1, i32 %15, i32* %7)
%25 = tail call i32 @mem_set_bios(i32* %23, i32 %15)
%26 = tail call i32 @fclose(i32* %7)
%27 = tail call i32 (...) @mem_enable_bios()
br label %28
28: ; preds = %20, %3
ret void
}