// File: WShabti-Team[P]WShabtiPict/wshabtilib/api/parameter.cpp
// Function: pictcore::Parameter::PickValue()
int Parameter::PickValue()
{
    assert(!m_bound);

    int maxComplete    = 0;
    int maxTotal       = 0;
    int bestValue      = 0;
    int bestValueCount = 0;

    m_bound = true; // a white lie to facilitate Combination::Feasible()

    for (int value = 0; value < m_valueCount; ++value)
    {
        int totalZeros = 0;
        int complete   = 0;
        // try binding that value and see how feasible it is
        m_currentValue = value;

        for (ComboCollection::iterator iter = m_combinations.begin(); iter != m_combinations.end(); ++iter)
        {
            int zeros = (*iter)->Feasible();
            totalZeros += zeros;
            if ((*iter)->GetBoundCount() >= (*iter)->GetParameterCount() - 1)
            {
                if (zeros)
                    ++complete;

                if ((*iter)->ViolatesExclusion())
                {
                    // make sure we don't pick this value, and move on
                    totalZeros = -1;
                    complete   = -1;
                    break;
                }
            }
        }
        if (complete > maxComplete)
        {
            maxComplete    = complete;
            bestValue      = value;
            maxTotal       = totalZeros;
            bestValueCount = 1;
        }
        else if (complete == maxComplete)
        {
            if (totalZeros > maxTotal)
            {
                bestValue      = value;
                maxTotal       = totalZeros;
                bestValueCount = 1;
            }
            else if (!m_valueWeights.empty() && 0 == totalZeros && 0 == maxTotal)
            {
                // Arbitrary choice - we already have this parameter covered
                // Use weights if they exist
                bestValueCount += GetWeight(value);
                if (rand() % bestValueCount < GetWeight(value))
                {
                    bestValue = value;
                }
            }
            else if (totalZeros == maxTotal && !(rand() % ++bestValueCount))
            {
                bestValue = value;
                maxTotal  = totalZeros;
            }
        }
    }
    m_bound = false;

    // What if bestValueCount is 0 here due to exclusions?
    // That would be a bug, but better put in a check.
    if( m_task->GetGenerationMode() != Preview )
    {
        assert( bestValueCount > 0 );
        if( bestValueCount <= 0 )
        {
            throw GenerationError( __FILE__, __LINE__, GenerationFailure );
        }
    }

    return bestValue;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movb 0x3d(%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2d4a2
jmp 0x2d4a4
jmp 0x2d4c3
leaq 0x37458(%rip), %rdi     # 0x64903
leaq 0x3745a(%rip), %rsi     # 0x6490c
movl $0x3d, %edx
leaq 0x374b0(%rip), %rcx     # 0x6496e
callq 0x7450
movq -0x80(%rbp), %rax
movl $0x0, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movb $0x1, 0x3d(%rax)
movl $0x0, -0x1c(%rbp)
movq -0x80(%rbp), %rcx
movl -0x1c(%rbp), %eax
cmpl 0x38(%rcx), %eax
jge 0x2d6dd
movq -0x80(%rbp), %rdi
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movslq -0x1c(%rbp), %rax
movq %rax, 0x30(%rdi)
addq $0x40, %rdi
callq 0x1b860
movq %rax, -0x30(%rbp)
movq -0x80(%rbp), %rdi
addq $0x40, %rdi
callq 0x1b8d0
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1b890
testb $0x1, %al
jne 0x2d54c
jmp 0x2d5e8
leaq -0x30(%rbp), %rdi
callq 0x1b900
movq (%rax), %rdi
callq 0x8130
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x1b900
movq (%rax), %rdi
callq 0x1dd80
movl %eax, -0x84(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x1b900
movq (%rax), %rdi
callq 0x92c0
movl %eax, %ecx
movl -0x84(%rbp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jl 0x2d5d8
cmpl $0x0, -0x3c(%rbp)
je 0x2d5af
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x1b900
movq (%rax), %rdi
callq 0x89e0
testb $0x1, %al
jne 0x2d5c6
jmp 0x2d5d6
movl $0xffffffff, -0x20(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
jmp 0x2d5e8
jmp 0x2d5d8
jmp 0x2d5da
leaq -0x30(%rbp), %rdi
callq 0x1b940
jmp 0x2d525
movl -0x24(%rbp), %eax
cmpl -0xc(%rbp), %eax
jle 0x2d60e
movl -0x24(%rbp), %eax
movl %eax, -0xc(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x10(%rbp)
movl $0x1, -0x18(%rbp)
jmp 0x2d6cd
movl -0x24(%rbp), %eax
cmpl -0xc(%rbp), %eax
jne 0x2d6cb
movl -0x20(%rbp), %eax
cmpl -0x10(%rbp), %eax
jle 0x2d63a
movl -0x1c(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x10(%rbp)
movl $0x1, -0x18(%rbp)
jmp 0x2d6c9
movq -0x80(%rbp), %rdi
addq $0xa8, %rdi
callq 0x1bc40
testb $0x1, %al
jne 0x2d69b
xorl %eax, %eax
cmpl -0x20(%rbp), %eax
jne 0x2d69b
xorl %eax, %eax
cmpl -0x10(%rbp), %eax
jne 0x2d69b
movq -0x80(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x8f60
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
callq 0x7170
movq -0x80(%rbp), %rdi
cltd
idivl -0x18(%rbp)
movl %edx, -0x88(%rbp)
movl -0x1c(%rbp), %esi
callq 0x8f60
movl -0x88(%rbp), %edx
cmpl %eax, %edx
jge 0x2d699
movl -0x1c(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x2d6c7
movl -0x20(%rbp), %eax
cmpl -0x10(%rbp), %eax
jne 0x2d6c5
callq 0x7170
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x18(%rbp)
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x2d6c5
movl -0x1c(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x10(%rbp)
jmp 0x2d6c7
jmp 0x2d6c9
jmp 0x2d6cb
jmp 0x2d6cd
jmp 0x2d6cf
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2d4ee
movq -0x80(%rbp), %rax
movb $0x0, 0x3d(%rax)
movq 0xc0(%rax), %rdi
callq 0xb310
cmpl $0x1, %eax
je 0x2d7e9
cmpl $0x0, -0x18(%rbp)
jle 0x2d702
jmp 0x2d721
leaq 0x3728a(%rip), %rdi     # 0x64993
leaq 0x371fc(%rip), %rsi     # 0x6490c
movl $0x85, %edx
leaq 0x37252(%rip), %rcx     # 0x6496e
callq 0x7450
cmpl $0x0, -0x18(%rbp)
jg 0x2d7e7
movb $0x1, -0x75(%rbp)
movl $0x30, %edi
callq 0x7300
movq %rax, -0x98(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x90(%rbp)
callq 0x79e0
movq -0x90(%rbp), %rdx
leaq 0x371ae(%rip), %rsi     # 0x6490c
leaq -0x60(%rbp), %rdi
callq 0xd700
jmp 0x2d769
movq -0x98(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movl $0x88, %edx
movl $0x2, %ecx
callq 0xd7b0
jmp 0x2d785
movq -0x98(%rbp), %rdi
movb $0x0, -0x75(%rbp)
leaq 0x64259(%rip), %rsi     # 0x919f0
leaq -0x1ffae(%rip), %rdx    # 0xd7f0
callq 0x7980
jmp 0x2d7fe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x2d7c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x73e0
leaq -0x61(%rbp), %rdi
callq 0x7660
testb $0x1, -0x75(%rbp)
jne 0x2d7d9
jmp 0x2d7e5
movq -0x98(%rbp), %rdi
callq 0x74a0
jmp 0x2d7f5
jmp 0x2d7e9
movl -0x14(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x79d0
nop