#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ *stderr;
int main(int argc, char **argv) {
  FILE *in, *out;
  int col;
  int c;
  if (argc < 4) {
    fprintf(stderr,
            "Usage: bin2c <output bin file> <output h file> <array name>\n");
    return 1;
  }
  if ((in = fopen(argv[1], "rb")) == NULL) {
    fprintf(stderr, "Cannot open input file (%s).\n", argv[1]);
    return 1;
  }
  if ((out = fopen(argv[2], "wt")) == NULL) {
    fprintf(stderr, "Cannot open output file (%s).\n", argv[2]);
    return 1;
  }
  col = 0;
  fprintf(out, "unsigned char %s[] = {\n  ", argv[3]);
  while ((c = fgetc(in)) != EOF) {
    if (col) {
      fprintf(out, ", ");
    }
    if (col >= 8) {
      fprintf(out, "\n  ");
      col = 0;
    }
    fprintf(out, "0x%02X", c);
    col++;
  }
  fprintf(out, "\n};\n");
  fclose(in);
  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)
cmpl   $0x4,-0x8(%rbp)
jge    1184 <main+0x44>
mov    0x2e71(%rip),%rax
mov    (%rax),%rdi
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e9 <main+0x1a9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d2 <main+0x92>
mov    0x2e2b(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe81(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e9 <main+0x1a9>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe7d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1220 <main+0xe0>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e9 <main+0x1a9>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d94(%rip),%rcx
cmp    (%rcx),%eax
je     12be <main+0x17e>
cmpl   $0x0,-0x24(%rbp)
je     1278 <main+0x138>
mov    -0x20(%rbp),%rdi
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x8,-0x24(%rbp)
jl     129b <main+0x15b>
mov    -0x20(%rbp),%rdi
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%edx
lea    0xdf4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1241 <main+0x101>
mov    -0x20(%rbp),%rdi
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jg     1185 <main+0x35>
mov    0x2e71(%rip),%rax
mov    (%rax),%rcx
lea    0xe8f(%rip),%rdi
mov    $0x3c,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
jmp    1242 <main+0xf2>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     120f <main+0xbf>
mov    %rax,%r14
mov    0x10(%rbp),%rdi
lea    0xeb3(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1226 <main+0xd6>
mov    %rax,%rbx
mov    0x18(%rbp),%rdx
lea    0xebd(%rip),%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    0x2e0c(%rip),%r13
cmp    0x0(%r13),%eax
jne    1256 <main+0x106>
lea    0xebb(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1247 <main+0xf7>
mov    0x2dc2(%rip),%rax
mov    (%rax),%rdi
mov    0x8(%rbp),%rdx
lea    0xe1c(%rip),%rsi
jmp    123b <main+0xeb>
mov    0x2dab(%rip),%rax
mov    (%rax),%rdi
mov    0x10(%rbp),%rdx
lea    0xe26(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
lea    0xe3e(%rip),%r15
xor    %r12d,%r12d
jmp    129a <main+0x14a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %r12d,%r12d
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r12d
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
cmp    0x0(%r13),%eax
je     11e2 <main+0x92>
test   %r12d,%r12d
je     1270 <main+0x120>
mov    $0x2,%esi
mov    $0x1,%edx
lea    0xdea(%rip),%rdi
mov    %rbx,%rcx
call   1070 <fwrite@plt>
cmp    $0x8,%r12d
jl     1273 <main+0x123>
mov    $0x3,%esi
mov    $0x1,%edx
lea    0xdc7(%rip),%rdi
mov    %rbx,%rcx
call   1070 <fwrite@plt>
jmp    1270 <main+0x120>

LLVM IR (-O0):
@stderr = global i32* null
@.str = [61 x i8] c"Usage: bin2c <output bin file> <output h file> <array name>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [30 x i8] c"Cannot open input file (%s).\0A\00"
@.str.3 = [3 x i8] c"wt\00"
@.str.4 = [31 x i8] c"Cannot open output file (%s).\0A\00"
@.str.5 = [26 x i8] c"unsigned char %s[] = {\0A \00"
@EOF = global i32 0
@.str.6 = [3 x i8] c", \00"
@.str.7 = [4 x i8] c"\0A \00"
@.str.8 = [7 x i8] c"0x%02X\00"
@.str.9 = [5 x i8] c"\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 = load i32, i32* %4
%11 = icmp slt i32 %10, 4
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32*, i32** @stderr
%14 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %75
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%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 = icmp eq i32* %19, null
br i1 %20, label %21, label %27
21: ; preds = %15
%22 = load i32*, i32** @stderr
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %25)
store i32 1, i32* %3
br label %75
27: ; preds = %15
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %31, i32** %7
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %39
33: ; preds = %27
%34 = load i32*, i32** @stderr
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %37)
store i32 1, i32* %3
br label %75
39: ; preds = %27
store i32 0, i32* %8
%40 = load i32*, i32** %7
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 3
%43 = load i8*, i8** %42
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %62, %39
%46 = load i32*, i32** %6
%47 = call i32 @fgetc(i32* %46)
store i32 %47, i32* %9
%48 = load i32, i32* @EOF
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %68
50: ; preds = %45
%51 = load i32, i32* %8
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32*, i32** %7
%55 = call i32 (i32*, i8*, ...) @fprintf(i32* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %56
56: ; preds = %53, %50
%57 = load i32, i32* %8
%58 = icmp sge i32 %57, 8
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i32*, i32** %7
%61 = call i32 (i32*, i8*, ...) @fprintf(i32* %60, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %8
br label %62
62: ; preds = %59, %56
%63 = load i32*, i32** %7
%64 = load i32, i32* %9
%65 = call i32 (i32*, i8*, ...) @fprintf(i32* %63, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i32 %64)
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %45
68: ; preds = %45
%69 = load i32*, i32** %7
%70 = call i32 (i32*, i8*, ...) @fprintf(i32* %69, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%71 = load i32*, i32** %6
%72 = call i32 @fclose(i32* %71)
%73 = load i32*, i32** %7
%74 = call i32 @fclose(i32* %73)
store i32 0, i32* %3
br label %75
75: ; preds = %68, %33, %21, %12
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@stderr = global i32* null
@.str = [61 x i8] c"Usage: bin2c <output bin file> <output h file> <array name>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [30 x i8] c"Cannot open input file (%s).\0A\00"
@.str.3 = [3 x i8] c"wt\00"
@.str.4 = [31 x i8] c"Cannot open output file (%s).\0A\00"
@.str.5 = [26 x i8] c"unsigned char %s[] = {\0A \00"
@EOF = global i32 0
@.str.6 = [3 x i8] c", \00"
@.str.7 = [4 x i8] c"\0A \00"
@.str.8 = [7 x i8] c"0x%02X\00"
@.str.9 = [5 x i8] c"\0A};\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 4
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32*, i32** @stderr
%6 = tail call i64 @fwrite(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i64 60, i64 1, i32* %5)
br label %52
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%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 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = load i32*, i32** @stderr
%14 = load i8*, i8** %8
%15 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %52
16: ; preds = %7
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32*, i32** @stderr
%23 = load i8*, i8** %17
%24 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %23)
br label %52
25: ; preds = %16
%26 = getelementptr inbounds i8*, i8** %1, i64 3
%27 = load i8*, i8** %26
%28 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %27)
%29 = tail call i32 @fgetc(i32* %10)
%30 = load i32, i32* @EOF
%31 = icmp eq i32 %29, %30
br i1 %31, label %48, label %32
32: ; preds = %25, %41
%33 = phi i32 [ %45, %41 ], [ %29, %25 ]
%34 = phi i32 [ %44, %41 ], [ 0, %25 ]
%35 = icmp eq i32 %34, 0
br i1 %35, label %41, label %36
36: ; preds = %32
%37 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i64 2, i64 1, i32* %19)
%38 = icmp sgt i32 %34, 7
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i64 3, i64 1, i32* %19)
br label %41
41: ; preds = %32, %39, %36
%42 = phi i32 [ 0, %39 ], [ %34, %36 ], [ 0, %32 ]
%43 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i32 %33)
%44 = add nsw i32 %42, 1
%45 = tail call i32 @fgetc(i32* %10)
%46 = load i32, i32* @EOF
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %32
48: ; preds = %41, %25
%49 = tail call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i64 4, i64 1, i32* %19)
%50 = tail call i32 @fclose(i32* %10)
%51 = tail call i32 @fclose(i32* %19)
br label %52
52: ; preds = %48, %21, %12, %4
%53 = phi i32 [ 1, %4 ], [ 1, %12 ], [ 1, %21 ], [ 0, %48 ]
ret i32 %53
}