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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, scalar_t__, int,
                          int /*<<< orphan*/ *);
void write_fat(FILE *rom, uint8_t *entry, uint16_t length, uint32_t *fatptr) {
  *fatptr -= length;
  fseek(rom, *fatptr, SEEK_SET);
  fwrite(entry, length, 1, rom);
  fflush(rom);
}


x86 assembly (clang, -O0):
<write_fat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rax
movslq (%rax),%rcx
sub    %rdx,%rcx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
call   1040 <fseek@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fwrite@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fflush@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_fat>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rcx),%esi
sub    %r15d,%esi
mov    %esi,(%rcx)
mov    0x2e94(%rip),%rax
mov    (%rax),%edx
call   1040 <fseek@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fwrite@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <fflush@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
define void @write_fat(i32* %0, i32* %1, i64 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32*
store i32* %0, i32** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%9 = load i64, i64* %7
%10 = load i32*, i32** %8
%11 = load i32, i32* %10
%12 = sext i32 %11 to i64
%13 = sub nsw i64 %12, %9
%14 = trunc i64 %13 to i32
store i32 %14, i32* %10
%15 = load i32*, i32** %5
%16 = load i32*, i32** %8
%17 = load i32, i32* %16
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @fseek(i32* %15, i32 %17, i32 %18)
%20 = load i32*, i32** %6
%21 = load i64, i64* %7
%22 = load i32*, i32** %5
%23 = call i32 @fwrite(i32* %20, i64 %21, i32 1, i32* %22)
%24 = load i32*, i32** %5
%25 = call i32 @fflush(i32* %24)
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define void @write_fat(i32* nocapture %0, i32* nocapture %1, i64 %2, i32* nocapture %3) {
%5 = load i32, i32* %3
%6 = trunc i64 %2 to i32
%7 = sub i32 %5, %6
store i32 %7, i32* %3
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %0, i32 %7, i32 %8)
%10 = tail call i32 @fwrite(i32* %1, i64 %2, i32 1, i32* %0)
%11 = tail call i32 @fflush(i32* %0)
ret void
}