// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x988, %rsp            # imm = 0x988
movq %rsi, %rbx
callq 0x2310
movq %rax, %r15
pushq $0x23
popq %rsi
movq %rax, %rdi
callq 0x2070
testq %rax, %rax
je 0xd699
movb $0x0, (%rax)
pushq $0x2b
popq %rsi
movq %r15, %rdi
callq 0x2070
testq %rax, %rax
je 0xd6ac
movb $0x0, (%rax)
leaq 0x1d7dc(%rip), %rsi     # 0x2ae8f
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xd785
leaq 0x1d7cb(%rip), %rsi     # 0x2ae95
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xd785
leaq 0x1d7ba(%rip), %rsi     # 0x2ae9b
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xd785
leaq 0x1d795(%rip), %rsi     # 0x2ae8d
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xd7cc
leaq 0x1d784(%rip), %rsi     # 0x2ae93
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xd7cc
leaq 0x1d773(%rip), %rsi     # 0x2ae99
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xd7cc
movq %r15, %rdi
callq 0x2080
movzbl -0x1(%r15,%rax), %ecx
cmpl $0x42, %ecx
je 0xd781
cmpl $0x4e, %ecx
je 0xd7e9
cmpl $0x51, %ecx
je 0xd7ed
pushq $0x4
popq %rbp
cmpl $0x52, %ecx
je 0xd7f0
cmpl $0x72, %ecx
je 0xd7f0
cmpl $0x6e, %ecx
je 0xd7e9
cmpl $0x71, %ecx
je 0xd7ed
xorl %ebp, %ebp
cmpl $0x62, %ecx
jne 0xd7f6
pushq $0x3
jmp 0xd7ef
cmpl $0x1, 0x138(%rbx)
jne 0xd799
pushq $0x6
popq %rdi
pushq $0x3c
popq %rsi
pushq $0x3e
popq %rdx
jmp 0xd7a1
pushq $0x4
popq %rsi
pushq $0x6
popq %rdi
movl %edi, %edx
callq 0x7284
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
movq %r14, %rdi
callq 0x7366
movq (%r14), %rdi
movq %rbx, %rsi
callq 0xd9e8
testb %al, %al
je 0xd825
movq 0x20(%rsp), %r14
jmp 0xd828
cmpl $0x1, 0x138(%rbx)
jne 0xd7df
pushq $0x6
popq %rdi
pushq $0x3c
popq %rsi
pushq $0x3a
jmp 0xd796
pushq $0x6
popq %rdi
pushq $0x4
popq %rsi
pushq $0x2
jmp 0xd796
pushq $0x2
jmp 0xd7ef
pushq $0x5
popq %rbp
movb $0x0, -0x1(%r15,%rax)
pushq $0x3d
popq %rsi
movq %r15, %rdi
callq 0x2070
testq %rax, %rax
je 0xd809
movb $0x0, (%rax)
movq %r15, %rdi
callq 0x2080
cmpq $0x2, %rax
jb 0xd825
movzbl -0x1(%r15,%rax), %edx
leal -0x39(%rdx), %ecx
cmpb $-0x8, %cl
jae 0xd83d
xorl %r14d, %r14d
movq %r14, %rax
addq $0x988, %rsp            # imm = 0x988
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, -0x1(%r15,%rax)
movb -0x2(%r15,%rax), %r12b
leal -0x69(%r12), %ecx
xorl %r14d, %r14d
cmpb $-0x8, %cl
jb 0xd828
movb $0x0, -0x2(%r15,%rax)
movzbl (%r15), %eax
movb $0x1, %sil
leal -0x4b(%rax), %ecx
cmpl $0x7, %ecx
ja 0xd87e
leaq 0x1d3f7(%rip), %rax     # 0x2ac68
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
pushq $0x6
jmp 0xd895
cmpl $0x42, %eax
jne 0xd89c
pushq $0x3
jmp 0xd895
pushq $0x4
jmp 0xd895
pushq $0x1
jmp 0xd895
pushq $0x2
jmp 0xd895
pushq $0x5
popq %r14
incq %r15
xorl %esi, %esi
movl %esi, 0x8(%rsp)
shll $0x3, %edx
movq %rdx, %r13
addb $-0x61, %r12b
movq %r15, %rdi
callq 0x2080
cmpb $0x78, -0x1(%rax,%r15)
jne 0xd8c0
movb $0x0, -0x1(%r15,%rax)
movl %ebp, 0x14(%rsp)
movq %r13, %rdx
andl $0x78, %edx
movzbl %r12b, %r12d
movb (%r15), %al
testb %al, %al
movl 0x8(%rsp), %esi
je 0xd903
addb $-0x61, %al
cmpb $0x8, %al
adcq $0x0, %r15
cmpb $0x8, %al
movzbl %al, %ebp
pushq $-0x1
popq %r13
cmovael %r13d, %ebp
movzbl (%r15), %eax
leal -0x31(%rax), %ecx
cmpb $0x7, %cl
ja 0xd909
pushq $0x38
popq %r13
subl %eax, %r13d
jmp 0xd909
pushq $-0x1
popq %rbp
movl %ebp, %r13d
subl %edx, %r12d
testb %sil, %sil
je 0xd930
pushq $0x1
popq %r14
testl %r13d, %r13d
js 0xd930
testl %ebp, %ebp
js 0xd930
leal (%rbp,%r13,8), %eax
movl (%rbx,%rax,4), %eax
movl %eax, %r14d
negl %r14d
cmovsl %eax, %r14d
addl $0x40, %r12d
leaq 0x20(%rsp), %r15
pushq $0x1
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
movl %ecx, %edx
callq 0x73f8
movq %rax, %rbx
movq $0x0, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
cmpq %rbx, %r15
jae 0xd9d5
testl %r13d, %r13d
js 0xd97c
movq (%r15), %rdi
callq 0x736b
movl %eax, %edi
callq 0xc001
cmpl %eax, %r13d
jne 0xd9cf
testl %ebp, %ebp
js 0xd993
movq (%r15), %rdi
callq 0x736b
movl %eax, %edi
callq 0xbfd8
cmpl %eax, %ebp
jne 0xd9cf
movq (%r15), %rdi
callq 0x7372
cmpl %r12d, %eax
jne 0xd9cf
movq (%r15), %rdi
callq 0x737c
cmpl %r14d, %eax
jne 0xd9cf
movq (%r15), %rdi
callq 0x7386
cmpl 0x14(%rsp), %eax
jne 0xd9cf
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
movq (%r15), %rax
movq %rax, 0x18(%rsp)
addq $0x8, %r15
jmp 0xd95e
xorl %r14d, %r14d
cmpl $0x1, 0x8(%rsp)
cmoveq 0x18(%rsp), %r14
jmp 0xd828