// File: /workspace/github_cpp_makefile_star2_O2/bjohnsonfl[P]Overhead-Chess-Engine-/search.cpp
// Function: perft(int, position&)
bitboard perft (int depth, position& pos)
{
    
    candMoveList moves ;
   
    
    bitboard nodes=0;
    returnState state = returnState();
    if(depth == 0) return 1;
  //  std::cout<<"Depth: "<<depth << "\n";
   // std::cout<<"====================\n";
    if(depth == 1 ){
     // std::cout<<"Depth: "<<depth << "\n";
       // pos.printBoard();
    }
   
  /// moves.end = generateCapture(pos, moves.end);
    moves.end = generateAllLegal(pos, moves.end);
    while(moves.start != moves.end)
    {
        if(pos.legal(moves.start -> mv)){
            
            
           // pos.printBoard();
            //printmove(moves.start -> mv);
            //std::cout<<pos.get_enPassent()  <<"\n";
            if(moves.start -> mv == move(919))// && state.prev->m == move(324))
            {
               // std::cout<<"this move\n";
              //  printBitboard(pos.get_pinned(pos.get_sideToPlay()));
               // pos.printBoard();
               // pos.printAllBitboards();
                
            }
            if(state.captured == b_king || state.captured == w_king)
            {
                //std::cout<<"this move\n";
                //printmove(state.m);
                //printmove(state.prev->m);
                //printmove(moves.start -> mv);
                //pos.printBoard();
            }
            bitboard newBoards [12] ;
            for(int i=0; i<12; i++) newBoards[i] = pos.get_pieces(piece(i));
           
            int whiteCount = popcount(pos.get_pieces(white));
            int blackCount =  popcount(pos.get_pieces(black));
            
            
            //pos.printBoard();
            pos.do_move(moves.start -> mv, state);
            if(pos.get_pieces(pos.get_sideToPlay()) & pos.squareAttackedBy(pos.get_king_sq(player(!pos.get_sideToPlay()))))
            {
                printmove(moves.start -> mv);
                pos.printBoard();
            }
            
            
            if((popcount(pos.get_pieces(white)) < whiteCount) && pos.get_sideToPlay() != white)
            {
                pos.printBoard();
                printmove(moves.start -> mv);
            }
            
            if((popcount(pos.get_pieces(black)) < blackCount) && pos.get_sideToPlay() != black)
            {
                pos.printBoard();
                printmove(moves.start -> mv);
            }
           // printmove(moves.start -> mv);
           // pos.printBoard();
            if( popcount(pos.get_pieces(p_king)) < 2)
            {
                
             //   pos.printAllBitboards();
            }
            nodes += perft(depth -1, pos);
            pos.undo_move(moves.start -> mv);
            
            if(popcount(pos.get_pieces(white)) != whiteCount || popcount(pos.get_pieces(black)) != blackCount)
            {
                pos.printBoard();
            }
            
            
          //  pos.printBoard();
            for(int i=0; i<12; i++) {
              if(  newBoards[i] != pos.get_pieces(piece(i)))
              {
                  pos.printBoard();
              }
            }
            
           // pos.printBoard();
            if( popcount(pos.get_pieces(p_king)) < 2)
            {
               
              //  pos.printAllBitboards();
            }
            if(pos.numberOfPieces() > 32)  {
                std::cout<<"Number oF Pieces: "<< pos.numberOfPieces() << " Depth: " << depth <<"\n";
                std::cout<<"Number oF white: "<< popcount(pos.get_pieces(white)) << "\n";
                std::cout<<"Number oF black: "<< popcount(pos.get_pieces(black)) << "\n";
                std::cout<<"Number oF black Pawns: "<< popcount(pos.get_pieces(black, p_pawn)) << "\n";
                
                std::cout<<pos.get_sideToPlay() << "\n";
                printmove(moves.start -> mv);
                pos.printBoard();
                pos.printAllBitboards();
                printBitboard(pos.get_pieces());
                
            }
           // pos.printBoard();
           // std::cout<< "LEGALLLLLLL\n";
        }
        
        moves.start++;
    }
    return nodes;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8d8, %rsp            # imm = 0x8D8
movq %rsi, %rbx
leaq 0xd8(%rsp), %rsi
movq %rsi, -0x10(%rsi)
movq %rsi, -0x8(%rsi)
movaps 0x1b69(%rip), %xmm0     # 0x7270
movaps %xmm0, 0x10(%rsp)
andl $0x0, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rsp)
movups %xmm0, 0x38(%rsp)
movups %xmm0, 0x48(%rsp)
testl %edi, %edi
je 0x5a6e
movl %edi, %r14d
movq %rbx, %rdi
callq 0x3bd2
movq %rax, 0xc8(%rsp)
movq %r14, 0x8(%rsp)
leal -0x1(%r14), %ecx
movl %ecx, 0x4(%rsp)
xorl %r14d, %r14d
movq 0xd0(%rsp), %rcx
leaq 0x1c40(%rip), %r13      # 0x739d
cmpq %rax, %rcx
je 0x5a72
movl (%rcx), %esi
movq %rbx, %rdi
callq 0x53ae
testb %al, %al
je 0x5a4d
xorl %eax, %eax
cmpq $0xc, %rax
je 0x5792
movq 0x100(%rbx,%rax,8), %rcx
movq %rcx, 0x60(%rsp,%rax,8)
incq %rax
jmp 0x577a
movq 0x160(%rbx), %rdi
callq 0x2801
movl %eax, %r12d
movq 0x168(%rbx), %rdi
callq 0x2801
movl %eax, %ebp
movq 0xd0(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
callq 0x4840
bsfq 0x128(%rbx), %rsi
bsfq 0x158(%rbx), %rax
xorl %ecx, %ecx
cmpl $0x0, 0x17c(%rbx)
setne %cl
movq 0x160(%rbx,%rcx,8), %r15
cmovel %eax, %esi
movq %rbx, %rdi
callq 0x525a
testq %r15, %rax
je 0x5811
movq 0xd0(%rsp), %rax
movl (%rax), %edi
callq 0x3ff0
movq %rbx, %rdi
callq 0x4db0
movq 0x160(%rbx), %rdi
callq 0x2801
cmpl %r12d, %eax
jge 0x5842
cmpl $0x0, 0x17c(%rbx)
je 0x5842
movq %rbx, %rdi
callq 0x4db0
movq 0xd0(%rsp), %rax
movl (%rax), %edi
callq 0x3ff0
movq 0x168(%rbx), %rdi
callq 0x2801
cmpl %ebp, %eax
jge 0x5872
cmpl $0x1, 0x17c(%rbx)
je 0x5872
movq %rbx, %rdi
callq 0x4db0
movq 0xd0(%rsp), %rax
movl (%rax), %edi
callq 0x3ff0
movq 0x158(%rbx), %rdi
orq 0x128(%rbx), %rdi
callq 0x2801
movl 0x4(%rsp), %edi
movq %rbx, %rsi
callq 0x56dc
movq %rax, %r15
movq 0xd0(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
callq 0x4ece
movq 0x160(%rbx), %rdi
callq 0x2801
cmpl %r12d, %eax
jne 0x58c7
movq 0x168(%rbx), %rdi
callq 0x2801
cmpl %ebp, %eax
je 0x58cf
movq %rbx, %rdi
callq 0x4db0
addq %r14, %r15
xorl %r14d, %r14d
cmpq $0xc, %r14
je 0x58f7
movq 0x60(%rsp,%r14,8), %rax
cmpq 0x100(%rbx,%r14,8), %rax
je 0x58f2
movq %rbx, %rdi
callq 0x4db0
incq %r14
jmp 0x58d5
movq 0x158(%rbx), %rdi
orq 0x128(%rbx), %rdi
callq 0x2801
movq %rbx, %rdi
callq 0x5de8
cmpl $0x20, %eax
jle 0x5a4a
movq 0x568e(%rip), %r12      # 0xafb0
movq %r12, %rdi
leaq 0x1aba(%rip), %rsi      # 0x73e6
callq 0x2170
movq %rax, %r14
movq %rbx, %rdi
callq 0x5de8
movq %r14, %rdi
movl %eax, %esi
callq 0x2220
movq %rax, %rdi
leaq 0x1aa9(%rip), %rsi      # 0x73f9
callq 0x2170
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x2220
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
movq %r12, %rdi
leaq 0x1a8b(%rip), %rsi      # 0x7402
callq 0x2170
movq %rax, %r14
movq 0x160(%rbx), %rdi
callq 0x2801
movq %r14, %rdi
movl %eax, %esi
callq 0x2220
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
movq %r12, %rdi
leaq 0x1a6a(%rip), %rsi      # 0x7414
callq 0x2170
movq %rax, %r14
movq 0x168(%rbx), %rdi
callq 0x2801
movq %r14, %rdi
movl %eax, %esi
callq 0x2220
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
movq %r12, %rdi
leaq 0x1a49(%rip), %rsi      # 0x7426
callq 0x2170
movq %rax, %r14
movq 0x130(%rbx), %rdi
callq 0x2801
movq %r14, %rdi
movl %eax, %esi
callq 0x2220
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
movl 0x17c(%rbx), %esi
movq %r12, %rdi
callq 0x2220
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
movq 0xd0(%rsp), %rax
movl (%rax), %edi
callq 0x3ff0
movq %rbx, %rdi
callq 0x4db0
movq %rbx, %rdi
callq 0x552e
movq 0x170(%rbx), %rdi
callq 0x341d
movq %r15, %r14
movq 0xc8(%rsp), %rax
movq 0xd0(%rsp), %rcx
addq $0x8, %rcx
movq %rcx, 0xd0(%rsp)
jmp 0x575d
pushq $0x1
popq %r14
movq %r14, %rax
addq $0x8d8, %rsp            # imm = 0x8D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq