#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 /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int i;
  FILE *infile = fopen("vga.dat", "r");
  int num;
  unsigned char buffer[2048];
  if (argc != 2) {
    fprintf(stderr, "usage: dat2c file.dat\n");
    return 1;
  }
  infile = fopen(argv[1], "r");
  if (!infile) {
    fprintf(stderr, "Can't open %s\n", argv[1]);
    return 1;
  }
  num = fread(buffer, 1, 2048, infile);
  printf("#include <stdint.h>\n\n");
  printf("uint8_t vga_array[] = {\n");
  for (i = 0; i < num; i++) {
    printf(" 0x%2.2x,", buffer[i]);
    if ((i & 7) == 7)
      printf("\n");
  }
  printf("};\n\n");
  printf("int vga_size = sizeof(vga_array);\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea0(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     119d <main+0x5d>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a4 <main+0x164>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe5c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11eb <main+0xab>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a4 <main+0x164>
lea    -0x830(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x800,%edx
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1281 <main+0x141>
movslq -0x14(%rbp),%rax
movzbl -0x830(%rbp,%rax,1),%esi
ff
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
and    $0x7,%eax
cmp    $0x7,%eax
jne    126e <main+0x12e>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1273 <main+0x133>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    122b <main+0xeb>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe89(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1080 <fopen@plt>
cmp    $0x2,%ebp
jne    121b <main+0xbb>
mov    0x8(%rbx),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1250 <main+0xf0>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x800,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %eax,%ebx
lea    0xe77(%rip),%rdi
call   1040 <puts@plt>
lea    0xe80(%rip),%rdi
call   1040 <puts@plt>
test   %ebx,%ebx
jle    1234 <main+0xd4>
mov    %ebx,%ebp
lea    0xe4f(%rip),%r14
xor    %ebx,%ebx
jmp    11f9 <main+0x99>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbx
cmp    %rbx,%rbp
je     1234 <main+0xd4>
movzbl (%rsp,%rbx,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
not    %eax
test   $0x7,%al
jne    11f0 <main+0x90>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11f0 <main+0x90>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    126b <main+0x10b>
lea    0xe2c(%rip),%rdi
call   1040 <puts@plt>
lea    0xe24(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    1270 <main+0x110>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xdbd(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"vga.dat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"usage: dat2c file.dat\0A\00"
@.str.3 = [15 x i8] c"Can't open %s\0A\00"
@.str.4 = [22 x i8] c"#include <stdint.h>\0A\0A\00"
@.str.5 = [25 x i8] c"uint8_t vga_array[] = {\0A\00"
@.str.6 = [10 x i8] c" 0x%2.2x,\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [5 x i8] c"};\0A\0A\00"
@.str.9 = [35 x i8] c"int vga_size = sizeof(vga_array);\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 [2048 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %7
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %58
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %7
%21 = load i32*, i32** %7
%22 = icmp ne i32* %21, null
br i1 %22, label %29, label %23
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %27)
store i32 1, i32* %3
br label %58
29: ; preds = %16
%30 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%31 = load i32*, i32** %7
%32 = call i32 @fread(i8* %30, i32 1, i32 2048, i32* %31)
store i32 %32, i32* %8
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %6
br label %35
35: ; preds = %52, %29
%36 = load i32, i32* %6
%37 = load i32, i32* %8
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %55
39: ; preds = %35
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %6
%47 = and i32 %46, 7
%48 = icmp eq i32 %47, 7
br i1 %48, label %49, label %51
49: ; preds = %39
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %51
51: ; preds = %49, %39
br label %52
52: ; preds = %51
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %35
55: ; preds = %35
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
store i32 0, i32* %3
br label %58
58: ; preds = %55, %23, %13
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [8 x i8] c"vga.dat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"usage: dat2c file.dat\0A\00"
@.str.3 = [15 x i8] c"Can't open %s\0A\00"
@.str.6 = [10 x i8] c" 0x%2.2x,\00"
@str = [21 x i8] c"#include <stdint.h>\0A\00"
@str.10 = [24 x i8] c"uint8_t vga_array[] = {\00"
@str.11 = [4 x i8] c"};\0A\00"
@str.12 = [34 x i8] c"int vga_size = sizeof(vga_array);\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2048 x i8]
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%6 = icmp eq i32 %0, 2
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %42
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %11
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %17)
br label %42
19: ; preds = %10
%20 = call i32 @fread(i8* %5, i32 1, i32 2048, i32* %13)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.10, i64 0, i64 0))
%23 = icmp sgt i32 %20, 0
br i1 %23, label %24, label %39
24: ; preds = %19
%25 = zext i32 %20 to i64
br label %26
26: ; preds = %24, %36
%27 = phi i64 [ 0, %24 ], [ %37, %36 ]
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %30)
%32 = and i64 %27, 7
%33 = icmp eq i64 %32, 7
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = tail call i32 @putchar(i32 10)
br label %36
36: ; preds = %26, %34
%37 = add nuw nsw i64 %27, 1
%38 = icmp eq i64 %37, %25
br i1 %38, label %39, label %26
39: ; preds = %36, %19
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.11, i64 0, i64 0))
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.12, i64 0, i64 0))
br label %42
42: ; preds = %39, %15, %7
%43 = phi i32 [ 1, %7 ], [ 0, %39 ], [ 1, %15 ]
ret i32 %43
}