// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/edbg.c
// Function: main
int main(int argc, char **argv)
{
  debugger_t debuggers[MAX_DEBUGGERS] = {0};
  int n_debuggers = 0;
  int debugger = -1;
  target_ops_t *target_ops;
  bool active_actions;

  parse_command_line(argc, argv);

  active_actions = g_target_options.unlock || g_target_options.erase ||
      g_target_options.program || g_target_options.verify || g_target_options.lock ||
      g_target_options.read || g_target_options.fuse_cmd;

  if (!(active_actions || g_list || g_target || (g_target_options.reset == 0)))
    error_exit("no actions specified");

  if (g_target_options.read && (g_target_options.erase || g_target_options.program ||
      g_target_options.verify || g_target_options.lock))
    error_exit("mutually exclusive actions specified");

  n_debuggers = dbg_enumerate(debuggers, MAX_DEBUGGERS);

  if (g_list)
  {
    message("Attached debuggers:\n");

    for (int i = 0; i < n_debuggers; i++)
    {
      char ver[8] = "";

      if (debuggers[i].versions & DBG_CMSIS_DAP_V1)
        strcat(ver, "1");

      if (debuggers[i].versions & DBG_CMSIS_DAP_V2)
        strcat(ver, "2");

      message("  %d: %s - %s %s (%s)\n", i, debuggers[i].serial, debuggers[i].manufacturer, debuggers[i].product, ver);
    }

    return 0;
  }

  if (NULL == g_target)
    error_exit("no target type specified (use '-t' option)");

  if (0 == strcmp(g_target, "list"))
  {
    target_list();
    return 0;
  }

  target_ops = target_get_ops(g_target);

  if (g_serial)
  {
    char *end = NULL;
    int index = strtoul(g_serial, &end, 10);

    if (index < n_debuggers && end[0] == 0)
    {
      debugger = index;
    }
    else
    {
      for (int i = 0; i < n_debuggers; i++)
      {
        if (0 == strcmp(debuggers[i].serial, g_serial))
        {
          debugger = i;
          break;
        }
      }
    }

    if (-1 == debugger)
      error_exit("unable to find a debugger with a specified serial number");
  }

  if (0 == n_debuggers)
    error_exit("no debuggers found");
  else if (1 == n_debuggers)
    debugger = 0;
  else if (n_debuggers > 1 && -1 == debugger)
    error_exit("more than one debugger found, please specify a serial number");

  if (-1 == g_version)
    g_version = (debuggers[debugger].versions & DBG_CMSIS_DAP_V2) ? DBG_CMSIS_DAP_V2 : DBG_CMSIS_DAP_V1;
  else if (1 == g_version)
    g_version = DBG_CMSIS_DAP_V1;
  else if (2 == g_version)
    g_version = DBG_CMSIS_DAP_V2;
  else
    error_exit("unsupported CMSIS-DAP version: %d", g_version);

  if (0 == (g_version & debuggers[debugger].versions))
    error_exit("selected debugger does not support this CMSIS-DAP version");

  dbg_open(&debuggers[debugger], g_version);

  g_debugger_open = true;

  print_debugger_info(&debuggers[debugger]);
  verbose("Using CMSIS-DAP v%d\n", (DBG_CMSIS_DAP_V1 == g_version) ? 1 : 2);
  print_clock_freq(g_clock);

  reconnect_debugger();

  if (g_target_options.reset > 0)
  {
    verbose("Resetting...");
    dap_reset_pin(0);
    sleep_ms(g_target_options.reset);
    dap_reset_pin(1);
    sleep_ms(10);
    verbose(" done.\n");
  }

  if (!active_actions)
  {
    disconnect_debugger();
    return 0;
  }

  target_ops->select(&g_target_options);

  if (g_target_options.unlock)
  {
    verbose("Unlocking...");
    target_ops->unlock();
    verbose(" done.\n");
  }

  if (g_target_options.erase)
  {
    verbose("Erasing...");
    target_ops->erase();
    verbose(" done.\n");
  }

  if (g_target_options.program)
  {
    verbose("Programming...");
    target_ops->program();
    verbose(" done.\n");
  }

  if (g_target_options.verify)
  {
    verbose("Verification...");
    target_ops->verify();
    verbose(" done.\n");
  }

  if (g_target_options.lock)
  {
    verbose("Locking...");
    target_ops->lock();
    verbose(" done.\n");
  }

  if (g_target_options.read)
  {
    verbose("Reading...");
    target_ops->read();
    verbose(" done.\n");
  }

  if (g_target_options.fuse_cmd)
  {
    verbose("Fuses:\n");
    target_fuse_commands(target_ops, g_target_options.fuse_cmd);
    verbose("done.\n");
  }

  target_ops->deselect();

  dap_reset_target_hw(1);

  disconnect_debugger();

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x870, %rsp            # imm = 0x870
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x830(%rbp), %rdi
xorl %esi, %esi
movl $0x820, %edx            # imm = 0x820
callq 0x60f0
movl $0x0, -0x834(%rbp)
movl $0xffffffff, -0x838(%rbp) # imm = 0xFFFFFFFF
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x94e0
movb $0x1, %al
testb $0x1, 0x133b3(%rip)     # 0x1c198
movb %al, -0x861(%rbp)
jne 0x8e53
movb $0x1, %al
testb $0x1, 0x1339e(%rip)     # 0x1c194
movb %al, -0x861(%rbp)
jne 0x8e53
movb $0x1, %al
testb $0x1, 0x1338e(%rip)     # 0x1c195
movb %al, -0x861(%rbp)
jne 0x8e53
movb $0x1, %al
testb $0x1, 0x1337e(%rip)     # 0x1c196
movb %al, -0x861(%rbp)
jne 0x8e53
movb $0x1, %al
testb $0x1, 0x1336e(%rip)     # 0x1c197
movb %al, -0x861(%rbp)
jne 0x8e53
movb $0x1, %al
testb $0x1, 0x1335f(%rip)     # 0x1c199
movb %al, -0x861(%rbp)
jne 0x8e53
cmpq $0x0, 0x13366(%rip)     # 0x1c1b0
setne %al
movb %al, -0x861(%rbp)
movb -0x861(%rbp), %al
andb $0x1, %al
movb %al, -0x841(%rbp)
testb $0x1, -0x841(%rbp)
jne 0x8e94
testb $0x1, 0x79318(%rip)     # 0x82189
jne 0x8e94
cmpq $0x0, 0x79315(%rip)     # 0x82190
jne 0x8e94
cmpl $0x0, 0x1330c(%rip)     # 0x1c190
je 0x8e94
leaq 0xb7f2(%rip), %rdi      # 0x1467f
movb $0x0, %al
callq 0x8970
testb $0x1, 0x132fe(%rip)     # 0x1c199
je 0x8ecf
testb $0x1, 0x132f0(%rip)     # 0x1c194
jne 0x8ec1
testb $0x1, 0x132e8(%rip)     # 0x1c195
jne 0x8ec1
testb $0x1, 0x132e0(%rip)     # 0x1c196
jne 0x8ec1
testb $0x1, 0x132d8(%rip)     # 0x1c197
je 0x8ecf
leaq 0xb7cc(%rip), %rdi      # 0x14694
movb $0x0, %al
callq 0x8970
leaq -0x830(%rbp), %rdi
movl $0x14, %esi
callq 0x12930
movl %eax, -0x834(%rbp)
testb $0x1, 0x7929c(%rip)     # 0x82189
je 0x901c
leaq 0xb7bf(%rip), %rdi      # 0x146b9
movb $0x0, %al
callq 0x86f0
movl $0x0, -0x848(%rbp)
movl -0x848(%rbp), %eax
cmpl -0x834(%rbp), %eax
jge 0x9010
leaq -0x850(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x60f0
movslq -0x848(%rbp), %rcx
leaq -0x830(%rbp), %rax
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x8f63
leaq -0x850(%rbp), %rdi
leaq 0xd211(%rip), %rsi      # 0x1616f
callq 0x6260
movslq -0x848(%rbp), %rcx
leaq -0x830(%rbp), %rax
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movl 0x30(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x8f96
leaq -0x850(%rbp), %rdi
leaq 0xd6a9(%rip), %rsi      # 0x1663a
callq 0x6260
movl -0x848(%rbp), %esi
movslq -0x848(%rbp), %rcx
leaq -0x830(%rbp), %rax
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdx
movslq -0x848(%rbp), %rcx
leaq -0x830(%rbp), %rax
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rcx
movslq -0x848(%rbp), %rdi
leaq -0x830(%rbp), %rax
imulq $0x68, %rdi, %rdi
addq %rdi, %rax
movq 0x20(%rax), %r8
leaq -0x850(%rbp), %r9
leaq 0xb6d9(%rip), %rdi      # 0x146ce
movb $0x0, %al
callq 0x86f0
movl -0x848(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x848(%rbp)
jmp 0x8f0b
movl $0x0, -0x4(%rbp)
jmp 0x94d1
xorl %eax, %eax
cmpq 0x7916b(%rip), %rax     # 0x82190
jne 0x9035
leaq 0xb6b7(%rip), %rdi      # 0x146e5
movb $0x0, %al
callq 0x8970
movq 0x79154(%rip), %rdi     # 0x82190
leaq 0xb6cd(%rip), %rsi      # 0x14710
callq 0x6180
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x9061
callq 0xa2b0
movl $0x0, -0x4(%rbp)
jmp 0x94d1
movq 0x79128(%rip), %rdi     # 0x82190
callq 0xa490
movq %rax, -0x840(%rbp)
cmpq $0x0, 0x7911c(%rip)     # 0x82198
je 0x915b
movq $0x0, -0x858(%rbp)
movq 0x79104(%rip), %rdi     # 0x82198
leaq -0x858(%rbp), %rsi
movl $0xa, %edx
callq 0x6250
movl %eax, -0x85c(%rbp)
movl -0x85c(%rbp), %eax
cmpl -0x834(%rbp), %eax
jge 0x90d6
movq -0x858(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x90d6
movl -0x85c(%rbp), %eax
movl %eax, -0x838(%rbp)
jmp 0x913e
movl $0x0, -0x860(%rbp)
movl -0x860(%rbp), %eax
cmpl -0x834(%rbp), %eax
jge 0x913c
movslq -0x860(%rbp), %rcx
leaq -0x830(%rbp), %rax
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq 0x7908a(%rip), %rsi     # 0x82198
callq 0x6180
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x9129
movl -0x860(%rbp), %eax
movl %eax, -0x838(%rbp)
jmp 0x913c
jmp 0x912b
movl -0x860(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x860(%rbp)
jmp 0x90e0
jmp 0x913e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl -0x838(%rbp), %eax
jne 0x9159
leaq 0xb5c3(%rip), %rdi      # 0x14715
movb $0x0, %al
callq 0x8970
jmp 0x915b
xorl %eax, %eax
cmpl -0x834(%rbp), %eax
jne 0x9175
leaq 0xb5e2(%rip), %rdi      # 0x1474e
movb $0x0, %al
callq 0x8970
jmp 0x91b6
movl $0x1, %eax
cmpl -0x834(%rbp), %eax
jne 0x918e
movl $0x0, -0x838(%rbp)
jmp 0x91b4
cmpl $0x1, -0x834(%rbp)
jle 0x91b2
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl -0x838(%rbp), %eax
jne 0x91b2
leaq 0xb5b6(%rip), %rdi      # 0x14761
movb $0x0, %al
callq 0x8970
jmp 0x91b4
jmp 0x91b6
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x13007(%rip), %eax     # 0x1c1c8
jne 0x91f6
movslq -0x838(%rbp), %rcx
leaq -0x830(%rbp), %rax
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movl 0x30(%rax), %edx
andl $0x4, %edx
movl $0x2, %eax
movl $0x4, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0x12fd4(%rip)     # 0x1c1c8
jmp 0x9240
movl $0x1, %eax
cmpl 0x12fc7(%rip), %eax     # 0x1c1c8
jne 0x920f
movl $0x2, 0x12fbb(%rip)     # 0x1c1c8
jmp 0x923e
movl $0x2, %eax
cmpl 0x12fae(%rip), %eax     # 0x1c1c8
jne 0x9228
movl $0x4, 0x12fa2(%rip)     # 0x1c1c8
jmp 0x923c
movl 0x12f9a(%rip), %esi     # 0x1c1c8
leaq 0xb569(%rip), %rdi      # 0x1479e
movb $0x0, %al
callq 0x8970
jmp 0x923e
jmp 0x9240
movl 0x12f82(%rip), %ecx     # 0x1c1c8
movslq -0x838(%rbp), %rdx
leaq -0x830(%rbp), %rax
imulq $0x68, %rdx, %rdx
addq %rdx, %rax
andl 0x30(%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x9272
leaq 0xb555(%rip), %rdi      # 0x147c0
movb $0x0, %al
callq 0x8970
movslq -0x838(%rbp), %rax
leaq -0x830(%rbp), %rdi
imulq $0x68, %rax, %rax
addq %rax, %rdi
movl 0x12f3b(%rip), %esi     # 0x1c1c8
callq 0x12e90
movb $0x1, 0x78f07(%rip)     # 0x821a0
movslq -0x838(%rbp), %rax
leaq -0x830(%rbp), %rdi
imulq $0x68, %rax, %rax
addq %rax, %rdi
callq 0x9720
movl 0x12f0f(%rip), %ecx     # 0x1c1c8
movl $0x2, %esi
movl $0x1, %eax
cmpl %ecx, %esi
cmovel %eax, %esi
leaq 0xb52b(%rip), %rdi      # 0x147fa
movb $0x0, %al
callq 0x8630
movq 0x12ef3(%rip), %rax     # 0x1c1d0
movl %eax, %edi
callq 0x9a50
callq 0x9b00
cmpl $0x0, 0x12ea0(%rip)     # 0x1c190
jle 0x9334
leaq 0xb516(%rip), %rdi      # 0x1480f
movb $0x0, %al
callq 0x8630
xorl %edi, %edi
callq 0x6f90
movl 0x12e83(%rip), %edi     # 0x1c190
callq 0x8a90
movl $0x1, %edi
callq 0x6f90
movl $0xa, %edi
callq 0x8a90
leaq 0xb4ef(%rip), %rdi      # 0x1481c
movb $0x0, %al
callq 0x8630
testb $0x1, -0x841(%rbp)
jne 0x934e
callq 0x9b50
movl $0x0, -0x4(%rbp)
jmp 0x94d1
movq -0x840(%rbp), %rax
leaq 0x12e34(%rip), %rdi     # 0x1c190
callq *(%rax)
testb $0x1, 0x12e33(%rip)     # 0x1c198
je 0x938d
leaq 0xb4b6(%rip), %rdi      # 0x14824
movb $0x0, %al
callq 0x8630
movq -0x840(%rbp), %rax
callq *0x20(%rax)
leaq 0xb496(%rip), %rdi      # 0x1481c
movb $0x0, %al
callq 0x8630
testb $0x1, 0x12e00(%rip)     # 0x1c194
je 0x93bc
leaq 0xb494(%rip), %rdi      # 0x14831
movb $0x0, %al
callq 0x8630
movq -0x840(%rbp), %rax
callq *0x10(%rax)
leaq 0xb467(%rip), %rdi      # 0x1481c
movb $0x0, %al
callq 0x8630
testb $0x1, 0x12dd2(%rip)     # 0x1c195
je 0x93eb
leaq 0xb470(%rip), %rdi      # 0x1483c
movb $0x0, %al
callq 0x8630
movq -0x840(%rbp), %rax
callq *0x28(%rax)
leaq 0xb438(%rip), %rdi      # 0x1481c
movb $0x0, %al
callq 0x8630
testb $0x1, 0x12da4(%rip)     # 0x1c196
je 0x941a
leaq 0xb450(%rip), %rdi      # 0x1484b
movb $0x0, %al
callq 0x8630
movq -0x840(%rbp), %rax
callq *0x30(%rax)
leaq 0xb409(%rip), %rdi      # 0x1481c
movb $0x0, %al
callq 0x8630
testb $0x1, 0x12d76(%rip)     # 0x1c197
je 0x9449
leaq 0xb431(%rip), %rdi      # 0x1485b
movb $0x0, %al
callq 0x8630
movq -0x840(%rbp), %rax
callq *0x18(%rax)
leaq 0xb3da(%rip), %rdi      # 0x1481c
movb $0x0, %al
callq 0x8630
testb $0x1, 0x12d49(%rip)     # 0x1c199
je 0x9478
leaq 0xb40d(%rip), %rdi      # 0x14866
movb $0x0, %al
callq 0x8630
movq -0x840(%rbp), %rax
callq *0x38(%rax)
leaq 0xb3ab(%rip), %rdi      # 0x1481c
movb $0x0, %al
callq 0x8630
cmpq $0x0, 0x12d30(%rip)     # 0x1c1b0
je 0x94b1
leaq 0xb3e8(%rip), %rdi      # 0x14871
movb $0x0, %al
callq 0x8630
movq -0x840(%rbp), %rdi
movq 0x12d12(%rip), %rsi     # 0x1c1b0
callq 0xa7e0
leaq 0xb373(%rip), %rdi      # 0x1481d
movb $0x0, %al
callq 0x8630
movq -0x840(%rbp), %rax
callq *0x8(%rax)
movl $0x1, %edi
callq 0x6ef0
callq 0x9b50
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x870, %rsp            # imm = 0x870
popq %rbp
retq
nopl (%rax)