#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*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE *fp;
  char *filename = NULL;
  char *varname = NULL;
  int letter;
  size_t size;
  if (argc < 3) {
    fprintf(stderr, "too few input parameters!\n\n");
    printf("bin2c: convert a binary file to a large C array\n"
           "USAGE:\n"
           "   bin2c file_name variable_name\n\n");
    return 1;
  }
  filename = argv[1];
  varname = argv[2];
  fp = fopen(filename, "rb");
  if (!fp) {
    fprintf(stderr, "bin2c error: cannot open the file %s\n", filename);
    return 2;
  }
  size = 0;
  printf("unsigned char %s[] = { ", varname);
  while ((letter = fgetc(fp)) != EOF) {
    assert(letter >= 0 && letter <= 255);
    printf("0x%02X,\n", letter);
    size++;
  }
  printf("};\n\n");
  printf("/* The size of the array is %lu. */", size);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11c1 <main+0x61>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cf <main+0x16f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121f <main+0xbf>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe6d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12cf <main+0x16f>
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%eax
je     129f <main+0x13f>
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x39(%rbp)
jl     1270 <main+0x110>
cmpl   $0xff,-0x2c(%rbp)
setle  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x2c(%rbp),%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1239 <main+0xd9>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rsi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     11ae <main+0x3e>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
lea    0xef0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
jmp    1286 <main+0x116>
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%rbp
lea    0xe5f(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11fa <main+0x8a>
mov    %rax,%r15
lea    0xe71(%rip),%rdi
xor    %r14d,%r14d
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1060 <fgetc@plt>
mov    0x2df8(%rip),%r13
cmp    0x0(%r13),%eax
jne    121c <main+0xac>
xor    %ebx,%ebx
jmp    1261 <main+0xf1>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x2,%r14d
jmp    1286 <main+0x116>
mov    %eax,%ebp
lea    0xe38(%rip),%r12
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %edi,%edi
cmp    $0x100,%ebp
setb   %dil
call   1070 <assert@plt>
mov    %r12,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
mov    %r15,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebp
cmp    0x0(%r13),%eax
jne    1230 <main+0xc0>
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
lea    0xdf2(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"too few input parameters!\0A\0A\00"
@.str.1 = [90 x i8] c"bin2c: convert a binary file to a large C array\0AUSAGE:\0A bin2c file_name variable_name\0A\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [38 x i8] c"bin2c error: cannot open the file %s\0A\00"
@.str.4 = [24 x i8] c"unsigned char %s[] = { \00"
@EOF = global i32 0
@.str.5 = [9 x i8] c"0x%02X,\0A\00"
@.str.6 = [5 x i8] c"};\0A\0A\00"
@.str.7 = [36 x i8] c"/* The size of the array is %lu. */\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i8* null, i8** %8
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %60
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %7
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
store i8* %23, i8** %8
%24 = load i8*, i8** %7
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %6
%26 = load i32*, i32** %6
%27 = icmp ne i32* %26, null
br i1 %27, label %32, label %28
28: ; preds = %17
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %7
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %30)
store i32 2, i32* %3
br label %60
32: ; preds = %17
store i64 0, i64* %10
%33 = load i8*, i8** %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %46, %32
%36 = load i32*, i32** %6
%37 = call i32 @fgetc(i32* %36)
store i32 %37, i32* %9
%38 = load i32, i32* @EOF
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %54
40: ; preds = %35
%41 = load i32, i32* %9
%42 = icmp sge i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* %9
%45 = icmp sle i32 %44, 255
br label %46
46: ; preds = %43, %40
%47 = phi i1 [ false, %40 ], [ %45, %43 ]
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = load i32, i32* %9
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %50)
%52 = load i64, i64* %10
%53 = add i64 %52, 1
store i64 %53, i64* %10
br label %35
54: ; preds = %35
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%56 = load i64, i64* %10
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i64 %56)
%58 = load i32*, i32** %6
%59 = call i32 @fclose(i32* %58)
store i32 0, i32* %3
br label %60
60: ; preds = %54, %28, %13
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"too few input parameters!\0A\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [38 x i8] c"bin2c error: cannot open the file %s\0A\00"
@.str.4 = [24 x i8] c"unsigned char %s[] = { \00"
@EOF = global i32 0
@.str.5 = [9 x i8] c"0x%02X,\0A\00"
@.str.7 = [36 x i8] c"/* The size of the array is %lu. */\00"
@str = [4 x i8] c"};\0A\00"
@str.8 = [89 x i8] c"bin2c: convert a binary file to a large C array\0AUSAGE:\0A bin2c file_name variable_name\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([89 x i8], [89 x i8]* @str.8, i64 0, i64 0))
br label %39
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %10)
br label %39
18: ; preds = %8
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %12)
%20 = tail call i32 @fgetc(i32* %13)
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %34, label %23
23: ; preds = %18, %23
%24 = phi i32 [ %31, %23 ], [ %20, %18 ]
%25 = phi i64 [ %30, %23 ], [ 0, %18 ]
%26 = icmp ult i32 %24, 256
%27 = zext i1 %26 to i32
%28 = tail call i32 @assert(i32 %27)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %24)
%30 = add i64 %25, 1
%31 = tail call i32 @fgetc(i32* %13)
%32 = load i32, i32* @EOF
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %23
34: ; preds = %23, %18
%35 = phi i64 [ 0, %18 ], [ %30, %23 ]
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i64 %35)
%38 = tail call i32 @fclose(i32* %13)
br label %39
39: ; preds = %34, %15, %4
%40 = phi i32 [ 1, %4 ], [ 0, %34 ], [ 2, %15 ]
ret i32 %40
}