#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 uint8_t;
typedef int uint32_t;
typedef int uint16_t;
typedef int /*<<< orphan*/ pSID;
typedef int /*<<< orphan*/ nSID;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ write_block(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
void write_dat(FILE *rom, FILE *file, uint32_t length, uint16_t *sectionId) {
  uint16_t pSID = 0xFFFF;
  fseek(file, 0L, SEEK_SET);
  while (length > 0) {
    uint16_t flashPage = *sectionId >> 8;
    uint8_t index = *sectionId & 0xFF;
    uint16_t nSID = 0xFFFF;
    uint32_t header_addr = 0x4000 * flashPage + index * 4;
    index++;
    if (index > 0x3F) {
      index = 1;
      flashPage++;
      fseek(rom, flashPage * 0x4000, SEEK_SET);
      fprintf(rom, "KFS");
      fputc(0xFF << 0, rom);
    }
    if (length > 0x100) {
      nSID = (flashPage << 8) | index;
    }
    fseek(rom, header_addr, SEEK_SET);
    pSID &= 0x7FFF;
    fwrite(&pSID, sizeof(pSID), 1, rom);
    fwrite(&nSID, sizeof(nSID), 1, rom);
    write_block(rom, file, *sectionId);
    fflush(rom);
    if (length < 0x100) {
      length = 0;
    } else {
      length -= 0x100;
    }
    pSID = *sectionId;
    *sectionId = (flashPage << 8) | index;
  }
}


x86 assembly (clang, -O0):
<write_dat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffff,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmpl   $0x0,-0x14(%rbp)
jle    12eb <write_dat+0x18b>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
sar    $0x8,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
and    $0xff,%eax
mov    %eax,-0x2c(%rbp)
movl   $0xffff,-0x30(%rbp)
mov    -0x28(%rbp),%eax
shl    $0xe,%eax
mov    -0x2c(%rbp),%ecx
shl    $0x2,%ecx
add    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x3f,-0x2c(%rbp)
jle    1227 <write_dat+0xc7>
movl   $0x1,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%esi
shl    $0xe,%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x8(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rsi
mov    $0xff,%edi
call   1040 <fputc@plt>
cmpl   $0x100,-0x14(%rbp)
jle    1240 <write_dat+0xe0>
mov    -0x28(%rbp),%eax
shl    $0x8,%eax
or     -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x24(%rbp),%eax
and    $0x7fff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x8(%rbp),%rcx
lea    -0x30(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1030 <write_block@plt>
mov    -0x8(%rbp),%rdi
call   1060 <fflush@plt>
cmpl   $0x100,-0x14(%rbp)
jge    12c3 <write_dat+0x163>
movl   $0x0,-0x14(%rbp)
jmp    12ce <write_dat+0x16e>
mov    -0x14(%rbp),%eax
sub    $0x100,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%ecx
shl    $0x8,%ecx
or     -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1192 <write_dat+0x32>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_dat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x10(%rsp)
mov    %edx,%r15d
mov    %rdi,0x18(%rsp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
mov    %rsi,0x20(%rsp)
mov    %rsi,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
test   %r15d,%r15d
jle    12c8 <write_dat+0x178>
mov    0x10(%rsp),%rax
mov    (%rax),%r12d
mov    $0xffff,%r13d
jmp    1234 <write_dat+0xe4>
nop
lea    (%r14,%rbp,4),%esi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    0x18(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <fseek@plt>
and    $0x7fff,%r13d
mov    %r13d,0xc(%rsp)
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1070 <fwrite@plt>
lea    0x8(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1070 <fwrite@plt>
mov    0x10(%rsp),%rbx
mov    (%rbx),%edx
mov    %rbp,%rdi
mov    0x20(%rsp),%rsi
call   1030 <write_block@plt>
mov    %rbp,%rdi
call   1050 <fflush@plt>
mov    (%rbx),%r13d
mov    %r13d,0xc(%rsp)
mov    %r12d,(%rbx)
sub    $0x100,%r15d
mov    $0x0,%eax
cmovb  %eax,%r15d
test   %r15d,%r15d
jle    12c8 <write_dat+0x178>
mov    %r12d,%r14d
sar    $0x8,%r14d
movzbl %r12b,%ebp
movl   $0xffff,0x8(%rsp)
cmp    $0x3f,%ebp
jb     12a0 <write_dat+0x150>
lea    0x1(%r14),%r12d
mov    %r12d,%esi
shl    $0xe,%esi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
mov    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <fseek@plt>
mov    $0x3,%esi
mov    $0x1,%edx
lea    0xd83(%rip),%rdi
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    $0xff,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
mov    $0x1,%eax
jmp    12a6 <write_dat+0x156>
nopl   0x0(%rax)
lea    0x1(%rbp),%eax
mov    %r14d,%r12d
shl    $0xe,%r14d
shl    $0x8,%r12d
or     %eax,%r12d
cmp    $0x101,%r15d
jb     11a0 <write_dat+0x50>
mov    %r12d,0x8(%rsp)
jmp    11a0 <write_dat+0x50>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [4 x i8] c"KFS\00"
define void @write_dat(i32* %0, i32* %1, i32 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
store i32 65535, i32* %9
%14 = load i32*, i32** %6
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @fseek(i32* %14, i32 0, i32 %15)
br label %17
17: ; preds = %80, %4
%18 = load i32, i32* %7
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %88
20: ; preds = %17
%21 = load i32*, i32** %8
%22 = load i32, i32* %21
%23 = ashr i32 %22, 8
store i32 %23, i32* %10
%24 = load i32*, i32** %8
%25 = load i32, i32* %24
%26 = and i32 %25, 255
store i32 %26, i32* %11
store i32 65535, i32* %12
%27 = load i32, i32* %10
%28 = mul nsw i32 16384, %27
%29 = load i32, i32* %11
%30 = mul nsw i32 %29, 4
%31 = add nsw i32 %28, %30
store i32 %31, i32* %13
%32 = load i32, i32* %11
%33 = add nsw i32 %32, 1
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp sgt i32 %34, 63
br i1 %35, label %36, label %48
36: ; preds = %20
store i32 1, i32* %11
%37 = load i32, i32* %10
%38 = add nsw i32 %37, 1
store i32 %38, i32* %10
%39 = load i32*, i32** %5
%40 = load i32, i32* %10
%41 = mul nsw i32 %40, 16384
%42 = load i32, i32* @SEEK_SET
%43 = call i32 @fseek(i32* %39, i32 %41, i32 %42)
%44 = load i32*, i32** %5
%45 = call i32 @fprintf(i32* %44, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%46 = load i32*, i32** %5
%47 = call i32 @fputc(i32 255, i32* %46)
br label %48
48: ; preds = %36, %20
%49 = load i32, i32* %7
%50 = icmp sgt i32 %49, 256
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i32, i32* %10
%53 = shl i32 %52, 8
%54 = load i32, i32* %11
%55 = or i32 %53, %54
store i32 %55, i32* %12
br label %56
56: ; preds = %51, %48
%57 = load i32*, i32** %5
%58 = load i32, i32* %13
%59 = load i32, i32* @SEEK_SET
%60 = call i32 @fseek(i32* %57, i32 %58, i32 %59)
%61 = load i32, i32* %9
%62 = and i32 %61, 32767
store i32 %62, i32* %9
%63 = load i32*, i32** %5
%64 = call i32 @fwrite(i32* %9, i32 4, i32 1, i32* %63)
%65 = load i32*, i32** %5
%66 = call i32 @fwrite(i32* %12, i32 4, i32 1, i32* %65)
%67 = load i32*, i32** %5
%68 = load i32*, i32** %6
%69 = load i32*, i32** %8
%70 = load i32, i32* %69
%71 = call i32 @write_block(i32* %67, i32* %68, i32 %70)
%72 = load i32*, i32** %5
%73 = call i32 @fflush(i32* %72)
%74 = load i32, i32* %7
%75 = icmp slt i32 %74, 256
br i1 %75, label %76, label %77
76: ; preds = %56
store i32 0, i32* %7
br label %80
77: ; preds = %56
%78 = load i32, i32* %7
%79 = sub nsw i32 %78, 256
store i32 %79, i32* %7
br label %80
80: ; preds = %77, %76
%81 = load i32*, i32** %8
%82 = load i32, i32* %81
store i32 %82, i32* %9
%83 = load i32, i32* %10
%84 = shl i32 %83, 8
%85 = load i32, i32* %11
%86 = or i32 %84, %85
%87 = load i32*, i32** %8
store i32 %86, i32* %87
br label %17
88: ; preds = %17
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [4 x i8] c"KFS\00"
define void @write_dat(i32* %0, i32* %1, i32 %2, i32* nocapture %3) {
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %1, i32 0, i32 %8)
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %51
11: ; preds = %4
%12 = bitcast i32* %6 to i8*
%13 = load i32, i32* %3
br label %14
14: ; preds = %11, %39
%15 = phi i32 [ 65535, %11 ], [ %48, %39 ]
%16 = phi i32 [ %13, %11 ], [ %37, %39 ]
%17 = phi i32 [ %2, %11 ], [ %49, %39 ]
%18 = ashr i32 %16, 8
%19 = and i32 %16, 255
store i32 65535, i32* %6
%20 = shl nsw i32 %18, 14
%21 = shl nuw nsw i32 %19, 2
%22 = or i32 %20, %21
%23 = add nuw nsw i32 %19, 1
%24 = icmp ugt i32 %19, 62
br i1 %24, label %25, label %32
25: ; preds = %14
%26 = add nsw i32 %18, 1
%27 = shl nsw i32 %26, 14
%28 = load i32, i32* @SEEK_SET
%29 = tail call i32 @fseek(i32* %0, i32 %27, i32 %28)
%30 = tail call i64 bitcast (i32 (i32*, i32, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 3, i64 1, i32* %0)
%31 = tail call i32 @fputc(i32 255, i32* %0)
br label %32
32: ; preds = %25, %14
%33 = phi i32 [ %26, %25 ], [ %18, %14 ]
%34 = phi i32 [ 1, %25 ], [ %23, %14 ]
%35 = icmp ugt i32 %17, 256
%36 = shl i32 %33, 8
%37 = or i32 %36, %34
br i1 %35, label %38, label %39
38: ; preds = %32
store i32 %37, i32* %6
br label %39
39: ; preds = %32, %38
%40 = load i32, i32* @SEEK_SET
%41 = tail call i32 @fseek(i32* %0, i32 %22, i32 %40)
%42 = and i32 %15, 32767
store i32 %42, i32* %5
%43 = call i32 @fwrite(i32* %5, i32 4, i32 1, i32* %0)
%44 = call i32 @fwrite(i32* %6, i32 4, i32 1, i32* %0)
%45 = load i32, i32* %3
%46 = tail call i32 @write_block(i32* %0, i32* %1, i32 %45)
%47 = tail call i32 @fflush(i32* %0)
%48 = load i32, i32* %3
store i32 %48, i32* %5
store i32 %37, i32* %3
%49 = tail call i32 @llvm.usub.sat.i32(i32 %17, i32 256)
%50 = icmp sgt i32 %49, 0
br i1 %50, label %14, label %51
51: ; preds = %39, %4
ret void
}