// File: jorbs[P]stellar-keypair-tool/keypair.cpp
// Function: main
int main(int argc, char* argv[]) {
  if (argc == 1) {
    usage(argv[0]);
    return 1;
  }

  std::string term;
  int threadsCount = -1, c;
  bool (*matchFunction)(const std::string &input, const std::string &suffix);

  while ((c = getopt(argc, argv, "p:m:s:j:")) != -1) {
    switch (c) {
      case 'p':
        if (optarg[0] != 'g' && optarg[0] != 'G') {
          std::cout << "Prepending 'G' to the search term.\n";
          term = "G";
        }

        term += optarg;
        matchFunction = &hasPrefix;
        break;
      case 's':
        term = optarg;
        matchFunction = &hasSuffix;
        break;
      case 'm':
        term = optarg;
        matchFunction = &hasSubstr;
        break;
      case 'j':
        threadsCount = atoi(optarg);
        break;
      case '?':
      default:
        usage(argv[0]);
        return 0;
    }
  }

  if (threadsCount < 1) {
    threadsCount = getProcessingUnits();
  }

  std::transform(term.begin(), term.end(), term.begin(), ::toupper);

  char invalidChar = checkInvalidChar(term);

  if (invalidChar != -1) {
    std::cerr << "\"" << invalidChar << "\" is not allowed. The search term must be scoped to:\n\t"
              << base32Dictionary << std::endl;
    return 0;
  }

  if (sodium_init() == -1) {
    std::cerr << "Unable to init libsodium.\n";
    return 1;
  }

  std::cout << "Searching...\n";

  std::vector<std::thread> threads(threadsCount);
  auto start = std::chrono::system_clock::now();

  for (int i = 0; i < threadsCount; i++)
    threads[i] = std::thread(process, term, matchFunction);

  std::for_each(threads.begin(), threads.end(), [](std::thread &t) { t.join(); });

  auto end = std::chrono::system_clock::now();
  std::chrono::duration<double> elapsed_seconds = end - start;

  std::cout << count << " tries in "
            << elapsed_seconds.count() << "s using "
            << threadsCount << " threads.\n";

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x1, -0x8(%rbp)
jne 0x3c27
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3840
movl $0x1, -0x4(%rbp)
jmp 0x41a5
leaq -0x30(%rbp), %rdi
callq 0x32f0
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x5476(%rip), %rdx      # 0x90bb
callq 0x3050
movl %eax, -0x38(%rbp)
cmpl $-0x1, %eax
je 0x3dad
movl -0x38(%rbp), %eax
movl %eax, -0xe4(%rbp)
subl $0x3f, %eax
je 0x3d85
jmp 0x3c6a
movl -0xe4(%rbp), %eax
subl $0x6a, %eax
je 0x3d71
jmp 0x3c7b
movl -0xe4(%rbp), %eax
subl $0x6d, %eax
je 0x3d4f
jmp 0x3c8c
movl -0xe4(%rbp), %eax
subl $0x70, %eax
je 0x3cad
jmp 0x3c99
movl -0xe4(%rbp), %eax
subl $0x73, %eax
je 0x3d2d
jmp 0x3d87
movq 0xa2f4(%rip), %rax      # 0xdfa8
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x67, %eax
je 0x3d0b
movq 0xa2e2(%rip), %rax      # 0xdfa8
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x47, %eax
je 0x3d0b
movq 0xa2f0(%rip), %rdi      # 0xdfc8
leaq 0x53e5(%rip), %rsi      # 0x90c4
callq 0x3200
jmp 0x3ce6
leaq 0x53fb(%rip), %rsi      # 0x90e8
leaq -0x30(%rbp), %rdi
callq 0x32d0
jmp 0x3cf8
jmp 0x3d0b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x419a
movq 0xa296(%rip), %rax      # 0xdfa8
movq (%rax), %rsi
leaq -0x30(%rbp), %rdi
callq 0x31f0
jmp 0x3d20
leaq 0xbe9(%rip), %rax       # 0x4910
movq %rax, -0x40(%rbp)
jmp 0x3da8
movq 0xa274(%rip), %rax      # 0xdfa8
movq (%rax), %rsi
leaq -0x30(%rbp), %rdi
callq 0x32d0
jmp 0x3d42
leaq 0xc27(%rip), %rax       # 0x4970
movq %rax, -0x40(%rbp)
jmp 0x3da8
movq 0xa252(%rip), %rax      # 0xdfa8
movq (%rax), %rsi
leaq -0x30(%rbp), %rdi
callq 0x32d0
jmp 0x3d64
leaq 0xc75(%rip), %rax       # 0x49e0
movq %rax, -0x40(%rbp)
jmp 0x3da8
movq 0xa230(%rip), %rax      # 0xdfa8
movq (%rax), %rdi
callq 0x3330
movl %eax, -0x34(%rbp)
jmp 0x3da8
jmp 0x3d87
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x3840
jmp 0x3d95
movl $0x0, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
jmp 0x418f
jmp 0x3c37
cmpl $0x1, -0x34(%rbp)
jge 0x3dc9
callq 0x3660
movl %eax, -0xe8(%rbp)
jmp 0x3dc0
movl -0xe8(%rbp), %eax
movl %eax, -0x34(%rbp)
leaq -0x30(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x31a0
movq -0xf8(%rbp), %rdi
movq %rax, -0x58(%rbp)
callq 0x30f0
movq -0xf8(%rbp), %rdi
movq %rax, -0x60(%rbp)
callq 0x31a0
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq 0xa1c8(%rip), %rcx      # 0xdfd8
callq 0x4a10
movq %rax, -0xf0(%rbp)
jmp 0x3e1e
movq -0xf0(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x35d0
movb %al, -0x71(%rbp)
movsbl -0x71(%rbp), %eax
cmpl $-0x1, %eax
je 0x3ed7
movq 0xa1a7(%rip), %rdi      # 0xdff0
leaq 0x529a(%rip), %rsi      # 0x90ea
callq 0x3200
movq %rax, -0x100(%rbp)
jmp 0x3e5e
movq -0x100(%rbp), %rdi
movsbl -0x71(%rbp), %esi
callq 0x3290
movq %rax, -0x108(%rbp)
jmp 0x3e77
movq -0x108(%rbp), %rdi
leaq 0x5267(%rip), %rsi      # 0x90ec
callq 0x3200
movq %rax, -0x110(%rbp)
jmp 0x3e93
movq -0x110(%rbp), %rdi
leaq 0xa38f(%rip), %rsi      # 0xe230
callq 0x31c0
movq %rax, -0x118(%rbp)
jmp 0x3eaf
movq -0x118(%rbp), %rdi
movq 0xa0e3(%rip), %rsi      # 0xdfa0
callq 0x3240
jmp 0x3ec4
movl $0x0, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
jmp 0x418f
callq 0x33b0
movl %eax, -0x11c(%rbp)
jmp 0x3ee4
movl -0x11c(%rbp), %eax
cmpl $-0x1, %eax
jne 0x3f17
movq 0xa0fa(%rip), %rdi      # 0xdff0
leaq 0x5226(%rip), %rsi      # 0x9123
callq 0x3200
jmp 0x3f04
movl $0x1, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
jmp 0x418f
movq 0xa0aa(%rip), %rdi      # 0xdfc8
leaq 0x5219(%rip), %rsi      # 0x913e
callq 0x3200
jmp 0x3f2c
movslq -0x34(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x91(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x4a90
movq -0x130(%rbp), %rsi
movq -0x128(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x4ab0
jmp 0x3f66
leaq -0x91(%rbp), %rdi
callq 0x4b20
callq 0x3030
movq %rax, -0xa0(%rbp)
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x402d
leaq -0x71e(%rip), %rsi      # 0x3880
leaq -0xb0(%rbp), %rdi
leaq -0x30(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0x4b40
jmp 0x3fb4
movslq -0xa4(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x4c10
movq %rax, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x4c30
leaq -0xb0(%rbp), %rdi
callq 0x4c70
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x3f88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x91(%rbp), %rdi
callq 0x4b20
jmp 0x419a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x4e30
jmp 0x419a
leaq -0x90(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x4ca0
movq -0x138(%rbp), %rdi
movq %rax, -0xb8(%rbp)
callq 0x4cd0
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x41c0
jmp 0x406f
callq 0x3030
movq %rax, -0xd0(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x4d00
movq %rax, -0x140(%rbp)
jmp 0x4097
movq -0x140(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x4d50
jmp 0x40ba
leaq 0xa193(%rip), %rdi      # 0xe254
callq 0x4d90
movl %eax, %esi
movq 0x9ef9(%rip), %rdi      # 0xdfc8
callq 0x3380
movq %rax, -0x148(%rbp)
jmp 0x40dd
movq -0x148(%rbp), %rdi
leaq 0x5061(%rip), %rsi      # 0x914c
callq 0x3200
movq %rax, -0x150(%rbp)
jmp 0x40f9
leaq -0xd8(%rbp), %rdi
callq 0x4e10
movsd %xmm0, -0x158(%rbp)
jmp 0x410f
movsd -0x158(%rbp), %xmm0
movq -0x150(%rbp), %rdi
callq 0x33e0
movq %rax, -0x160(%rbp)
jmp 0x412c
movq -0x160(%rbp), %rdi
leaq 0x501d(%rip), %rsi      # 0x9157
callq 0x3200
movq %rax, -0x168(%rbp)
jmp 0x4148
movq -0x168(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x3380
movq %rax, -0x170(%rbp)
jmp 0x4160
movq -0x170(%rbp), %rdi
leaq 0x4ff2(%rip), %rsi      # 0x9160
callq 0x3200
jmp 0x4175
movl $0x0, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x4e30
leaq -0x30(%rbp), %rdi
callq 0x3418
jmp 0x41a5
leaq -0x30(%rbp), %rdi
callq 0x3418
jmp 0x41b1
movl -0x4(%rbp), %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3390
nopw (%rax,%rax)