// File: 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
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq $-0x77359400, -0x38(%rbp) # imm = 0x88CA6C00
leaq -0x50(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x4dc0
leaq -0x68(%rbp), %rdi
callq 0x4dc0
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rdx
movsbl 0x198(%rdi), %esi
callq 0x3780
movl %eax, -0xe4(%rbp)
jmp 0x3f3c
movl -0xe4(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl $0x0, -0x80(%rbp)
movslq -0x80(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4ec0
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jae 0x408b
movslq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x4ef0
movq -0x110(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x104(%rbp)
movslq -0x80(%rbp), %rsi
callq 0x4ef0
movq -0xf8(%rbp), %rdi
movl -0x104(%rbp), %esi
movl 0x4(%rax), %edx
movsbl 0x198(%rdi), %ecx
callq 0x12e0
jmp 0x3fbd
movslq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x4ef0
movl (%rax), %eax
movl %eax, -0x124(%rbp)
movslq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x4ef0
movl -0x124(%rbp), %esi
movq %rax, %rcx
movq -0xf8(%rbp), %rax
movl 0x4(%rcx), %edx
xorl %r8d, %r8d
movq %rax, %rdi
movsbl 0x198(%rax), %ecx
callq 0x3940
movq %rax, -0x120(%rbp)
movslq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x4ef0
movq -0x120(%rbp), %rcx
movq %rcx, 0x8(%rax)
movslq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x4ef0
movl (%rax), %eax
movl %eax, -0x114(%rbp)
movslq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x4ef0
movq -0xf8(%rbp), %rdi
movl -0x114(%rbp), %esi
movl 0x4(%rax), %edx
callq 0x1360
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x3f4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x4fd0
leaq -0x50(%rbp), %rdi
callq 0x4fd0
jmp 0x44be
leaq -0x50(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x4f50
movq -0x130(%rbp), %rdi
movq %rax, -0x88(%rbp)
callq 0x4f80
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x4f10
jmp 0x40ca
movl $0x0, -0x98(%rbp)
movslq -0x98(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4ec0
movq %rax, %rcx
movq -0x138(%rbp), %rax
cmpq %rcx, %rax
jae 0x4230
movslq -0x98(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x4ef0
movq -0xf8(%rbp), %rdi
movq 0x10(%rax), %rcx
movq %rcx, -0xa0(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0xb0(%rbp)
movl -0xb0(%rbp), %esi
movl -0xac(%rbp), %edx
movsbl 0x198(%rdi), %ecx
callq 0x12e0
jmp 0x4144
movq -0xf8(%rbp), %rdi
movl $0x0, -0xb4(%rbp)
movl -0x14(%rbp), %esi
decl %esi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x44d0
movq %rax, -0x140(%rbp)
jmp 0x4170
movq -0xf8(%rbp), %rdi
movq -0x140(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl -0xb0(%rbp), %esi
movl -0xac(%rbp), %edx
callq 0x1360
movq -0x38(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jge 0x41d5
movq -0xc0(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0x68(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x4fb0
movq -0x148(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x4e20
jmp 0x41d3
jmp 0x41d5
movq -0xc0(%rbp), %rax
cmpq -0x38(%rbp), %rax
jne 0x41f6
leaq -0x68(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x4e20
jmp 0x41f4
jmp 0x41f6
movq -0xc0(%rbp), %rax
cmpq -0x28(%rbp), %rax
jle 0x420e
movq -0xc0(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jl 0x421a
jmp 0x4230
jmp 0x421c
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x40d4
movl $0x0, -0xc4(%rbp)
movslq -0xc4(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x4ec0
movq %rax, %rcx
movq -0x150(%rbp), %rax
cmpq %rcx, %rax
jae 0x43ff
movslq -0xc4(%rbp), %rsi
leaq -0x68(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x4ef0
movq -0x160(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x154(%rbp)
movslq -0xc4(%rbp), %rsi
callq 0x4ef0
movq -0xf8(%rbp), %rdi
movl -0x154(%rbp), %esi
movl 0x4(%rax), %edx
movsbl 0x198(%rdi), %ecx
callq 0x12e0
jmp 0x42b4
movslq -0xc4(%rbp), %rsi
leaq -0x68(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x4ef0
movq -0x178(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x18c(%rbp)
movslq -0xc4(%rbp), %rsi
callq 0x4ef0
movl -0x18c(%rbp), %esi
movq -0xf8(%rbp), %rdi
movl 0x4(%rax), %edx
movsbl 0x198(%rdi), %ecx
xorl %r8d, %r8d
movl %r8d, -0x164(%rbp)
callq 0x3940
movq -0x178(%rbp), %rdi
movq %rax, -0x188(%rbp)
movslq -0xc4(%rbp), %rsi
callq 0x4ef0
movq -0x188(%rbp), %rcx
movq -0x178(%rbp), %rdi
movq %rcx, 0x8(%rax)
movslq -0xc4(%rbp), %rsi
callq 0x4ef0
movq -0x178(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x17c(%rbp)
movslq -0xc4(%rbp), %rsi
callq 0x4ef0
movq -0xf8(%rbp), %rdi
movl -0x17c(%rbp), %esi
movl 0x4(%rax), %edx
callq 0x1360
movq -0x178(%rbp), %rdi
movslq -0xc4(%rbp), %rsi
callq 0x4ef0
movq -0x178(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x16c(%rbp)
movslq -0xc4(%rbp), %rsi
callq 0x4ef0
movq -0x178(%rbp), %rdi
movl 0x4(%rax), %eax
movl %eax, -0x168(%rbp)
movslq -0xc4(%rbp), %rsi
callq 0x4ef0
movl -0x16c(%rbp), %esi
movl -0x168(%rbp), %edx
movq %rax, %rcx
movl -0x164(%rbp), %eax
movq 0x8(%rcx), %rcx
movq -0x38(%rbp), %r8
leaq 0x3c5a(%rip), %rdi      # 0x803c
callq 0x1030
jmp 0x43e9
jmp 0x43eb
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x423a
leaq -0x68(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x4f50
movq -0x198(%rbp), %rdi
movq %rax, -0xd0(%rbp)
callq 0x4f80
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x4f10
jmp 0x443e
leaq -0x68(%rbp), %rdi
callq 0x4ec0
cmpq $0x0, %rax
jbe 0x448d
leaq -0x68(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4ef0
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
leaq -0x68(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4ef0
movl 0x4(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq $0x0, -0x8(%rbp)
movl $0x1, -0xe0(%rbp)
jmp 0x449f
movq $-0x1, -0x8(%rbp)
movl $0x1, -0xe0(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x4fd0
leaq -0x50(%rbp), %rdi
callq 0x4fd0
movq -0x8(%rbp), %rax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x10f0
nopw (%rax,%rax)