#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 size_t uint32_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ checkAddress(size_t);
int /*<<< orphan*/ sFLASH_EraseSector(int);
int /*<<< orphan*/ sFLASH_ReadBuffer(int *, int, int);
int /*<<< orphan*/ sFLASH_WriteBuffer(int *, int, int);
void HAL_EEPROM_Write(uint32_t address, uint8_t data) {
  if (checkAddress(address)) {
    uint8_t buf[0x200 + 1];
    buf[0x200] = 1;
    sFLASH_ReadBuffer(buf, 0x03E000, 0x200);
    sFLASH_WriteBuffer(buf, 0x03F000, 0x200);
    sFLASH_EraseSector(0x03F000);
    buf[address] = data;
    sFLASH_EraseSector(0x03E000);
    sFLASH_WriteBuffer(buf, 0x03E000, sizeof(buf));
    sFLASH_EraseSector(0x03F000);
  }
}


x86 assembly (clang, -O0):
<HAL_EEPROM_Write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x810,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <checkAddress@plt>
cmp    $0x0,%rax
je     11da <HAL_EEPROM_Write+0x9a>
movl   $0x1,-0x10(%rbp)
lea    -0x810(%rbp),%rdi
mov    $0x3e000,%esi
mov    $0x200,%edx
call   1040 <sFLASH_ReadBuffer@plt>
lea    -0x810(%rbp),%rdi
mov    $0x3f000,%esi
mov    $0x200,%edx
call   1050 <sFLASH_WriteBuffer@plt>
mov    $0x3f000,%edi
call   1060 <sFLASH_EraseSector@plt>
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,-0x810(%rbp,%rax,4)
mov    $0x3e000,%edi
call   1060 <sFLASH_EraseSector@plt>
lea    -0x810(%rbp),%rdi
mov    $0x3e000,%esi
mov    $0x804,%edx
call   1050 <sFLASH_WriteBuffer@plt>
mov    $0x3f000,%edi
call   1060 <sFLASH_EraseSector@plt>
add    $0x810,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HAL_EEPROM_Write>:
push   %rbp
push   %r14
push   %rbx
sub    $0x810,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <checkAddress@plt>
test   %rax,%rax
je     11bf <HAL_EEPROM_Write+0x7f>
movl   $0x1,0x800(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x3e000,%esi
mov    $0x200,%edx
call   1040 <sFLASH_ReadBuffer@plt>
mov    %r14,%rdi
mov    $0x3f000,%esi
mov    $0x200,%edx
call   1050 <sFLASH_WriteBuffer@plt>
mov    $0x3f000,%edi
call   1060 <sFLASH_EraseSector@plt>
mov    %ebp,(%rsp,%rbx,4)
mov    $0x3e000,%edi
call   1060 <sFLASH_EraseSector@plt>
mov    %r14,%rdi
mov    $0x3e000,%esi
mov    $0x804,%edx
call   1050 <sFLASH_WriteBuffer@plt>
mov    $0x3f000,%edi
call   1060 <sFLASH_EraseSector@plt>
add    $0x810,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @HAL_EEPROM_Write(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca [513 x i32]
store i64 %0, i64* %3
store i32 %1, i32* %4
%6 = load i64, i64* %3
%7 = call i64 @checkAddress(i64 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %23
9: ; preds = %2
%10 = getelementptr inbounds [513 x i32], [513 x i32]* %5, i64 0, i64 512
store i32 1, i32* %10
%11 = getelementptr inbounds [513 x i32], [513 x i32]* %5, i64 0, i64 0
%12 = call i32 @sFLASH_ReadBuffer(i32* %11, i32 253952, i32 512)
%13 = getelementptr inbounds [513 x i32], [513 x i32]* %5, i64 0, i64 0
%14 = call i32 @sFLASH_WriteBuffer(i32* %13, i32 258048, i32 512)
%15 = call i32 @sFLASH_EraseSector(i32 258048)
%16 = load i32, i32* %4
%17 = load i64, i64* %3
%18 = getelementptr inbounds [513 x i32], [513 x i32]* %5, i64 0, i64 %17
store i32 %16, i32* %18
%19 = call i32 @sFLASH_EraseSector(i32 253952)
%20 = getelementptr inbounds [513 x i32], [513 x i32]* %5, i64 0, i64 0
%21 = call i32 @sFLASH_WriteBuffer(i32* %20, i32 253952, i32 2052)
%22 = call i32 @sFLASH_EraseSector(i32 258048)
br label %23
23: ; preds = %9, %2
ret void
}

LLVM IR (-O2):
define void @HAL_EEPROM_Write(i64 %0, i32 %1) {
%3 = alloca [513 x i32]
%4 = tail call i64 @checkAddress(i64 %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %2
%7 = bitcast [513 x i32]* %3 to i8*
%8 = getelementptr inbounds [513 x i32], [513 x i32]* %3, i64 0, i64 512
store i32 1, i32* %8
%9 = getelementptr inbounds [513 x i32], [513 x i32]* %3, i64 0, i64 0
%10 = call i32 @sFLASH_ReadBuffer(i32* %9, i32 253952, i32 512)
%11 = call i32 @sFLASH_WriteBuffer(i32* %9, i32 258048, i32 512)
%12 = call i32 @sFLASH_EraseSector(i32 258048)
%13 = getelementptr inbounds [513 x i32], [513 x i32]* %3, i64 0, i64 %0
store i32 %1, i32* %13
%14 = call i32 @sFLASH_EraseSector(i32 253952)
%15 = call i32 @sFLASH_WriteBuffer(i32* %9, i32 253952, i32 2052)
%16 = call i32 @sFLASH_EraseSector(i32 258048)
br label %17
17: ; preds = %6, %2
ret void
}