#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 *, ...);
unsigned int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  unsigned char buf[8];
  unsigned int i, count, bytes = 0;
  FILE *fd_in, *fd_out;
  if (argc != 4) {
    fprintf(stderr, "\n\tusage: %s <ucode.bin> <array_name> <output_name>\n\n",
            argv[0]);
    return -1;
  }
  fd_in = fopen(argv[1], "rb");
  if (fd_in == NULL) {
    fprintf(stderr, "firmware file '%s' not found\n", argv[1]);
    return -1;
  }
  fd_out = fopen(argv[3], "w+");
  if (fd_out == NULL) {
    fprintf(stderr, "cannot create output file '%s'\n", argv[3]);
    return -1;
  }
  fprintf(fd_out, "\n#include <asm/types.h>\n\nu8 %s [] = {", argv[2]);
  while ((count = fread(buf, 1, 8, fd_in)) > 0) {
    fprintf(fd_out, "\n\t");
    for (i = 0; i < count; i++, bytes++)
      fprintf(fd_out, "0x%02x, ", buf[i]);
  }
  fprintf(fd_out, "\n};\n\n");
  fclose(fd_in);
  fclose(fd_out);
  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   $0x0,-0x24(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     1192 <main+0x52>
mov    0x2e72(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe81(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <main+0x1b3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe94(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e1 <main+0xa1>
mov    0x2e24(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe6a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <main+0x1b3>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe66(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1230 <main+0xf0>
mov    0x2dd5(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <main+0x1b3>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1030 <fread@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jbe    12c8 <main+0x188>
mov    -0x38(%rbp),%rdi
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    12c3 <main+0x183>
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%eax
movzbl -0x18(%rbp,%rax,1),%edx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1286 <main+0x146>
jmp    124a <main+0x10a>
mov    -0x38(%rbp),%rdi
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1040 <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
mov    %rsi,%rbx
cmp    $0x4,%edi
jne    1263 <main+0x113>
mov    0x8(%rbx),%rdi
lea    0xec3(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1279 <main+0x129>
mov    %rax,%r12
mov    0x18(%rbx),%rdi
lea    0xec8(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1290 <main+0x140>
mov    %rax,%r13
mov    0x10(%rbx),%rdx
lea    0xecf(%rip),%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     1236 <main+0xe6>
mov    %eax,%r14d
lea    0xecb(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    $0x2,%esi
mov    $0x1,%edx
lea    0xeae(%rip),%rdi
mov    %r13,%rcx
call   1070 <fwrite@plt>
mov    %r14d,%ebx
xor    %ebp,%ebp
xchg   %ax,%ax
movzbl (%rsp,%rbp,1),%edx
mov    %r13,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1200 <main+0xb0>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    11e0 <main+0x90>
lea    0xe6e(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    12b1 <main+0x161>
mov    0x2d76(%rip),%rax
movslq (%rax),%rdi
mov    (%rbx),%rdx
lea    0xd89(%rip),%rsi
jmp    12a5 <main+0x155>
mov    0x2d60(%rip),%rax
movslq (%rax),%rdi
mov    0x8(%rbx),%rdx
lea    0xdaa(%rip),%rsi
jmp    12a5 <main+0x155>
mov    0x2d49(%rip),%rax
movslq (%rax),%rdi
mov    0x18(%rbx),%rdx
lea    0xdb4(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%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 = [53 x i8] c"\0A\09usage: %s <ucode.bin> <array_name> <output_name>\0A\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [30 x i8] c"firmware file '%s' not found\0A\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [32 x i8] c"cannot create output file '%s'\0A\00"
@.str.5 = [38 x i8] c"\0A#include <asm/types.h>\0A\0Au8 %s [] = {\00"
@.str.6 = [3 x i8] c"\0A\09\00"
@.str.7 = [9 x i8] c"0x%02x, \00"
@.str.8 = [6 x i8] c"\0A};\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [8 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 4
br i1 %13, label %14, label %22
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i32*
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 -1, i32* %3
br label %91
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %10
%27 = load i32*, i32** %10
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %37
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = sext i32 %30 to i64
%32 = inttoptr i64 %31 to i32*
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %32, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %35)
store i32 -1, i32* %3
br label %91
37: ; preds = %22
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 3
%40 = load i8*, i8** %39
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %41, i32** %11
%42 = load i32*, i32** %11
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %52
44: ; preds = %37
%45 = load i32, i32* @stderr
%46 = sext i32 %45 to i64
%47 = inttoptr i64 %46 to i32*
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 3
%50 = load i8*, i8** %49
%51 = call i32 (i32*, i8*, ...) @fprintf(i32* %47, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %50)
store i32 -1, i32* %3
br label %91
52: ; preds = %37
%53 = load i32*, i32** %11
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 2
%56 = load i8*, i8** %55
%57 = call i32 (i32*, i8*, ...) @fprintf(i32* %53, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %83, %52
%59 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%60 = load i32*, i32** %10
%61 = call i32 @fread(i8* %59, i32 1, i32 8, i32* %60)
store i32 %61, i32* %8
%62 = icmp ugt i32 %61, 0
br i1 %62, label %63, label %84
63: ; preds = %58
%64 = load i32*, i32** %11
%65 = call i32 (i32*, i8*, ...) @fprintf(i32* %64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %7
br label %66
66: ; preds = %78, %63
%67 = load i32, i32* %7
%68 = load i32, i32* %8
%69 = icmp ult i32 %67, %68
br i1 %69, label %70, label %83
70: ; preds = %66
%71 = load i32*, i32** %11
%72 = load i32, i32* %7
%73 = zext i32 %72 to i64
%74 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 %73
%75 = load i8, i8* %74
%76 = zext i8 %75 to i32
%77 = call i32 (i32*, i8*, ...) @fprintf(i32* %71, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %76)
br label %78
78: ; preds = %70
%79 = load i32, i32* %7
%80 = add i32 %79, 1
store i32 %80, i32* %7
%81 = load i32, i32* %9
%82 = add i32 %81, 1
store i32 %82, i32* %9
br label %66
83: ; preds = %66
br label %58
84: ; preds = %58
%85 = load i32*, i32** %11
%86 = call i32 (i32*, i8*, ...) @fprintf(i32* %85, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%87 = load i32*, i32** %10
%88 = call i32 @fclose(i32* %87)
%89 = load i32*, i32** %11
%90 = call i32 @fclose(i32* %89)
store i32 0, i32* %3
br label %91
91: ; preds = %84, %44, %29, %14
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [53 x i8] c"\0A\09usage: %s <ucode.bin> <array_name> <output_name>\0A\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [30 x i8] c"firmware file '%s' not found\0A\00"
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [32 x i8] c"cannot create output file '%s'\0A\00"
@.str.5 = [38 x i8] c"\0A#include <asm/types.h>\0A\0Au8 %s [] = {\00"
@.str.6 = [3 x i8] c"\0A\09\00"
@.str.7 = [9 x i8] c"0x%02x, \00"
@.str.8 = [6 x i8] c"\0A};\0A\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8 x i8]
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 4
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i32*
%10 = load i8*, i8** %1
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %9, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %59
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = sext i32 %18 to i64
%20 = inttoptr i64 %19 to i32*
%21 = load i8*, i8** %13
%22 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %59
23: ; preds = %12
%24 = getelementptr inbounds i8*, i8** %1, i64 3
%25 = load i8*, i8** %24
%26 = tail call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i32*
%32 = load i8*, i8** %24
%33 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %59
34: ; preds = %23
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %36)
%38 = call i32 @fread(i8* %4, i32 1, i32 8, i32* %15)
%39 = icmp eq i32 %38, 0
br i1 %39, label %55, label %43
40: ; preds = %47
%41 = call i32 @fread(i8* %4, i32 1, i32 8, i32* %15)
%42 = icmp eq i32 %41, 0
br i1 %42, label %55, label %43
43: ; preds = %34, %40
%44 = phi i32 [ %41, %40 ], [ %38, %34 ]
%45 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i64 2, i64 1, i32* %26)
%46 = zext i32 %44 to i64
br label %47
47: ; preds = %43, %47
%48 = phi i64 [ 0, %43 ], [ %53, %47 ]
%49 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %51)
%53 = add nuw nsw i64 %48, 1
%54 = icmp eq i64 %53, %46
br i1 %54, label %40, label %47
55: ; preds = %40, %34
%56 = tail call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i64 5, i64 1, i32* %26)
%57 = tail call i32 @fclose(i32* %15)
%58 = tail call i32 @fclose(i32* %26)
br label %59
59: ; preds = %55, %28, %17, %6
%60 = phi i32 [ -1, %6 ], [ -1, %17 ], [ -1, %28 ], [ 0, %55 ]
ret i32 %60
}