// File: SethKasmann[P]clever-girl/src/state.cpp
// Function: State::insufficientMaterial() const
bool State::insufficientMaterial() const
{
    bool ret = false;

// -------------------------------------------------------------------------- //
//                                                                            //
// First check that there are no pawns or major pieces on the board.          //
//                                                                            //
// -------------------------------------------------------------------------- //
    if (getPieceCount<pawn>() +
        getPieceCount<rook>() +
        getPieceCount<queen>() == 0)
    {
        switch (getPieceCount<knight>())
        {
// -------------------------------------------------------------------------- //
//                                                                            //
// If no knights are present, check if all remaining bishops are on the same  //
// color square.                                                              //
//                                                                            //
// -------------------------------------------------------------------------- //
            case 0:
                if ((getPieceBB<bishop>() & Dark_squares) == 
                        getPieceBB<bishop>() ||
                    (getPieceBB<bishop>() & Light_squares) == 
                        getPieceBB<bishop>())
                    ret = true;
                break;
// -------------------------------------------------------------------------- //
//                                                                            //
// If only 1 knight is present, check if there are no bishops on the board.   //
//                                                                            //
// -------------------------------------------------------------------------- //                
            case 1:
                if (!getPieceCount<bishop>())
                    ret = true;
                break;
// -------------------------------------------------------------------------- //
//                                                                            //
// For two knights, check if both sides have a knight and neither side has a  //
// bishop.                                                                    //
//                                                                            //
// -------------------------------------------------------------------------- //  
            case 2:
                if (getPieceCount<knight>(white) &&
                    getPieceCount<knight>(black) &&
                    !getPieceCount<bishop>())
                    ret = true;
                break;
            default:
                break;
        }
    }
    return ret;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x30(%rsp), %rdi
movq %rdi, 0x18(%rsp)
movb $0x0, 0x2f(%rsp)
callq 0x24990
movq 0x18(%rsp), %rdi
movl %eax, 0x24(%rsp)
callq 0x3b2c0
movq 0x18(%rsp), %rdi
movl %eax, %ecx
movl 0x24(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x28(%rsp)
callq 0x3b330
movl %eax, %ecx
movl 0x28(%rsp), %eax
addl %ecx, %eax
cmpl $0x0, %eax
jne 0x38ad5
movq 0x18(%rsp), %rdi
callq 0x3b3a0
movl %eax, %ecx
movl %ecx, 0x14(%rsp)
testl %eax, %eax
je 0x38a15
jmp 0x389f8
movl 0x14(%rsp), %eax
subl $0x1, %eax
je 0x38a80
jmp 0x38a03
movl 0x14(%rsp), %eax
subl $0x2, %eax
je 0x38a96
jmp 0x38ad1
movq 0x18(%rsp), %rdi
callq 0x3b410
movq 0x18(%rsp), %rdi
movabsq $-0x55aa55aa55aa55ab, %rcx # imm = 0xAA55AA55AA55AA55
andq %rcx, %rax
movq %rax, 0x8(%rsp)
callq 0x3b410
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
je 0x38a79
movq 0x18(%rsp), %rdi
callq 0x3b410
movq 0x18(%rsp), %rdi
movabsq $0x55aa55aa55aa55aa, %rcx # imm = 0x55AA55AA55AA55AA
andq %rcx, %rax
movq %rax, (%rsp)
callq 0x3b410
movq %rax, %rcx
movq (%rsp), %rax
cmpq %rcx, %rax
jne 0x38a7e
movb $0x1, 0x2f(%rsp)
jmp 0x38ad3
movq 0x18(%rsp), %rdi
callq 0x3b480
cmpl $0x0, %eax
jne 0x38a94
movb $0x1, 0x2f(%rsp)
jmp 0x38ad3
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x3b4f0
cmpl $0x0, %eax
je 0x38acf
movq 0x18(%rsp), %rdi
movl $0x1, %esi
callq 0x3b4f0
cmpl $0x0, %eax
je 0x38acf
movq 0x18(%rsp), %rdi
callq 0x3b480
cmpl $0x0, %eax
jne 0x38acf
movb $0x1, 0x2f(%rsp)
jmp 0x38ad3
jmp 0x38ad3
jmp 0x38ad5
movb 0x2f(%rsp), %al
andb $0x1, %al
addq $0x38, %rsp
retq