// File: /workspace/github_c_makefile_star2_O3/ataradov[P]edbg/target_atmel_cm4.c
// Function: target_program
static void target_program(void)
{
  uint32_t addr = FLASH_START + target_options.offset;
  uint32_t number_of_pages, plane, page_offset;
  uint32_t offs = 0;
  uint8_t *buf = target_options.file_data;
  uint32_t size = target_options.file_size;

  number_of_pages = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;
  page_offset = target_options.offset / FLASH_PAGE_SIZE;

  for (uint32_t page = 0; page < number_of_pages; page += PAGES_IN_ERASE_BLOCK)
  {
    plane = (page + page_offset) / (target_device.flash_size / FLASH_PAGE_SIZE);

    dap_write_word(EEFC_FCR(plane), CMD_EPA | (((page_offset + page) | 2) << 8));
    while (0 == (dap_read_word(EEFC_FSR(plane)) & FSR_FRDY));

    verbose(".");
  }

  verbose(",");

  for (uint32_t page = 0; page < number_of_pages; page++)
  {
    dap_write_block(addr, &buf[offs], FLASH_PAGE_SIZE);
    addr += FLASH_PAGE_SIZE;
    offs += FLASH_PAGE_SIZE;

    plane = (page + page_offset) / (target_device.flash_size / FLASH_PAGE_SIZE);

    dap_write_word(EEFC_FCR(plane), CMD_WP | ((page + page_offset) << 8));
    while (0 == (dap_read_word(EEFC_FSR(plane)) & FSR_FRDY));

    verbose(".");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x74594(%rip), %ebx     # 0x7eb48
movl $0x1ff, %ecx            # imm = 0x1FF
leal (%rbx,%rcx), %r12d
addl 0x74595(%rip), %ecx     # 0x7eb58
testl %ebx, %ebx
cmovnsl %ebx, %r12d
cmpl $0x200, %ecx            # imm = 0x200
jae 0xa5ed
leaq 0x87e4(%rip), %rdi      # 0x12dbc
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7a90
addl $0x400000, %ebx         # imm = 0x400000
movq 0x74566(%rip), %rax     # 0x7eb60
movq %rax, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movl %ecx, %ebp
shrl $0x9, %ebp
sarl $0x9, %r12d
leaq 0x61fb(%rip), %r14      # 0x1080f
xorl %r13d, %r13d
leal (%r12,%r13), %esi
movl 0x7450b(%rip), %ecx     # 0x7eb2c
shrl $0x9, %ecx
movl %esi, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r15d
shll $0x9, %r15d
leal 0x400e0a04(%r15), %edi
shll $0x8, %esi
orl $0x5a000207, %esi       # imm = 0x5A000207
callq 0x7269
addl $0x400e0a08, %r15d      # imm = 0x400E0A08
movl %r15d, %edi
callq 0x721a
testb $0x1, %al
je 0xa64d
movq %r14, %rdi
xorl %eax, %eax
callq 0x7a90
addl $0x10, %r13d
cmpl %ebp, %r13d
jb 0xa617
leaq 0x8749(%rip), %rdi      # 0x12dbc
xorl %eax, %eax
callq 0x7a90
cmpl $0x200, 0x8(%rsp)       # imm = 0x200
jb 0xa708
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x10(%rsp), %rax
leaq (%rax,%r13), %rsi
movl %ebx, %edi
movl $0x200, %edx            # imm = 0x200
callq 0x73df
addl $0x200, %ebx            # imm = 0x200
leal (%r14,%r12), %esi
movl 0x74479(%rip), %ecx     # 0x7eb2c
shrl $0x9, %ecx
movl %esi, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r15d
shll $0x9, %r15d
leal 0x400e0a04(%r15), %edi
shll $0x8, %esi
orl $0x5a000001, %esi       # imm = 0x5A000001
callq 0x7269
addl $0x400e0a08, %r15d      # imm = 0x400E0A08
movl %r15d, %edi
callq 0x721a
testb $0x1, %al
je 0xa6df
addq $0x200, %r13            # imm = 0x200
leaq 0x6116(%rip), %rdi      # 0x1080f
xorl %eax, %eax
callq 0x7a90
incl %r14d
cmpl %ebp, %r14d
jne 0xa68e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq