// File: /workspace/github_cpp_makefile_star2_O3/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 0x61c4
movl %esi, %r13d
movl %edi, %ebp
incq 0x180(%rbx)
movl 0x17c(%rbx), %eax
leaq 0x88(%rsp), %r14
movq %r14, -0x10(%r14)
movq %r14, -0x8(%r14)
movaps 0x2192(%rip), %xmm0     # 0x82e0
movaps %xmm0, 0x30(%rsp)
movl $0x0, 0x40(%rsp)
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 0x57e6
movq -0x10(%r14), %rsi
andq %r15, %rax
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
je 0x61dd
callq 0x380b
movq %rax, 0x78(%rsp)
movq 0x80(%rsp), %r12
cmpq %rax, %r12
jne 0x61f8
jmp 0x6391
movq %rbx, %rdi
addq $0x888, %rsp            # imm = 0x888
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6401
callq 0x3a3e
movq %rax, 0x78(%rsp)
movq 0x80(%rsp), %r12
cmpq %rax, %r12
je 0x639e
movl %r13d, 0x8(%rsp)
negl %r13d
movl %r13d, 0x14(%rsp)
movq 0x18(%rsp), %rax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsp)
cltq
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
movl $0xffffd508, %r14d      # imm = 0xFFFFD508
movl (%r12), %esi
movq %rbx, %rdi
callq 0x5952
testb %al, %al
je 0x6363
incl %r13d
movl (%r12), %esi
movq %rbx, %rdi
leaq 0x30(%rsp), %rdx
callq 0x4d16
movl %ebp, %esi
negl %esi
movl 0x14(%rsp), %edi
movq %rbx, %rdx
movl 0x10(%rsp), %ecx
callq 0x6104
movl %eax, %r15d
negl %r15d
movl (%r12), %esi
movq %rbx, %rdi
callq 0x53d6
cmpl %r15d, %r14d
jge 0x6363
cmpl %r15d, %ebp
jge 0x6366
movl (%r12), %edx
cmpl $0x7, 0x18(%rsp)
jne 0x6342
movl %edx, 0xc(%rsp)
movl $0x7, %edx
movq 0x4d20(%rip), %r14      # 0xafc0
movq %r14, %rdi
leaq 0x2206(%rip), %rsi      # 0x84b0
callq 0x2180
movq %r14, %rdi
movl %r15d, %esi
callq 0x2220
movq %rax, %r14
movl $0xd, %edx
movq %rax, %rdi
leaq 0x21de(%rip), %rsi      # 0x84aa
callq 0x2180
movq %r14, %rdi
movl %r15d, %esi
callq 0x2220
movq %rax, %r14
movl $0x8, %edx
movq %rax, %rdi
leaq 0x21ca(%rip), %rsi      # 0x84b8
callq 0x2180
movq %r14, %rdi
movl $0x7, %esi
callq 0x2220
movq %rax, %r14
movl $0x8, %edx
movq %rax, %rdi
leaq 0x21af(%rip), %rsi      # 0x84c1
callq 0x2180
movq %r14, %rdi
movl %ebp, %esi
callq 0x2220
movl $0x1, %edx
movq %rax, %rdi
leaq 0x20d9(%rip), %rsi      # 0x8409
callq 0x2180
movl (%r12), %edi
callq 0x41d7
movl 0xc(%rsp), %edx
movq 0x28(%rsp), %rax
leaq 0xea222(%rip), %rcx     # 0xf0570
movl %edx, (%rcx,%rax,4)
movl %edx, 0x190(%rbx)
movl %r15d, %ebp
cmpl 0x8(%rsp), %r15d
jl 0x6366
jmp 0x63a2
movl %r14d, %r15d
addq $0x8, %r12
movl %r15d, %r14d
cmpq 0x78(%rsp), %r12
jne 0x6221
cmpq $0x0, 0x20(%rsp)
sete %al
testl %r13d, %r13d
setne %cl
cmovel %r13d, %ebp
orb %al, %cl
movl %ebp, %eax
jne 0x63a6
movl $0xffffd508, %eax       # imm = 0xFFFFD508
addl 0x178(%rbx), %eax
jmp 0x63a6
xorl %eax, %eax
jmp 0x63a6
movl 0x8(%rsp), %eax
addq $0x888, %rsp            # imm = 0x888
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq