// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_atmel_cm4v2.c
// Function: target_program
static void target_program(void)
{
  uint32_t addr = FLASH_ADDR + target_options.offset;
  uint32_t offs = 0;
  uint32_t number_of_rows;
  uint8_t *buf = target_options.file_data;
  uint32_t size = target_options.file_size;

  dap_write_half(NVMCTRL_CTRLA, NVMCTRL_CTRLA_AUTOWS | NVMCTRL_CTRLA_WMODE_MAN |
      NVMCTRL_CTRLA_PRM_MANUAL | NVMCTRL_CTRLA_CACHEDIS0 | NVMCTRL_CTRLA_CACHEDIS1);

  number_of_rows = (size + FLASH_ROW_SIZE - 1) / FLASH_ROW_SIZE;

  for (uint32_t row = 0; row < number_of_rows; row++)
  {
    dap_write_word(NVMCTRL_ADDR, addr);

    dap_write_half(NVMCTRL_CTRLB, NVMCTRL_CMD_UR); // Unlock Region
    while (0 == (dap_read_half(NVMCTRL_STATUS) & NVMCTRL_STATUS_READY));

    dap_write_half(NVMCTRL_CTRLB, NVMCTRL_CMD_EB);
    while (0 == (dap_read_half(NVMCTRL_STATUS) & NVMCTRL_STATUS_READY));

    for (int page = 0; page < PAGES_IN_ERASE_BLOCK; page++)
    {
      dap_write_word(NVMCTRL_ADDR, addr);

      dap_write_half(NVMCTRL_CTRLB, NVMCTRL_CMD_PBC);
      while (0 == (dap_read_half(NVMCTRL_STATUS) & NVMCTRL_STATUS_READY));

      dap_write_block(addr, &buf[offs], FLASH_PAGE_SIZE);

      dap_write_half(NVMCTRL_CTRLB, NVMCTRL_CMD_WP); // Write page
      while (0 == (dap_read_half(NVMCTRL_STATUS) & NVMCTRL_STATUS_READY));

      addr += FLASH_PAGE_SIZE;
      offs += FLASH_PAGE_SIZE;
    }

    verbose(".");
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl 0x74eda(%rip), %eax     # 0x82358
addl $0x0, %eax
movl %eax, -0x4(%rbp)
movl $0x0, -0x8(%rbp)
movq 0x74ede(%rip), %rax     # 0x82370
movq %rax, -0x18(%rbp)
movl 0x74ecc(%rip), %eax     # 0x82368
movl %eax, -0x1c(%rbp)
movl $0x41004000, %edi       # imm = 0x41004000
movl $0xc0c4, %esi           # imm = 0xC0C4
callq 0x77a0
movl -0x1c(%rbp), %eax
addl $0x2000, %eax           # imm = 0x2000
subl $0x1, %eax
shrl $0xd, %eax
movl %eax, -0xc(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0xc(%rbp), %eax
jae 0xd5f0
movl -0x4(%rbp), %esi
movl $0x41004014, %edi       # imm = 0x41004014
callq 0x77d0
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa512, %esi           # imm = 0xA512
callq 0x77a0
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x7710
movzwl %ax, %ecx
andl $0x1, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xd506
jmp 0xd4ee
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa501, %esi           # imm = 0xA501
callq 0x77a0
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x7710
movzwl %ax, %ecx
andl $0x1, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xd52d
jmp 0xd515
movl $0x0, -0x24(%rbp)
cmpl $0x10, -0x24(%rbp)
jge 0xd5d4
movl -0x4(%rbp), %esi
movl $0x41004014, %edi       # imm = 0x41004014
callq 0x77d0
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa515, %esi           # imm = 0xA515
callq 0x77a0
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x7710
movzwl %ax, %ecx
andl $0x1, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xd572
jmp 0xd55a
movl -0x4(%rbp), %edi
movq -0x18(%rbp), %rsi
movl -0x8(%rbp), %eax
addq %rax, %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x79c0
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa503, %esi           # imm = 0xA503
callq 0x77a0
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x7710
movzwl %ax, %ecx
andl $0x1, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xd5b0
jmp 0xd598
movl -0x4(%rbp), %eax
addl $0x200, %eax            # imm = 0x200
movl %eax, -0x4(%rbp)
movl -0x8(%rbp), %eax
addl $0x200, %eax            # imm = 0x200
movl %eax, -0x8(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xd534
leaq 0x7294(%rip), %rdi      # 0x1486f
movb $0x0, %al
callq 0x8630
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xd4c6
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)