// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_lattice_lcmxo2.c
// Function: target_verify
static void target_verify(void)
{
  uint8_t *file_data;
  int file_size, row_count;
  jed_file_t jed;
  uint16_t feabits;
  uint64_t feature;

  file_data = buf_alloc(MAX_FILE_SIZE);
  file_size = load_file(target_options.name, file_data, MAX_FILE_SIZE);

  jed.config = buf_alloc(MAX_CONFIG_SIZE);
  jed.config_size = MAX_CONFIG_SIZE;

  parse_jed_file(&jed, file_data, file_size);

  row_count = jed.config_size / FLASH_ROW_SIZE;

  erase_sram();

  // Enable flash
  dap_jtag_write_ir(CMD_ISC_ENABLE, IR_LENGTH);
  dap_jtag_write_dr((uint8_t[]){ ISC_ENABLE_FLASH }, 8);
  dap_jtag_idle(8);

  // Verify configuration data
  dap_jtag_write_ir(CMD_LSC_INIT_ADDRESS, IR_LENGTH);
  dap_jtag_idle(8);

  dap_jtag_write_ir(CMD_LSC_READ_INCR_NV, IR_LENGTH);
  dap_jtag_idle(8);

  for (int row = 0; row < row_count; row++)
  {
    uint8_t tmp[16];
    dap_jtag_read_dr(tmp, FLASH_ROW_SIZE);
    dap_jtag_idle(8);

    if (memcmp(tmp, &jed.config[row * 16], 16))
      error_exit("configuration verification failed");
  }

  dap_jtag_write_ir(CMD_LSC_INIT_ADDRESS, IR_LENGTH);
  dap_jtag_idle(8);

  // Verify Feature Row
  dap_jtag_write_ir(CMD_LSC_READ_FEATURE, IR_LENGTH);
  dap_jtag_read_dr((uint8_t *)&feature, 64);
  dap_jtag_idle(8);

  check(feature == jed.feature, "Feature Row verification failed");

  // Verify FEABITS
  dap_jtag_write_ir(CMD_LSC_READ_FEABITS, IR_LENGTH);
  dap_jtag_idle(8);
  dap_jtag_read_dr((uint8_t *)&feabits, 16);

  check(feabits == jed.feabits, "FEABITS verification failed");

  buf_free(file_data);
  buf_free(jed.config);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl $0x1000000, %edi        # imm = 0x1000000
callq 0x8ae0
movq %rax, -0x8(%rbp)
movq 0x71aeb(%rip), %rdi     # 0x82568
movq -0x8(%rbp), %rsi
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x8b50
movl %eax, -0xc(%rbp)
movl $0x200000, %edi         # imm = 0x200000
callq 0x8ae0
movq %rax, -0x28(%rbp)
movl $0x200000, -0x20(%rbp)  # imm = 0x200000
movq -0x8(%rbp), %rsi
movl -0xc(%rbp), %edx
leaq -0x28(%rbp), %rdi
callq 0x10e40
movl -0x20(%rbp), %eax
movl $0x80, %ecx
cltd
idivl %ecx
movl %eax, -0x10(%rbp)
callq 0x10dc0
movl $0xc6, %edi
movl $0x8, %esi
callq 0x8260
movb $0x8, -0x39(%rbp)
leaq -0x39(%rbp), %rdi
movl $0x8, %esi
callq 0x82f0
movl $0x8, %edi
callq 0x81e0
movl $0x46, %edi
movl $0x8, %esi
callq 0x8260
movl $0x8, %edi
callq 0x81e0
movl $0x73, %edi
movl $0x8, %esi
callq 0x8260
movl $0x8, %edi
callq 0x81e0
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x10b97
leaq -0x50(%rbp), %rdi
movl $0x80, %esi
callq 0x83a0
movl $0x8, %edi
callq 0x81e0
leaq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
movl -0x40(%rbp), %edx
shll $0x4, %edx
movslq %edx, %rdx
addq %rdx, %rcx
movups (%rcx), %xmm1
movups (%rax), %xmm0
pcmpeqb %xmm1, %xmm0
pmovmskb %xmm0, %eax
subl $0xffff, %eax           # imm = 0xFFFF
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x10b8a
leaq 0x6efb(%rip), %rdi      # 0x17a7e
movb $0x0, %al
callq 0x8970
jmp 0x10b8c
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x10b2a
movl $0x46, %edi
movl $0x8, %esi
callq 0x8260
movl $0x8, %edi
callq 0x81e0
movl $0xe7, %edi
movl $0x8, %esi
callq 0x8260
leaq -0x38(%rbp), %rdi
movl $0x40, %esi
callq 0x83a0
movl $0x8, %edi
callq 0x81e0
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
sete %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x6eb1(%rip), %rsi      # 0x17aa0
xorl %eax, %eax
callq 0x8880
movl $0xfb, %edi
movl $0x8, %esi
callq 0x8260
movl $0x8, %edi
callq 0x81e0
leaq -0x2a(%rbp), %rdi
movl $0x10, %esi
callq 0x83a0
movzwl -0x2a(%rbp), %eax
movzwl -0x1c(%rbp), %ecx
cmpl %ecx, %eax
sete %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x6e89(%rip), %rsi      # 0x17ac0
xorl %eax, %eax
callq 0x8880
movq -0x8(%rbp), %rdi
callq 0x8b30
movq -0x28(%rbp), %rdi
callq 0x8b30
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)