// File: ATOMIC-LICHESS[P]ATOMKRAFT/bitbase.cpp
// Function: (anonymous namespace)::classify_wtm((anonymous namespace)::KPKPosition const&, (anonymous namespace)::Result const*)
Result classify_wtm(const KPKPosition& pos, const Result bb[]) {

    // If one move leads to a position classified as RESULT_LOSS, the result
    // of the current position is RESULT_WIN. If all moves lead to positions
    // classified as RESULT_DRAW, the current position is classified RESULT_DRAW
    // otherwise the current position is classified as RESULT_UNKNOWN.

    bool unknownFound = false;
    Bitboard b;
    Square s;
    Result r;

    // King moves
    b = pos.wk_attacks();
    while (b)
    {
        s = pop_1st_bit(&b);
        r = bb[compute_index(s, pos.blackKingSquare, pos.pawnSquare, BLACK)];

        if (r == RESULT_LOSS)
            return RESULT_WIN;

        if (r == RESULT_UNKNOWN)
            unknownFound = true;
    }

    // Pawn moves
    if (square_rank(pos.pawnSquare) < RANK_7)
    {
        s = pos.pawnSquare + DELTA_N;
        r = bb[compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK)];

        if (r == RESULT_LOSS)
            return RESULT_WIN;

        if (r == RESULT_UNKNOWN)
            unknownFound = true;

        // Double pawn push
        if (   square_rank(s) == RANK_3
            && s != pos.whiteKingSquare
            && s != pos.blackKingSquare)
        {
            s += DELTA_N;
            r = bb[compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK)];

            if (r == RESULT_LOSS)
                return RESULT_WIN;

            if (r == RESULT_UNKNOWN)
                unknownFound = true;
        }
    }
    return unknownFound ? RESULT_UNKNOWN : RESULT_DRAW;
  }

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movb $0x0, 0x2f(%rsp)
movq 0x38(%rsp), %rdi
callq 0xd6e0
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
je 0xd4f3
leaq 0x20(%rsp), %rdi
callq 0xd940
movl %eax, 0x1c(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x1c(%rsp), %edi
movq 0x38(%rsp), %rax
movl 0x4(%rax), %esi
movq 0x38(%rsp), %rax
movl 0x8(%rax), %edx
movl $0x1, %ecx
callq 0xceb0
movl %eax, %ecx
movq 0x10(%rsp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x18(%rsp)
cmpl $0x3, 0x18(%rsp)
jne 0xd4e5
movl $0x2, 0x44(%rsp)
jmp 0xd61b
cmpl $0x0, 0x18(%rsp)
jne 0xd4f1
movb $0x1, 0x2f(%rsp)
jmp 0xd482
movq 0x38(%rsp), %rax
movl 0x8(%rax), %edi
callq 0xce40
cmpl $0x6, %eax
jge 0xd606
movq 0x38(%rsp), %rax
movl 0x8(%rax), %edi
movl $0x8, %esi
callq 0xd780
movl %eax, 0x1c(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rax
movl (%rax), %edi
movq 0x38(%rsp), %rax
movl 0x4(%rax), %esi
movl 0x1c(%rsp), %edx
movl $0x1, %ecx
callq 0xceb0
movl %eax, %ecx
movq 0x8(%rsp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x18(%rsp)
cmpl $0x3, 0x18(%rsp)
jne 0xd56b
movl $0x2, 0x44(%rsp)
jmp 0xd61b
cmpl $0x0, 0x18(%rsp)
jne 0xd577
movb $0x1, 0x2f(%rsp)
movl 0x1c(%rsp), %edi
callq 0xce40
cmpl $0x2, %eax
jne 0xd604
movl 0x1c(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl (%rcx), %eax
je 0xd604
movl 0x1c(%rsp), %eax
movq 0x38(%rsp), %rcx
cmpl 0x4(%rcx), %eax
je 0xd604
leaq 0x1c(%rsp), %rdi
movl $0x8, %esi
callq 0xd7a0
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq 0x38(%rsp), %rax
movl (%rax), %edi
movq 0x38(%rsp), %rax
movl 0x4(%rax), %esi
movl 0x1c(%rsp), %edx
movl $0x1, %ecx
callq 0xceb0
movl %eax, %ecx
movq (%rsp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x18(%rsp)
cmpl $0x3, 0x18(%rsp)
jne 0xd5f6
movl $0x2, 0x44(%rsp)
jmp 0xd61b
cmpl $0x0, 0x18(%rsp)
jne 0xd602
movb $0x1, 0x2f(%rsp)
jmp 0xd604
jmp 0xd606
movb 0x2f(%rsp), %dl
movl $0x4, %eax
xorl %ecx, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)