// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
cmpl $0x1, %edi
jne 0x2a0b
movq (%r14), %rdi
callq 0x267a
movl $0x1, %ebx
jmp 0x2c41
movl %edi, %ebp
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
leaq 0x15e0(%rip), %r15      # 0x4010
movq 0x3589(%rip), %r12      # 0x5fc0
movl %ebp, %edi
movq %r14, %rsi
leaq 0x1690(%rip), %rdx      # 0x40d3
callq 0x2050
leal -0x6a(%rax), %ecx
cmpl $0x9, %ecx
ja 0x2b3e
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movq (%r12), %rbx
movq 0x10(%rsp), %r13
movq %rbx, %rdi
callq 0x2080
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x2200
leaq 0x488(%rip), %rax       # 0x2f12
movq %rax, 0x28(%rsp)
jmp 0x2a37
movq (%r12), %rbx
movq 0x10(%rsp), %r13
movq %rbx, %rdi
callq 0x2080
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x2200
leaq 0x3dd(%rip), %rax       # 0x2e9b
movq %rax, 0x28(%rsp)
jmp 0x2a37
movq (%r12), %rax
movzbl (%rax), %eax
orl $0x20, %eax
cmpl $0x67, %eax
je 0x2b0d
movl $0x23, %edx
movq 0x34ed(%rip), %rdi      # 0x5fd0
leaq 0x15f2(%rip), %rsi      # 0x40dc
callq 0x21b0
movq 0x10(%rsp), %rdx
movl $0x1, %r8d
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x15f8(%rip), %rcx      # 0x4100
callq 0x2200
movq (%r12), %rsi
leaq 0x8(%rsp), %rdi
callq 0x22d0
leaq 0x30c(%rip), %rax       # 0x2e2e
movq %rax, 0x28(%rsp)
jmp 0x2a37
movq (%r12), %rdi
callq 0x2210
movl %eax, 0x4(%rsp)
jmp 0x2a37
cmpl $-0x1, %eax
jne 0x2c1b
movl 0x4(%rsp), %ebp
testl %ebp, %ebp
jg 0x2b56
callq 0x250d
movl %eax, %ebp
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x2b7e
movq 0x8(%rsp), %r14
xorl %r15d, %r15d
movsbl (%r14,%r15), %edi
callq 0x22c0
movb %al, (%r14,%r15)
incq %r15
cmpq %r15, %rbx
jne 0x2b68
leaq 0x8(%rsp), %rdi
callq 0x24ac
cmpb $-0x1, %al
je 0x2c52
movl %eax, %ebx
movq 0x3457(%rip), %rdi      # 0x5ff0
leaq 0x1562(%rip), %rsi      # 0x4102
movl $0x1, %edx
callq 0x21b0
leaq 0x38(%rsp), %rsi
movb %bl, (%rsi)
movq 0x3438(%rip), %rdi      # 0x5ff0
movl $0x1, %edx
callq 0x21b0
movq %rax, %rbx
leaq 0x1538(%rip), %rsi      # 0x4104
movl $0x36, %edx
movq %rax, %rdi
callq 0x21b0
movq 0x35b8(%rip), %rsi      # 0x6198
movq 0x35b9(%rip), %rdx      # 0x61a0
movq %rbx, %rdi
callq 0x21b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20e0
jmp 0x2c23
movq (%r14), %rdi
callq 0x267a
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2c41
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x22a0
cmpl $-0x1, %eax
je 0x2d9a
movq 0x3369(%rip), %rdi      # 0x5fd0
leaq 0x14e8(%rip), %rsi      # 0x4156
movl $0xd, %edx
callq 0x21b0
movl %ebp, %esi
leaq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x2f2e
callq 0x2040
movq %rax, 0x50(%rsp)
cmpl $0x1, %ebp
movl %ebp, 0x4(%rsp)
adcl $0x0, %ebp
xorl %ebx, %ebx
leaq -0x5b2(%rip), %r14      # 0x26f4
leaq 0x30(%rsp), %r15
leaq 0x8(%rsp), %r12
leaq 0x28(%rsp), %r13
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x2fa6
movq 0x38(%rsp), %rax
cmpq $0x0, (%rax,%rbx,8)
jne 0x2dbc
movq 0x30(%rsp), %rcx
movq %rcx, (%rax,%rbx,8)
incq %rbx
cmpq %rbx, %rbp
jne 0x2cb5
movq 0x38(%rsp), %r14
movq 0x40(%rsp), %rbx
cmpq %rbx, %r14
movl 0x4(%rsp), %ebp
je 0x2d0b
movq %r14, %rdi
callq 0x2260
addq $0x8, %r14
cmpq %rbx, %r14
jne 0x2cfa
callq 0x2040
movq %rax, %r14
movl 0x34a3(%rip), %esi      # 0x61bc
movq 0x32b0(%rip), %rdi      # 0x5fd0
callq 0x2270
movq %rax, %r15
leaq 0x1435(%rip), %rsi      # 0x4164
movl $0xa, %edx
movq %rax, %rdi
callq 0x21b0
subq 0x50(%rsp), %r14
cvtsi2sd %r14, %xmm0
divsd 0x12ba(%rip), %xmm0     # 0x4008
movq %r15, %rdi
callq 0x2230
movq %rax, %rbx
leaq 0x140f(%rip), %rsi      # 0x416f
movl $0x8, %edx
movq %rax, %rdi
callq 0x21b0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2270
leaq 0x13fa(%rip), %rsi      # 0x4178
movl $0xa, %edx
movq %rax, %rdi
callq 0x21b0
leaq 0x38(%rsp), %rdi
callq 0x3054
jmp 0x2c23
movl $0x1, %ebx
movq 0x324a(%rip), %rdi      # 0x5ff0
leaq 0x138e(%rip), %rsi      # 0x413b
movl $0x1a, %edx
callq 0x21b0
jmp 0x2c25
callq 0x2060
jmp 0x2dda
jmp 0x2dcb
jmp 0x2dda
jmp 0x2dcb
jmp 0x2dda
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3054
jmp 0x2ddd
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2df9
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x2290
nop