#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 EOF;
int EXIT_FAILURE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, unsigned char);
int /*<<< orphan*/ stderr;
void make_hex(char *infile, char *outfile) {
  FILE *fi, *fo;
  int length, dh, dl, n;
  char data[16];
  fi = fopen(infile, "rb");
  if (fi == NULL) {
    fprintf(stderr, "Cannot open input file\n");
    exit(1);
  }
  fseek(fi, 0L, SEEK_END);
  length = ftell(fi);
  if (length != 0x10000) {
    printf("oops, that is not the bios file, must be 64K ROM!\n");
    fclose(fi);
    exit(EXIT_FAILURE);
  } else
    printf("BIOS ROM File size = %u bytes\n", length);
  fo = fopen(outfile, "wt");
  if (fo == NULL) {
    fprintf(stderr, "Cannot open output file\n");
    exit(EXIT_FAILURE);
  }
  fseek(fi, 0xFF00L, SEEK_SET);
  n = 0;
  do {
    dh = fgetc(fi);
    dl = fgetc(fi);
    if (dl == EOF)
      break;
    sprintf(data, "%02x%02x\n", (unsigned char)dl, (unsigned char)dh);
    fputs(data, fo);
    n += 2;
  } while (dl != EOF);
  fprintf(stderr, "%d bytes read and converted\n", n);
  fclose(fi);
  fclose(fo);
}


