#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 */
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fwrite(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(unsigned char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int write_ines(FILE *out_f, const char *filename, unsigned prg_rom_size,
               const void *prg_base, unsigned chr_rom_size,
               const void *chr_base, unsigned mapper, unsigned extended_mapper,
               unsigned ram_size) {
  size_t res;
  unsigned char ines_hdr[16], zeropad[16384];
  memset(ines_hdr, 0, sizeof ines_hdr);
  memcpy(ines_hdr + 0, "NES\x1a", strlen("NES\x1a"));
  ines_hdr[4] = (prg_rom_size + 16384 - 1) / 16384;
  ines_hdr[5] = (chr_rom_size + 8192 - 1) / 8192;
  ines_hdr[6] = mapper;
  ines_hdr[7] = extended_mapper;
  ines_hdr[8] = (ram_size + 8192 - 1) / 8192;
  if (fwrite(ines_hdr, 1l, sizeof ines_hdr, out_f) != sizeof ines_hdr) {
    fprintf(stderr, "Short write.\n");
    return 0;
  }
  fprintf(stderr, "%s:\n", filename);
  fprintf(stderr, "  PRG-ROM %ldK\n", ines_hdr[4] * 16l);
  fprintf(stderr, "  CHR-ROM %ldK\n", ines_hdr[5] * 8l);
  memset(zeropad, 0, sizeof zeropad);
  res = fwrite(prg_base, 1, prg_rom_size, out_f);
  if (ferror(out_f))
    goto error_f;
  fwrite(zeropad, 1, (prg_rom_size % 16384), out_f);
  if (ferror(out_f))
    goto error_f;
  res = fwrite(chr_base, 1, chr_rom_size, out_f);
  if (ferror(out_f))
    goto error_f;
  fwrite(zeropad, 1, (chr_rom_size % 8192), out_f);
  if (ferror(out_f))
    goto error_f;
  return 1;
error_f:
  perror(filename);
  return 0;
}


x86 assembly (clang, -O0):
<write_ines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4060,%rsp
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    %r9,-0x38(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x4058(%rbp)
lea    0xe43(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x4058(%rbp),%rdi
mov    %eax,%edx
lea    0xe2e(%rip),%rsi
call   1070 <memcpy@plt>
mov    -0x1c(%rbp),%eax
add    $0x4000,%eax
sub    $0x1,%eax
shr    $0xe,%eax
mov    %al,-0x4c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x2000,%eax
sub    $0x1,%eax
shr    $0xd,%eax
mov    %al,-0x4b(%rbp)
mov    0x10(%rbp),%eax
mov    %al,-0x4a(%rbp)
mov    0x18(%rbp),%eax
mov    %al,-0x49(%rbp)
mov    0x20(%rbp),%eax
add    $0x2000,%eax
sub    $0x1,%eax
shr    $0xd,%eax
mov    %al,-0x48(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1090 <fwrite@plt>
cltq
cmp    $0x10,%rax
je     125c <write_ines+0xec>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ba <write_ines+0x24a>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
movzbl -0x4c(%rbp),%eax
mov    %eax,%edx
shl    $0x4,%rdx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
movzbl -0x4b(%rbp),%eax
mov    %eax,%edx
shl    $0x3,%rdx
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    -0x4050(%rbp),%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1050 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cltq
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12ff <write_ines+0x18f>
jmp    13aa <write_ines+0x23a>
lea    -0x4050(%rbp),%rdi
mov    -0x1c(%rbp),%edx
and    $0x3fff,%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1335 <write_ines+0x1c5>
jmp    13aa <write_ines+0x23a>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cltq
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1368 <write_ines+0x1f8>
jmp    13aa <write_ines+0x23a>
lea    -0x4050(%rbp),%rdi
mov    -0x2c(%rbp),%edx
and    $0x1fff,%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     139e <write_ines+0x22e>
jmp    13aa <write_ines+0x23a>
movl   $0x1,-0x4(%rbp)
jmp    13ba <write_ines+0x24a>
mov    -0x18(%rbp),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4038,%rsp
mov    %r9,0x18(%rsp)
mov    %r8d,%r15d
mov    %rcx,0x20(%rsp)
mov    %edx,%r13d
mov    %rsi,0x28(%rsp)
mov    %rdi,%rbx
mov    %rsp,%r12
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    0xe4b(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
lea    0x3fff(%r13),%eax
shr    $0xe,%eax
mov    %al,0x4(%rsp)
lea    0x1fff(%r15),%eax
shr    $0xd,%eax
mov    %al,0x5(%rsp)
mov    0x4070(%rsp),%al
mov    %al,0x6(%rsp)
mov    0x4078(%rsp),%al
mov    %al,0x7(%rsp)
mov    $0x1fff,%eax
add    0x4080(%rsp),%eax
shr    $0xd,%eax
mov    %al,0x8(%rsp)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    0x2db5(%rip),%rbp
mov    0x0(%rbp),%edi
cmp    $0x10,%eax
jne    133e <write_ines+0x1ce>
lea    0xdd5(%rip),%rsi
mov    0x28(%rsp),%r14
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x0(%rbp),%edi
movzbl 0x4(%rsp),%edx
shl    $0x4,%rdx
lea    0xdb8(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x0(%rbp),%edi
movzbl 0x5(%rsp),%edx
shl    $0x3,%rdx
lea    0xdae(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0x30(%rsp),%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1050 <memset@plt>
mov    0x20(%rsp),%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
mov    %r14,%rbp
jne    1331 <write_ines+0x1c1>
and    $0x3fff,%r13d
lea    0x30(%rsp),%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1331 <write_ines+0x1c1>
mov    $0x1,%r14d
mov    0x18(%rsp),%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1331 <write_ines+0x1c1>
and    $0x1fff,%r15d
lea    0x30(%rsp),%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     134c <write_ines+0x1dc>
mov    %rbp,%rdi
call   1080 <perror@plt>
xor    %r14d,%r14d
jmp    134c <write_ines+0x1dc>
lea    0xcc0(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%eax
add    $0x4038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"NES\1A\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"Short write.\0A\00"
@.str.2 = [5 x i8] c"%s:\0A\00"
@.str.3 = [16 x i8] c" PRG-ROM %ldK\0A\00"
@.str.4 = [16 x i8] c" CHR-ROM %ldK\0A\00"
define i32 @write_ines(i32* %0, i8* %1, i32 %2, i8* %3, i32 %4, i8* %5, i32 %6, i32 %7, i32 %8) {
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i64
%21 = alloca [16 x i8]
%22 = alloca [16384 x i8]
store i32* %0, i32** %11
store i8* %1, i8** %12
store i32 %2, i32* %13
store i8* %3, i8** %14
store i32 %4, i32* %15
store i8* %5, i8** %16
store i32 %6, i32* %17
store i32 %7, i32* %18
store i32 %8, i32* %19
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 16)
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 0
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @memcpy(i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %13
%30 = add i32 %29, 16384
%31 = sub i32 %30, 1
%32 = udiv i32 %31, 16384
%33 = trunc i32 %32 to i8
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 4
store i8 %33, i8* %34
%35 = load i32, i32* %15
%36 = add i32 %35, 8192
%37 = sub i32 %36, 1
%38 = udiv i32 %37, 8192
%39 = trunc i32 %38 to i8
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 5
store i8 %39, i8* %40
%41 = load i32, i32* %17
%42 = trunc i32 %41 to i8
%43 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 6
store i8 %42, i8* %43
%44 = load i32, i32* %18
%45 = trunc i32 %44 to i8
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 7
store i8 %45, i8* %46
%47 = load i32, i32* %19
%48 = add i32 %47, 8192
%49 = sub i32 %48, 1
%50 = udiv i32 %49, 8192
%51 = trunc i32 %50 to i8
%52 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 8
store i8 %51, i8* %52
%53 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 0
%54 = load i32*, i32** %11
%55 = call i32 @fwrite(i8* %53, i32 1, i32 16, i32* %54)
%56 = sext i32 %55 to i64
%57 = icmp ne i64 %56, 16
br i1 %57, label %58, label %61
58: ; preds = %9
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %10
br label %122
61: ; preds = %9
%62 = load i32, i32* @stderr
%63 = load i8*, i8** %12
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %63)
%65 = load i32, i32* @stderr
%66 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 4
%67 = load i8, i8* %66
%68 = zext i8 %67 to i64
%69 = mul nsw i64 %68, 16
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i64 %69)
%71 = load i32, i32* @stderr
%72 = getelementptr inbounds [16 x i8], [16 x i8]* %21, i64 0, i64 5
%73 = load i8, i8* %72
%74 = zext i8 %73 to i64
%75 = mul nsw i64 %74, 8
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i64 %75)
%77 = getelementptr inbounds [16384 x i8], [16384 x i8]* %22, i64 0, i64 0
%78 = call i32 @memset(i8* %77, i32 0, i32 16384)
%79 = load i8*, i8** %14
%80 = load i32, i32* %13
%81 = load i32*, i32** %11
%82 = call i32 @fwrite(i8* %79, i32 1, i32 %80, i32* %81)
%83 = sext i32 %82 to i64
store i64 %83, i64* %20
%84 = load i32*, i32** %11
%85 = call i64 @ferror(i32* %84)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %88
87: ; preds = %61
br label %119
88: ; preds = %61
%89 = getelementptr inbounds [16384 x i8], [16384 x i8]* %22, i64 0, i64 0
%90 = load i32, i32* %13
%91 = urem i32 %90, 16384
%92 = load i32*, i32** %11
%93 = call i32 @fwrite(i8* %89, i32 1, i32 %91, i32* %92)
%94 = load i32*, i32** %11
%95 = call i64 @ferror(i32* %94)
%96 = icmp ne i64 %95, 0
br i1 %96, label %97, label %98
97: ; preds = %88
br label %119
98: ; preds = %88
%99 = load i8*, i8** %16
%100 = load i32, i32* %15
%101 = load i32*, i32** %11
%102 = call i32 @fwrite(i8* %99, i32 1, i32 %100, i32* %101)
%103 = sext i32 %102 to i64
store i64 %103, i64* %20
%104 = load i32*, i32** %11
%105 = call i64 @ferror(i32* %104)
%106 = icmp ne i64 %105, 0
br i1 %106, label %107, label %108
107: ; preds = %98
br label %119
108: ; preds = %98
%109 = getelementptr inbounds [16384 x i8], [16384 x i8]* %22, i64 0, i64 0
%110 = load i32, i32* %15
%111 = urem i32 %110, 8192
%112 = load i32*, i32** %11
%113 = call i32 @fwrite(i8* %109, i32 1, i32 %111, i32* %112)
%114 = load i32*, i32** %11
%115 = call i64 @ferror(i32* %114)
%116 = icmp ne i64 %115, 0
br i1 %116, label %117, label %118
117: ; preds = %108
br label %119
118: ; preds = %108
store i32 1, i32* %10
br label %122
119: ; preds = %117, %107, %97, %87
%120 = load i8*, i8** %12
%121 = call i32 @perror(i8* %120)
store i32 0, i32* %10
br label %122
122: ; preds = %119, %118, %58
%123 = load i32, i32* %10
ret i32 %123
}

LLVM IR (-O2):
@.str = [5 x i8] c"NES\1A\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"Short write.\0A\00"
@.str.2 = [5 x i8] c"%s:\0A\00"
@.str.3 = [16 x i8] c" PRG-ROM %ldK\0A\00"
@.str.4 = [16 x i8] c" CHR-ROM %ldK\0A\00"
define i32 @write_ines(i32* nocapture %0, i8* %1, i32 %2, i8* nocapture %3, i32 %4, i8* nocapture %5, i32 %6, i32 %7, i32 %8) {
%10 = alloca [16 x i8]
%11 = alloca [16384 x i8]
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%13 = getelementptr inbounds [16384 x i8], [16384 x i8]* %11, i64 0, i64 0
%14 = call i32 @memset(i8* %12, i32 0, i32 16)
%15 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @memcpy(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = add i32 %2, 16383
%18 = lshr i32 %17, 14
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 4
store i8 %19, i8* %20
%21 = add i32 %4, 8191
%22 = lshr i32 %21, 13
%23 = trunc i32 %22 to i8
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 5
store i8 %23, i8* %24
%25 = trunc i32 %6 to i8
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 6
store i8 %25, i8* %26
%27 = trunc i32 %7 to i8
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 7
store i8 %27, i8* %28
%29 = add i32 %8, 8191
%30 = lshr i32 %29, 13
%31 = trunc i32 %30 to i8
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 8
store i8 %31, i8* %32
%33 = call i32 @fwrite(i8* %12, i32 1, i32 16, i32* %0)
%34 = icmp eq i32 %33, 16
%35 = load i32, i32* @stderr
br i1 %34, label %38, label %36
36: ; preds = %9
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %70
38: ; preds = %9
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%40 = load i32, i32* @stderr
%41 = load i8, i8* %20
%42 = zext i8 %41 to i64
%43 = shl nuw nsw i64 %42, 4
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i64 %43)
%45 = load i32, i32* @stderr
%46 = load i8, i8* %24
%47 = zext i8 %46 to i64
%48 = shl nuw nsw i64 %47, 3
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i64 %48)
%50 = call i32 @memset(i8* %13, i32 0, i32 16384)
%51 = call i32 @fwrite(i8* %3, i32 1, i32 %2, i32* %0)
%52 = call i64 @ferror(i32* %0)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %68
54: ; preds = %38
%55 = and i32 %2, 16383
%56 = call i32 @fwrite(i8* %13, i32 1, i32 %55, i32* %0)
%57 = call i64 @ferror(i32* %0)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %68
59: ; preds = %54
%60 = call i32 @fwrite(i8* %5, i32 1, i32 %4, i32* %0)
%61 = call i64 @ferror(i32* %0)
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %59
%64 = and i32 %4, 8191
%65 = call i32 @fwrite(i8* %13, i32 1, i32 %64, i32* %0)
%66 = call i64 @ferror(i32* %0)
%67 = icmp eq i64 %66, 0
br i1 %67, label %70, label %68
68: ; preds = %63, %59, %54, %38
%69 = call i32 @perror(i8* %1)
br label %70
70: ; preds = %63, %68, %36
%71 = phi i32 [ 0, %36 ], [ 0, %68 ], [ 1, %63 ]
ret i32 %71
}