// File: /workspace/github_cpp_makefile_star2_O1/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)
movabsq $0xc00000000, %rax      # imm = 0xC00000000
movq %rax, 0x20(%rsp)
movabsq $0x4000000000, %rax     # imm = 0x4000000000
movq %rax, 0x18(%rsp)
movl $0x0, 0x28(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsp)
movups %xmm0, 0x40(%rsp)
movups %xmm0, 0x50(%rsp)
movq %rdi, 0x8(%rsp)
testl %edi, %edi
je 0x5fa2
movq %rbx, %rdi
callq 0x3d21
movq %rax, 0xc8(%rsp)
movq 0xd0(%rsp), %rcx
cmpq %rax, %rcx
je 0x5faa
leaq 0x100(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
decl %eax
movl %eax, 0x4(%rsp)
xorl %r13d, %r13d
movq 0x5355(%rip), %r12      # 0xafc0
movl (%rcx), %esi
movq %rbx, %rdi
callq 0x58f8
testb %al, %al
je 0x5f7b
movl $0xc, %ecx
leaq 0x60(%rsp), %rdi
movq 0x10(%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 0xd0(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
leaq 0x18(%rsp), %rdx
callq 0x4c60
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 0x578c
testq %r14, %rax
je 0x5d0e
movq 0xd0(%rsp), %rax
movl (%rax), %edi
callq 0x4139
movq %rbx, %rdi
callq 0x5244
movq 0x160(%rbx), %rdi
callq 0x28c9
cmpl %ebp, %eax
jge 0x5d3e
cmpl $0x0, 0x17c(%rbx)
je 0x5d3e
movq %rbx, %rdi
callq 0x5244
movq 0xd0(%rsp), %rax
movl (%rax), %edi
callq 0x4139
movq 0x168(%rbx), %rdi
callq 0x28c9
cmpl %r15d, %eax
jge 0x5d6f
cmpl $0x1, 0x17c(%rbx)
je 0x5d6f
movq %rbx, %rdi
callq 0x5244
movq 0xd0(%rsp), %rax
movl (%rax), %edi
callq 0x4139
movq 0x158(%rbx), %rdi
orq 0x128(%rbx), %rdi
callq 0x28c9
movl 0x4(%rsp), %edi
movq %rbx, %rsi
callq 0x5bc0
movq %rax, %r14
movq 0xd0(%rsp), %rax
movl (%rax), %esi
movq %rbx, %rdi
callq 0x536a
movq 0x160(%rbx), %rdi
callq 0x28c9
cmpl %ebp, %eax
jne 0x5dc4
movq 0x168(%rbx), %rdi
callq 0x28c9
cmpl %r15d, %eax
je 0x5dcc
movq %rbx, %rdi
callq 0x5244
addq %r13, %r14
xorl %r15d, %r15d
movq 0x60(%rsp,%r15,8), %rax
cmpq 0x100(%rbx,%r15,8), %rax
je 0x5de9
movq %rbx, %rdi
callq 0x5244
incq %r15
cmpq $0xc, %r15
jne 0x5dd2
movq 0x158(%rbx), %rdi
orq 0x128(%rbx), %rdi
callq 0x28c9
movq 0x170(%rbx), %rdi
callq 0x28c9
cmpl $0x20, %eax
jle 0x5f76
movl $0x12, %edx
movq %r12, %rdi
leaq 0x2591(%rip), %rsi      # 0x83ba
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 0x2577(%rip), %rsi      # 0x83cd
callq 0x2180
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x2220
movl $0x1, %edx
movq %rax, %rdi
leaq 0x24fa(%rip), %r15      # 0x8371
movq %r15, %rsi
callq 0x2180
movl $0x11, %edx
movq %r12, %rdi
leaq 0x2548(%rip), %rsi      # 0x83d6
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 0x2520(%rip), %rsi      # 0x83e8
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 0x24f8(%rip), %rsi      # 0x83fa
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 0xd0(%rsp), %rax
movl (%rax), %edi
callq 0x4139
movq %rbx, %rdi
callq 0x5244
movq %rbx, %rdi
callq 0x5a7c
movq 0x170(%rbx), %rdi
callq 0x3532
movq %r14, %r13
jmp 0x5f7e
movq %r13, %r14
movq 0xd0(%rsp), %rcx
addq $0x8, %rcx
movq %rcx, 0xd0(%rsp)
cmpq 0xc8(%rsp), %rcx
jne 0x5c6b
jmp 0x5fad
movl $0x1, %r14d
jmp 0x5fad
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8d8, %rsp            # imm = 0x8D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq