// File: /workspace/github_cpp_makefile_star2_O3/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 $0x8e8, %rsp            # imm = 0x8E8
movq %rsi, %rbx
leaq 0xe8(%rsp), %rsi
movq %rsi, -0x10(%rsi)
movq %rsi, -0x8(%rsi)
movaps 0x269d(%rip), %xmm0     # 0x82e0
movaps %xmm0, 0x20(%rsp)
movl $0x0, 0x30(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rsp)
movups %xmm0, 0x48(%rsp)
movups %xmm0, 0x58(%rsp)
movq %rdi, 0x10(%rsp)
testl %edi, %edi
je 0x5fe8
movq %rbx, %rdi
callq 0x3dbf
movq %rax, 0xd8(%rsp)
movq 0xe0(%rsp), %rcx
cmpq %rax, %rcx
je 0x5ff0
leaq 0x100(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rax
decl %eax
movl %eax, 0xc(%rsp)
xorl %r13d, %r13d
movq 0x530f(%rip), %r12      # 0xafc0
movl (%rcx), %esi
movq %rbx, %rdi
callq 0x5952
testb %al, %al
je 0x5fc1
movl $0xc, %ecx
leaq 0x70(%rsp), %rdi
movq 0x18(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x160(%rbx), %rdi
callq 0x28c9
movl %eax, %ebp
movq 0x168(%rbx), %rdi
callq 0x28c9
movl %eax, %r15d
movq 0xe0(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
leaq 0x20(%rsp), %rdx
callq 0x4d16
bsfq 0x128(%rbx), %rsi
bsfq 0x158(%rbx), %rax
xorl %ecx, %ecx
cmpl $0x0, 0x17c(%rbx)
setne %cl
movq 0x160(%rbx,%rcx,8), %r14
cmovel %eax, %esi
movq %rbx, %rdi
callq 0x57e6
testq %r14, %rax
je 0x5d54
movq 0xe0(%rsp), %rax
movl (%rax), %edi
callq 0x41d7
movq %rbx, %rdi
callq 0x5298
movq 0x160(%rbx), %rdi
callq 0x28c9
cmpl %ebp, %eax
jge 0x5d84
cmpl $0x0, 0x17c(%rbx)
je 0x5d84
movq %rbx, %rdi
callq 0x5298
movq 0xe0(%rsp), %rax
movl (%rax), %edi
callq 0x41d7
movq 0x168(%rbx), %rdi
callq 0x28c9
cmpl %r15d, %eax
jge 0x5db5
cmpl $0x1, 0x17c(%rbx)
je 0x5db5
movq %rbx, %rdi
callq 0x5298
movq 0xe0(%rsp), %rax
movl (%rax), %edi
callq 0x41d7
movq 0x158(%rbx), %rdi
orq 0x128(%rbx), %rdi
callq 0x28c9
movl 0xc(%rsp), %edi
movq %rbx, %rsi
callq 0x5c18
movq %rax, %r14
movq 0xe0(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
callq 0x53d6
movq 0x160(%rbx), %rdi
callq 0x28c9
cmpl %ebp, %eax
jne 0x5e0a
movq 0x168(%rbx), %rdi
callq 0x28c9
cmpl %r15d, %eax
je 0x5e12
movq %rbx, %rdi
callq 0x5298
addq %r13, %r14
xorl %r15d, %r15d
movq 0x70(%rsp,%r15,8), %rax
cmpq 0x100(%rbx,%r15,8), %rax
je 0x5e2f
movq %rbx, %rdi
callq 0x5298
incq %r15
cmpq $0xc, %r15
jne 0x5e18
movq 0x158(%rbx), %rdi
orq 0x128(%rbx), %rdi
callq 0x28c9
movq 0x170(%rbx), %rdi
callq 0x28c9
cmpl $0x20, %eax
jle 0x5fbc
movl $0x12, %edx
movq %r12, %rdi
leaq 0x25e3(%rip), %rsi      # 0x8452
callq 0x2180
movq 0x170(%rbx), %rdi
callq 0x28c9
movq %r12, %rdi
movl %eax, %esi
callq 0x2220
movq %rax, %r15
movl $0x8, %edx
movq %rax, %rdi
leaq 0x25c9(%rip), %rsi      # 0x8465
callq 0x2180
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x2220
movl $0x1, %edx
movq %rax, %rdi
leaq 0x254c(%rip), %r15      # 0x8409
movq %r15, %rsi
callq 0x2180
movl $0x11, %edx
movq %r12, %rdi
leaq 0x259a(%rip), %rsi      # 0x846e
callq 0x2180
movq 0x160(%rbx), %rdi
callq 0x28c9
movq %r12, %rdi
movl %eax, %esi
callq 0x2220
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2180
movl $0x11, %edx
movq %r12, %rdi
leaq 0x2572(%rip), %rsi      # 0x8480
callq 0x2180
movq 0x168(%rbx), %rdi
callq 0x28c9
movq %r12, %rdi
movl %eax, %esi
callq 0x2220
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2180
movl $0x17, %edx
movq %r12, %rdi
leaq 0x254a(%rip), %rsi      # 0x8492
callq 0x2180
movq 0x130(%rbx), %rdi
callq 0x28c9
movq %r12, %rdi
movl %eax, %esi
callq 0x2220
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2180
movl 0x17c(%rbx), %esi
movq %r12, %rdi
callq 0x2220
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2180
movq 0xe0(%rsp), %rax
movl (%rax), %edi
callq 0x41d7
movq %rbx, %rdi
callq 0x5298
movq %rbx, %rdi
callq 0x5ad6
movq 0x170(%rbx), %rdi
callq 0x35f7
movq %r14, %r13
jmp 0x5fc4
movq %r13, %r14
movq 0xe0(%rsp), %rcx
addq $0x8, %rcx
movq %rcx, 0xe0(%rsp)
cmpq 0xd8(%rsp), %rcx
jne 0x5cb1
jmp 0x5ff3
movl $0x1, %r14d
jmp 0x5ff3
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8e8, %rsp            # imm = 0x8E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq