// File: /workspace/github_cpp_makefile_star2_O1/chrischute[P]are-you-the-one/src/ayto.cpp
// Function: getNextGuessUsingMinimax[abi:cxx11](PerfectMatchingSet const*, PerfectMatchingSet const*)
Pm getNextGuessUsingMinimax(const PmSet* possibleAnswers, const PmSet* guessesAlreadyMade)
{
    // Spin up threads to run minimax in parallel.
    map<Pm, int>* bestGuessFromEachThread = new map<Pm, int>();
    mutex* writeLock = new mutex();
    vector<thread> minimaxThreads;
    vector<ArgsForMinimaxThread*> argsForMinimaxThreads;
    PmSet** chunksToEvaluate;

    if (possibleAnswers->size() > START_PART_MM) {
        // Select the best guess from a fixed pool of possibilities.
        PmSet* fixedPoolToEvaluate = new PmSet();
        fixedPoolToEvaluate->populateFromFile(POOL_FILENAME);
        chunksToEvaluate = fixedPoolToEvaluate->copyIntoChunks(NUM_THREADS);
        delete fixedPoolToEvaluate;
    } else if (possibleAnswers->size() > START_FULL_MM) {
        // Select the best guess from the possible remaining solutions.
        chunksToEvaluate = possibleAnswers->copyIntoChunks(NUM_THREADS);
    } else {
        // Select the best guess from all possible permutations.
        cout << "    Full minimax... May take up to one minute." << endl;
        PmSet* allPossible = new PmSet();
        allPossible->populateAll();
        chunksToEvaluate = allPossible->copyIntoChunks(NUM_THREADS);
        delete allPossible;
    }

    for (int id = 0; id < NUM_THREADS; ++id) {
        ArgsForMinimaxThread* args = new ArgsForMinimaxThread(
                id,
                possibleAnswers,
                chunksToEvaluate[id],
                guessesAlreadyMade,
                bestGuessFromEachThread,
                writeLock);
        argsForMinimaxThreads.push_back(args);
        minimaxThreads.push_back(thread(getBestGuessFromSubset, args));
    }

    // Wait for all threads to finish up.
    for (vector<thread>::iterator it = minimaxThreads.begin();
         it != minimaxThreads.end();
         ++it) {
        it->join();
    }

    // Find the best guess in the set bestGuessFromEachThread.
    Pm bestGuess("");
    long numRemainingAfterBestGuess = possibleAnswers->size();
    for (map<Pm, int>::const_iterator it = bestGuessFromEachThread->begin();
         it != bestGuessFromEachThread->end();
         ++it) {
        Pm guess = it->first;
        int numRemainingAfterGuess = it->second;

        if (numRemainingAfterGuess < numRemainingAfterBestGuess) {
            bestGuess = guess;
            numRemainingAfterBestGuess = numRemainingAfterGuess;
        }
    }

    // Clean up allocated args, chunks, map, and mutex.
    for (vector<ArgsForMinimaxThread*>::iterator it = argsForMinimaxThreads.begin();
         it != argsForMinimaxThreads.end();
         ++it) {
        delete *it;
    }
    for (int i = 0; i < NUM_THREADS; ++i) {
        if (chunksToEvaluate && chunksToEvaluate[i]) {
            delete chunksToEvaluate[i];
        }
    }
    if (chunksToEvaluate) {
        delete chunksToEvaluate;
    }
    delete bestGuessFromEachThread;
    delete writeLock;

    return bestGuess;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movl $0x30, %edi
callq 0x2240
leaq 0x8(%rax), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x48(%rsp)
movq %rcx, 0x20(%rax)
xorl %ebx, %ebx
movq %rax, 0x40(%rsp)
movq %rbx, 0x28(%rax)
movl $0x28, %edi
callq 0x2240
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movq %rax, 0x50(%rsp)
movq %rbx, 0x20(%rax)
movq %rbx, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq %rbx, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movq %r14, %rdi
callq 0x64f0
cmpq $0x2711, %rax           # imm = 0x2711
jb 0x4255
movl $0x8, %edi
callq 0x2240
movq %rax, %rbx
movq %rax, %rdi
callq 0x616c
leaq 0xa8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2ff8(%rip), %rsi      # 0x71fa
leaq 0x3001(%rip), %rdx      # 0x720a
leaq 0x98(%rsp), %rdi
callq 0x5320
leaq 0x98(%rsp), %rsi
movq %rbx, %rdi
callq 0x641e
movq 0x98(%rsp), %rdi
cmpq %r15, %rdi
je 0x4243
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x2250
movq %rbx, %rdi
movl $0xa, %esi
callq 0x6500
jmp 0x42ee
movq 0x18(%rsp), %rdi
callq 0x64f0
cmpq $0x13b, %rax            # imm = 0x13B
jb 0x427e
movq 0x18(%rsp), %rdi
movl $0xa, %esi
callq 0x6500
movq %rax, %rbp
jmp 0x4306
movq 0x6d4b(%rip), %rbx      # 0xafd0
leaq 0x2f7f(%rip), %rsi      # 0x720b
movl $0x2e, %edx
movq %rbx, %rdi
callq 0x2280
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2230
movsbl %al, %esi
movq 0x6d19(%rip), %rdi      # 0xafd0
callq 0x2040
movq %rax, %rdi
callq 0x2170
movl $0x8, %edi
callq 0x2240
movq %rax, %rbx
movq %rax, %rdi
callq 0x616c
movq %rbx, %rdi
callq 0x638e
movq %rbx, %rdi
movl $0xa, %esi
callq 0x6500
movq %rax, %rbp
movq %rbx, %rdi
callq 0x61e6
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2250
xorl %r13d, %r13d
leaq 0x78(%rsp), %rbx
leaq 0x481(%rip), %r15       # 0x4796
leaq 0x58(%rsp), %r14
leaq 0x60(%rsp), %r12
movl $0x30, %edi
callq 0x2240
movq (%rbp,%r13,8), %rcx
movl %r13d, (%rax)
movq 0x18(%rsp), %rdx
movq %rdx, 0x8(%rax)
movq %rcx, 0x10(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq 0x40(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x50(%rsp), %rcx
movq %rcx, 0x28(%rax)
movq %rax, 0x78(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x4375
movq %rax, (%rsi)
addq $0x8, 0x28(%rsp)
jmp 0x4382
leaq 0x20(%rsp), %rdi
movq %rbx, %rdx
callq 0x5c64
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4f60
movq %r12, %rdi
movq %r14, %rsi
callq 0x5dc2
cmpq $0x0, 0x58(%rsp)
jne 0x45c0
incq %r13
cmpq $0xa, %r13
jne 0x431f
movq 0x60(%rsp), %rbx
cmpq 0x68(%rsp), %rbx
je 0x43ce
movq %rbx, %rdi
callq 0x2350
addq $0x8, %rbx
jmp 0x43b9
movq 0x8(%rsp), %rdi
leaq 0x10(%rdi), %rax
movq %rax, 0x10(%rsp)
movq %rax, (%rdi)
leaq 0x2e58(%rip), %rdx      # 0x723e
movq %rdx, %rsi
callq 0x5320
movq 0x18(%rsp), %rdi
callq 0x64f0
movq %rax, %r12
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rbx
cmpq 0x48(%rsp), %rbx
je 0x4475
leaq 0x88(%rsp), %r13
leaq 0x78(%rsp), %r15
movq %r13, 0x78(%rsp)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x53b0
movslq 0x40(%rbx), %r14
cmpq %r14, %r12
jle 0x4449
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x2120
movq %r14, %r12
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x4463
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2250
movq %rbx, %rdi
callq 0x2290
movq %rax, %rbx
cmpq 0x48(%rsp), %rax
jne 0x4418
movq 0x20(%rsp), %rbx
cmpq 0x28(%rsp), %rbx
je 0x4499
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x4493
movl $0x30, %esi
callq 0x2250
addq $0x8, %rbx
jmp 0x447a
xorl %r14d, %r14d
testq %rbp, %rbp
je 0x44c0
movq (%rbp,%r14,8), %rbx
testq %rbx, %rbx
je 0x44c0
movq %rbx, %rdi
callq 0x61e6
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2250
incq %r14
cmpq $0xa, %r14
jne 0x449c
testq %rbp, %rbp
je 0x44db
movl $0x8, %esi
movq %rbp, %rdi
callq 0x2250
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x52a8
movl $0x30, %esi
movq %rbx, %rdi
callq 0x2250
movl $0x28, %esi
movq 0x50(%rsp), %rdi
callq 0x2250
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x451b
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
leaq 0x60(%rsp), %rdi
callq 0x4ff2
movq 0x8(%rsp), %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x455a
movq %rax, %r14
movq 0x98(%rsp), %rdi
cmpq %r15, %rdi
je 0x45c8
movq 0xa8(%rsp), %rsi
jmp 0x45aa
jmp 0x45c5
movq %rax, %r14
movl $0x8, %esi
movq %rbx, %rdi
jmp 0x45ad
jmp 0x45c5
jmp 0x4590
jmp 0x45c5
jmp 0x45c5
jmp 0x45c5
movq %rax, %r14
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x4593
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2250
jmp 0x4593
movq %rax, %r14
movq 0x8(%rsp), %rax
movq (%rax), %rdi
cmpq 0x10(%rsp), %rdi
je 0x45c8
movq 0x10(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x2250
jmp 0x45c8
jmp 0x45c5
jmp 0x45c5
cmpq $0x0, 0x58(%rsp)
je 0x45c5
callq 0x20a0
movq %rax, %r14
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x45df
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2250
leaq 0x60(%rsp), %rdi
callq 0x4ff2
movq %r14, %rdi
callq 0x2370