// File: /workspace/github_cpp_makefile_star2_O1/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 0x618d
movl %esi, %ebp
movl %edi, %r14d
incq 0x180(%rbx)
movl 0x17c(%rbx), %eax
leaq 0x88(%rsp), %r15
movq %r15, -0x10(%r15)
movq %r15, -0x8(%r15)
movabsq $0xc00000000, %rcx      # imm = 0xC00000000
movq %rcx, 0x38(%rsp)
movabsq $0x4000000000, %rcx     # imm = 0x4000000000
movq %rcx, 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), %r12
cmovel %ecx, %esi
movq %rbx, %rdi
callq 0x578c
movq -0x10(%r15), %rsi
andq %r12, %rax
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
je 0x61a6
callq 0x3746
jmp 0x61ab
movq %rbx, %rdi
addq $0x888, %rsp            # imm = 0x888
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x63ac
callq 0x3979
movq %rax, 0x78(%rsp)
movq 0x80(%rsp), %r13
xorl %r12d, %r12d
cmpq %rax, %r13
je 0x6346
movl %ebp, 0x8(%rsp)
negl %ebp
movl %ebp, 0x14(%rsp)
movq 0x18(%rsp), %rax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsp)
cltq
movq %rax, 0x28(%rsp)
movl $0xffffd508, %ebp       # imm = 0xFFFFD508
movl (%r13), %esi
movq %rbx, %rdi
callq 0x58f8
testb %al, %al
je 0x632a
incl %r12d
movl (%r13), %esi
movq %rbx, %rdi
leaq 0x30(%rsp), %rdx
callq 0x4c60
movl %r14d, %esi
negl %esi
movl 0x14(%rsp), %edi
movq %rbx, %rdx
movl 0x10(%rsp), %ecx
callq 0x60d0
movl %eax, %r15d
negl %r15d
movl (%r13), %esi
movq %rbx, %rdi
callq 0x536a
cmpl %r15d, %ebp
jge 0x632a
cmpl %r15d, %r14d
jge 0x632d
movl (%r13), %edx
cmpl $0x7, 0x18(%rsp)
jne 0x6309
movl %edx, 0xc(%rsp)
movl $0x7, %edx
movq 0x4d5a(%rip), %rbp      # 0xafc0
movq %rbp, %rdi
leaq 0x21a8(%rip), %rsi      # 0x8418
callq 0x2180
movq %rbp, %rdi
movl %r15d, %esi
callq 0x2220
movq %rax, %rbp
movl $0xd, %edx
movq %rax, %rdi
leaq 0x2180(%rip), %rsi      # 0x8412
callq 0x2180
movq %rbp, %rdi
movl %r15d, %esi
callq 0x2220
movq %rax, %rbp
movl $0x8, %edx
movq %rax, %rdi
leaq 0x216c(%rip), %rsi      # 0x8420
callq 0x2180
movq %rbp, %rdi
movl $0x7, %esi
callq 0x2220
movq %rax, %rbp
movl $0x8, %edx
movq %rax, %rdi
leaq 0x2151(%rip), %rsi      # 0x8429
callq 0x2180
movq %rbp, %rdi
movl %r14d, %esi
callq 0x2220
movl $0x1, %edx
movq %rax, %rdi
leaq 0x207a(%rip), %rsi      # 0x8371
callq 0x2180
movl (%r13), %edi
callq 0x4139
movl 0xc(%rsp), %edx
movq 0x28(%rsp), %rax
leaq 0xea25b(%rip), %rcx     # 0xf0570
movl %edx, (%rcx,%rax,4)
movl %edx, 0x190(%rbx)
movl %r15d, %r14d
cmpl 0x8(%rsp), %r15d
jl 0x632d
jmp 0x637c
movl %ebp, %r15d
addq $0x8, %r13
movl %r15d, %ebp
cmpq 0x78(%rsp), %r13
jne 0x61e6
testl %r12d, %r12d
setne %r12b
cmpq $0x0, 0x20(%rsp)
sete %cl
orb %r12b, %cl
xorl %eax, %eax
testb %r12b, %r12b
cmovnel %r14d, %eax
testb %cl, %cl
jne 0x636a
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 0x8(%rsp), %eax
jmp 0x636a
nop