// File: /workspace/github_cpp_makefile_star2_O2/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, 0x60(%rsp)
movq %rsi, %rbx
movq %rdi, (%rsp)
pushq $0x30
popq %rdi
callq 0x2250
leaq 0x8(%rax), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x68(%rsp)
movq %rcx, 0x20(%rax)
movq %rax, 0x10(%rsp)
andq $0x0, 0x28(%rax)
pushq $0x28
popq %rdi
callq 0x2250
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movq %rax, 0x18(%rsp)
andq $0x0, 0x20(%rax)
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
callq 0x5b24
cmpq $0x2711, %rax           # imm = 0x2711
jb 0x3772
pushq $0x8
popq %rdi
callq 0x2250
movq %rax, %rbx
movq %rax, %rdi
callq 0x5834
leaq 0x3ab5(%rip), %rsi      # 0x71eb
leaq 0x98(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x4034
leaq 0x98(%rsp), %rsi
movq %rbx, %rdi
callq 0x5a76
leaq 0x98(%rsp), %rdi
callq 0x20e0
pushq $0xa
popq %rsi
movq %rbx, %rdi
callq 0x5b34
jmp 0x37d7
movq 0x8(%rsp), %rdi
callq 0x5b24
cmpq $0x13b, %rax            # imm = 0x13B
jb 0x3796
pushq $0xa
popq %rsi
movq 0x8(%rsp), %rdi
callq 0x5b34
movq %rax, %rbp
jmp 0x37ed
movq 0x8833(%rip), %rdi      # 0xbfd0
leaq 0x3a58(%rip), %rsi      # 0x71fc
callq 0x2240
movq %rax, %rdi
callq 0x2100
pushq $0x8
popq %rdi
callq 0x2250
movq %rax, %rbx
movq %rax, %rdi
callq 0x5834
movq %rbx, %rdi
callq 0x5a0a
pushq $0xa
popq %rsi
movq %rbx, %rdi
callq 0x5b34
movq %rax, %rbp
movq %rbx, %rdi
callq 0x58a4
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x2260
xorl %r15d, %r15d
leaq 0x78(%rsp), %r14
leaq 0x3b6(%rip), %r12       # 0x3bb2
leaq 0x70(%rsp), %r13
leaq 0x40(%rsp), %rbx
cmpq $0xa, %r15
je 0x387c
pushq $0x30
popq %rdi
callq 0x2250
movq (%rbp,%r15,8), %rcx
movl %r15d, (%rax)
movq 0x8(%rsp), %rdx
movq %rdx, 0x8(%rax)
movq %rcx, 0x10(%rax)
movq 0x60(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x18(%rsp), %rcx
movq %rcx, 0x28(%rax)
movq %rax, 0x78(%rsp)
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x430a
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x4328
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5286
movq %r13, %rdi
callq 0x43a2
incq %r15
jmp 0x3806
movq 0x40(%rsp), %rbx
cmpq 0x48(%rsp), %rbx
je 0x3896
movq %rbx, %rdi
callq 0x2360
addq $0x8, %rbx
jmp 0x3881
leaq 0x3992(%rip), %rsi      # 0x722f
leaq 0x78(%rsp), %rdx
movq (%rsp), %rdi
callq 0x4034
movq 0x8(%rsp), %rdi
callq 0x5b24
movq %rax, %r15
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r14
leaq 0x78(%rsp), %rbx
cmpq 0x68(%rsp), %r14
je 0x3906
leaq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0x22c0
movslq 0x40(%r14), %r12
cmpq %r12, %r15
jle 0x38f1
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x2120
movq %r12, %r15
movq %rbx, %rdi
callq 0x20e0
movq %r14, %rdi
callq 0x2280
movq %rax, %r14
jmp 0x38c6
movq 0x20(%rsp), %r14
pushq $0x30
popq %rbx
cmpq 0x28(%rsp), %r14
je 0x3926
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x2260
addq $0x8, %r14
jmp 0x390e
xorl %r15d, %r15d
pushq $0x8
popq %rbx
cmpq $0xa, %r15
je 0x3959
testq %rbp, %rbp
je 0x3954
movq (%rbp,%r15,8), %r14
testq %r14, %r14
je 0x3954
movq %r14, %rdi
callq 0x58a4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2260
incq %r15
jmp 0x392c
testq %rbp, %rbp
je 0x3969
pushq $0x8
popq %rsi
movq %rbp, %rdi
callq 0x2260
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x4648
pushq $0x30
popq %rsi
movq %rbx, %rdi
callq 0x2260
pushq $0x28
popq %rsi
movq 0x18(%rsp), %rdi
callq 0x2260
leaq 0x20(%rsp), %rdi
callq 0x50ee
leaq 0x40(%rsp), %rdi
callq 0x43b0
movq (%rsp), %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x39c9
movq %rax, %r14
leaq 0x98(%rsp), %rdi
jmp 0x39f9
jmp 0x3a13
movq %rax, %r14
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x2260
jmp 0x3a16
jmp 0x3a13
jmp 0x39f2
jmp 0x3a13
jmp 0x3a13
jmp 0x3a13
movq %rax, %r14
leaq 0x78(%rsp), %rdi
callq 0x20e0
jmp 0x39f5
movq %rax, %r14
movq (%rsp), %rdi
callq 0x20e0
jmp 0x3a16
jmp 0x3a13
jmp 0x3a13
movq %rax, %r14
leaq 0x70(%rsp), %rdi
callq 0x43a2
jmp 0x3a16
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0x50ee
leaq 0x40(%rsp), %rdi
callq 0x43b0
movq %r14, %rdi
callq 0x2380