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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int fread(int *, int, int, int /*<<< orphan*/ *);
int *malloc(int);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  FILE *in, *out;
  uint8_t *rom = malloc(4096);
  uint32_t i;
  if (argc != 3) {
    printf("usage ./rom_to_c video|ethernet rom.bin rom.c\n");
    return 0;
  }
  assert(in = fopen(argv[2], "rb"));
  assert(out = fopen(argv[3], "w"));
  assert(fread(rom, 4096, 1, in) == 1);
  fprintf(out, "static uint8_t _%s_rom[4096] = {\n\t", argv[1]);
  for (i = 0; i < 4095; i++) {
    fprintf(out, "0x%02x, ", rom[i]);
    if ((i % 8) == 7)
      fprintf(out, "\n\t");
  }
  fprintf(out, "0x%02x\n};\n", rom[i]);
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1000,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11b6 <main+0x46>
lea    0xe5b(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cc <main+0x15c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe6a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    %eax,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    %eax,%edi
call   1060 <assert@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1000,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0xfff,-0x2c(%rbp)
jge    129f <main+0x12f>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x2c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x7,%edx
jne    128c <main+0x11c>
mov    -0x20(%rbp),%rdi
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1291 <main+0x121>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    123c <main+0xcc>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdab(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %edi,%ebx
mov    $0x1000,%edi
call   1080 <malloc@plt>
cmp    $0x3,%ebx
jne    126d <main+0xed>
mov    %rax,%r14
mov    0x10(%r15),%rdi
lea    0xe51(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r12
mov    %r12d,%edi
call   1060 <assert@plt>
mov    0x18(%r15),%rdi
lea    0xe39(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r13
mov    %r13d,%edi
call   1060 <assert@plt>
mov    %r14,%rdi
mov    $0x1000,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1060 <assert@plt>
mov    0x8(%r15),%rdx
lea    0xdfd(%rip),%rsi
mov    %r13,%rdi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xe0f(%rip),%r15
lea    0xe05(%rip),%r12
xor    %ebx,%ebx
jmp    123d <main+0xbd>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbx
cmp    $0xfff,%rbx
je     127b <main+0xfb>
mov    (%r14,%rbx,4),%edx
mov    %r13,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%eax
not    %eax
test   $0x7,%al
jne    1230 <main+0xb0>
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r12,%rdi
mov    %r13,%rcx
call   10a0 <fwrite@plt>
jmp    1230 <main+0xb0>
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
jmp    129b <main+0x11b>
mov    0x3ffc(%r14),%edx
lea    0xda8(%rip),%rsi
mov    %r13,%rdi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [47 x i8] c"usage ./rom_to_c video|ethernet rom.bin rom.c\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"static uint8_t _%s_rom[4096] = {\0A\09\00"
@.str.4 = [9 x i8] c"0x%02x, \00"
@.str.5 = [3 x i8] c"\0A\09\00"
@.str.6 = [11 x i8] c"0x%02x\0A};\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32* @malloc(i32 4096)
store i32* %10, i32** %8
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %70
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = ptrtoint i32* %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 3
%24 = load i8*, i8** %23
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %7
%26 = ptrtoint i32* %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i32*, i32** %8
%29 = load i32*, i32** %6
%30 = call i32 @fread(i32* %28, i32 4096, i32 1, i32* %29)
%31 = icmp eq i32 %30, 1
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32*, i32** %7
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %37)
store i32 0, i32* %9
br label %39
39: ; preds = %57, %15
%40 = load i32, i32* %9
%41 = icmp slt i32 %40, 4095
br i1 %41, label %42, label %60
42: ; preds = %39
%43 = load i32*, i32** %7
%44 = load i32*, i32** %8
%45 = load i32, i32* %9
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %9
%51 = srem i32 %50, 8
%52 = icmp eq i32 %51, 7
br i1 %52, label %53, label %56
53: ; preds = %42
%54 = load i32*, i32** %7
%55 = call i32 (i32*, i8*, ...) @fprintf(i32* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %56
56: ; preds = %53, %42
br label %57
57: ; preds = %56
%58 = load i32, i32* %9
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
br label %39
60: ; preds = %39
%61 = load i32*, i32** %7
%62 = load i32*, i32** %8
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %62, i64 %64
%66 = load i32, i32* %65
%67 = call i32 (i32*, i8*, ...) @fprintf(i32* %61, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %66)
%68 = load i32*, i32** %7
%69 = call i32 @fclose(i32* %68)
store i32 0, i32* %3
br label %70
70: ; preds = %60, %13
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [35 x i8] c"static uint8_t _%s_rom[4096] = {\0A\09\00"
@.str.4 = [9 x i8] c"0x%02x, \00"
@.str.5 = [3 x i8] c"\0A\09\00"
@.str.6 = [11 x i8] c"0x%02x\0A};\0A\00"
@str = [46 x i8] c"usage ./rom_to_c video|ethernet rom.bin rom.c\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32* @malloc(i32 4096)
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
br label %44
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = ptrtoint i32* %10 to i64
%12 = trunc i64 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = ptrtoint i32* %16 to i64
%18 = trunc i64 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = tail call i32 @fread(i32* %3, i32 4096, i32 1, i32* %10)
%21 = icmp eq i32 %20, 1
%22 = zext i1 %21 to i32
%23 = tail call i32 @assert(i32 %22)
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %7, %36
%28 = phi i64 [ 0, %7 ], [ %37, %36 ]
%29 = getelementptr inbounds i32, i32* %3, i64 %28
%30 = load i32, i32* %29
%31 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = and i64 %28, 7
%33 = icmp eq i64 %32, 7
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i64 2, i64 1, i32* %16)
br label %36
36: ; preds = %27, %34
%37 = add nuw nsw i64 %28, 1
%38 = icmp eq i64 %37, 4095
br i1 %38, label %39, label %27
39: ; preds = %36
%40 = getelementptr inbounds i32, i32* %3, i64 4095
%41 = load i32, i32* %40
%42 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %41)
%43 = tail call i32 @fclose(i32* %16)
br label %44
44: ; preds = %39, %5
ret i32 0
}