// File: /workspace/github_cpp_makefile_star2_O2/smallHW89[P]gobang/GoBang_ChessBoard.cpp
// Function: ChessBoard::GetNextPosMinMax(int, StPos&, long, long)
int64_t ChessBoard::GetNextPosMinMax(int deep, StPos &pos, int64_t alpha , int64_t beta )
{
    int64_t bestVa = MM_MIN;
    std::vector<StPos>   nextList;
    std::vector<StPos>   bestList;

    int n = GetPossbilePos( m_aiside, nextList);
    for( int i=0; i<nextList.size(); i++ )
    {
        PutChess(nextList[i].x, nextList[i].y , m_aiside);
        nextList[i].score = CalculateOnePos(nextList[i].x, nextList[i].y,
                m_aiside,0);
        UnPutChess(nextList[i].x, nextList[i].y );
        //printf("Pos(%u %u), socre:%u \n", nextList[i].x, nextList[i].y ,nextList[i].score );
    }
    sort(nextList.begin(), nextList.end(), greater<StPos>()   );    

    for( int i=0; i< nextList.size(); i++ )
    {
        StPos pos = nextList[i];
        PutChess(pos.x, pos.y,m_aiside  ); 
        int win = 0; 
        int64_t va = GetMinValue(deep-1, alpha ,beta) ;
        UnPutChess(pos.x, pos.y);

        if(bestVa <  va)
        {
            bestVa = va;
            bestList.clear();
            bestList.push_back(pos);
        }
        if( va == bestVa)
        {
            bestList.push_back(pos);
        }
        if(va > alpha )
            alpha = va;
        if( alpha >= beta) 
            break;
    }

    for(int i=0 ; i< bestList.size(); i++ )
    {
        PutChess( bestList[i].x, bestList[i].y, m_aiside);
        bestList[i].score = CalculateOnePos( bestList[i].x , bestList[i].y , m_aiside, 0 ); 
        UnPutChess(bestList[i].x, bestList[i].y );
        printf("select pos(%u %u) score:%d , mapVar:%d\n",bestList[i].x ,bestList[i].y, bestList[i].score, bestVa );
    }
    sort(bestList.begin(),   bestList.end(), greater<StPos>()  );
    if(bestList.size() > 0  )
    {
        pos.x = bestList[0].x;
        pos.y = bestList[0].y;
        return 0;    
    }
    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, -0x60(%rbp)
movq %rcx, %r12
movq %rdx, -0xa8(%rbp)
movl %esi, -0x2c(%rbp)
movq %rdi, %r14
xorps %xmm0, %xmm0
leaq -0xa0(%rbp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
movaps %xmm0, -0x50(%rbp)
andq $0x0, -0x40(%rbp)
movsbl 0x198(%rdi), %esi
callq 0x274a
pushq $0x4
popq %r15
movq -0xa0(%rbp), %rbx
xorl %r13d, %r13d
movq -0x98(%rbp), %rsi
movq %rsi, %rax
subq %rbx, %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %r13, %rax
jbe 0x2bf4
movl -0x4(%rbx,%r15), %esi
movl (%rbx,%r15), %edx
movsbl 0x198(%r14), %ecx
movq %r14, %rdi
callq 0x128c
movq -0xa0(%rbp), %rbx
movl -0x4(%rbx,%r15), %esi
movl (%rbx,%r15), %edx
movsbl 0x198(%r14), %ecx
movq %r14, %rdi
xorl %r8d, %r8d
callq 0x2860
movq %rax, 0x4(%rbx,%r15)
movl -0x4(%rbx,%r15), %esi
movl (%rbx,%r15), %edx
movq %r14, %rdi
callq 0x12d2
incq %r13
addq $0x18, %r15
jmp 0x2b7f
movq %rbx, %rdi
callq 0x34ba
decl -0x2c(%rbp)
movq $-0x77359400, -0x58(%rbp) # imm = 0x88CA6C00
pushq $-0x1
popq %r15
xorl %r13d, %r13d
movq -0xa0(%rbp), %rcx
movq -0x98(%rbp), %rax
subq %rcx, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
incq %r15
cmpq %r15, %rax
jbe 0x2cc9
movups (%rcx,%r13), %xmm0
movaps %xmm0, -0x80(%rbp)
movq 0x10(%rcx,%r13), %rax
movq %rax, -0x70(%rbp)
movl -0x80(%rbp), %esi
movl -0x7c(%rbp), %edx
movsbl 0x198(%r14), %ecx
movq %r14, %rdi
callq 0x128c
movq %r14, %rdi
movl -0x2c(%rbp), %esi
movq %r12, %rdx
movq -0x60(%rbp), %rcx
callq 0x2de2
movq %rax, %rbx
movl -0x80(%rbp), %esi
movl -0x7c(%rbp), %edx
movq %r14, %rdi
callq 0x12d2
cmpq %rbx, -0x58(%rbp)
jge 0x2ca5
movq -0x50(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0x2c92
movq %rax, -0x48(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x32b2
movq %rbx, -0x58(%rbp)
jmp 0x2ca7
jne 0x2cb4
leaq -0x50(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x32b2
addq $0x18, %r13
cmpq %r12, %rbx
cmovgq %rbx, %r12
cmpq -0x60(%rbp), %r12
jl 0x2c0e
pushq $0x4
popq %rbx
leaq 0x1385(%rip), %r15      # 0x4058
xorl %r12d, %r12d
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq %rsi, %rax
subq %rdi, %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %r12, %rax
jbe 0x2d69
movl -0x4(%rdi,%rbx), %esi
movl (%rdi,%rbx), %edx
movsbl 0x198(%r14), %ecx
movq %r14, %rdi
callq 0x128c
movq -0x50(%rbp), %r13
movl -0x4(%r13,%rbx), %esi
movl (%r13,%rbx), %edx
movsbl 0x198(%r14), %ecx
movq %r14, %rdi
xorl %r8d, %r8d
callq 0x2860
movq %rax, 0x4(%r13,%rbx)
movl -0x4(%r13,%rbx), %esi
movl (%r13,%rbx), %edx
movq %r14, %rdi
callq 0x12d2
movl -0x4(%r13,%rbx), %esi
movl (%r13,%rbx), %edx
movq 0x4(%r13,%rbx), %rcx
movq %r15, %rdi
movq -0x58(%rbp), %r8
xorl %eax, %eax
callq 0x1030
incq %r12
addq $0x18, %rbx
jmp 0x2cd6
callq 0x34ba
movq -0x50(%rbp), %rax
cmpq %rax, -0x48(%rbp)
je 0x2d8d
movl (%rax), %ecx
movq -0xa8(%rbp), %rdx
movl %ecx, (%rdx)
movl 0x4(%rax), %eax
movl %eax, 0x4(%rdx)
xorl %ebx, %ebx
jmp 0x2d90
pushq $-0x1
popq %rbx
leaq -0x50(%rbp), %rdi
callq 0x32ec
leaq -0xa0(%rbp), %rdi
callq 0x32ec
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2dc2
jmp 0x2dc2
jmp 0x2dc2
jmp 0x2dc2
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x32ec
leaq -0xa0(%rbp), %rdi
callq 0x32ec
movq %rbx, %rdi
callq 0x1100