// File: jswaff[P]prophet3/src/utils.cpp
// Function: str_to_move(char const*, position*)
move str_to_move(const char* str_mv,position *pos) {
    char *my_mv = strdup(str_mv);

    // get rid of the mate and check designators if present
    char *p = strchr(my_mv,'#');
    if (p) *p = 0;
    p = strchr(my_mv,'+');
    if (p) *p = 0;

    // try short castle
    if (!strcmp(my_mv,"o-o") || !strcmp(my_mv,"O-O") || !strcmp(my_mv,"0-0")) {
        move mv;
        if (pos->player==WHITE) {
            mv = to_move(KING,E1,G1);
        } else {
            mv = to_move(KING,E8,G8);
        }
        set_castle(&mv);
        if (is_legal_move(mv,pos)) {
            return mv;
        } else {
            return BADMOVE;
        }
    }

    // try long castle
    if (!strcmp(my_mv,"o-o-o") || !strcmp(my_mv,"O-O-O") || !strcmp(my_mv,"0-0-0")) {
        move mv;
        if (pos->player==WHITE) {
            mv = to_move(KING,E1,C1);
        } else {
            mv = to_move(KING,E8,C8);
        }
        set_castle(&mv);
        if (is_legal_move(mv,pos)) {
            return mv;
        } else {
            return BADMOVE;
        }
    }

    // remove the promotion indicator if present and set the promotion piece.
    size_t len = strlen(my_mv);
    piece_t promo = NO_PIECE;

    switch(my_mv[len-1]) {
    case 'R':
    case 'r':
        promo = ROOK;
        break;
    case 'B':
    case 'b':
        promo = BISHOP;
        break;
    case 'N':
    case 'n':
        promo = KNIGHT;
        break;
    case 'Q':
    case 'q':
        promo = QUEEN;
        break;
    }

    if (promo != NO_PIECE) {
        my_mv[len-1]=0;
    }
    p = strchr(my_mv,'=');
    if (p) *p = 0;

    // translate the destination
    len = strlen(my_mv);
    if (len < 2) { return BADMOVE; }
    if (my_mv[len-1] < '1' || my_mv[len-1] > '8') {
        return BADMOVE;
    }
    int32 to_sq = 8*(8-(my_mv[len-1]-'0'));
    my_mv[len-1]=0;
    len--;

    if (my_mv[len-1] < 'a' || my_mv[len-1] > 'h') {
        return BADMOVE;
    }
    to_sq += my_mv[len-1]-'a';
    my_mv[len-1]=0;
    len--;

    // set the piece
    piece_t piece = NO_PIECE;
    if (my_mv[0]=='R') {
        piece = ROOK;
    } else if (my_mv[0]=='N') {
        piece = KNIGHT;
    } else if (my_mv[0]=='B') {
        piece = BISHOP;
    } else if (my_mv[0]=='Q') {
        piece = QUEEN;
    } else if (my_mv[0]=='K') {
        piece = KING;
    } else if (my_mv[0]=='P') {
        piece = PAWN;
    }

    if (piece != NO_PIECE) {
        my_mv++;
    }

    // is this a capture?
    if (my_mv[strlen(my_mv)-1]=='x') {
        my_mv[strlen(my_mv)-1] = 0;
    }

    // if anything is left, it must be the source/rank designator(s)
    int32 sfile = -1;
    if (strlen(my_mv) && my_mv[0] >= 'a' && my_mv[0] <= 'h') {
        sfile = my_mv[0] - 'a';
        my_mv++;
    }

    int32 srank = -1;
    if (strlen(my_mv) && my_mv[0] >= '1' && my_mv[0] <= '8') {
        srank = '8' - my_mv[0];
        my_mv++;
    }

    // if the piece didn't get set earlier, do it now
    if (piece==NO_PIECE) {
        if (srank > -1 && sfile > -1) {
            piece = (piece_t)pos->piece[(srank*8)+sfile];
            if (piece < 0) {
                piece = (piece_t)(0-piece);
            }
        } else {
            piece = PAWN;
        }
    }

    // check against the list of legal moves
    move moves[MAX_MOVES_PER_PLY];
    move *endp = gen_legal_moves(moves,pos,true,true);

    int32 matches = 0;
    move mv = BADMOVE;

    for (move *mp=moves;mp<endp;mp++) {
        // is this move a match?
        if (srank > -1 && srank != get_rank(get_from_sq(*mp))) {
            continue;
        }
        if (sfile > -1 && sfile != get_file(get_from_sq(*mp))) {
            continue;
        }
        if (get_to_sq(*mp)==to_sq && get_piece(*mp)==piece
                && get_promopiece(*mp)==promo) {
            matches++;
            mv = *mp;
        }
    }

    if (matches != 1) {
        mv = BADMOVE;
    }

    return mv;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa00, %rsp            # imm = 0xA00
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2310
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movl $0x23, %esi
callq 0x2070
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x14440
movq -0x28(%rbp), %rax
movb $0x0, (%rax)
movq -0x20(%rbp), %rdi
movl $0x2b, %esi
callq 0x2070
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x14460
movq -0x28(%rbp), %rax
movb $0x0, (%rax)
movq -0x20(%rbp), %rdi
leaq 0x24880(%rip), %rsi     # 0x38ceb
callq 0x2260
cmpl $0x0, %eax
je 0x1449f
movq -0x20(%rbp), %rdi
leaq 0x24871(%rip), %rsi     # 0x38cf1
callq 0x2260
cmpl $0x0, %eax
je 0x1449f
movq -0x20(%rbp), %rdi
leaq 0x24862(%rip), %rsi     # 0x38cf7
callq 0x2260
cmpl $0x0, %eax
jne 0x14511
movq -0x18(%rbp), %rax
cmpl $0x1, 0x138(%rax)
jne 0x144c6
movl $0x6, %edi
movl $0x3c, %esi
movl $0x3e, %edx
callq 0xa830
movq %rax, -0x30(%rbp)
jmp 0x144db
movl $0x6, %edx
movl $0x4, %esi
movl %edx, %edi
callq 0xa830
movq %rax, -0x30(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xaa60
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x14a90
testb $0x1, %al
jne 0x144f7
jmp 0x14504
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x14a7b
movq $0x0, -0x8(%rbp)
jmp 0x14a7b
movq -0x20(%rbp), %rdi
leaq 0x247cd(%rip), %rsi     # 0x38ce9
callq 0x2260
cmpl $0x0, %eax
je 0x14550
movq -0x20(%rbp), %rdi
leaq 0x247be(%rip), %rsi     # 0x38cef
callq 0x2260
cmpl $0x0, %eax
je 0x14550
movq -0x20(%rbp), %rdi
leaq 0x247af(%rip), %rsi     # 0x38cf5
callq 0x2260
cmpl $0x0, %eax
jne 0x145c5
movq -0x18(%rbp), %rax
cmpl $0x1, 0x138(%rax)
jne 0x14577
movl $0x6, %edi
movl $0x3c, %esi
movl $0x3a, %edx
callq 0xa830
movq %rax, -0x38(%rbp)
jmp 0x1458f
movl $0x6, %edi
movl $0x4, %esi
movl $0x2, %edx
callq 0xa830
movq %rax, -0x38(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xaa60
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x14a90
testb $0x1, %al
jne 0x145ab
jmp 0x145b8
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x14a7b
movq $0x0, -0x8(%rbp)
jmp 0x14a7b
movq -0x20(%rbp), %rdi
callq 0x2080
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %rax
movsbl -0x1(%rax,%rcx), %eax
addl $-0x42, %eax
movl %eax, %ecx
movq %rcx, -0x9e8(%rbp)
subl $0x30, %eax
ja 0x14630
movq -0x9e8(%rbp), %rax
leaq 0x245db(%rip), %rcx     # 0x38be0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x4, -0x44(%rbp)
jmp 0x14630
movl $0x3, -0x44(%rbp)
jmp 0x14630
movl $0x2, -0x44(%rbp)
jmp 0x14630
movl $0x5, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x14646
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
movq -0x20(%rbp), %rdi
movl $0x3d, %esi
callq 0x2070
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x14666
movq -0x28(%rbp), %rax
movb $0x0, (%rax)
movq -0x20(%rbp), %rdi
callq 0x2080
movq %rax, -0x40(%rbp)
cmpq $0x2, -0x40(%rbp)
jae 0x14687
movq $0x0, -0x8(%rbp)
jmp 0x14a7b
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x31, %eax
jl 0x146b1
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x38, %eax
jle 0x146be
movq $0x0, -0x8(%rbp)
jmp 0x14a7b
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %ecx
subl $0x30, %ecx
movl $0x8, %eax
subl %ecx, %eax
shll $0x3, %eax
movl %eax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x61, %eax
jl 0x14724
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x68, %eax
jle 0x14731
movq $0x0, -0x8(%rbp)
jmp 0x14a7b
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
subl $0x61, %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x52, %eax
jne 0x14782
movl $0x4, -0x4c(%rbp)
jmp 0x147f3
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x4e, %eax
jne 0x14797
movl $0x2, -0x4c(%rbp)
jmp 0x147f1
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x42, %eax
jne 0x147ac
movl $0x3, -0x4c(%rbp)
jmp 0x147ef
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x51, %eax
jne 0x147c1
movl $0x5, -0x4c(%rbp)
jmp 0x147ed
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x4b, %eax
jne 0x147d6
movl $0x6, -0x4c(%rbp)
jmp 0x147eb
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x50, %eax
jne 0x147e9
movl $0x1, -0x4c(%rbp)
jmp 0x147eb
jmp 0x147ed
jmp 0x147ef
jmp 0x147f1
jmp 0x147f3
cmpl $0x0, -0x4c(%rbp)
je 0x14805
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x9f0(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2080
movq %rax, %rcx
movq -0x9f0(%rbp), %rax
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x78, %eax
jne 0x14856
movq -0x20(%rbp), %rax
movq %rax, -0x9f8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2080
movq %rax, %rcx
movq -0x9f8(%rbp), %rax
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rdi
callq 0x2080
cmpq $0x0, %rax
je 0x1489d
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x61, %eax
jl 0x1489d
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x68, %eax
jg 0x1489d
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
subl $0x61, %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rdi
callq 0x2080
cmpq $0x0, %rax
je 0x148e8
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x31, %eax
jl 0x148e8
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x38, %eax
jg 0x148e8
movq -0x20(%rbp), %rax
movsbl (%rax), %ecx
movl $0x38, %eax
subl %ecx, %eax
movl %eax, -0x54(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x14929
cmpl $-0x1, -0x54(%rbp)
jle 0x14920
cmpl $-0x1, -0x50(%rbp)
jle 0x14920
movq -0x18(%rbp), %rax
movl -0x54(%rbp), %ecx
shll $0x3, %ecx
addl -0x50(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jge 0x1491e
xorl %eax, %eax
subl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x14927
movl $0x1, -0x4c(%rbp)
jmp 0x14929
leaq -0x9c0(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x1, %ecx
movl %ecx, %edx
callq 0xac70
movq %rax, -0x9c8(%rbp)
movl $0x0, -0x9cc(%rbp)
movq $0x0, -0x9d8(%rbp)
leaq -0x9c0(%rbp), %rax
movq %rax, -0x9e0(%rbp)
movq -0x9e0(%rbp), %rax
cmpq -0x9c8(%rbp), %rax
jae 0x14a5c
cmpl $-0x1, -0x54(%rbp)
jle 0x149b4
movl -0x54(%rbp), %eax
movl %eax, -0x9fc(%rbp)
movq -0x9e0(%rbp), %rax
movq (%rax), %rdi
callq 0xaa80
movl %eax, %edi
callq 0x120d0
movl %eax, %ecx
movl -0x9fc(%rbp), %eax
cmpl %ecx, %eax
je 0x149b4
jmp 0x14a45
cmpl $-0x1, -0x50(%rbp)
jle 0x149e7
movl -0x50(%rbp), %eax
movl %eax, -0xa00(%rbp)
movq -0x9e0(%rbp), %rax
movq (%rax), %rdi
callq 0xaa80
movl %eax, %edi
callq 0x12070
movl %eax, %ecx
movl -0xa00(%rbp), %eax
cmpl %ecx, %eax
je 0x149e7
jmp 0x14a45
movq -0x9e0(%rbp), %rax
movq (%rax), %rdi
callq 0xaaa0
cmpl -0x48(%rbp), %eax
jne 0x14a43
movq -0x9e0(%rbp), %rax
movq (%rax), %rdi
callq 0xaac0
cmpl -0x4c(%rbp), %eax
jne 0x14a43
movq -0x9e0(%rbp), %rax
movq (%rax), %rdi
callq 0xaae0
cmpl -0x44(%rbp), %eax
jne 0x14a43
movl -0x9cc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9cc(%rbp)
movq -0x9e0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x9d8(%rbp)
jmp 0x14a45
movq -0x9e0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x9e0(%rbp)
jmp 0x1496a
cmpl $0x1, -0x9cc(%rbp)
je 0x14a70
movq $0x0, -0x9d8(%rbp)
movq -0x9d8(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa00, %rsp            # imm = 0xA00
popq %rbp
retq
nopl (%rax,%rax)