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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
long getromsize(FILE *fd) {
  unsigned char buffer[3];
  long size, i;
  if (fread(buffer, sizeof(char), 3, fd) != 3) {
    fprintf(stderr, "Cannot get first 3 bytes of file\n");
    exit(1);
  }
  if (fseek(fd, (off_t)0, SEEK_SET) < 0) {
    perror("fseek");
    exit(1);
  }
  if (buffer[0] != 0x55 || buffer[1] != 0xAA)
    fprintf(stderr, "BIOS extension ROM Image did not start with 0x55 0xAA\n");
  size = buffer[2] * 512L;
  for (i = 8192L; i < 262144L && i < size; i *= 2)
    ;
  if (size > 0 && i > size)
    fprintf(stderr, "%ld is a strange size for a boot ROM\n", size);
  return (size);
}


x86 assembly (clang, -O0):
<getromsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0xb(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1030 <fread@plt>
cmp    $0x3,%eax
je     119d <getromsize+0x4d>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
cmp    $0x0,%rax
jge    11d1 <getromsize+0x81>
lea    0xe60(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movzbl -0xb(%rbp),%eax
cmp    $0x55,%eax
jne    11ed <getromsize+0x9d>
movzbl -0xa(%rbp),%eax
cmp    $0xaa,%eax
je     1204 <getromsize+0xb4>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movzbl -0x9(%rbp),%eax
shl    $0x9,%rax
mov    %rax,-0x18(%rbp)
movq   $0x2000,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x40000,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jge    1239 <getromsize+0xe9>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
setl   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1249 <getromsize+0xf9>
jmp    125f <getromsize+0x10f>
jmp    124e <getromsize+0xfe>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1218 <getromsize+0xc8>
cmpq   $0x0,-0x18(%rbp)
jle    1293 <getromsize+0x143>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jle    1293 <getromsize+0x143>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdd3(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getromsize>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xd(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x3,%eax
jne    121f <getromsize+0xcf>
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
test   %rax,%rax
js     1240 <getromsize+0xf0>
cmpb   $0x55,0xd(%rsp)
jne    11a2 <getromsize+0x52>
cmpb   $0xaa,0xe(%rsp)
je     11b9 <getromsize+0x69>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
movzbl 0xf(%rsp),%ebx
mov    %ebx,%eax
mov    $0x2000,%ecx
cmp    $0x11,%rbx
jb     11ef <getromsize+0x9f>
mov    $0x4000,%ecx
cmp    $0x21,%al
jb     11ef <getromsize+0x9f>
mov    $0x8000,%ecx
cmp    $0x41,%al
jb     11ef <getromsize+0x9f>
xor    %ecx,%ecx
cmp    $0x81,%al
setae  %cl
shl    $0x10,%rcx
add    $0x10000,%rcx
shl    $0x9,%rbx
test   %al,%al
je     1216 <getromsize+0xc6>
cmp    %rbx,%rcx
jbe    1216 <getromsize+0xc6>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xddb(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"Cannot get first 3 bytes of file\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"fseek\00"
@.str.2 = [55 x i8] c"BIOS extension ROM Image did not start with 0x55 0xAA\0A\00"
@.str.3 = [38 x i8] c"%ld is a strange size for a boot ROM\0A\00"
define i64 @getromsize(i32* %0) {
%2 = alloca i32*
%3 = alloca [3 x i8]
%4 = alloca i64
%5 = alloca i64
store i32* %0, i32** %2
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%7 = load i32*, i32** %2
%8 = call i32 @fread(i8* %6, i32 1, i32 3, i32* %7)
%9 = icmp ne i32 %8, 3
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i32*, i32** %2
%16 = load i32, i32* @SEEK_SET
%17 = call i64 @fseek(i32* %15, i32 0, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = icmp ne i32 %25, 85
br i1 %26, label %32, label %27
27: ; preds = %22
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = icmp ne i32 %30, 170
br i1 %31, label %32, label %35
32: ; preds = %27, %22
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %32, %27
%36 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
%37 = load i8, i8* %36
%38 = zext i8 %37 to i64
%39 = mul nsw i64 %38, 512
store i64 %39, i64* %4
store i64 8192, i64* %5
br label %40
40: ; preds = %50, %35
%41 = load i64, i64* %5
%42 = icmp slt i64 %41, 262144
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i64, i64* %5
%45 = load i64, i64* %4
%46 = icmp slt i64 %44, %45
br label %47
47: ; preds = %43, %40
%48 = phi i1 [ false, %40 ], [ %46, %43 ]
br i1 %48, label %49, label %53
49: ; preds = %47
br label %50
50: ; preds = %49
%51 = load i64, i64* %5
%52 = mul nsw i64 %51, 2
store i64 %52, i64* %5
br label %40
53: ; preds = %47
%54 = load i64, i64* %4
%55 = icmp sgt i64 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %53
%57 = load i64, i64* %5
%58 = load i64, i64* %4
%59 = icmp sgt i64 %57, %58
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = load i32, i32* @stderr
%62 = load i64, i64* %4
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i64 %62)
br label %64
64: ; preds = %60, %56, %53
%65 = load i64, i64* %4
ret i64 %65
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"Cannot get first 3 bytes of file\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"fseek\00"
@.str.2 = [55 x i8] c"BIOS extension ROM Image did not start with 0x55 0xAA\0A\00"
@.str.3 = [38 x i8] c"%ld is a strange size for a boot ROM\0A\00"
define i64 @getromsize(i32* nocapture %0) {
%2 = alloca [3 x i8]
%3 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 0
%4 = call i32 @fread(i8* %3, i32 1, i32 3, i32* %0)
%5 = icmp eq i32 %4, 3
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = load i32, i32* @SEEK_SET
%12 = tail call i64 @fseek(i32* %0, i32 0, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = load i8, i8* %3
%19 = icmp ne i8 %18, 85
%20 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 1
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, -86
%23 = select i1 %19, i1 true, i1 %22
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %17, %24
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 2
%29 = load i8, i8* %28
%30 = zext i8 %29 to i64
%31 = shl nuw nsw i64 %30, 9
%32 = icmp ugt i8 %29, 16
br i1 %32, label %33, label %40
33: ; preds = %27
%34 = icmp ugt i8 %29, 32
br i1 %34, label %35, label %40
35: ; preds = %33
%36 = icmp ugt i8 %29, 64
br i1 %36, label %37, label %40
37: ; preds = %35
%38 = icmp ugt i8 %29, -128
%39 = select i1 %38, i64 131072, i64 65536
br label %40
40: ; preds = %37, %35, %33, %27
%41 = phi i64 [ 8192, %27 ], [ 16384, %33 ], [ 32768, %35 ], [ %39, %37 ]
%42 = icmp ne i8 %29, 0
%43 = icmp ugt i64 %41, %31
%44 = select i1 %42, i1 %43, i1 false
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = load i32, i32* @stderr
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i64 %31)
br label %48
48: ; preds = %45, %40
ret i64 %31
}