#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void bytes_to_file(char *filename, FILE *infile, int len) {
  int i;
  int c;
  FILE *outfile;
  if ((outfile = fopen(filename, "w")) == 0) {
    fprintf(stderr, "Can't open output file %s\n", filename);
    exit(1);
  }
  for (i = 0; i < len; i++) {
    c = fgetc(infile);
    fprintf(outfile, "0x%02x, ", c);
    if ((i & 7) == 7) {
      fputc('\n', outfile);
    }
  }
  if ((i & 7) != 0) {
    fputc('\n', outfile);
  }
  fclose(outfile);
}


x86 assembly (clang, -O0):
<bytes_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b7 <bytes_to_file+0x57>
mov    0x2e48(%rip),%rax
movslq (%rax),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    121b <bytes_to_file+0xbb>
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%eax
and    $0x7,%eax
cmp    $0x7,%eax
jne    1208 <bytes_to_file+0xa8>
mov    -0x28(%rbp),%rsi
mov    $0xa,%edi
call   1050 <fputc@plt>
jmp    120d <bytes_to_file+0xad>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11be <bytes_to_file+0x5e>
mov    -0x18(%rbp),%eax
and    $0x7,%eax
cmp    $0x0,%eax
je     1238 <bytes_to_file+0xd8>
mov    -0x28(%rbp),%rsi
mov    $0xa,%edi
call   1050 <fputc@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bytes_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe88(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f9 <bytes_to_file+0x99>
mov    %rax,%rbx
test   %r12d,%r12d
jle    11e9 <bytes_to_file+0x89>
lea    0xe8c(%rip),%r15
xor    %ebp,%ebp
jmp    11a8 <bytes_to_file+0x48>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%ebp
cmp    %ebp,%r12d
je     11d6 <bytes_to_file+0x76>
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%eax
not    %eax
test   $0x7,%al
jne    11a0 <bytes_to_file+0x40>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
jmp    11a0 <bytes_to_file+0x40>
test   $0x7,%r12b
je     11e9 <bytes_to_file+0x89>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2de0(%rip),%rax
movslq (%rax),%rdi
lea    0xdf8(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Can't open output file %s\0A\00"
@.str.2 = [9 x i8] c"0x%02x, \00"
define void @bytes_to_file(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %20
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = sext i32 %14 to i64
%16 = inttoptr i64 %15 to i32*
%17 = load i8*, i8** %4
%18 = call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %3
store i32 0, i32* %7
br label %21
21: ; preds = %38, %20
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %41
25: ; preds = %21
%26 = load i32*, i32** %5
%27 = call i32 @fgetc(i32* %26)
store i32 %27, i32* %8
%28 = load i32*, i32** %9
%29 = load i32, i32* %8
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %7
%32 = and i32 %31, 7
%33 = icmp eq i32 %32, 7
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = load i32*, i32** %9
%36 = call i32 @fputc(i8 signext 10, i32* %35)
br label %37
37: ; preds = %34, %25
br label %38
38: ; preds = %37
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %21
41: ; preds = %21
%42 = load i32, i32* %7
%43 = and i32 %42, 7
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32*, i32** %9
%47 = call i32 @fputc(i8 signext 10, i32* %46)
br label %48
48: ; preds = %45, %41
%49 = load i32*, i32** %9
%50 = call i32 @fclose(i32* %49)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Can't open output file %s\0A\00"
@.str.2 = [9 x i8] c"0x%02x, \00"
define void @bytes_to_file(i8* %0, i32* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = icmp sgt i32 %2, 0
br i1 %7, label %14, label %30
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = sext i32 %9 to i64
%11 = inttoptr i64 %10 to i32*
%12 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %6, %22
%15 = phi i32 [ %23, %22 ], [ 0, %6 ]
%16 = tail call i32 @fgetc(i32* %1)
%17 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = and i32 %15, 7
%19 = icmp eq i32 %18, 7
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @fputc(i8 signext 10, i32* %4)
br label %22
22: ; preds = %14, %20
%23 = add nuw nsw i32 %15, 1
%24 = icmp eq i32 %23, %2
br i1 %24, label %25, label %14
25: ; preds = %22
%26 = and i32 %2, 7
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = tail call i32 @fputc(i8 signext 10, i32* %4)
br label %30
30: ; preds = %6, %28, %25
%31 = tail call i32 @fclose(i32* %4)
ret void
}