// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movq %r8, -0xa8(%rbp)
movq %rcx, -0x58(%rbp)
movq %rdx, -0xb0(%rbp)
movl %esi, -0x5c(%rbp)
movq %rdi, %r14
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rdx
movaps %xmm0, (%rdx)
xorl %eax, %eax
movq %rax, 0x10(%rdx)
movq %rax, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
movsbl 0x198(%rdi), %esi
callq 0x2a1e
movq -0x80(%rbp), %r13
movq -0x78(%rbp), %rbx
movq %rbx, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
subq %r13, %rax
je 0x2f9c
leaq 0x4(%r14), %rax
movq %rax, -0x30(%rbp)
movl $0x4, %r15d
xorl %r12d, %r12d
movl -0x4(%r13,%r15), %esi
movl (%r13,%r15), %edx
movsbl 0x198(%r14), %ecx
movq %r14, %rdi
callq 0x128e
movq -0x80(%rbp), %r13
movl -0x4(%r13,%r15), %esi
movl (%r13,%r15), %edx
movsbl 0x198(%r14), %ecx
movq %r14, %rdi
xorl %r8d, %r8d
callq 0x2ba8
movq %rax, 0x4(%r13,%r15)
movslq -0x4(%r13,%r15), %rax
movslq (%r13,%r15), %rcx
leaq (%rax,%rax,4), %rax
movq -0x30(%rbp), %rdx
leaq (%rdx,%rax,4), %rax
movb $0x65, (%rcx,%rax)
addq $-0x18, 0x248(%r14)
decl (%r14)
incq %r12
movq -0x78(%rbp), %rbx
movq %rbx, %rax
subq %r13, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
addq $0x18, %r15
cmpq %r12, %rax
ja 0x2f09
jmp 0x2fa4
sarq $0x3, %rax
imulq %rcx, %rax
cmpq %rbx, %r13
je 0x2fe0
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3b0e
movabsq $-0x5555555555555555, %r12 # imm = 0xAAAAAAAAAAAAAAAB
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3c39
movq -0x80(%rbp), %r13
movq -0x78(%rbp), %rbx
jmp 0x2fea
movabsq $-0x5555555555555555, %r12 # imm = 0xAAAAAAAAAAAAAAAB
cmpq %r13, %rbx
je 0x3153
decl -0x5c(%rbp)
leaq 0x4(%r14), %rax
movq %rax, -0xb8(%rbp)
movq $-0x77359400, -0x30(%rbp) # imm = 0x88CA6C00
xorl %r15d, %r15d
xorl %ebx, %ebx
movups (%r13,%r15), %xmm0
movaps %xmm0, -0xa0(%rbp)
movq 0x10(%r13,%r15), %rax
movq %rax, -0x90(%rbp)
movl -0xa0(%rbp), %esi
movl -0x9c(%rbp), %edx
movsbl 0x198(%r14), %ecx
movq %r14, %rdi
callq 0x128e
movq %r14, %rdi
movl -0x5c(%rbp), %esi
movq -0x58(%rbp), %rdx
movq -0xa8(%rbp), %rcx
callq 0x330a
movq %rax, %r13
movslq -0xa0(%rbp), %rax
movslq -0x9c(%rbp), %rcx
leaq (%rax,%rax,4), %rax
movq -0xb8(%rbp), %rdx
leaq (%rdx,%rax,4), %rax
movb $0x65, (%rcx,%rax)
addq $-0x18, 0x248(%r14)
decl (%r14)
cmpq %r13, -0x30(%rbp)
jge 0x30c8
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rsi
cmpq %rax, %rsi
je 0x30a2
movq %rax, -0x48(%rbp)
movq %rax, %rsi
cmpq -0x40(%rbp), %rsi
je 0x30cc
movq -0x90(%rbp), %rax
movq %rax, 0x10(%rsi)
movaps -0xa0(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, -0x48(%rbp)
movq %r13, -0x30(%rbp)
jmp 0x30e0
je 0x30e0
jmp 0x3116
leaq -0x50(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x3978
movq %r13, -0x30(%rbp)
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x3106
movq -0x90(%rbp), %rax
movq %rax, 0x10(%rsi)
movaps -0xa0(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, -0x48(%rbp)
jmp 0x3116
leaq -0x50(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x3978
movq -0x58(%rbp), %rax
cmpq %rax, %r13
cmovgq %r13, %rax
movq %rax, -0x58(%rbp)
cmpq -0xa8(%rbp), %rax
jge 0x315b
incq %rbx
movq -0x80(%rbp), %r13
movq -0x78(%rbp), %rax
subq %r13, %rax
sarq $0x3, %rax
imulq %r12, %rax
addq $0x18, %r15
cmpq %rbx, %rax
ja 0x300e
jmp 0x315b
movq $-0x77359400, -0x30(%rbp) # imm = 0x88CA6C00
movq -0x50(%rbp), %r15
movq -0x48(%rbp), %r13
movq %r13, %rax
subq %r15, %rax
je 0x3233
leaq 0x4(%r14), %rax
movq %rax, -0x58(%rbp)
movl $0x4, %r12d
xorl %ebx, %ebx
movl -0x4(%r15,%r12), %esi
movl (%r15,%r12), %edx
movsbl 0x198(%r14), %ecx
movq %r14, %rdi
callq 0x128e
movq -0x50(%rbp), %r15
movl -0x4(%r15,%r12), %esi
movl (%r15,%r12), %edx
movsbl 0x198(%r14), %ecx
movq %r14, %rdi
xorl %r8d, %r8d
callq 0x2ba8
movq %rax, 0x4(%r15,%r12)
movslq -0x4(%r15,%r12), %rax
movslq (%r15,%r12), %rcx
leaq (%rax,%rax,4), %rax
movq -0x58(%rbp), %rdx
leaq (%rdx,%rax,4), %rax
movb $0x65, (%rcx,%rax)
addq $-0x18, 0x248(%r14)
decl (%r14)
movl -0x4(%r15,%r12), %esi
movl (%r15,%r12), %edx
movq 0x4(%r15,%r12), %rcx
leaq 0x1e62(%rip), %rdi      # 0x5058
movq -0x30(%rbp), %r8
xorl %eax, %eax
callq 0x1030
incq %rbx
movq -0x50(%rbp), %r15
movq -0x48(%rbp), %r13
movq %r13, %rax
subq %r15, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
addq $0x18, %r12
cmpq %rbx, %rax
ja 0x317f
jmp 0x323b
sarq $0x3, %rax
imulq %r12, %rax
cmpq %r13, %r15
je 0x326b
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x3b0e
movq %r15, %rdi
movq %r13, %rsi
callq 0x3c39
movq -0x50(%rbp), %r15
movq -0x48(%rbp), %r13
cmpq %r15, %r13
je 0x3287
movl (%r15), %eax
movq -0xb0(%rbp), %rcx
movl %eax, (%rcx)
movl 0x4(%r15), %eax
movl %eax, 0x4(%rcx)
xorl %ebx, %ebx
jmp 0x3293
movq $-0x1, %rbx
testq %r15, %r15
je 0x32a2
movq -0x40(%rbp), %rsi
subq %r15, %rsi
movq %r15, %rdi
callq 0x10a0
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x32b7
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x10a0
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x32d4
jmp 0x32d4
jmp 0x32d4
jmp 0x32d4
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x32ec
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x10a0
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x3301
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x10a0
movq %rbx, %rdi
callq 0x10e0
nop