// File: /workspace/github_cpp_makefile_star2_O2/chuckwolber[P]Cube/order/cli.cpp
// Function: main
int main(int argc, char *argv[]) {
    int ch;
    char* algorithmStart = nullptr;
    unsigned long long int algmathAddVal = 0;
    char* algmathLtVal = nullptr;
    bool algmathAdd = false;
    bool algmathLt = false;
    bool algBench = false;
    bool algBenchLite = false;
    algorithmCountMax = DEFAULT_ALG_MAX;
    keepDuplicates = false;
    skipFoundOrders = false;
    showFoundOrder = false;
    skip_nth = 1;
    heartbeat = 0;
    numThreads = std::thread::hardware_concurrency();

    opterr = 0;
    while((ch = getopt_long(argc, argv, "a:gep:l:c:b:ks:t:f:o:ih", longopts, NULL)) != -1) {
        switch(ch) {
            case 'a':
                algorithmStart = optarg;
                break;
            case 'g':
                algBench = true;
                break;
            case 'e':
                algBench = true;
                algBenchLite = true;
                break;
            case 'p':
                algmathAddVal = (unsigned long long int)(std::strtoll(optarg, nullptr, 10));
                algmathAdd = true;
                break;
            case 'l':
                algmathLtVal = optarg;
                algmathLt = true;
                break;
            case 'c':
                algorithmCountMax = (unsigned long long int)(std::strtoll(optarg, nullptr, 10));
                break;
            case 'b':
                heartbeat = (unsigned long long int)(std::strtoll(optarg, nullptr, 10));
                break;
            case 'k':
                keepDuplicates = true;
                break;
            case 's':
                skip_nth = (unsigned long long int)std::strtoll(optarg, nullptr, 10);
                ++skip_nth;
                break;
            case 't':
                numThreads = (unsigned int)std::stoul(optarg, nullptr, 10);
                break;
            case 'i':
                setFindAllOrders();
                break;
            case 'f':
                setFindOrders(optarg);
                break;
            case 'o':
                showFoundOrder = true;
                try {
                    foundOrder = (unsigned int)std::stoul(optarg, nullptr, 10);
                } catch (std::invalid_argument) {
                    usage(argv[0]);
                    return 0;
                }
                break;
            case 'h':
            default:
                usage(argv[0]);
                return 0;
                break;
        }
    }

    if (skip_nth == 0)
        skip_nth = 1;
    if (numThreads < 1)
        numThreads = 1;
    std::vector<std::thread> threads(numThreads);

    initialAlgorithm.reset();
    if (algorithmStart != nullptr)
        initialAlgorithm.setAlgorithm(algorithmStart);

    if (algmathAdd) {
        Algorithm endAlgorithm(initialAlgorithm);
        endAlgorithm += algmathAddVal;
        std::cout << endAlgorithm.getAlgorithmStr() << std::endl;
    } else if (algmathLt) {
        Algorithm ltAlgorithm(algmathLtVal);
        if (initialAlgorithm < ltAlgorithm)
            std::cout << "true" << std::endl;
        else
            std::cout << "false" << std::endl;
    } else if (algBench) {
        std::cerr << "Threads: " << numThreads << std::endl;
        doAlgBench(algBenchLite);
    } else {
        std::cerr << "Algorithm Count: " << algorithmCountMax << std::endl;
        std::cerr << "Algorithm Start: " << initialAlgorithm.getAlgorithmStr() << std::endl;

        if (skipFoundOrders)
            std::cerr << "Finding Orders: " << findOrders << std::endl;
        std::cerr << "Threads: " << numThreads << std::endl;
   
        for (unsigned int i=0; i<numThreads; i++)
            threads.at(i) = std::thread(calculateOrder, i);
        for (std::thread &t : threads)
            t.join();

        if (heartbeat > 0)
            std::cout << "HB:-1" << std::endl;
    }
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movl %edi, %ebp
movabsq $0x183870714, %rax      # imm = 0x183870714
movq %rax, 0xb7fd(%rip)      # 0x12640
xorl %eax, %eax
movb %al, 0xb815(%rip)       # 0x12660
movb %al, 0xb810(%rip)       # 0x12661
movq $0x0, 0x48(%rsp)
movb %al, 0xb802(%rip)       # 0x12662
movq $0x1, 0xb7cd(%rip) # 0x12638
andq $0x0, 0xb7dd(%rip)      # 0x12650
callq 0x43a0
movl %eax, 0xb7da(%rip)      # 0x12658
movq 0xb13b(%rip), %rax      # 0x11fc0
andl $0x0, (%rax)
leaq 0x4461(%rip), %r13      # 0xb2f0
leaq 0xb52a(%rip), %r15      # 0x123c0
leaq 0x43bb(%rip), %r12      # 0xb258
movq $0x0, 0x38(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x40(%rsp)
xorl %eax, %eax
movq $0x0, 0x30(%rsp)
movq $0x0, 0x58(%rsp)
movl %eax, %ebx
movl %ebp, %edi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x4450
leal -0x61(%rax), %ecx
cmpl $0x13, %ecx
ja 0x706d
movb $0x1, %al
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
movq 0xb079(%rip), %rax      # 0x11f78
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movl %ebx, %eax
jmp 0x6ecc
movq 0xb066(%rip), %rax      # 0x11f78
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movb $0x1, %al
movq %rax, 0x40(%rsp)
movl %ebx, %eax
jmp 0x6ecc
movq 0xb04c(%rip), %rax      # 0x11f78
movq (%rax), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x4060
movq %rax, 0x48(%rsp)
movb $0x1, %al
movq %rax, 0x50(%rsp)
movl %ebx, %eax
jmp 0x6ecc
movb $0x1, 0xb710(%rip)      # 0x12660
movl %ebx, %eax
jmp 0x6ecc
movq 0xb01a(%rip), %rax      # 0x11f78
movq (%rax), %rdi
callq 0x756a
movl %ebx, %eax
jmp 0x6ecc
callq 0x745b
movl %ebx, %eax
jmp 0x6ecc
movq 0xaff8(%rip), %rax      # 0x11f78
movq (%rax), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x4060
movq %rax, 0xb6ac(%rip)      # 0x12640
movl %ebx, %eax
jmp 0x6ecc
movb $0x1, %al
movb $0x1, %cl
movq %rcx, 0x30(%rsp)
jmp 0x6ecc
movb $0x1, 0xb6b2(%rip)      # 0x12662
movq 0xafc1(%rip), %rax      # 0x11f78
movq (%rax), %rsi
leaq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x60(%rsp), %rdx
callq 0x58d2
movq %r13, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x8515
movl %eax, 0xb67d(%rip)      # 0x1265c
jmp 0x7057
movq 0xaf90(%rip), %rax      # 0x11f78
movq (%rax), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x4060
movq %rax, 0xb654(%rip)      # 0x12650
movl %ebx, %eax
jmp 0x6ecc
movq 0xaf6e(%rip), %rax      # 0x11f78
movq (%rax), %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x4060
incq %rax
movq %rax, 0xb617(%rip)      # 0x12638
movl %ebx, %eax
jmp 0x6ecc
movq 0xaf49(%rip), %rax      # 0x11f78
movq (%rax), %rsi
leaq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x60(%rsp), %rdx
callq 0x58d2
movq %r13, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x8515
movl %eax, 0xb601(%rip)      # 0x12658
movq %r13, %rdi
callq 0x4508
movl %ebx, %eax
leaq 0x4288(%rip), %r13      # 0xb2f0
jmp 0x6ecc
cmpl $-0x1, %eax
jne 0x7136
cmpq $0x0, 0xb5ba(%rip)      # 0x12638
jne 0x708b
movq $0x1, 0xb5ad(%rip) # 0x12638
movl 0xb5c7(%rip), %eax      # 0x12658
testl %eax, %eax
movq 0x58(%rsp), %r14
jne 0x70a7
movl $0x1, 0xb5b4(%rip)      # 0x12658
pushq $0x1
popq %rax
movl %eax, %esi
leaq 0x80(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x854c
leaq 0xb5a6(%rip), %rdi      # 0x12668
callq 0x4c98
testq %r14, %r14
je 0x70db
leaq 0xb595(%rip), %rdi      # 0x12668
movq %r14, %rsi
callq 0x4902
testb $0x1, 0x50(%rsp)
je 0x7140
leaq 0xb57f(%rip), %rsi      # 0x12668
leaq 0x8(%rsp), %rdi
callq 0x4884
leaq 0x8(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x4b00
leaq 0x60(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4cde
movq 0xae98(%rip), %rdi      # 0x11fb0
leaq 0x60(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
callq 0x4120
leaq 0x60(%rsp), %rdi
callq 0x4508
jmp 0x718f
movq (%r14), %rdi
callq 0x77ba
jmp 0x71a6
testb $0x1, 0x40(%rsp)
je 0x71ba
leaq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x48cc
leaq 0xb50b(%rip), %rdi      # 0x12668
leaq 0x8(%rsp), %rsi
callq 0x4b1a
leaq 0x419a(%rip), %rcx      # 0xb308
leaq 0x4198(%rip), %rsi      # 0xb30d
testb %al, %al
cmovneq %rcx, %rsi
movq 0xae2e(%rip), %rdi      # 0x11fb0
callq 0x42c0
movq %rax, %rdi
callq 0x4120
leaq 0x8(%rsp), %rdi
callq 0x5918
leaq 0x80(%rsp), %rdi
callq 0x8660
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x1, %bl
je 0x71f7
movq 0xae2a(%rip), %rdi      # 0x11ff0
leaq 0x4146(%rip), %rsi      # 0xb313
callq 0x42c0
movl 0xb480(%rip), %esi      # 0x12658
movq %rax, %rdi
callq 0x41d0
movq %rax, %rdi
callq 0x4120
movzbl 0x30(%rsp), %edi
andl $0x1, %edi
callq 0x7ada
jmp 0x7199
movq 0xadf2(%rip), %rdi      # 0x11ff0
leaq 0x4118(%rip), %rsi      # 0xb31d
callq 0x42c0
movq 0xb42f(%rip), %rsi      # 0x12640
movq %rax, %rdi
callq 0x41c0
movq %rax, %rdi
callq 0x4120
movq 0xadc8(%rip), %rdi      # 0x11ff0
leaq 0x4100(%rip), %rsi      # 0xb32f
callq 0x42c0
movq %rax, %rbx
leaq 0xb42a(%rip), %rsi      # 0x12668
leaq 0x8(%rsp), %rdi
callq 0x4cde
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4120
leaq 0x8(%rsp), %rdi
callq 0x4508
cmpb $0x1, 0xb3f3(%rip)      # 0x12661
jne 0x729a
movq 0xad79(%rip), %rdi      # 0x11ff0
leaq 0x40c3(%rip), %rsi      # 0xb341
callq 0x42c0
leaq 0xb366(%rip), %rsi      # 0x125f0
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4120
movq 0xad4f(%rip), %rdi      # 0x11ff0
leaq 0x406b(%rip), %rsi      # 0xb313
callq 0x42c0
movl 0xb3a5(%rip), %esi      # 0x12658
movq %rax, %rdi
callq 0x41d0
movq %rax, %rdi
callq 0x4120
xorl %eax, %eax
leaq 0xca4(%rip), %rbx       # 0x7f70
leaq 0x8(%rsp), %r14
leaq 0x60(%rsp), %r15
leaq 0x80(%rsp), %r12
movl %eax, 0x60(%rsp)
cmpl 0xb370(%rip), %eax      # 0x12658
jae 0x731f
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x859e
movl 0x60(%rsp), %esi
movq %r12, %rdi
callq 0x8616
movq %rax, %rdi
movq %r14, %rsi
callq 0x8638
movq %r14, %rdi
callq 0x8652
movl 0x60(%rsp), %eax
incl %eax
jmp 0x72de
movq 0x80(%rsp), %rbx
movq 0x88(%rsp), %r14
cmpq %r14, %rbx
je 0x7342
movq %rbx, %rdi
callq 0x4510
addq $0x8, %rbx
jmp 0x732f
cmpq $0x0, 0xb306(%rip)      # 0x12650
je 0x7199
movq 0xac59(%rip), %rdi      # 0x11fb0
leaq 0x3ff4(%rip), %rsi      # 0xb352
callq 0x42c0
movq %rax, %rdi
callq 0x4120
jmp 0x7199
jmp 0x73bc
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4508
jmp 0x73bf
jmp 0x73bc
jmp 0x7389
jmp 0x7389
jmp 0x73bc
movq %rax, %rbx
jmp 0x739b
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x4508
leaq 0x8(%rsp), %rdi
callq 0x5918
jmp 0x73bf
jmp 0x73ce
jmp 0x73bc
jmp 0x73bc
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x8652
jmp 0x73bf
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x8660
jmp 0x73e8
movq %rax, %rbx
jmp 0x73e8
movq %rdx, %r15
movq %rax, %rbx
jmp 0x7400
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4508
movq %rbx, %rdi
callq 0x4490
movq %rdx, %r15
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4508
cmpl $0x1, %r15d
jne 0x73e8
movq %rbx, %rdi
callq 0x4350
leaq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x8532
movq %rbx, %rdi
callq 0x40e0
movq (%r14), %rdi
callq 0x77ba
leaq 0x8(%rsp), %rdi
callq 0x4460
callq 0x4440
jmp 0x71a6
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4460
callq 0x4440
jmp 0x73e8
movq %rax, %rdi
callq 0x590d