#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*/ *);
char *filename2varname(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int main(int argc, char **argv) {
  FILE *fd;
  int bytebuff, column = 1;
  long bytecount = 0;
  char *varname, *filename;
  if ((argc != 2) || (argv[1][0] == '-')) {
    puts("file2c transforms a data file into C code. Copyright (C) Mateusz "
         "Viste 2014");
    puts("Usage: file2c file.dat");
    return (1);
  }
  filename = argv[1];
  varname = filename2varname(filename);
  if (varname == NULL) {
    puts("Error: unable to parse filename.");
    return (2);
  }
  fd = fopen(filename, "rb");
  if (fd == NULL) {
    printf("Error: failed to open '%s'.\n", argv[1]);
    return (3);
  }
  printf("unsigned char %s[] = {\n", varname);
  for (;;) {
    bytebuff = getc(fd);
    if (bytebuff < 0)
      break;
    if (bytecount > 0)
      printf(",");
    if (column > 15) {
      column = 1;
      printf("\n");
    }
    column += 1;
    bytecount += 1;
    printf("0x%02X", bytebuff);
  }
  if (bytecount > 0)
    printf(",");
  printf("0x00};\n");
  printf("long %s_len = %ldl;\n", varname, bytecount);
  fclose(fd);
  free(varname);
  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)
movl   $0x1,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11b3 <main+0x43>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    11d7 <main+0x67>
lea    0xe46(%rip),%rdi
call   1040 <puts@plt>
lea    0xe86(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    133a <main+0x1ca>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <filename2varname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1213 <main+0xa3>
lea    0xe61(%rip),%rdi
call   1040 <puts@plt>
movl   $0x2,-0x4(%rbp)
jmp    133a <main+0x1ca>
mov    -0x38(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1254 <main+0xe4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    133a <main+0x1ca>
mov    -0x30(%rbp),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <getc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1281 <main+0x111>
jmp    12e4 <main+0x174>
cmpq   $0x0,-0x28(%rbp)
jle    129a <main+0x12a>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0xf,-0x20(%rbp)
jle    12b9 <main+0x149>
movl   $0x1,-0x20(%rbp)
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1266 <main+0xf6>
cmpq   $0x0,-0x28(%rbp)
jle    12fd <main+0x18d>
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@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
jne    119d <main+0x1d>
mov    %rsi,%rbx
mov    0x8(%rsi),%rbp
cmpb   $0x2d,0x0(%rbp)
jne    11cd <main+0x4d>
lea    0xe5c(%rip),%rdi
call   1050 <puts@plt>
lea    0xe9c(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
call   1090 <filename2varname@plt>
test   %rax,%rax
je     12a4 <main+0x124>
mov    %rax,%r14
lea    0xe9c(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12bb <main+0x13b>
mov    %rax,%r15
lea    0xea1(%rip),%rdi
xor    %r12d,%r12d
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
call   10a0 <getc@plt>
test   %eax,%eax
js     12d8 <main+0x158>
mov    %r14,(%rsp)
lea    0xe91(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
call   10a0 <getc@plt>
test   %eax,%eax
js     12dc <main+0x15c>
mov    %eax,%ebp
mov    $0x1,%ebx
mov    $0x2,%r14d
lea    0xe64(%rip),%r13
jmp    1282 <main+0x102>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14d
add    $0x1,%rbx
mov    %r13,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
call   10a0 <getc@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12e1 <main+0x161>
mov    $0x2c,%edi
call   1040 <putchar@plt>
cmp    $0x10,%r14d
jl     1260 <main+0xe0>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    $0x1,%r14d
jmp    1260 <main+0xe0>
lea    0xdb8(%rip),%rdi
call   1050 <puts@plt>
mov    $0x2,%r12d
jmp    11bb <main+0x3b>
mov    0x8(%rbx),%rsi
lea    0xdc1(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x3,%r12d
jmp    11bb <main+0x3b>
xor    %ebx,%ebx
jmp    12ef <main+0x16f>
mov    $0x1,%ebx
mov    $0x2c,%edi
call   1040 <putchar@plt>
mov    (%rsp),%r14
lea    0xde2(%rip),%rdi
call   1050 <puts@plt>
lea    0xdc1(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11bb <main+0x3b>

LLVM IR (-O0):
@.str = [76 x i8] c"file2c transforms a data file into C code. Copyright (C) Mateusz Viste 2014\00"
@.str.1 = [23 x i8] c"Usage: file2c file.dat\00"
@.str.2 = [33 x i8] c"Error: unable to parse filename.\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [29 x i8] c"Error: failed to open '%s'.\0A\00"
@.str.5 = [24 x i8] c"unsigned char %s[] = {\0A\00"
@.str.6 = [2 x i8] c",\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [7 x i8] c"0x%02X\00"
@.str.9 = [8 x i8] c"0x00};\0A\00"
@.str.10 = [21 x i8] c"long %s_len = %ldl;\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 i64
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
store i64 0, i64* %9
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %22, label %14
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 45
br i1 %21, label %22, label %25
22: ; preds = %14, %2
%23 = call i32 @puts(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %85
25: ; preds = %14
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %11
%29 = load i8*, i8** %11
%30 = call i8* @filename2varname(i8* %29)
store i8* %30, i8** %10
%31 = load i8*, i8** %10
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %85
35: ; preds = %25
%36 = load i8*, i8** %11
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %37, i32** %6
%38 = load i32*, i32** %6
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %43)
store i32 3, i32* %3
br label %85
45: ; preds = %35
%46 = load i8*, i8** %10
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %64, %45
%49 = load i32*, i32** %6
%50 = call i32 @getc(i32* %49)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %48
br label %71
54: ; preds = %48
%55 = load i64, i64* %9
%56 = icmp sgt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %59
59: ; preds = %57, %54
%60 = load i32, i32* %8
%61 = icmp sgt i32 %60, 15
br i1 %61, label %62, label %64
62: ; preds = %59
store i32 1, i32* %8
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %64
64: ; preds = %62, %59
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
%67 = load i64, i64* %9
%68 = add nsw i64 %67, 1
store i64 %68, i64* %9
%69 = load i32, i32* %7
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i32 %69)
br label %48
71: ; preds = %53
%72 = load i64, i64* %9
%73 = icmp sgt i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %71
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %76
76: ; preds = %74, %71
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
%78 = load i8*, i8** %10
%79 = load i64, i64* %9
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0), i8* %78, i64 %79)
%81 = load i32*, i32** %6
%82 = call i32 @fclose(i32* %81)
%83 = load i8*, i8** %10
%84 = call i32 @free(i8* %83)
store i32 0, i32* %3
br label %85
85: ; preds = %76, %40, %33, %22
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
@.str = [76 x i8] c"file2c transforms a data file into C code. Copyright (C) Mateusz Viste 2014\00"
@.str.1 = [23 x i8] c"Usage: file2c file.dat\00"
@.str.2 = [33 x i8] c"Error: unable to parse filename.\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [29 x i8] c"Error: failed to open '%s'.\0A\00"
@.str.5 = [24 x i8] c"unsigned char %s[] = {\0A\00"
@.str.8 = [7 x i8] c"0x%02X\00"
@.str.10 = [21 x i8] c"long %s_len = %ldl;\0A\00"
@str = [7 x i8] c"0x00};\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 45
br i1 %8, label %9, label %12
9: ; preds = %4, %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([76 x i8], [76 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %55
12: ; preds = %4
%13 = tail call i8* @filename2varname(i8* %6)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %55
17: ; preds = %12
%18 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i8*, i8** %5
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %21)
br label %55
23: ; preds = %17
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %13)
%25 = tail call i32 @getc(i32* %18)
%26 = icmp slt i32 %25, 0
br i1 %26, label %49, label %27
27: ; preds = %23
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i32 %25)
%29 = tail call i32 @getc(i32* %18)
%30 = icmp slt i32 %29, 0
br i1 %30, label %46, label %31
31: ; preds = %27, %39
%32 = phi i32 [ %44, %39 ], [ %29, %27 ]
%33 = phi i32 [ %41, %39 ], [ 2, %27 ]
%34 = phi i64 [ %42, %39 ], [ 1, %27 ]
%35 = tail call i32 @putchar(i32 44)
%36 = icmp sgt i32 %33, 15
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = tail call i32 @putchar(i32 10)
br label %39
39: ; preds = %37, %31
%40 = phi i32 [ 1, %37 ], [ %33, %31 ]
%41 = add nsw i32 %40, 1
%42 = add nuw nsw i64 %34, 1
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i32 %32)
%44 = tail call i32 @getc(i32* %18)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %31
46: ; preds = %39, %27
%47 = phi i64 [ 1, %27 ], [ %42, %39 ]
%48 = tail call i32 @putchar(i32 44)
br label %49
49: ; preds = %23, %46
%50 = phi i64 [ %47, %46 ], [ 0, %23 ]
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0), i8* %13, i64 %50)
%53 = tail call i32 @fclose(i32* %18)
%54 = tail call i32 @free(i8* %13)
br label %55
55: ; preds = %49, %20, %15, %9
%56 = phi i32 [ 1, %9 ], [ 2, %15 ], [ 3, %20 ], [ 0, %49 ]
ret i32 %56
}