#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 scalar_t__ 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_hfile(unsigned char *buf, off_t len, char *cname) {
  char filename[256];
  FILE *fp;
  (void)buf;
  snprintf(filename, 256, "%s.h", 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, "#define LEN_%s %d\n", cname, (int)len);
  fprintf(fp, "extern unsigned char %s[];\n", cname);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<write_hfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x100,%esi
lea    0xe92(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    11c6 <write_hfile+0x86>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rdi
lea    -0x120(%rbp),%rdx
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <write_hfile+0xe6>
mov    -0x128(%rbp),%rdi
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x128(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    %eax,%ecx
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x128(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe3d(%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_hfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
lea    0xe96(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r15,%rcx
call   1040 <snprintf@plt>
lea    0xe81(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e4 <write_hfile+0x94>
mov    %rax,%rbx
lea    0xe7f(%rip),%rdi
mov    $0x1b,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe82(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe7c(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1204 <write_hfile+0xb4>
mov    0x2df5(%rip),%rax
mov    (%rax),%rdi
lea    0xe13(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s.h\00"
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32* null
@.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 = [19 x i8] c"#define LEN_%s %d\0A\00"
@.str.5 = [28 x i8] c"extern unsigned char %s[];\0A\00"
define i32 @write_hfile(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca i32*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%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 %22
18: ; preds = %3
%19 = load i32*, i32** @stderr
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %20)
store i32 -1, i32* %4
br label %35
22: ; preds = %3
%23 = load i32*, i32** %9
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32*, i32** %9
%26 = load i8*, i8** %7
%27 = load i64, i64* %6
%28 = trunc i64 %27 to i32
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %26, i32 %28)
%30 = load i32*, i32** %9
%31 = load i8*, i8** %7
%32 = call i32 (i32*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %31)
%33 = load i32*, i32** %9
%34 = call i32 @fclose(i32* %33)
store i32 0, i32* %4
br label %35
35: ; preds = %22, %18
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s.h\00"
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32* null
@.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 = [19 x i8] c"#define LEN_%s %d\0A\00"
@.str.5 = [28 x i8] c"extern unsigned char %s[];\0A\00"
define i32 @write_hfile(i8* nocapture readnone %0, i64 %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 %12
9: ; preds = %3
%10 = load i32*, i32** @stderr
%11 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %18
12: ; preds = %3
%13 = call i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 27, i64 1, i32* %7)
%14 = trunc i64 %1 to i32
%15 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %2, i32 %14)
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%17 = call i32 @fclose(i32* %7)
br label %18
18: ; preds = %12, %9
%19 = phi i32 [ -1, %9 ], [ 0, %12 ]
ret i32 %19
}