#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 uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  size_t count;
  if (argc < 3) {
    fprintf(stderr, "Usage: %s <infile> <arrayname>\n", argv[0]);
    return 1;
  }
  FILE *in;
  if ((in = fopen(argv[1], "rb")) == NULL) {
    perror("could not open input file");
    return 1;
  }
  printf("const uint8_t %s[] = {", argv[2]);
  count = 0;
  while (1) {
    uint8_t c = fgetc(in);
    if (feof(in))
      break;
    if (!(count % 8)) {
      printf("%s\n  0x%02x", count ? "," : "", c);
    } else {
      printf(", 0x%02x", c);
    }
    count++;
  }
  printf("\n};");
  fclose(in);
  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   $0x3,-0x8(%rbp)
jge    11b8 <main+0x48>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <main+0x146>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe59(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11f2 <main+0x82>
lea    0xe42(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b6 <main+0x146>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     1234 <main+0xc4>
jmp    1298 <main+0x128>
mov    -0x18(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
jne    1276 <main+0x106>
mov    -0x18(%rbp),%rcx
lea    0xdce(%rip),%rsi
lea    0xe08(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rsi
mov    -0x24(%rbp),%edx
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1287 <main+0x117>
mov    -0x24(%rbp),%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1210 <main+0xa0>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@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
mov    %rsi,%rbp
cmp    $0x2,%edi
jg     11a4 <main+0x34>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xe69(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%r15d
jmp    1202 <main+0x92>
mov    0x8(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1280 <main+0x110>
mov    %rax,%rbx
mov    0x10(%rbp),%rsi
lea    0xe72(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     1214 <main+0xa4>
lea    0xe78(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe39(%rip),%r14
lea    0xe40(%rip),%r12
xor    %r13d,%r13d
jmp    1257 <main+0xe7>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r13
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    11ec <main+0x7c>
test   $0x7,%r13b
jne    1230 <main+0xc0>
test   %r13,%r13
lea    0xdf9(%rip),%rsi
lea    0xdb1(%rip),%rax
cmove  %rax,%rsi
mov    %r14,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    123c <main+0xcc>
lea    0xd9c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%r15d
jmp    1202 <main+0x92>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Usage: %s <infile> <arrayname>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"could not open input file\00"
@.str.3 = [23 x i8] c"const uint8_t %s[] = {\00"
@.str.4 = [12 x i8] c"%s\0A 0x%02x\00"
@.str.5 = [2 x i8] c",\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [9 x i8] c", 0x%02x\00"
@.str.8 = [4 x i8] c"\0A};\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %58
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %7
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %58
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %28)
store i64 0, i64* %6
br label %30
30: ; preds = %25, %51
%31 = load i32*, i32** %7
%32 = call i32 @fgetc(i32* %31)
store i32 %32, i32* %8
%33 = load i32*, i32** %7
%34 = call i64 @feof(i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %30
br label %54
37: ; preds = %30
%38 = load i64, i64* %6
%39 = urem i64 %38, 8
%40 = icmp ne i64 %39, 0
br i1 %40, label %48, label %41
41: ; preds = %37
%42 = load i64, i64* %6
%43 = icmp ne i64 %42, 0
%44 = zext i1 %43 to i64
%45 = select i1 %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0)
%46 = load i32, i32* %8
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %45, i32 %46)
br label %51
48: ; preds = %37
%49 = load i32, i32* %8
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %48, %41
%52 = load i64, i64* %6
%53 = add i64 %52, 1
store i64 %53, i64* %6
br label %30
54: ; preds = %36
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
%56 = load i32*, i32** %7
%57 = call i32 @fclose(i32* %56)
store i32 0, i32* %3
br label %58
58: ; preds = %54, %23, %11
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Usage: %s <infile> <arrayname>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"could not open input file\00"
@.str.3 = [23 x i8] c"const uint8_t %s[] = {\00"
@.str.4 = [12 x i8] c"%s\0A 0x%02x\00"
@.str.5 = [2 x i8] c",\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [9 x i8] c", 0x%02x\00"
@.str.8 = [4 x i8] c"\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 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %41
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %41
15: ; preds = %8
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = tail call i32 @fgetc(i32* %11)
%20 = tail call i64 @feof(i32* %11)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %38
22: ; preds = %15, %33
%23 = phi i32 [ %35, %33 ], [ %19, %15 ]
%24 = phi i64 [ %34, %33 ], [ 0, %15 ]
%25 = and i64 %24, 7
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = icmp eq i64 %24, 0
%29 = select i1 %28, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0)
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %29, i32 %23)
br label %33
31: ; preds = %22
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %23)
br label %33
33: ; preds = %27, %31
%34 = add i64 %24, 1
%35 = tail call i32 @fgetc(i32* %11)
%36 = tail call i64 @feof(i32* %11)
%37 = icmp eq i64 %36, 0
br i1 %37, label %22, label %38
38: ; preds = %33, %15
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
%40 = tail call i32 @fclose(i32* %11)
br label %41
41: ; preds = %13, %38, %4
%42 = phi i32 [ 1, %4 ], [ 1, %13 ], [ 0, %38 ]
ret i32 %42
}