// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_gd_gd32f4xx.c
// Function: target_select
static void target_select(target_options_t *options)
{
  uint32_t idcode, flash_size;
  bool locked, ctl_lk, ob_lk;

  dap_reset_pin(0);
  dap_reset_link();
  dap_write_word(DHCSR, DHCSR_DBGKEY | DHCSR_DEBUGEN | DHCSR_HALT);
  dap_write_word(DEMCR, DEMCR_VC_CORERESET);
  dap_write_word(AIRCR, AIRCR_VECTKEY | AIRCR_SYSRESETREQ);
  dap_reset_pin(1);

  idcode = dap_read_word(DBG_ID);

  for (int i = 0; i < ARRAY_SIZE(devices); i++)
  {
    if (devices[i].idcode != idcode)
      continue;

    verbose("Target: %s\n", devices[i].name);

    target_device = devices[i];
    target_options = *options;

    locked = (0xaa != dap_read_byte(OPTIONS_SPC));

    if (locked && !options->unlock)
      error_exit("target is locked, unlock is necessary");

    flash_size = (dap_read_word(FLASH_SRAM_SIZE_REG) >> FLASH_SIZE_REG_OFFS) * FLASH_SIZE_REG_MULT;

    target_check_options(&target_options, flash_size, FLASH_ALIGN_SIZE);

    dap_write_word(FMC_KEY, FMC_KEY_KEY1);
    dap_write_word(FMC_KEY, FMC_KEY_KEY2);
    dap_write_word(FMC_OBKEY, FMC_OBKEY_KEY1);
    dap_write_word(FMC_OBKEY, FMC_OBKEY_KEY2);
    dap_write_word(FMC_CTL, 0);

    ctl_lk = (dap_read_word(FMC_CTL) & FMC_CTL_LK) > 0;
    ob_lk  = (dap_read_word(FMC_OBCTL0) & FMC_OBCTL0_OB_LK) > 0;
    check(!ctl_lk && !ob_lk, "Failed to unlock the flash for write operation. Try to power cycle the target.");

    return;
  }

  error_exit("unknown target device (DBG_ID = 0x%08x)", idcode);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
xorl %edi, %edi
callq 0x6f90
callq 0x6800
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x77d0
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl $0x1, %esi
callq 0x77d0
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x77d0
movl $0x1, %edi
callq 0x6f90
movl $0xe0042000, %edi       # imm = 0xE0042000
callq 0x7740
movl %eax, -0xc(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x1, -0x18(%rbp)
jge 0xf51b
movslq -0x18(%rbp), %rcx
leaq 0xca28(%rip), %rax      # 0x1bdb0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0xc(%rbp), %eax
je 0xf39b
jmp 0xf50d
movslq -0x18(%rbp), %rcx
leaq 0xca0a(%rip), %rax      # 0x1bdb0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
leaq 0x751d(%rip), %rdi      # 0x168d5
movb $0x0, %al
callq 0x8630
movslq -0x18(%rbp), %rcx
leaq 0xc9e6(%rip), %rax      # 0x1bdb0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, 0x730bd(%rip)     # 0x82498
movq 0x8(%rax), %rcx
movq %rcx, 0x730ba(%rip)     # 0x824a0
movq 0x10(%rax), %rax
movq %rax, 0x730b7(%rip)     # 0x824a8
movq -0x8(%rbp), %rsi
leaq 0x730b4(%rip), %rdi     # 0x824b0
movl $0x38, %edx
callq 0x61a0
movl $0x1fffc001, %edi       # imm = 0x1FFFC001
callq 0x76e0
movzbl %al, %ecx
movl $0xaa, %eax
cmpl %ecx, %eax
setne %al
andb $0x1, %al
movb %al, -0x11(%rbp)
testb $0x1, -0x11(%rbp)
je 0xf440
movq -0x8(%rbp), %rax
testb $0x1, 0x8(%rax)
jne 0xf440
leaq 0x6c62(%rip), %rdi      # 0x1609b
movb $0x0, %al
callq 0x8970
movl $0x1fff7a20, %edi       # imm = 0x1FFF7A20
callq 0x7740
shrl $0x10, %eax
shll $0xa, %eax
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %esi
leaq 0x73053(%rip), %rdi     # 0x824b0
movl $0x100, %edx            # imm = 0x100
callq 0xa5f0
movl $0x40023c04, %edi       # imm = 0x40023C04
movl $0x45670123, %esi       # imm = 0x45670123
callq 0x77d0
movl $0x40023c04, %edi       # imm = 0x40023C04
movl $0xcdef89ab, %esi       # imm = 0xCDEF89AB
callq 0x77d0
movl $0x40023c08, %edi       # imm = 0x40023C08
movl $0x8192a3b, %esi        # imm = 0x8192A3B
callq 0x77d0
movl $0x40023c08, %edi       # imm = 0x40023C08
movl $0x4c5d6e7f, %esi       # imm = 0x4C5D6E7F
callq 0x77d0
movl $0x40023c10, %edi       # imm = 0x40023C10
xorl %esi, %esi
callq 0x77d0
movl $0x40023c10, %edi       # imm = 0x40023C10
callq 0x7740
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
seta %al
andb $0x1, %al
movb %al, -0x12(%rbp)
movl $0x40023c14, %edi       # imm = 0x40023C14
callq 0x7740
andl $0x1, %eax
cmpl $0x0, %eax
seta %al
andb $0x1, %al
movb %al, -0x13(%rbp)
xorl %eax, %eax
testb $0x1, -0x12(%rbp)
movb %al, -0x19(%rbp)
jne 0xf4f4
movb -0x13(%rbp), %al
xorb $-0x1, %al
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x7f00(%rip), %rsi      # 0x17404
xorl %eax, %eax
callq 0x8880
jmp 0xf52c
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xf373
movl -0xc(%rbp), %esi
leaq 0x80cb(%rip), %rdi      # 0x175f0
movb $0x0, %al
callq 0x8970
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)