// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl $0x8000000, %ebx        # imm = 0x8000000
addl 0x724e3(%rip), %ebx     # 0x7ed38
movq 0x724f4(%rip), %r14     # 0x7ed50
movl 0x724e6(%rip), %edi     # 0x7ed48
movl $0x100, %esi            # imm = 0x100
callq 0x7d7d
xorl %r10d, %r10d
leaq 0x6d8a(%rip), %rcx      # 0x13600
movl 0x724bc(%rip), %edx     # 0x7ed38
xorl %esi, %esi
movq %rbx, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movl %eax, 0x4(%rsp)
movl (%rcx), %edi
shll $0xa, %edi
addl %esi, %edi
cmpl %edx, %esi
setle %r8b
cmpl %edi, %edx
setl %r9b
testb %r9b, %r8b
jne 0xc8c1
decq %r10
addq $0x4, %rcx
movl %edi, %esi
cmpq $-0x1c, %r10
jne 0xc88c
movl $0x1c, %r13d
movl $0x1c, %r10d
jmp 0xc902
movq %r10, %r13
negq %r13
cmpq $0x1c, %r13
jae 0xc8f5
addl %eax, %edx
movl %esi, %edi
movl (%rcx), %esi
shll $0xa, %esi
addl %edi, %esi
cmpl %edx, %edi
jg 0xc8e0
cmpl %esi, %edx
jle 0xc8fa
decq %r10
addq $0x4, %rcx
cmpq $-0x1c, %r10
jne 0xc8cf
movl $0x1c, %r10d
jmp 0xc8fd
movl %r13d, %r10d
jmp 0xc902
negl %r10d
cmpl %r13d, %r10d
jb 0xc96d
movl %r10d, %r12d
subq %r13, %r12
shlq $0x2, %r13
incq %r12
leaq 0x6d5a(%rip), %r15      # 0x13670
leaq 0x3ef2(%rip), %r14      # 0x1080f
xorl %ebx, %ebx
leaq (,%rbx,4), %rax
addq %r13, %rax
movl (%r15,%rax), %eax
leal 0x2(,%rax,8), %esi
movl %eax, %ebp
shll $0x3, %ebp
movl $0x40023c10, %edi       # imm = 0x40023C10
callq 0x7269
orl $0x10002, %ebp          # imm = 0x10002
movl $0x40023c10, %edi       # imm = 0x40023C10
movl %ebp, %esi
callq 0x7269
callq 0xcc33
movq %r14, %rdi
xorl %eax, %eax
callq 0x7a90
incq %rbx
cmpq %rbx, %r12
jne 0xc91f
leaq 0x6448(%rip), %rdi      # 0x12dbc
xorl %eax, %eax
callq 0x7a90
movl $0x40023c10, %edi       # imm = 0x40023C10
movl $0x201, %esi            # imm = 0x201
callq 0x7269
movl 0x4(%rsp), %ebp
testl %ebp, %ebp
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %r12
je 0xc9d4
leaq 0x3e6c(%rip), %r14      # 0x1080f
xorl %ebx, %ebx
leal (%r15,%rbx), %edi
movl %ebx, %esi
addq %r12, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x73df
addl $0x100, %ebx            # imm = 0x100
testl $0x3f00, %ebx           # imm = 0x3F00
jne 0xc9d0
movq %r14, %rdi
xorl %eax, %eax
callq 0x7a90
cmpl %ebx, %ebp
jne 0xc9a5
callq 0xcc33
movl $0x40023c10, %edi       # imm = 0x40023C10
xorl %esi, %esi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7269