// File: cpirc[P]Monochrome/src/move.cpp
// Function: lan_to_move(Position const&, char const*, unsigned int&)
bool lan_to_move(const Position& pos, const char* lan_str, Move& move) {
    assert(lan_str);

    Square from = (Square)((lan_str[0] - 'a') + ((lan_str[1] - '1') * 8)),
           to = (Square)((lan_str[2] - 'a') + ((lan_str[3] - '1') * 8));
    Piece promo = NO_PIECE;

    switch (lan_str[4]) {
        case '\0':
            break;
        case 'n':
            promo = KNIGHT;
            break;
        case 'b':
            promo = BISHOP;
            break;
        case 'r':
            promo = ROOK;
            break;
        case 'q':
            promo = QUEEN;
            break;
        default:
            break;
    }

    SearchStack ss[1];
    clear_ss(ss, 1);

    int movecount;
    movecount = generate(pos, ss->ml);

    Move current_move;
    while ((current_move = next_move(ss, movecount))) {
        if (pos.flipped) {
            current_move = flip_move(current_move);
        }

        if (from_square(current_move) == from &&
            to_square(current_move) == to) {
            // FIX ME: This is very ugly
            if (promo != NO_PIECE && promotion_type(current_move) != promo) {
                continue;
            }
            move = current_move;
            return true;
        }
    }

    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x850, %rsp            # imm = 0x850
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x5f30
jmp 0x5f4f
leaq 0xd989(%rip), %rdi      # 0x138c0
leaq 0xd98a(%rip), %rsi      # 0x138c8
movl $0xaa, %edx
leaq 0xd9b0(%rip), %rcx      # 0x138fa
callq 0x3150
movq -0x18(%rbp), %rax
movzbl (%rax), %edx
movzbl 0x1(%rax), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,8), %eax
addb $0x17, %al
movb %al, -0x21(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x2(%rax), %edx
movzbl 0x3(%rax), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,8), %eax
addb $0x17, %al
movb %al, -0x22(%rbp)
movb $0x6, -0x23(%rbp)
movq -0x18(%rbp), %rax
movsbl 0x4(%rax), %eax
movl %eax, -0x84c(%rbp)
testl %eax, %eax
je 0x5fca
jmp 0x5f96
movl -0x84c(%rbp), %eax
subl $0x62, %eax
je 0x5fd2
jmp 0x5fa3
movl -0x84c(%rbp), %eax
subl $0x6e, %eax
je 0x5fcc
jmp 0x5fb0
movl -0x84c(%rbp), %eax
subl $0x71, %eax
je 0x5fde
jmp 0x5fbd
movl -0x84c(%rbp), %eax
subl $0x72, %eax
je 0x5fd8
jmp 0x5fe4
jmp 0x5fe6
movb $0x1, -0x23(%rbp)
jmp 0x5fe6
movb $0x2, -0x23(%rbp)
jmp 0x5fe6
movb $0x3, -0x23(%rbp)
jmp 0x5fe6
movb $0x4, -0x23(%rbp)
jmp 0x5fe6
jmp 0x5fe6
leaq -0x840(%rbp), %rdi
movl $0x1, %esi
callq 0xb540
movq -0x10(%rbp), %rdi
leaq -0x840(%rbp), %rsi
addq $0x4, %rsi
callq 0x8b20
movl %eax, -0x844(%rbp)
leaq -0x840(%rbp), %rdi
leaq -0x844(%rbp), %rsi
callq 0xb1c0
movl %eax, -0x848(%rbp)
cmpl $0x0, %eax
je 0x60b5
movq -0x10(%rbp), %rax
testb $0x1, 0x41(%rax)
je 0x604e
movl -0x848(%rbp), %edi
callq 0x69d0
movl %eax, -0x848(%rbp)
movl -0x848(%rbp), %edi
callq 0x66b0
movzbl %al, %eax
movzbl -0x21(%rbp), %ecx
cmpl %ecx, %eax
jne 0x60b0
movl -0x848(%rbp), %edi
callq 0x66c0
movzbl %al, %eax
movzbl -0x22(%rbp), %ecx
cmpl %ecx, %eax
jne 0x60b0
movzbl -0x23(%rbp), %eax
cmpl $0x6, %eax
je 0x609e
movl -0x848(%rbp), %edi
callq 0x68f0
movzbl %al, %eax
movzbl -0x23(%rbp), %ecx
cmpl %ecx, %eax
je 0x609e
jmp 0x6011
movl -0x848(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
jmp 0x60b9
jmp 0x6011
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x850, %rsp            # imm = 0x850
popq %rbp
retq
nopw (%rax,%rax)