x86 assembly (clang, -O0):
<make_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f0 <make_hex+0x50>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x10000,-0x24(%rbp)
je     1242 <make_hex+0xa2>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x24(%rbp),%esi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1297 <make_hex+0xf7>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d26(%rip),%rax
mov    (%rax),%edx
mov    $0xff00,%esi
call   1090 <fseek@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%eax
jne    12e4 <make_hex+0x144>
jmp    132a <make_hex+0x18a>
lea    -0x40(%rbp),%rdi
mov    -0x2c(%rbp),%eax
mov    %al,%cl
mov    -0x28(%rbp),%eax
lea    0xd92(%rip),%rsi
movzbl %cl,%edx
movzbl %al,%ecx
call   10b0 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x30(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2cbe(%rip),%rcx
cmp    (%rcx),%eax
jne    12b5 <make_hex+0x115>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%edx
lea    0xd56(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
lea    0xe38(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12de <make_hex+0x12e>
mov    %rax,%r13
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r13,%rdi
call   1090 <ftell@plt>
cmp    $0x10000,%eax
jne    12ff <make_hex+0x14f>
lea    0xe15(%rip),%rdi
xor    %ebp,%ebp
mov    $0x10000,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe1f(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1315 <make_hex+0x165>
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    $0xff00,%esi
call   10a0 <fseek@plt>
mov    0x2d92(%rip),%r14
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
call   1070 <fgetc@plt>
mov    %eax,%r15d
mov    %r13,%rdi
call   1070 <fgetc@plt>
cmp    (%r14),%eax
je     12a5 <make_hex+0xf5>
mov    %eax,%ebx
movzbl %bl,%edx
movzbl %r15b,%ecx
mov    %r12,%rdi
lea    0xdcb(%rip),%rsi
call   10c0 <sprintf@plt>
mov    %r12,%rdi
mov    0x8(%rsp),%rsi
call   1060 <fputs@plt>
add    $0x2,%ebp
cmp    (%r14),%ebx
jne    1260 <make_hex+0xb0>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    0xd15(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd77(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
jmp    132c <make_hex+0x17c>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
lea    0xd18(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Cannot open input file\0A\00"
@SEEK_END = global i32 0
@.str.2 = [51 x i8] c"oops, that is not the bios file, must be 64K ROM!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [31 x i8] c"BIOS ROM File size = %u bytes\0A\00"
@.str.4 = [3 x i8] c"wt\00"
@.str.5 = [25 x i8] c"Cannot open output file\0A\00"
@SEEK_SET = global i32 0
@EOF = global i32 0
@.str.6 = [10 x i8] c"%02x%02x\0A\00"
@.str.7 = [29 x i8] c"%d bytes read and converted\0A\00"
define void @make_hex(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [16 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32*, i32** %5
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i32*, i32** %5
%25 = call i32 @ftell(i32* %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp ne i32 %26, 65536
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32*, i32** %5
%31 = call i32 @fclose(i32* %30)
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %20
%35 = load i32, i32* %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34
%38 = load i8*, i8** %4
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %39, i32** %6
%40 = load i32*, i32** %6
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %37
%48 = load i32*, i32** %5
%49 = load i32, i32* @SEEK_SET
%50 = call i32 @fseek(i32* %48, i32 65280, i32 %49)
store i32 0, i32* %10
br label %51
51: ; preds = %72, %47
%52 = load i32*, i32** %5
%53 = call i32 @fgetc(i32* %52)
store i32 %53, i32* %8
%54 = load i32*, i32** %5
%55 = call i32 @fgetc(i32* %54)
store i32 %55, i32* %9
%56 = load i32, i32* %9
%57 = load i32, i32* @EOF
%58 = icmp eq i32 %56, %57
br i1 %58, label %59, label %60
59: ; preds = %51
br label %76
60: ; preds = %51
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%62 = load i32, i32* %9
%63 = trunc i32 %62 to i8
%64 = load i32, i32* %8
%65 = trunc i32 %64 to i8
%66 = call i32 @sprintf(i8* %61, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8 zeroext %63, i8 zeroext %65)
%67 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%68 = load i32*, i32** %6
%69 = call i32 @fputs(i8* %67, i32* %68)
%70 = load i32, i32* %10
%71 = add nsw i32 %70, 2
store i32 %71, i32* %10
br label %72
72: ; preds = %60
%73 = load i32, i32* %9
%74 = load i32, i32* @EOF
%75 = icmp ne i32 %73, %74
br i1 %75, label %51, label %76
76: ; preds = %72, %59
%77 = load i32, i32* @stderr
%78 = load i32, i32* %10
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %78)
%80 = load i32*, i32** %5
%81 = call i32 @fclose(i32* %80)
%82 = load i32*, i32** %6
%83 = call i32 @fclose(i32* %82)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Cannot open input file\0A\00"
@SEEK_END = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [31 x i8] c"BIOS ROM File size = %u bytes\0A\00"
@.str.4 = [3 x i8] c"wt\00"
@.str.5 = [25 x i8] c"Cannot open output file\0A\00"
@SEEK_SET = global i32 0
@EOF = global i32 0
@.str.6 = [10 x i8] c"%02x%02x\0A\00"
@.str.7 = [29 x i8] c"%d bytes read and converted\0A\00"
@str = [50 x i8] c"oops, that is not the bios file, must be 64K ROM!\00"
define void @make_hex(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = load i32, i32* @SEEK_END
%13 = tail call i32 @fseek(i32* %5, i32 0, i32 %12)
%14 = tail call i32 @ftell(i32* %5)
%15 = icmp eq i32 %14, 65536
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @fclose(i32* %5)
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %11
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 65536)
%23 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %21
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @fseek(i32* %5, i32 65280, i32 %31)
br label %33
33: ; preds = %39, %30
%34 = phi i32 [ 0, %30 ], [ %44, %39 ]
%35 = tail call i32 @fgetc(i32* %5)
%36 = tail call i32 @fgetc(i32* %5)
%37 = load i32, i32* @EOF
%38 = icmp eq i32 %36, %37
br i1 %38, label %47, label %39
39: ; preds = %33
%40 = trunc i32 %36 to i8
%41 = trunc i32 %35 to i8
%42 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8 zeroext %40, i8 zeroext %41)
%43 = call i32 @fputs(i8* %4, i32* %23)
%44 = add nuw nsw i32 %34, 2
%45 = load i32, i32* @EOF
%46 = icmp eq i32 %36, %45
br i1 %46, label %47, label %33
47: ; preds = %33, %39
%48 = phi i32 [ %34, %33 ], [ %44, %39 ]
%49 = load i32, i32* @stderr
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %48)
%51 = tail call i32 @fclose(i32* %5)
%52 = tail call i32 @fclose(i32* %23)
ret void
}