// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_atmel_cm4.c
// Function: target_select
static void target_select(target_options_t *options)
{
  uint32_t chip_id, chip_exid;

  dap_reset_target_hw(1);
  dap_reset_link();

  // Stop the core
  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);

  chip_id = dap_read_word(CHIPID_CIDR);
  chip_exid = dap_read_word(CHIPID_EXID);

  for (int i = 0; i < ARRAY_SIZE(devices); i++)
  {
    uint32_t fl_id, fl_size, fl_page_size, fl_nb_palne, fl_nb_lock;

    if (devices[i].chip_id != chip_id || devices[i].chip_exid != chip_exid)
      continue;

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

    for (uint32_t plane = 0; plane < devices[i].n_planes; plane++)
    {
      dap_write_word(EEFC_FCR(plane), CMD_GETD);
      while (0 == (dap_read_word(EEFC_FSR(plane)) & FSR_FRDY));

      fl_id = dap_read_word(EEFC_FRR(plane));
      check(fl_id, "Cannot read flash descriptor, check Erase pin state");

      fl_size = dap_read_word(EEFC_FRR(plane));
      check(fl_size == devices[i].flash_size, "Invalid reported Flash size (%d)", fl_size);

      fl_page_size = dap_read_word(EEFC_FRR(plane));
      check(fl_page_size == FLASH_PAGE_SIZE, "Invalid reported page size (%d)", fl_page_size);

      fl_nb_palne = dap_read_word(EEFC_FRR(plane));
      for (uint32_t i = 0; i < fl_nb_palne; i++)
        dap_read_word(EEFC_FRR(plane));

      fl_nb_lock =  dap_read_word(EEFC_FRR(plane));
      for (uint32_t i = 0; i < fl_nb_lock; i++)
        dap_read_word(EEFC_FRR(plane));
    }

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

    target_check_options(&target_options, devices[i].flash_size * target_device.n_planes,
        FLASH_PAGE_SIZE * PAGES_IN_ERASE_BLOCK);

    return;
  }

  error_exit("unknown target device (CHIP_ID = 0x%08x)", chip_id);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, %edi
callq 0x6ef0
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 $0x400e0740, %edi       # imm = 0x400E0740
callq 0x7740
movl %eax, -0xc(%rbp)
movl $0x400e0744, %edi       # imm = 0x400E0744
callq 0x7740
movl %eax, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
cmpl $0x37, -0x14(%rbp)
jge 0xc4bf
movslq -0x14(%rbp), %rcx
leaq 0x11712(%rip), %rax     # 0x1d960
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0xc276
movslq -0x14(%rbp), %rcx
leaq 0x116f9(%rip), %rax     # 0x1d960
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x10(%rbp), %eax
je 0xc27b
jmp 0xc4b1
movslq -0x14(%rbp), %rcx
leaq 0x116da(%rip), %rax     # 0x1d960
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
leaq 0xa63d(%rip), %rdi      # 0x168d5
movb $0x0, %al
callq 0x8630
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movslq -0x14(%rbp), %rdx
leaq 0x116ac(%rip), %rcx     # 0x1d960
shlq $0x5, %rdx
addq %rdx, %rcx
cmpl 0x18(%rcx), %eax
jae 0xc430
movl -0x2c(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a04, %edi       # imm = 0x400E0A04
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x77d0
movl -0x2c(%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 0xc2f8
jmp 0xc2da
movl -0x2c(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a0c, %edi       # imm = 0x400E0A0C
callq 0x7740
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0xa5c1(%rip), %rsi      # 0x168e1
xorl %eax, %eax
callq 0x8880
movl -0x2c(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a0c, %edi       # imm = 0x400E0A0C
callq 0x7740
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movslq -0x14(%rbp), %rdx
leaq 0x11617(%rip), %rcx     # 0x1d960
shlq $0x5, %rdx
addq %rdx, %rcx
cmpl 0x1c(%rcx), %eax
sete %al
movl -0x1c(%rbp), %edx
movzbl %al, %edi
andl $0x1, %edi
leaq 0xa5af(%rip), %rsi      # 0x16915
xorl %eax, %eax
callq 0x8880
movl -0x2c(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a0c, %edi       # imm = 0x400E0A0C
callq 0x7740
movl %eax, -0x20(%rbp)
cmpl $0x200, -0x20(%rbp)     # imm = 0x200
sete %al
movl -0x20(%rbp), %edx
movzbl %al, %edi
andl $0x1, %edi
leaq 0xa59b(%rip), %rsi      # 0x16936
xorl %eax, %eax
callq 0x8880
movl -0x2c(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a0c, %edi       # imm = 0x400E0A0C
callq 0x7740
movl %eax, -0x24(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0xc3e1
movl -0x2c(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a0c, %edi       # imm = 0x400E0A0C
callq 0x7740
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xc3bd
movl -0x2c(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a0c, %edi       # imm = 0x400E0A0C
callq 0x7740
movl %eax, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0xc420
movl -0x2c(%rbp), %edi
shll $0x9, %edi
addl $0x400e0a0c, %edi       # imm = 0x400E0A0C
callq 0x7740
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xc3fc
jmp 0xc422
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xc2a6
movslq -0x14(%rbp), %rcx
leaq 0x11525(%rip), %rax     # 0x1d960
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, 0x75e24(%rip)     # 0x82270
movq 0x8(%rax), %rcx
movq %rcx, 0x75e21(%rip)     # 0x82278
movq 0x10(%rax), %rcx
movq %rcx, 0x75e1e(%rip)     # 0x82280
movq 0x18(%rax), %rax
movq %rax, 0x75e1b(%rip)     # 0x82288
movq -0x8(%rbp), %rsi
leaq 0x75e18(%rip), %rdi     # 0x82290
movl $0x38, %edx
callq 0x61a0
movslq -0x14(%rbp), %rcx
leaq 0x114d3(%rip), %rax     # 0x1d960
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %esi
imull 0x75dea(%rip), %esi     # 0x82288
leaq 0x75deb(%rip), %rdi     # 0x82290
movl $0x2000, %edx           # imm = 0x2000
callq 0xa5f0
jmp 0xc4d0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xc239
movl -0xc(%rbp), %esi
leaq 0xa57c(%rip), %rdi      # 0x16a45
movb $0x0, %al
callq 0x8970
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)