#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int uint8_t;
typedef int uint32_t;
typedef int uint16_t;
struct TYPE_3__ {
  int quot;
  scalar_t__ rem;
};
typedef TYPE_1__ div_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
TYPE_1__ div(int, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ write_recursive(char *, int /*<<< orphan*/ *, int *, int *,
                                   int *);
uint16_t write_filesystem(char *model, FILE *rom, uint8_t fat_start,
                          uint8_t dat_start) {
  uint16_t parentId = 0;
  uint16_t sectionId = (dat_start << 8) | 1;
  uint32_t fatptr = (fat_start + 1) * 0x4000;
  uint32_t fatptrStart = fatptr;
  fseek(rom, dat_start * 0x4000, SEEK_SET);
  fprintf(rom, "KFS");
  fflush(rom);
  write_recursive(model, rom, &parentId, &sectionId, &fatptr);
  div_t d = div(fatptrStart - fatptr, 0x4000);
  uint16_t result = d.quot;
  if (d.rem) {
    result++;
  }
  result <<= 8;
  result += (sectionId >> 8) - dat_start + 1;
  return result;
}


x86 assembly (clang, -O0):
<write_filesystem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x8,%eax
or     $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
shl    $0xe,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
shl    $0xe,%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fflush@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%r8
call   1030 <write_recursive@plt>
mov    -0x28(%rbp),%edi
sub    -0x24(%rbp),%edi
mov    $0x4000,%esi
call   1050 <div@plt>
mov    %eax,-0x38(%rbp)
mov    %rdx,-0x30(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1206 <write_filesystem+0xb6>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%eax
sar    $0x8,%eax
sub    -0x18(%rbp),%eax
add    $0x1,%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_filesystem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r15
movl   $0x0,0x14(%rsp)
mov    %ecx,%eax
shl    $0x8,%eax
or     $0x1,%eax
mov    %eax,0x10(%rsp)
shl    $0xe,%edx
lea    0x4000(%rdx),%ebp
mov    %ebp,0xc(%rsp)
mov    %ecx,%esi
shl    $0xe,%esi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1060 <fseek@plt>
lea    0xe5f(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fflush@plt>
lea    0x14(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <write_recursive@plt>
sub    0xc(%rsp),%ebp
mov    %ebp,%edi
mov    $0x4000,%esi
call   1040 <div@plt>
cmp    $0x1,%rdx
sbb    $0xffffffff,%eax
shl    $0x8,%eax
mov    0x10(%rsp),%ecx
sar    $0x8,%ecx
sub    %r14d,%ecx
add    %ecx,%eax
add    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i64 }
@SEEK_SET = global i32 0
@.str = [4 x i8] c"KFS\00"
define i32 @write_filesystem(i8* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.TYPE_3__
%14 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %9
%15 = load i32, i32* %8
%16 = shl i32 %15, 8
%17 = or i32 %16, 1
store i32 %17, i32* %10
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 1
%20 = mul nsw i32 %19, 16384
store i32 %20, i32* %11
%21 = load i32, i32* %11
store i32 %21, i32* %12
%22 = load i32*, i32** %6
%23 = load i32, i32* %8
%24 = mul nsw i32 %23, 16384
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @fseek(i32* %22, i32 %24, i32 %25)
%27 = load i32*, i32** %6
%28 = call i32 @fprintf(i32* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%29 = load i32*, i32** %6
%30 = call i32 @fflush(i32* %29)
%31 = load i8*, i8** %5
%32 = load i32*, i32** %6
%33 = call i32 @write_recursive(i8* %31, i32* %32, i32* %9, i32* %10, i32* %11)
%34 = load i32, i32* %12
%35 = load i32, i32* %11
%36 = sub nsw i32 %34, %35
%37 = call { i32, i64 } @div(i32 %36, i32 16384)
%38 = bitcast %struct.TYPE_3__* %13 to { i32, i64 }*
%39 = getelementptr inbounds { i32, i64 }, { i32, i64 }* %38, i32 0, i32 0
%40 = extractvalue { i32, i64 } %37, 0
store i32 %40, i32* %39
%41 = getelementptr inbounds { i32, i64 }, { i32, i64 }* %38, i32 0, i32 1
%42 = extractvalue { i32, i64 } %37, 1
store i64 %42, i64* %41
%43 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 0
%44 = load i32, i32* %43
store i32 %44, i32* %14
%45 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 1
%46 = load i64, i64* %45
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %4
%49 = load i32, i32* %14
%50 = add nsw i32 %49, 1
store i32 %50, i32* %14
br label %51
51: ; preds = %48, %4
%52 = load i32, i32* %14
%53 = shl i32 %52, 8
store i32 %53, i32* %14
%54 = load i32, i32* %10
%55 = ashr i32 %54, 8
%56 = load i32, i32* %8
%57 = sub nsw i32 %55, %56
%58 = add nsw i32 %57, 1
%59 = load i32, i32* %14
%60 = add nsw i32 %59, %58
store i32 %60, i32* %14
%61 = load i32, i32* %14
ret i32 %61
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [4 x i8] c"KFS\00"
define i32 @write_filesystem(i8* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%9 = bitcast i32* %6 to i8*
%10 = shl i32 %3, 8
%11 = or i32 %10, 1
store i32 %11, i32* %6
%12 = bitcast i32* %7 to i8*
%13 = shl i32 %2, 14
%14 = add i32 %13, 16384
store i32 %14, i32* %7
%15 = shl nsw i32 %3, 14
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @fseek(i32* %1, i32 %15, i32 %16)
%18 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 3, i64 1, i32* %1)
%19 = tail call i32 @fflush(i32* %1)
%20 = call i32 @write_recursive(i8* %0, i32* %1, i32* %5, i32* %6, i32* %7)
%21 = load i32, i32* %7
%22 = sub nsw i32 %14, %21
%23 = call { i32, i64 } @div(i32 %22, i32 16384)
%24 = extractvalue { i32, i64 } %23, 0
%25 = extractvalue { i32, i64 } %23, 1
%26 = icmp ne i64 %25, 0
%27 = zext i1 %26 to i32
%28 = add nsw i32 %24, %27
%29 = shl i32 %28, 8
%30 = load i32, i32* %6
%31 = ashr i32 %30, 8
%32 = sub i32 1, %3
%33 = add i32 %32, %31
%34 = add i32 %33, %29
ret i32 %34
}