// File: 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
movq %rsp, %rbp
subq $0x8c0, %rsp            # imm = 0x8C0
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0xa9b5
movq -0x18(%rbp), %rdi
callq 0xae90
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xacf1
movq -0x18(%rbp), %rdi
callq 0xad70
movb $0x0, -0x21(%rbp)
movq -0x18(%rbp), %rdi
callq 0x75c0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
leaq -0x840(%rbp), %rdi
callq 0xad00
leaq -0x888(%rbp), %rdi
callq 0x9f10
movl $0x0, -0x88c(%rbp)
movl $0x2af8, %edi           # imm = 0x2AF8
callq 0xad60
movl %eax, -0x890(%rbp)
movl $0x2af8, %edi           # imm = 0x2AF8
callq 0xad60
movl %eax, -0x894(%rbp)
movl $0x0, -0x898(%rbp)
movq -0x18(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x7600
movq %rax, -0x8a8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x8b0(%rbp)
movq -0x18(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x7640
movq -0x8b0(%rbp), %rdi
movl %eax, %esi
callq 0x98b0
movq %rax, %rcx
movq -0x8a8(%rbp), %rax
andq %rcx, %rax
cmpq $0x0, %rax
je 0xaa7a
movb $0x1, -0x21(%rbp)
movb -0x21(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xaaa0
movq -0x18(%rbp), %rdi
movq -0x840(%rbp), %rsi
callq 0x5d20
movq %rax, -0x8b8(%rbp)
jmp 0xaab7
movq -0x18(%rbp), %rdi
movq -0x840(%rbp), %rsi
callq 0x6180
movq %rax, -0x8b8(%rbp)
movq -0x8b8(%rbp), %rax
movq %rax, -0x840(%rbp)
movq -0x838(%rbp), %rax
movq %rax, -0x8a0(%rbp)
movq -0x8a0(%rbp), %rax
cmpq -0x840(%rbp), %rax
je 0xac95
movq -0x18(%rbp), %rdi
movq -0x8a0(%rbp), %rax
movl (%rax), %esi
callq 0x9b10
testb $0x1, %al
jne 0xab02
jmp 0xac7e
movl -0x88c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x8a0(%rbp), %rax
movl (%rax), %esi
leaq -0x888(%rbp), %rdx
callq 0x86f0
xorl %edi, %edi
subl -0xc(%rbp), %edi
xorl %esi, %esi
subl -0x8(%rbp), %esi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
callq 0xa980
movl %eax, %edi
callq 0xad60
movl %eax, -0x890(%rbp)
movq -0x18(%rbp), %rdi
movq -0x8a0(%rbp), %rax
movl (%rax), %esi
callq 0x92f0
movl -0x890(%rbp), %eax
cmpl -0x894(%rbp), %eax
jle 0xac7c
movl -0x890(%rbp), %eax
movl %eax, -0x894(%rbp)
movq -0x8a0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x898(%rbp)
movl -0x890(%rbp), %eax
cmpl -0x8(%rbp), %eax
jle 0xac7a
cmpl $0x7, -0x1c(%rbp)
jne 0xac37
movq 0x7419(%rip), %rdi      # 0x11fc8
leaq 0x38b6(%rip), %rsi      # 0xe46c
callq 0x31e0
movq %rax, %rdi
movl -0x890(%rbp), %esi
callq 0x3380
movq %rax, %rdi
leaq 0x3893(%rip), %rsi      # 0xe466
callq 0x31e0
movq %rax, %rdi
movl -0x894(%rbp), %esi
callq 0x3380
movq %rax, %rdi
leaq 0x3884(%rip), %rsi      # 0xe474
callq 0x31e0
movq %rax, %rdi
movl -0x1c(%rbp), %esi
callq 0x3380
movq %rax, %rdi
leaq 0x3873(%rip), %rsi      # 0xe47d
callq 0x31e0
movq %rax, %rdi
movl -0x8(%rbp), %esi
callq 0x3380
movq %rax, %rdi
leaq 0x37a1(%rip), %rsi      # 0xe3c5
callq 0x31e0
movq -0x8a0(%rbp), %rax
movl (%rax), %edi
callq 0x6fc0
movl -0x898(%rbp), %edx
movslq -0x1c(%rbp), %rcx
leaq 0xec9e8(%rip), %rax     # 0xf7630
movl %edx, (%rax,%rcx,4)
movl -0x898(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x190(%rax)
movl -0x890(%rbp), %eax
cmpl -0xc(%rbp), %eax
jl 0xac71
movl -0xc(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xacf1
movl -0x890(%rbp), %eax
movl %eax, -0x8(%rbp)
jmp 0xac7c
jmp 0xac7e
movq -0x8a0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x8a0(%rbp)
jmp 0xaad3
cmpl $0x0, -0x88c(%rbp)
jne 0xaceb
movb -0x21(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xacd6
movl $0x2af8, %edi           # imm = 0x2AF8
callq 0xad60
movl %eax, -0x8c0(%rbp)
movq -0x18(%rbp), %rdi
callq 0xad90
movl %eax, %ecx
movl -0x8c0(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x8bc(%rbp)
jmp 0xace0
xorl %eax, %eax
movl %eax, -0x8bc(%rbp)
jmp 0xace0
movl -0x8bc(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xacf1
movl -0x8(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x8c0, %rsp            # imm = 0x8C0
popq %rbp
retq
nopl (%rax)