// File: chrischute[P]are-you-the-one/src/ayto.cpp
// Function: getNextTruthBoothGuess(PerfectMatchingSet const*, TruthBoothSet const*)
Tb getNextTruthBoothGuess(const PmSet* possibleAnswers, const TbSet* guessesAlreadyMade)
{
    Tb nextGuess(-1, '_');
    map<Tb, int> numOccurrencesOfMatch;

    if (guessesAlreadyMade->size() == 0 ||
            possibleAnswers->size() == 1) {
        // Nothing to gain from running minimax here.
        return Tb(0, '0');
    }

    // Count the number of occurrences in possible answers of each match.
    for (PmSet::const_iterator iterPerm = possibleAnswers->begin();
         iterPerm != possibleAnswers->end();
         ++iterPerm) {
	int i = 0;
        for (Pm::const_iterator iterChar = iterPerm->begin();
            iterChar != iterPerm->end();
            ++iterChar) {
            ++numOccurrencesOfMatch[Tb(i++, *iterChar)];
        }
    }

    // Ideally a pairing occurs in 1/2 of all possible answers.
    // The worst-case response is always the complement of the
    // best-case response. We want to optimize the worst case, which
    // occurs when both yes/no cut the remaining solutions in half.
    long optimalNumOccurrences = possibleAnswers->size() / 2;
    long closestNumOccurrences = possibleAnswers->size() + 1;

    for (map<Tb, int>::const_iterator it = numOccurrencesOfMatch.begin();
        it != numOccurrencesOfMatch.end();
        ++it) {

        if (!guessesAlreadyMade->contains(it->first) &&
                (optimalNumOccurrences - it->second) <
                abs(optimalNumOccurrences - closestNumOccurrences)) {
            nextGuess = it->first;
            closestNumOccurrences = it->second;
        }
    }

    return nextGuess;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x20(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x5f, %edx
callq 0x7910
leaq -0x50(%rbp), %rdi
callq 0x7940
movq -0x18(%rbp), %rdi
callq 0x120b0
movq %rax, -0xd0(%rbp)
jmp 0x60e1
movq -0xd0(%rbp), %rax
cmpq $0x0, %rax
je 0x610d
movq -0x10(%rbp), %rdi
callq 0x107a0
movq %rax, -0xd8(%rbp)
jmp 0x6100
movq -0xd8(%rbp), %rax
cmpq $0x1, %rax
jne 0x6145
leaq -0x8(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x7910
jmp 0x611f
movl $0x1, -0x60(%rbp)
jmp 0x6406
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x7c00
jmp 0x641c
movq -0x10(%rbp), %rdi
callq 0x10280
movq %rax, -0xe0(%rbp)
jmp 0x6157
movq -0xe0(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x6f70
movq -0x10(%rbp), %rdi
callq 0x10320
movq %rax, -0xe8(%rbp)
jmp 0x6181
movq -0xe8(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x6fb0
testb $0x1, %al
jne 0x61a2
jmp 0x626b
movl $0x0, -0x7c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x7960
movq %rax, %rdi
callq 0x32b0
movq %rax, -0x88(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x7960
movq %rax, %rdi
callq 0x3500
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x7980
testb $0x1, %al
jne 0x61f2
jmp 0x625b
movl -0x7c(%rbp), %eax
movl %eax, -0xec(%rbp)
incl %eax
movl %eax, -0x7c(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x7aa0
movl -0xec(%rbp), %esi
movsbl (%rax), %edx
leaq -0x98(%rbp), %rdi
callq 0x7910
jmp 0x6223
leaq -0x50(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x79c0
movq %rax, -0xf8(%rbp)
jmp 0x623c
movq -0xf8(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
leaq -0x88(%rbp), %rdi
callq 0x7ac0
jmp 0x61c1
jmp 0x625d
leaq -0x68(%rbp), %rdi
callq 0x7010
jmp 0x616f
movq -0x10(%rbp), %rdi
callq 0x107a0
movq %rax, -0x100(%rbp)
jmp 0x627d
movq -0x100(%rbp), %rax
shrq %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x107a0
movq %rax, -0x108(%rbp)
jmp 0x62a0
movq -0x108(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x7ae0
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x7b10
leaq -0x50(%rbp), %rdi
callq 0x7b60
movq %rax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x7b10
leaq -0xb0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x7b30
testb $0x1, %al
jne 0x6314
jmp 0x63f7
movq -0x18(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x7b90
movq -0x118(%rbp), %rdi
movq %rax, %rsi
callq 0x12000
movb %al, -0x109(%rbp)
jmp 0x6342
movb -0x109(%rbp), %al
testb $0x1, %al
jne 0x63e4
jmp 0x6352
movq -0xa0(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x7b90
movq %rax, %rcx
movq -0x130(%rbp), %rax
movslq 0x8(%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x128(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rax
subq %rax, %rdi
callq 0x7bb0
movq %rax, -0x120(%rbp)
jmp 0x63a3
movq -0x128(%rbp), %rax
movq -0x120(%rbp), %rcx
cmpq %rcx, %rax
jge 0x63e4
leaq -0xb0(%rbp), %rdi
callq 0x7b90
movl (%rax), %ecx
movl %ecx, -0x20(%rbp)
movb 0x4(%rax), %al
movb %al, -0x1c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x7b90
movslq 0x8(%rax), %rax
movq %rax, -0xa8(%rbp)
jmp 0x63e6
leaq -0xb0(%rbp), %rdi
callq 0x7bd0
jmp 0x62d5
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x60(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x7c00
movq -0x8(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x34d0
nopw %cs:(%rax,%rax)