#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 feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
int write_file(FILE *dest, const char *input_file_name,
               const char *symbol_name) {
  FILE *file;
  char buf[16];
  int chars_read;
  int i, eof, count = 0;
  file = fopen(input_file_name, "rb");
  fprintf(dest, "unsigned char %s[] = {\n", symbol_name);
  do {
    chars_read = fread(buf, 1, 16, file);
    eof = feof(file);
    fprintf(dest, "   ");
    for (i = 0; i < chars_read; i++) {
      count++;
      fprintf(dest, "0x%02x", buf[i]);
      if (i < chars_read - 1 || !eof) {
        fprintf(dest, ",");
      }
    }
    fprintf(dest, "\n");
  } while (!eof);
  fprintf(dest, "};\n");
  fclose(file);
  return count;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe75(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <feof@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1244 <write_file+0xf4>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x38(%rbp),%rax
movsbl -0x30(%rbp,%rax,1),%edx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%eax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     121f <write_file+0xcf>
cmpl   $0x0,-0x3c(%rbp)
jne    1231 <write_file+0xe1>
mov    -0x8(%rbp),%rdi
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1236 <write_file+0xe6>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11d4 <write_file+0x84>
mov    -0x8(%rbp),%rdi
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x0,-0x3c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1195 <write_file+0x45>
mov    -0x8(%rbp),%rdi
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x40(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%rbp
mov    %rsi,%rax
mov    %rdi,%rbx
lea    0xe72(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
mov    %rax,0x8(%rsp)
lea    0xe61(%rip),%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %r14d,%r14d
jmp    11dd <write_file+0x6d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    0x4(%rsp),%ebp
mov    %ebp,%r14d
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
test   %r15d,%r15d
jne    127d <write_file+0x10d>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    0x8(%rsp),%rbp
mov    %rbp,%rcx
call   1030 <fread@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
call   1070 <feof@plt>
mov    %eax,%r15d
mov    $0x3,%esi
mov    $0x1,%edx
lea    0xe03(%rip),%rdi
mov    %rbx,%rcx
call   1090 <fwrite@plt>
test   %r12d,%r12d
jle    11c7 <write_file+0x57>
mov    %r14d,0x4(%rsp)
lea    -0x1(%r12),%r13d
mov    %r12,%rbp
mov    %r12d,%r14d
xor    %r12d,%r12d
jmp    125a <write_file+0xea>
nopw   0x0(%rax,%rax,1)
mov    $0x2c,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0x1,%r12
cmp    %r12,%r14
je     11c0 <write_file+0x50>
movsbl 0x10(%rsp,%r12,1),%edx
mov    %rbx,%rdi
lea    0xdb5(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %r15d,%r15d
je     1240 <write_file+0xd0>
cmp    %r13,%r12
jae    124d <write_file+0xdd>
jmp    1240 <write_file+0xd0>
lea    0xda2(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    0x8(%rsp),%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [24 x i8] c"unsigned char %s[] = {\0A\00"
@.str.2 = [4 x i8] c" \00"
@.str.3 = [7 x i8] c"0x%02x\00"
@.str.4 = [2 x i8] c",\00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [4 x i8] c"};\0A\00"
define i32 @write_file(i32* %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [16 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %12
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %4
%16 = load i8*, i8** %6
%17 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %57, %3
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%20 = load i32*, i32** %7
%21 = call i32 @fread(i8* %19, i32 1, i32 16, i32* %20)
store i32 %21, i32* %9
%22 = load i32*, i32** %7
%23 = call i32 @feof(i32* %22)
store i32 %23, i32* %11
%24 = load i32*, i32** %4
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %10
br label %26
26: ; preds = %51, %18
%27 = load i32, i32* %10
%28 = load i32, i32* %9
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %54
30: ; preds = %26
%31 = load i32, i32* %12
%32 = add nsw i32 %31, 1
store i32 %32, i32* %12
%33 = load i32*, i32** %4
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %10
%41 = load i32, i32* %9
%42 = sub nsw i32 %41, 1
%43 = icmp slt i32 %40, %42
br i1 %43, label %47, label %44
44: ; preds = %30
%45 = load i32, i32* %11
%46 = icmp ne i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %44, %30
%48 = load i32*, i32** %4
%49 = call i32 (i32*, i8*, ...) @fprintf(i32* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %47, %44
br label %51
51: ; preds = %50
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1
store i32 %53, i32* %10
br label %26
54: ; preds = %26
%55 = load i32*, i32** %4
%56 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %57
57: ; preds = %54
%58 = load i32, i32* %11
%59 = icmp ne i32 %58, 0
%60 = xor i1 %59, true
br i1 %60, label %18, label %61
61: ; preds = %57
%62 = load i32*, i32** %4
%63 = call i32 (i32*, i8*, ...) @fprintf(i32* %62, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%64 = load i32*, i32** %7
%65 = call i32 @fclose(i32* %64)
%66 = load i32, i32* %12
ret i32 %66
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [24 x i8] c"unsigned char %s[] = {\0A\00"
@.str.2 = [4 x i8] c" \00"
@.str.3 = [7 x i8] c"0x%02x\00"
@.str.6 = [4 x i8] c"};\0A\00"
define i32 @write_file(i32* nocapture %0, i8* nocapture %1, i8* %2) {
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %8
8: ; preds = %34, %3
%9 = phi i32 [ 0, %3 ], [ %35, %34 ]
%10 = call i32 @fread(i8* %5, i32 1, i32 16, i32* %6)
%11 = tail call i32 @feof(i32* %6)
%12 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 3, i64 1, i32* %0)
%13 = icmp sgt i32 %10, 0
br i1 %13, label %14, label %34
14: ; preds = %8
%15 = add nsw i32 %10, -1
%16 = icmp ne i32 %11, 0
%17 = zext i32 %15 to i64
%18 = zext i32 %10 to i64
br label %19
19: ; preds = %14, %29
%20 = phi i64 [ 0, %14 ], [ %30, %29 ]
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = icmp uge i64 %20, %17
%26 = and i1 %16, %25
br i1 %26, label %29, label %27
27: ; preds = %19
%28 = tail call i32 @fputc(i32 44, i32* %0)
br label %29
29: ; preds = %27, %19
%30 = add nuw nsw i64 %20, 1
%31 = icmp eq i64 %30, %18
br i1 %31, label %32, label %19
32: ; preds = %29
%33 = add i32 %10, %9
br label %34
34: ; preds = %32, %8
%35 = phi i32 [ %9, %8 ], [ %33, %32 ]
%36 = tail call i32 @fputc(i32 10, i32* %0)
%37 = icmp eq i32 %11, 0
br i1 %37, label %8, label %38
38: ; preds = %34
%39 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i64 3, i64 1, i32* %0)
%40 = tail call i32 @fclose(i32* %6)
ret i32 %35
}