// File: /workspace/github_cpp_makefile_star2_O2/bjohnsonfl[P]Overhead-Chess-Engine-/search.cpp
// Function: alphaBeta(int, int, position&, int)
value alphaBeta(int alpha, int beta, position& pos, int depth)
{
    
    if( depth == 0) {
        value var = evaluate(pos);

        return var;
    }
    pos.inc_nodes();
    
    bool check = false;
    player us = pos.get_sideToPlay();
    player them = player(!us);
    candMoveList movs;
    returnState state = returnState();
    int legalMoves = 0;
    
    value score = -infinite;
    value bestScore = -infinite;
    move bestMove = none;
    
    //Determine if position is in check and if evasions are required. extend depth by 1
    if( pos.get_pieces(them) & pos.squareAttackedBy(pos.get_king_sq(us)))
    {
        check = true;
       // depth++;
    }
    
    movs.end = check == true ? generateEvasion(pos, movs.end)
                             : generateNonEvasions(pos, movs.end);
    candidate * begin = movs.start;
  
    while(begin != movs.end)
    {
        //could use a move picker function here and check for legality in it
        //Only use legal moves
        if(pos.legal(begin -> mv))
        {
            legalMoves++;
            pos.do_move(begin -> mv, state);
            score = -alphaBeta(-beta, -alpha, pos, depth - 1);
           /* if(depth == 7)
            {
                std::cout << "depth: " << depth << " score: " <<score << " best score: " << bestScore << " alpha: "<< alpha <<"\n";
                printmove(begin -> mv);
                
            }*/
            pos.undo_move(begin -> mv);
            
            //std::cout<< "score: " <<score << " best score: " << bestScore << " depth: " << depth << " alpha: "<< alpha <<"\n";
            
            if(score > bestScore)
            {
               //Current best score at this root, but maybe less than alpha
               /* if(depth ==6)
                {
                    std::cout<< "score: " <<score << " best score: " << bestScore << " depth: " << depth << " alpha: "<< alpha <<"\n";
                    printmove(begin -> mv);
                } */
                bestScore = score;
                bestMove = begin -> mv;
                if(score > value(alpha))
                {
                  
                    if(depth == 7)
                   {
                      std::cout<< "score: " <<score << " best score: " << bestScore << " depth: " << depth << " alpha: "<< alpha <<"\n";
                       printmove(begin -> mv);
                   } 
                    pvTable[depth] = bestMove;
                    pos.bestMove = bestMove;
                   
                    if(score >= value(beta))
                    {
                        return value(beta);
                    }
                    
                    alpha = score;
                    
                }
            }
            
            
        } //legal moves
        
        begin++;
    }//while all moves
    
    
    //mate and statelate
    if(legalMoves == 0) {
        
        return check == true ? value(-infinite + pos.get_ply()) : draw;
    }
    
   
    return value(alpha);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x888, %rsp            # imm = 0x888
movq %rdx, %rbx
movq %rcx, 0x18(%rsp)
testl %ecx, %ecx
je 0x5c29
movl %esi, %r12d
movl %edi, %ebp
incq 0x180(%rbx)
movl 0x17c(%rbx), %eax
leaq 0x88(%rsp), %r14
movaps 0x16ad(%rip), %xmm0     # 0x7270
movaps %xmm0, 0x30(%rsp)
andl $0x0, 0x40(%rsp)
movq %r14, -0x10(%r14)
movq %r14, -0x8(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%rsp)
movups %xmm0, 0x58(%rsp)
movups %xmm0, 0x68(%rsp)
bsfq 0x128(%rbx), %rcx
bsfq 0x158(%rbx), %rsi
xorl %edx, %edx
testl %eax, %eax
sete %dl
movq 0x160(%rbx,%rdx,8), %r15
cmovel %ecx, %esi
movq %rbx, %rdi
callq 0x525a
movq -0x10(%r14), %rsi
andq %r15, %rax
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
je 0x5c42
callq 0x362e
jmp 0x5c47
movq %rbx, %rdi
addq $0x888, %rsp            # imm = 0x888
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5e1e
callq 0x386a
movq %rax, 0x78(%rsp)
movq 0x80(%rsp), %r14
movl %r12d, 0xc(%rsp)
negl %r12d
movl %r12d, 0x14(%rsp)
movq 0x18(%rsp), %rcx
leal -0x1(%rcx), %edx
movl %edx, 0x10(%rsp)
movslq %ecx, %rcx
movq %rcx, 0x28(%rsp)
movl $0xffffd508, %r13d      # imm = 0xFFFFD508
xorl %r12d, %r12d
cmpq %rax, %r14
je 0x5dab
movl (%r14), %esi
movq %rbx, %rdi
callq 0x53ae
testb %al, %al
je 0x5d97
incl %r12d
movl (%r14), %esi
movq %rbx, %rdi
leaq 0x30(%rsp), %rdx
callq 0x4840
movl %ebp, %esi
negl %esi
movl 0x14(%rsp), %edi
movq %rbx, %rdx
movl 0x10(%rsp), %ecx
callq 0x5b81
movl %eax, %r15d
negl %r15d
movl (%r14), %esi
movq %rbx, %rdi
callq 0x4ece
cmpl %r15d, %r13d
jge 0x5d97
cmpl %r15d, %ebp
jge 0x5d9a
movl (%r14), %r13d
cmpl $0x7, 0x18(%rsp)
jne 0x5d74
movq 0x52b7(%rip), %rdi      # 0xafb0
leaq 0x1744(%rip), %rsi      # 0x7444
callq 0x2170
movq %rax, %rdi
movl %r15d, %esi
callq 0x2220
movq %rax, %rdi
leaq 0x1724(%rip), %rsi      # 0x743e
callq 0x2170
movq %rax, %rdi
movl %r15d, %esi
callq 0x2220
movq %rax, %rdi
leaq 0x1718(%rip), %rsi      # 0x744c
callq 0x2170
movq %rax, %rdi
pushq $0x7
popq %rsi
callq 0x2220
movq %rax, %rdi
leaq 0x1707(%rip), %rsi      # 0x7455
callq 0x2170
movq %rax, %rdi
movl %ebp, %esi
callq 0x2220
movq %rax, %rdi
leaq 0x1636(%rip), %rsi      # 0x739d
callq 0x2170
movl (%r14), %edi
callq 0x3ff0
movq 0x28(%rsp), %rax
leaq 0xea7f0(%rip), %rcx     # 0xf0570
movl %r13d, (%rcx,%rax,4)
movl %r13d, 0x190(%rbx)
movl %r15d, %ebp
cmpl 0xc(%rsp), %r15d
jl 0x5d9a
jmp 0x5de1
movl %r13d, %r15d
addq $0x8, %r14
movq 0x78(%rsp), %rax
movl %r15d, %r13d
jmp 0x5c7e
cmpq $0x0, 0x20(%rsp)
sete %al
testl %r12d, %r12d
setne %cl
cmovel %r12d, %ebp
orb %al, %cl
movl %ebp, %eax
jne 0x5dcf
movl $0xffffd508, %eax       # imm = 0xFFFFD508
addl 0x178(%rbx), %eax
addq $0x888, %rsp            # imm = 0x888
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %eax
jmp 0x5dcf
nop