// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_mchp_cm23.c
// Function: target_select
static void target_select(target_options_t *options)
{
  uint32_t dsu_did, id, rev;

  reset_with_extension();

  dsu_did = dap_read_word(DSU_DID);
  id = dsu_did & DEVICE_ID_MASK;
  rev = (dsu_did >> DEVICE_REV_SHIFT) & DEVICE_REV_MASK;

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

    verbose("Target: %s (Rev %c)\n", devices[i].name, 'A' + rev);

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

    if (target_device.trust_zone)
    {
      NVMCTRL_CTRLA  = NVMCTRL_NSEC_CTRLA + NVMCTRL_SEC_OFFSET;
      NVMCTRL_CTRLB  = NVMCTRL_NSEC_CTRLB + NVMCTRL_SEC_OFFSET;
      NVMCTRL_CTRLC  = NVMCTRL_NSEC_CTRLC + NVMCTRL_SEC_OFFSET;
      NVMCTRL_STATUS = NVMCTRL_NSEC_STATUS + NVMCTRL_SEC_OFFSET;
      NVMCTRL_ADDR   = NVMCTRL_NSEC_ADDR + NVMCTRL_SEC_OFFSET;
    }
    else
    {
      NVMCTRL_CTRLA  = NVMCTRL_NSEC_CTRLA;
      NVMCTRL_CTRLB  = NVMCTRL_NSEC_CTRLB;
      NVMCTRL_CTRLC  = NVMCTRL_NSEC_CTRLC;
      NVMCTRL_STATUS = NVMCTRL_NSEC_STATUS;
      NVMCTRL_ADDR   = NVMCTRL_NSEC_ADDR;
    }

    target_check_options(&target_options, target_device.flash_size, FLASH_ROW_SIZE);

    return;
  }

  error_exit("unknown target device (DSU_DID = 0x%08x)", dsu_did);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
callq 0xe270
movl $0x41002118, %edi       # imm = 0x41002118
callq 0x7740
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
andl $0xfffff0ff, %eax       # imm = 0xFFFFF0FF
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
shrl $0x8, %eax
andl $0xf, %eax
movl %eax, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x13, -0x18(%rbp)
jge 0xdb4c
movslq -0x18(%rbp), %rcx
leaq 0x10fef(%rip), %rax     # 0x1ea30
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x10(%rbp), %eax
je 0xda54
jmp 0xdb3e
movslq -0x18(%rbp), %rcx
leaq 0x10fd1(%rip), %rax     # 0x1ea30
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
movl -0x14(%rbp), %edx
addl $0x41, %edx
leaq 0x860f(%rip), %rdi      # 0x16086
movb $0x0, %al
callq 0x8630
movslq -0x18(%rbp), %rax
leaq 0x10fa7(%rip), %rsi     # 0x1ea30
imulq $0x28, %rax, %rax
addq %rax, %rsi
leaq 0x748e1(%rip), %rdi     # 0x82378
movl $0x28, %edx
callq 0x61a0
movq -0x8(%rbp), %rsi
leaq 0x748f4(%rip), %rdi     # 0x823a0
movl $0x38, %edx
callq 0x61a0
testb $0x1, 0x748d7(%rip)     # 0x82394
je 0xdaf3
movl $0x41005000, 0x7490f(%rip) # imm = 0x41005000
movl $0x41005004, 0x74909(%rip) # imm = 0x41005004
movl $0x41005008, 0x74903(%rip) # imm = 0x41005008
movl $0x41005018, 0x748fd(%rip) # imm = 0x41005018
movl $0x4100501c, 0x748f7(%rip) # imm = 0x4100501C
jmp 0xdb25
movl $0x41004000, 0x748db(%rip) # imm = 0x41004000
movl $0x41004004, 0x748d5(%rip) # imm = 0x41004004
movl $0x41004008, 0x748cf(%rip) # imm = 0x41004008
movl $0x41004018, 0x748c9(%rip) # imm = 0x41004018
movl $0x4100401c, 0x748c3(%rip) # imm = 0x4100401C
movl 0x74865(%rip), %esi     # 0x82390
leaq 0x7486e(%rip), %rdi     # 0x823a0
movl $0x100, %edx            # imm = 0x100
callq 0xa5f0
jmp 0xdb5d
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xda2c
movl -0xc(%rbp), %esi
leaq 0x856b(%rip), %rdi      # 0x160c1
movb $0x0, %al
callq 0x8970
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)