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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
void write_block(FILE *rom, FILE *file, uint16_t sectionId) {
  uint16_t flashPage = sectionId >> 8;
  uint16_t index = sectionId & 0xFF;
  fseek(rom, flashPage * 0x4000 + index * 0x100, SEEK_SET);
  uint8_t *block = malloc(0x100);
  int len = fread(block, 1, 0x100, file);
  fwrite(block, len, 1, rom);
  fflush(rom);
  free(block);
}


x86 assembly (clang, -O0):
<write_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
shl    $0xe,%esi
mov    -0x1c(%rbp),%eax
shl    $0x8,%eax
add    %eax,%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    $0x100,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100,%edx
call   1040 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x8(%rbp),%rdi
call   1060 <fflush@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_block>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%eax
shl    $0x6,%eax
and    $0xffffc000,%eax
shl    $0x8,%edx
movzwl %dx,%esi
add    %eax,%esi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    $0x100,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
mov    %r15,%rdi
call   1060 <fflush@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
define void @write_block(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%11 = load i32, i32* %6
%12 = ashr i32 %11, 8
store i32 %12, i32* %7
%13 = load i32, i32* %6
%14 = and i32 %13, 255
store i32 %14, i32* %8
%15 = load i32*, i32** %4
%16 = load i32, i32* %7
%17 = mul nsw i32 %16, 16384
%18 = load i32, i32* %8
%19 = mul nsw i32 %18, 256
%20 = add nsw i32 %17, %19
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %15, i32 %20, i32 %21)
%23 = call i32* @malloc(i32 256)
store i32* %23, i32** %9
%24 = load i32*, i32** %9
%25 = load i32*, i32** %5
%26 = call i32 @fread(i32* %24, i32 1, i32 256, i32* %25)
store i32 %26, i32* %10
%27 = load i32*, i32** %9
%28 = load i32, i32* %10
%29 = load i32*, i32** %4
%30 = call i32 @fwrite(i32* %27, i32 %28, i32 1, i32* %29)
%31 = load i32*, i32** %4
%32 = call i32 @fflush(i32* %31)
%33 = load i32*, i32** %9
%34 = call i32 @free(i32* %33)
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define void @write_block(i32* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = shl nsw i32 %2, 6
%5 = and i32 %4, -16384
%6 = shl i32 %2, 8
%7 = and i32 %6, 65280
%8 = add nsw i32 %5, %7
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %0, i32 %8, i32 %9)
%11 = tail call i32* @malloc(i32 256)
%12 = tail call i32 @fread(i32* %11, i32 1, i32 256, i32* %1)
%13 = tail call i32 @fwrite(i32* %11, i32 %12, i32 1, i32* %0)
%14 = tail call i32 @fflush(i32* %0)
%15 = tail call i32 @free(i32* %11)
ret void
}