// File: jswaff[P]prophet3/src/draw.cpp
// Function: is_lack_of_mating_material(position*)
bool is_lack_of_mating_material(position *pos) {
    assert(pos);

    int num_knights=0,num_white_sq_bishops=0,num_black_sq_bishops=0;

    for (int sq=0;sq<64;sq++) {
        switch(pos->piece[sq]) {
            // any position with a pawn, rook, or queen is not drawn by lack of mating material
            case PAWN:
            case -PAWN:
            case ROOK:
            case -ROOK:
            case QUEEN:
            case -QUEEN:
                return false;

            // a position with more than one knight is not drawn by lack of mating material
            case KNIGHT:
            case -KNIGHT:
                num_knights++;
                if (num_knights>1) return false;
                if (num_white_sq_bishops>0 || num_black_sq_bishops>0) return false;
                break;

            // a position with bishops on opposite colors is not drawn
            case BISHOP:
            case -BISHOP:
                if (is_light_sq((square_t)sq)) {
                    num_white_sq_bishops++;
                } else {
                    num_black_sq_bishops++;
                }
                if (num_white_sq_bishops>0 && num_black_sq_bishops>0) return false;
                if (num_knights>0) return false;
                break;
        }
    }

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x6195
jmp 0x61b4
leaq 0x2f3ad(%rip), %rdi     # 0x35549
leaq 0x2f3aa(%rip), %rsi     # 0x3554d
movl $0x18, %edx
leaq 0x2f3ab(%rip), %rcx     # 0x3555a
callq 0x20c0
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
cmpl $0x40, -0x20(%rbp)
jge 0x62bc
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
addl $0x5, %eax
subl $0x2, %eax
jb 0x623b
jmp 0x61f2
movl -0x24(%rbp), %eax
subl $-0x3, %eax
je 0x626a
jmp 0x61fc
movl -0x24(%rbp), %eax
subl $-0x2, %eax
je 0x6241
jmp 0x6206
movl -0x24(%rbp), %eax
subl $-0x1, %eax
je 0x623b
jmp 0x6210
movl -0x24(%rbp), %eax
subl $0x1, %eax
je 0x623b
jmp 0x621a
movl -0x24(%rbp), %eax
subl $0x2, %eax
je 0x6241
jmp 0x6224
movl -0x24(%rbp), %eax
subl $0x3, %eax
je 0x626a
jmp 0x622e
movl -0x24(%rbp), %eax
addl $-0x4, %eax
subl $0x1, %eax
ja 0x62ac
jmp 0x623b
movb $0x0, -0x1(%rbp)
jmp 0x62c0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
cmpl $0x1, -0x14(%rbp)
jle 0x6256
movb $0x0, -0x1(%rbp)
jmp 0x62c0
cmpl $0x0, -0x18(%rbp)
jg 0x6262
cmpl $0x0, -0x1c(%rbp)
jle 0x6268
movb $0x0, -0x1(%rbp)
jmp 0x62c0
jmp 0x62ac
movl -0x20(%rbp), %edi
callq 0x12850
testb $0x1, %al
jne 0x6278
jmp 0x6283
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x628c
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x18(%rbp)
jle 0x629e
cmpl $0x0, -0x1c(%rbp)
jle 0x629e
movb $0x0, -0x1(%rbp)
jmp 0x62c0
cmpl $0x0, -0x14(%rbp)
jle 0x62aa
movb $0x0, -0x1(%rbp)
jmp 0x62c0
jmp 0x62ac
jmp 0x62ae
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x61d0
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)