#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 /*<<< orphan*/ buf;

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


x86 assembly (clang, -O0):
<HAL_EEPROM_Put>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <checkAddress@plt>
cmp    $0x0,%rax
je     11fe <HAL_EEPROM_Put+0xae>
movl   $0x1,-0x20(%rbp)
lea    -0x820(%rbp),%rdi
mov    $0x3e000,%esi
mov    $0x200,%edx
call   1040 <sFLASH_ReadBuffer@plt>
lea    -0x820(%rbp),%rdi
mov    $0x3f000,%esi
mov    $0x200,%edx
call   1060 <sFLASH_WriteBuffer@plt>
mov    $0x3f000,%edi
call   1070 <sFLASH_EraseSector@plt>
lea    -0x820(%rbp),%rdi
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    $0x3e000,%edi
call   1070 <sFLASH_EraseSector@plt>
lea    -0x820(%rbp),%rdi
mov    $0x3e000,%esi
mov    $0x804,%edx
call   1060 <sFLASH_WriteBuffer@plt>
mov    $0x3f000,%edi
call   1070 <sFLASH_EraseSector@plt>
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HAL_EEPROM_Put>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <checkAddress@plt>
test   %rax,%rax
je     11e3 <HAL_EEPROM_Put+0x93>
movl   $0x1,0x800(%rsp)
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x3e000,%esi
mov    $0x200,%edx
call   1040 <sFLASH_ReadBuffer@plt>
mov    %r15,%rdi
mov    $0x3f000,%esi
mov    $0x200,%edx
call   1060 <sFLASH_WriteBuffer@plt>
mov    $0x3f000,%edi
call   1070 <sFLASH_EraseSector@plt>
movslq %ebp,%rax
lea    (%rsp,%rax,4),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
mov    $0x3e000,%edi
call   1070 <sFLASH_EraseSector@plt>
mov    %r15,%rdi
mov    $0x3e000,%esi
mov    $0x804,%edx
call   1060 <sFLASH_WriteBuffer@plt>
mov    $0x3f000,%edi
call   1070 <sFLASH_EraseSector@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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