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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int write_cfile(unsigned char *buf, off_t len, char *cname) {
  char filename[256];
  FILE *fp;
  int i;
  snprintf(filename, 256, "%s.c", cname);
  fp = fopen(filename, "w+");
  if (fp == NULL) {
    fprintf(stderr, "Couldn't open %s\n", filename);
    return -1;
  }
  fprintf(fp, "/* Generated by ipod2c */\n\n");
  fprintf(fp, "unsigned char %s[] = {", cname);
  for (i = 0; i < len; i++) {
    if ((i % 16) == 0) {
      fprintf(fp, "\n    ");
    }
    if (i == (len - 1)) {
      fprintf(fp, "0x%02x", buf[i]);
    } else {
      fprintf(fp, "0x%02x, ", buf[i]);
    }
  }
  fprintf(fp, "\n};\n");
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<write_cfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x100,%esi
lea    0xe93(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    11c5 <write_cfile+0x85>
mov    0x2e3f(%rip),%rax
movslq (%rax),%rdi
lea    -0x120(%rbp),%rdx
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <write_cfile+0x19a>
mov    -0x128(%rbp),%rdi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x128(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x12c(%rbp)
mov    -0x12c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12b2 <write_cfile+0x172>
mov    -0x12c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1238 <write_cfile+0xf8>
mov    -0x128(%rbp),%rdi
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x12c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1275 <write_cfile+0x135>
mov    -0x128(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x12c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1299 <write_cfile+0x159>
mov    -0x128(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x12c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xdc8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    129e <write_cfile+0x15e>
mov    -0x12c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x12c(%rbp)
jmp    11fd <write_cfile+0xbd>
mov    -0x128(%rbp),%rdi
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x128(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_cfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdx,%rbp
mov    %esi,%r12d
mov    %rdi,%r14
lea    0xe8f(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %rbp,%rcx
call   1040 <snprintf@plt>
lea    0xe7a(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     125f <write_cfile+0x10f>
mov    %rax,%rbx
lea    0xe74(%rip),%rdi
mov    $0x1b,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe77(%rip),%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r12d,%r12d
jle    123a <write_cfile+0xea>
lea    -0x1(%r12),%r13d
mov    %r12d,%ebp
lea    0xe7a(%rip),%r12
xor    %r15d,%r15d
jmp    1219 <write_cfile+0xc9>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %r15,%r13
mov    %r12,%rsi
lea    0xe56(%rip),%rax
cmove  %rax,%rsi
movzbl (%r14,%r15,1),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r15
cmp    %r15,%rbp
je     123a <write_cfile+0xea>
test   $0xf,%r15b
jne    11f0 <write_cfile+0xa0>
mov    $0x5,%esi
mov    $0x1,%edx
lea    0xe1d(%rip),%rdi
mov    %rbx,%rcx
call   1070 <fwrite@plt>
jmp    11f0 <write_cfile+0xa0>
lea    0xe22(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    127f <write_cfile+0x12f>
mov    0x2d7a(%rip),%rax
movslq (%rax),%rdi
lea    0xd98(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s.c\00"
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"Couldn't open %s\0A\00"
@.str.3 = [28 x i8] c"/* Generated by ipod2c */\0A\0A\00"
@.str.4 = [23 x i8] c"unsigned char %s[] = {\00"
@.str.5 = [6 x i8] c"\0A \00"
@.str.6 = [7 x i8] c"0x%02x\00"
@.str.7 = [9 x i8] c"0x%02x, \00"
@.str.8 = [5 x i8] c"\0A};\0A\00"
define i32 @write_cfile(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%12 = load i8*, i8** %7
%13 = call i32 @snprintf(i8* %11, i32 256, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %9
%16 = load i32*, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %24
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %22)
store i32 -1, i32* %4
br label %73
24: ; preds = %3
%25 = load i32*, i32** %9
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32*, i32** %9
%28 = load i8*, i8** %7
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %28)
store i32 0, i32* %10
br label %30
30: ; preds = %65, %24
%31 = load i32, i32* %10
%32 = load i32, i32* %6
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %68
34: ; preds = %30
%35 = load i32, i32* %10
%36 = srem i32 %35, 16
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32*, i32** %9
%40 = call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %41
41: ; preds = %38, %34
%42 = load i32, i32* %10
%43 = load i32, i32* %6
%44 = sub nsw i32 %43, 1
%45 = icmp eq i32 %42, %44
br i1 %45, label %46, label %55
46: ; preds = %41
%47 = load i32*, i32** %9
%48 = load i8*, i8** %5
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = call i32 (i32*, i8*, ...) @fprintf(i32* %47, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %53)
br label %64
55: ; preds = %41
%56 = load i32*, i32** %9
%57 = load i8*, i8** %5
%58 = load i32, i32* %10
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = zext i8 %61 to i32
%63 = call i32 (i32*, i8*, ...) @fprintf(i32* %56, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %55, %46
br label %65
65: ; preds = %64
%66 = load i32, i32* %10
%67 = add nsw i32 %66, 1
store i32 %67, i32* %10
br label %30
68: ; preds = %30
%69 = load i32*, i32** %9
%70 = call i32 (i32*, i8*, ...) @fprintf(i32* %69, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%71 = load i32*, i32** %9
%72 = call i32 @fclose(i32* %71)
store i32 0, i32* %4
br label %73
73: ; preds = %68, %18
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s.c\00"
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.2 = [18 x i8] c"Couldn't open %s\0A\00"
@.str.3 = [28 x i8] c"/* Generated by ipod2c */\0A\0A\00"
@.str.4 = [23 x i8] c"unsigned char %s[] = {\00"
@.str.5 = [6 x i8] c"\0A \00"
@.str.6 = [7 x i8] c"0x%02x\00"
@.str.7 = [9 x i8] c"0x%02x, \00"
@.str.8 = [5 x i8] c"\0A};\0A\00"
define i32 @write_cfile(i8* nocapture %0, i32 %1, i8* %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %2)
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i32*
%13 = call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %40
14: ; preds = %3
%15 = call i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 27, i64 1, i32* %7)
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%17 = icmp sgt i32 %1, 0
br i1 %17, label %18, label %37
18: ; preds = %14
%19 = add nsw i32 %1, -1
%20 = zext i32 %19 to i64
%21 = zext i32 %1 to i64
br label %22
22: ; preds = %18, %28
%23 = phi i64 [ 0, %18 ], [ %35, %28 ]
%24 = and i64 %23, 15
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i64 5, i64 1, i32* %7)
br label %28
28: ; preds = %26, %22
%29 = icmp eq i64 %23, %20
%30 = getelementptr inbounds i8, i8* %0, i64 %23
%31 = select i1 %29, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0)
%32 = load i8, i8* %30
%33 = zext i8 %32 to i32
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* %31, i32 %33)
%35 = add nuw nsw i64 %23, 1
%36 = icmp eq i64 %35, %21
br i1 %36, label %37, label %22
37: ; preds = %28, %14
%38 = call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0), i64 4, i64 1, i32* %7)
%39 = call i32 @fclose(i32* %7)
br label %40
40: ; preds = %37, %9
%41 = phi i32 [ -1, %9 ], [ 0, %37 ]
ret i32 %41
}