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

  size = round_up(size, FLASH_ALIGN_SIZE);

  for (start_sector = 0; start_sector < FLASH_SECTOR_COUNT; start_sector++)
  {
    int sector_end = sector_offset + flash_sector_size[start_sector] * 1024;
    int start = target_options.offset;

    if (sector_offset <= start && start < sector_end)
      break;

    sector_offset = sector_end;
  }

  for (end_sector = start_sector; end_sector < FLASH_SECTOR_COUNT; end_sector++)
  {
    int sector_end = sector_offset + flash_sector_size[end_sector] * 1024;
    int end = target_options.offset + size;

    if (sector_offset <= end && end <= sector_end)
      break;

    sector_offset = sector_end;
  }

  for (int i = start_sector; i <= end_sector; i++)
  {
    uint32_t cmd = FMC_CTL_SER | FMC_CTL_SN(flash_sector_index[i]);

    dap_write_word(FMC_CTL, cmd);
    dap_write_word(FMC_CTL, cmd | FMC_CTL_START);
    flash_wait_done();

    verbose(".");
  }

  verbose(",");

  // TODO: Verify this works with fast SWD clocks

  dap_write_word(FMC_CTL, FMC_CTL_PSZ_WORD | FMC_CTL_PG);

  while (size)
  {
    dap_write_block(addr, &buf[offs], FLASH_ALIGN_SIZE);

    addr += FLASH_ALIGN_SIZE;
    offs += FLASH_ALIGN_SIZE;
    size -= FLASH_ALIGN_SIZE;

    if (0 == (offs % (FLASH_ALIGN_SIZE * 64)))
      verbose(".");
  }

  flash_wait_done();

  dap_write_word(FMC_CTL, 0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl 0x72e2a(%rip), %eax     # 0x824c8
addl $0x8000000, %eax        # imm = 0x8000000
movl %eax, -0x4(%rbp)
movl $0x0, -0x8(%rbp)
movq 0x72e2c(%rip), %rax     # 0x824e0
movq %rax, -0x10(%rbp)
movl 0x72e1a(%rip), %eax     # 0x824d8
movl %eax, -0x14(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x14(%rbp), %edi
movl $0x100, %esi            # imm = 0x100
callq 0x8a70
movl %eax, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x1c, -0x18(%rbp)
jge 0xf72a
movl -0x20(%rbp), %eax
movslq -0x18(%rbp), %rdx
leaq 0x7f6d(%rip), %rcx      # 0x17660
movl (%rcx,%rdx,4), %ecx
shll $0xa, %ecx
addl %ecx, %eax
movl %eax, -0x24(%rbp)
movl 0x72dc4(%rip), %eax     # 0x824c8
movl %eax, -0x28(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jg 0xf719
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0xf719
jmp 0xf72a
movl -0x24(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xf6df
movl -0x18(%rbp), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x1c, -0x1c(%rbp)
jge 0xf77e
movl -0x20(%rbp), %eax
movslq -0x1c(%rbp), %rdx
leaq 0x7f1c(%rip), %rcx      # 0x17660
movl (%rcx,%rdx,4), %ecx
shll $0xa, %ecx
addl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl 0x72d73(%rip), %eax     # 0x824c8
addl -0x14(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x30(%rbp), %eax
jg 0xf76d
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jg 0xf76d
jmp 0xf77e
movl -0x2c(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xf730
movl -0x18(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jg 0xf7e1
movslq -0x34(%rbp), %rcx
leaq 0x7f39(%rip), %rax      # 0x176d0
movl (%rax,%rcx,4), %eax
shll $0x3, %eax
orl $0x2, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %esi
movl $0x40023c10, %edi       # imm = 0x40023C10
callq 0x77d0
movl -0x38(%rbp), %esi
orl $0x10000, %esi          # imm = 0x10000
movl $0x40023c10, %edi       # imm = 0x40023C10
callq 0x77d0
callq 0xfc40
leaq 0x50a0(%rip), %rdi      # 0x1486f
movb $0x0, %al
callq 0x8630
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf784
leaq 0x7644(%rip), %rdi      # 0x16e2c
movb $0x0, %al
callq 0x8630
movl $0x40023c10, %edi       # imm = 0x40023C10
movl $0x201, %esi            # imm = 0x201
callq 0x77d0
cmpl $0x0, -0x14(%rbp)
je 0xf85b
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rsi
movl -0x8(%rbp), %eax
addq %rax, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x79c0
movl -0x4(%rbp), %eax
addl $0x100, %eax            # imm = 0x100
movl %eax, -0x4(%rbp)
movl -0x8(%rbp), %eax
addl $0x100, %eax            # imm = 0x100
movl %eax, -0x8(%rbp)
movl -0x14(%rbp), %eax
subl $0x100, %eax            # imm = 0x100
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xf859
leaq 0x501d(%rip), %rdi      # 0x1486f
movb $0x0, %al
callq 0x8630
jmp 0xf7fe
callq 0xfc40
movl $0x40023c10, %edi       # imm = 0x40023C10
xorl %esi, %esi
callq 0x77d0
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)