#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  if (argc < 4) {
    printf("Usage: bin2h [in] [out] [var]\n");
    return 1;
  }
  FILE *fptr = fopen(argv[1], "rb");
  if (fptr == NULL) {
    printf("Could not open %s\n", argv[1]);
    return 1;
  }
  unsigned char buf[4096];
  int l = fread(buf, 1, 4096, fptr);
  fclose(fptr);
  if (l <= 0) {
    printf("%s is empty\n", argv[1]);
    return 1;
  }
  fptr = fopen(argv[2], "w");
  if (fptr == NULL) {
    printf("Could not open %s\n", argv[2]);
    return 1;
  }
  fprintf(fptr, "unsigned char %s[] = {", argv[3]);
  for (int i = 0; i < l; ++i) {
    if ((i & 15) == 0) {
      fprintf(fptr, "\n  ");
    }
    fprintf(fptr, "0x%2.2x, ", buf[i]);
  }
  fprintf(fptr, "\n};\n");
  fclose(fptr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    118d <main+0x3d>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1320 <main+0x1d0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe83(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d2 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1320 <main+0x1d0>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %eax,-0x1024(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x1024(%rbp)
jg     122a <main+0xda>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1320 <main+0x1d0>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe09(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    126f <main+0x11f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1320 <main+0x1d0>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x1028(%rbp)
mov    -0x1028(%rbp),%eax
cmp    -0x1024(%rbp),%eax
jge    12fe <main+0x1ae>
mov    -0x1028(%rbp),%eax
and    $0xf,%eax
cmp    $0x0,%eax
jne    12c9 <main+0x179>
mov    -0x18(%rbp),%rdi
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
movslq -0x1028(%rbp),%rax
movzbl -0x1020(%rbp,%rax,1),%edx
ff
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1028(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1028(%rbp)
jmp    1293 <main+0x143>
mov    -0x18(%rbp),%rdi
lea    0xd60(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
cmp    $0x3,%edi
jg     1195 <main+0x25>
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
jmp    12b8 <main+0x148>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0xe5d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1267 <main+0xf7>
mov    %rax,%rbp
mov    %rsp,%rdi
mov    $0x1,%ebx
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1050 <fclose@plt>
test   %r15d,%r15d
jle    1292 <main+0x122>
mov    0x10(%r14),%rdi
lea    0xe36(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12a6 <main+0x136>
mov    %rax,%rbx
mov    0x18(%r14),%rdx
lea    0xe1c(%rip),%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15d,%r12d
lea    0xe1f(%rip),%r14
lea    0xe1c(%rip),%r15
xor    %ebp,%ebp
jmp    124a <main+0xda>
nopl   0x0(%rax,%rax,1)
movzbl (%rsp,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
je     126d <main+0xfd>
test   $0xf,%bpl
jne    1230 <main+0xc0>
mov    $0x3,%esi
mov    $0x1,%edx
mov    %r14,%rdi
mov    %rbx,%rcx
call   1090 <fwrite@plt>
jmp    1230 <main+0xc0>
mov    0x8(%r14),%rsi
jmp    12aa <main+0x13a>
lea    0xdd6(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %ebx,%ebx
jmp    12bd <main+0x14d>
mov    0x8(%r14),%rsi
lea    0xd79(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12bd <main+0x14d>
mov    0x10(%r14),%rsi
lea    0xd52(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Usage: bin2h [in] [out] [var]\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [19 x i8] c"Could not open %s\0A\00"
@.str.3 = [13 x i8] c"%s is empty\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [23 x i8] c"unsigned char %s[] = {\00"
@.str.6 = [4 x i8] c"\0A \00"
@.str.7 = [10 x i8] c"0x%2.2x, \00"
@.str.8 = [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 [4096 x i8]
%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 %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %84
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %6
%19 = load i32*, i32** %6
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 1, i32* %3
br label %84
26: ; preds = %14
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%28 = load i32*, i32** %6
%29 = call i32 @fread(i8* %27, i32 1, i32 4096, i32* %28)
store i32 %29, i32* %8
%30 = load i32*, i32** %6
%31 = call i32 @fclose(i32* %30)
%32 = load i32, i32* %8
%33 = icmp sle i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %26
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %37)
store i32 1, i32* %3
br label %84
39: ; preds = %26
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %43, i32** %6
%44 = load i32*, i32** %6
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %51
46: ; preds = %39
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 2
%49 = load i8*, i8** %48
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %49)
store i32 1, i32* %3
br label %84
51: ; preds = %39
%52 = load i32*, i32** %6
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 3
%55 = load i8*, i8** %54
%56 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %55)
store i32 0, i32* %9
br label %57
57: ; preds = %76, %51
%58 = load i32, i32* %9
%59 = load i32, i32* %8
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %79
61: ; preds = %57
%62 = load i32, i32* %9
%63 = and i32 %62, 15
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %61
%66 = load i32*, i32** %6
%67 = call i32 (i32*, i8*, ...) @fprintf(i32* %66, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
br label %68
68: ; preds = %65, %61
%69 = load i32*, i32** %6
%70 = load i32, i32* %9
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %71
%73 = load i8, i8* %72
%74 = zext i8 %73 to i32
%75 = call i32 (i32*, i8*, ...) @fprintf(i32* %69, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %74)
br label %76
76: ; preds = %68
%77 = load i32, i32* %9
%78 = add nsw i32 %77, 1
store i32 %78, i32* %9
br label %57
79: ; preds = %57
%80 = load i32*, i32** %6
%81 = call i32 (i32*, i8*, ...) @fprintf(i32* %80, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%82 = load i32*, i32** %6
%83 = call i32 @fclose(i32* %82)
store i32 0, i32* %3
br label %84
84: ; preds = %79, %46, %34, %21, %12
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [19 x i8] c"Could not open %s\0A\00"
@.str.3 = [13 x i8] c"%s is empty\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [23 x i8] c"unsigned char %s[] = {\00"
@.str.6 = [4 x i8] c"\0A \00"
@.str.7 = [10 x i8] c"0x%2.2x, \00"
@.str.8 = [5 x i8] c"\0A};\0A\00"
@str = [30 x i8] c"Usage: bin2h [in] [out] [var]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = icmp slt i32 %0, 4
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %54
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 %15
12: ; preds = %7
%13 = load i8*, i8** %8
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %54
15: ; preds = %7
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%17 = call i32 @fread(i8* %16, i32 1, i32 4096, i32* %10)
%18 = tail call i32 @fclose(i32* %10)
%19 = icmp slt i32 %17, 1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i8*, i8** %8
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %21)
br label %52
23: ; preds = %15
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = tail call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i8*, i8** %24
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %52
31: ; preds = %23
%32 = getelementptr inbounds i8*, i8** %1, i64 3
%33 = load i8*, i8** %32
%34 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %33)
%35 = zext i32 %17 to i64
br label %39
36: ; preds = %45
%37 = tail call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i64 4, i64 1, i32* %26)
%38 = tail call i32 @fclose(i32* %26)
br label %52
39: ; preds = %31, %45
%40 = phi i64 [ 0, %31 ], [ %50, %45 ]
%41 = and i64 %40, 15
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i64 3, i64 1, i32* %26)
br label %45
45: ; preds = %43, %39
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %40
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %48)
%50 = add nuw nsw i64 %40, 1
%51 = icmp eq i64 %50, %35
br i1 %51, label %36, label %39
52: ; preds = %36, %28, %20
%53 = phi i32 [ 1, %20 ], [ 1, %28 ], [ 0, %36 ]
br label %54
54: ; preds = %12, %52, %5
%55 = phi i32 [ 1, %5 ], [ 1, %12 ], [ %53, %52 ]
ret i32 %55
}