// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movl 0x75cea(%rip), %eax     # 0x822a8
addl $0x400000, %eax         # imm = 0x400000
movl %eax, -0x4(%rbp)
movl $0x0, -0x14(%rbp)
movq 0x75cec(%rip), %rax     # 0x822c0
movq %rax, -0x20(%rbp)
movl 0x75cda(%rip), %eax     # 0x822b8
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
addl $0x200, %eax            # imm = 0x200
subl $0x1, %eax
shrl $0x9, %eax
movl %eax, -0x8(%rbp)
movl 0x75cb0(%rip), %eax     # 0x822a8
movl $0x200, %ecx            # imm = 0x200
cltd
idivl %ecx
movl %eax, -0x10(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x8(%rbp), %eax
jae 0xc682
movl -0x28(%rbp), %eax
addl -0x10(%rbp), %eax
movl 0x75c6e(%rip), %ecx     # 0x8228c
shrl $0x9, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a04, %edi       # imm = 0x400E0A04
movl -0x10(%rbp), %esi
addl -0x28(%rbp), %esi
orl $0x2, %esi
shll $0x8, %esi
orl $0x5a000007, %esi       # imm = 0x5A000007
callq 0x77d0
movl -0xc(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a08, %edi       # imm = 0x400E0A08
callq 0x7740
movl %eax, %ecx
andl $0x1, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xc669
jmp 0xc64b
leaq 0x81ff(%rip), %rdi      # 0x1486f
movb $0x0, %al
callq 0x8630
movl -0x28(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x28(%rbp)
jmp 0xc60a
leaq 0xa7a3(%rip), %rdi      # 0x16e2c
movb $0x0, %al
callq 0x8630
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jae 0xc740
movl -0x4(%rbp), %edi
movq -0x20(%rbp), %rsi
movl -0x14(%rbp), %eax
addq %rax, %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x79c0
movl -0x4(%rbp), %eax
addl $0x200, %eax            # imm = 0x200
movl %eax, -0x4(%rbp)
movl -0x14(%rbp), %eax
addl $0x200, %eax            # imm = 0x200
movl %eax, -0x14(%rbp)
movl -0x2c(%rbp), %eax
addl -0x10(%rbp), %eax
movl 0x75bb0(%rip), %ecx     # 0x8228c
shrl $0x9, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a04, %edi       # imm = 0x400E0A04
movl -0x2c(%rbp), %esi
addl -0x10(%rbp), %esi
shll $0x8, %esi
orl $0x5a000001, %esi       # imm = 0x5A000001
callq 0x77d0
movl -0xc(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a08, %edi       # imm = 0x400E0A08
callq 0x7740
movl %eax, %ecx
andl $0x1, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xc724
jmp 0xc706
leaq 0x8144(%rip), %rdi      # 0x1486f
movb $0x0, %al
callq 0x8630
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xc697
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)