// File: /workspace/github_cpp_makefile_star2_O3/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 0x2230
leaq 0x8(%rax), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%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 0x2230
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 0x63ea
cmpq $0x2711, %rax           # imm = 0x2711
jb 0x419e
movl $0x8, %edi
callq 0x2230
movq %rax, %rbx
movq %rax, %rdi
callq 0x6070
leaq 0xa8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x30a0(%rip), %rsi      # 0x71eb
leaq 0x30a9(%rip), %rdx      # 0x71fb
leaq 0x98(%rsp), %rdi
callq 0x5256
leaq 0x98(%rsp), %rsi
movq %rbx, %rdi
callq 0x6318
movq 0x98(%rsp), %rdi
cmpq %r15, %rdi
je 0x418c
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x2240
movq %rbx, %rdi
movl $0xa, %esi
callq 0x63fa
jmp 0x4237
movq 0x18(%rsp), %rdi
callq 0x63ea
cmpq $0x13b, %rax            # imm = 0x13B
jb 0x41c7
movq 0x18(%rsp), %rdi
movl $0xa, %esi
callq 0x63fa
movq %rax, %rbp
jmp 0x424f
movq 0x6e02(%rip), %rbx      # 0xafd0
leaq 0x3027(%rip), %rsi      # 0x71fc
movl $0x2e, %edx
movq %rbx, %rdi
callq 0x2270
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2220
movsbl %al, %esi
movq 0x6dd0(%rip), %rdi      # 0xafd0
callq 0x2040
movq %rax, %rdi
callq 0x2160
movl $0x8, %edi
callq 0x2230
movq %rax, %rbx
movq %rax, %rdi
callq 0x6070
movq %rbx, %rdi
callq 0x6288
movq %rbx, %rdi
movl $0xa, %esi
callq 0x63fa
movq %rax, %rbp
movq %rbx, %rdi
callq 0x60ea
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2240
xorl %r13d, %r13d
leaq 0x78(%rsp), %rbx
leaq 0x489(%rip), %r15       # 0x46e7
leaq 0x58(%rsp), %r14
leaq 0x60(%rsp), %r12
movl $0x30, %edi
callq 0x2230
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 0x42be
movq %rax, (%rsi)
addq $0x8, 0x28(%rsp)
jmp 0x42cb
leaq 0x20(%rsp), %rdi
movq %rbx, %rdx
callq 0x5b6a
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4ec0
movq %r12, %rdi
movq %r14, %rsi
callq 0x5cc8
cmpq $0x0, 0x58(%rsp)
jne 0x4511
incq %r13
cmpq $0xa, %r13
jne 0x4268
movq 0x60(%rsp), %rbx
cmpq 0x68(%rsp), %rbx
je 0x4317
movq %rbx, %rdi
callq 0x2340
addq $0x8, %rbx
jmp 0x4302
movq 0x8(%rsp), %rdi
leaq 0x10(%rdi), %rax
movq %rax, 0x10(%rsp)
movq %rax, (%rdi)
leaq 0x2f00(%rip), %rdx      # 0x722f
movq %rdx, %rsi
callq 0x5256
movq 0x18(%rsp), %rdi
callq 0x63ea
movq %rax, %r12
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rbx
cmpq 0x48(%rsp), %rbx
je 0x43be
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 0x52ec
movslq 0x40(%rbx), %r14
cmpq %r14, %r12
jle 0x4392
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x2110
movq %r14, %r12
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x43ac
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2240
movq %rbx, %rdi
callq 0x2280
movq %rax, %rbx
cmpq 0x48(%rsp), %rax
jne 0x4361
movq 0x20(%rsp), %rbx
cmpq 0x28(%rsp), %rbx
je 0x43dd
movq (%rbx), %rdi
movl $0x30, %esi
callq 0x2240
addq $0x8, %rbx
jmp 0x43c3
xorl %r14d, %r14d
testq %rbp, %rbp
je 0x440f
movq (%rbp,%r14,8), %rbx
testq %rbx, %rbx
je 0x4404
movq %rbx, %rdi
callq 0x60ea
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2240
incq %r14
cmpq $0xa, %r14
jne 0x43e0
jmp 0x441a
incq %r14
cmpq $0xa, %r14
jne 0x43e0
jmp 0x442c
testq %rbp, %rbp
je 0x442c
movl $0x8, %esi
movq %rbp, %rdi
callq 0x2240
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x51de
movl $0x30, %esi
movq %rbx, %rdi
callq 0x2240
movl $0x28, %esi
movq 0x50(%rsp), %rdi
callq 0x2240
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x446c
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2240
leaq 0x60(%rsp), %rdi
callq 0x4f40
movq 0x8(%rsp), %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x44ab
movq %rax, %r14
movq 0x98(%rsp), %rdi
cmpq %r15, %rdi
je 0x4519
movq 0xa8(%rsp), %rsi
jmp 0x44fb
jmp 0x4516
movq %rax, %r14
movl $0x8, %esi
movq %rbx, %rdi
jmp 0x44fe
jmp 0x4516
jmp 0x44e1
jmp 0x4516
jmp 0x4516
jmp 0x4516
movq %rax, %r14
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x44e4
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2240
jmp 0x44e4
movq %rax, %r14
movq 0x8(%rsp), %rax
movq (%rax), %rdi
cmpq 0x10(%rsp), %rdi
je 0x4519
movq 0x10(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x2240
jmp 0x4519
jmp 0x4516
jmp 0x4516
cmpq $0x0, 0x58(%rsp)
je 0x4516
callq 0x20a0
movq %rax, %r14
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4530
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2240
leaq 0x60(%rsp), %rdi
callq 0x4f40
movq %r14, %rdi
callq 0x2360