// File: /workspace/github_c_makefile_star2_O0/orempel[P]multiboot_tool/funkboot.c
// Function: funk_write_memory
static int funk_write_memory(struct funk_privdata *funk,
                             uint8_t *buffer,
                             uint16_t size,
                             uint8_t memtype,
                             uint16_t address)
{
    if (memtype == MEMTYPE_EEPROM)
    {
        return __funk_write_memory(funk, buffer, size, memtype, address);
    }
    else if ((address & (funk->flashpage -1)) != 0x00)
    {
        fprintf(stderr, "funk_write_memory(): address 0x%04x not aligned to pagesize 0x%02x\n", address, funk->flashpage);
        return -1;
    }

    uint8_t *pagebuf = malloc(funk->flashpage);
    if (pagebuf == NULL)
    {
        perror("malloc()");
        return -1;
    }

    memcpy(pagebuf, buffer, size);
    memset(pagebuf + size, 0xFF, funk->flashpage - size);

    int pos = 0;
    int ret = 0;
    for (pos = 0; pos < funk->flashpage; pos += WRITE_BLOCK_SIZE)
    {
        ret = __funk_write_memory(funk, &pagebuf[pos], WRITE_BLOCK_SIZE, memtype, address + pos);
        if (ret < 0)
        {
            break;
        }
    }

    free(pagebuf);
    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movw %r8w, %ax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movw %dx, -0x1a(%rbp)
movb %cl, -0x1b(%rbp)
movw %ax, -0x1e(%rbp)
movzbl -0x1b(%rbp), %eax
cmpl $0x2, %eax
jne 0x736f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movw -0x1a(%rbp), %cx
movb -0x1b(%rbp), %al
movzwl %cx, %edx
movzbl %al, %ecx
movzwl -0x1e(%rbp), %r8d
callq 0x74a0
movl %eax, -0x4(%rbp)
jmp 0x7491
movzwl -0x1e(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
subl $0x1, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x73b3
movq 0x8c6d(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movzwl -0x1e(%rbp), %edx
movq -0x10(%rbp), %rax
movl 0x20(%rax), %ecx
leaq 0x4d3c(%rip), %rsi      # 0xc0dc
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7491
jmp 0x73b5
movq -0x10(%rbp), %rax
movslq 0x20(%rax), %rdi
callq 0x21b0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x73e5
leaq 0x4716(%rip), %rdi      # 0xbaea
callq 0x2210
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7491
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
movzwl -0x1a(%rbp), %eax
movl %eax, %edx
callq 0x2190
movq -0x28(%rbp), %rdi
movzwl -0x1a(%rbp), %eax
cltq
addq %rax, %rdi
movq -0x10(%rbp), %rax
movl 0x20(%rax), %eax
movzwl -0x1a(%rbp), %ecx
subl %ecx, %eax
movslq %eax, %rdx
movl $0xff, %esi
callq 0x2100
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x7482
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movslq -0x2c(%rbp), %rax
addq %rax, %rsi
movb -0x1b(%rbp), %cl
movzwl -0x1e(%rbp), %eax
addl -0x2c(%rbp), %eax
movl $0x20, %edx
movzbl %cl, %ecx
movzwl %ax, %r8d
callq 0x74a0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jge 0x7475
jmp 0x7482
jmp 0x7477
movl -0x2c(%rbp), %eax
addl $0x20, %eax
movl %eax, -0x2c(%rbp)
jmp 0x7434
movq -0x28(%rbp), %rdi
callq 0x2030
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)