// File: /workspace/github_cpp_makefile_star2_O3/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 $0x998, %rsp            # imm = 0x998
movq %rsi, %rbx
callq 0x2310
movq %rax, %r15
movq %rax, %rdi
movl $0x23, %esi
callq 0x2070
testq %rax, %rax
je 0xe170
movb $0x0, (%rax)
movq %r15, %rdi
movl $0x2b, %esi
callq 0x2070
testq %rax, %rax
je 0xe185
movb $0x0, (%rax)
leaq 0x1ed53(%rip), %rsi     # 0x2cedf
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xe24d
leaq 0x1ed42(%rip), %rsi     # 0x2cee5
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xe24d
leaq 0x1ed31(%rip), %rsi     # 0x2ceeb
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xe24d
leaq 0x1ed0c(%rip), %rsi     # 0x2cedd
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xe2c8
leaq 0x1ecfb(%rip), %rsi     # 0x2cee3
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xe2c8
leaq 0x1ecea(%rip), %rsi     # 0x2cee9
movq %r15, %rdi
callq 0x2260
testl %eax, %eax
je 0xe2c8
movq %r15, %rdi
callq 0x2080
movzbl -0x1(%r15,%rax), %edx
movl $0x4, %ecx
xorl %ebp, %ebp
cmpl $0x61, %edx
jg 0xe2f3
cmpl $0x50, %edx
jg 0xe309
cmpl $0x42, %edx
je 0xe321
cmpl $0x4e, %edx
je 0xe302
jmp 0xe335
cmpl $0x1, 0x138(%rbx)
jne 0xe267
movl $0x6, %edi
movl $0x3c, %esi
movl $0x3e, %edx
jmp 0xe276
movl $0x6, %edi
movl $0x4, %esi
movl $0x6, %edx
callq 0x775c
leaq 0x18(%rsp), %r14
movq %rax, (%r14)
movq %r14, %rdi
callq 0x784f
movq (%r14), %r14
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x78e0
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xe682
testb %al, %al
je 0xe366
movq 0x18(%rsp), %r14
jmp 0xe369
cmpl $0x1, 0x138(%rbx)
jne 0xe2e2
movl $0x6, %edi
movl $0x3c, %esi
movl $0x3a, %edx
jmp 0xe276
movl $0x6, %edi
movl $0x4, %esi
movl $0x2, %edx
jmp 0xe276
cmpl $0x70, %edx
jg 0xe315
cmpl $0x62, %edx
je 0xe321
cmpl $0x6e, %edx
jne 0xe335
movl $0x2, %ecx
jmp 0xe32d
cmpl $0x51, %edx
je 0xe328
cmpl $0x52, %edx
je 0xe32d
jmp 0xe335
cmpl $0x71, %edx
je 0xe328
cmpl $0x72, %edx
je 0xe32d
jmp 0xe335
movl $0x3, %ecx
jmp 0xe32d
movl $0x5, %ecx
movb $0x0, -0x1(%r15,%rax)
movl %ecx, %ebp
movq %r15, %rdi
movl $0x3d, %esi
callq 0x2070
testq %rax, %rax
je 0xe34a
movb $0x0, (%rax)
movq %r15, %rdi
callq 0x2080
cmpq $0x2, %rax
jb 0xe366
movzbl -0x1(%r15,%rax), %edx
leal -0x39(%rdx), %ecx
cmpb $-0x8, %cl
jae 0xe37e
xorl %r14d, %r14d
movq %r14, %rax
addq $0x998, %rsp            # imm = 0x998
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, -0x1(%r15,%rax)
movb -0x2(%r15,%rax), %sil
leal -0x69(%rsi), %ecx
xorl %r14d, %r14d
cmpb $-0x8, %cl
jb 0xe369
movl %ebp, 0x24(%rsp)
movb $0x0, -0x2(%r15,%rax)
movzbl (%r15), %eax
movb $0x1, %bpl
leal -0x4b(%rax), %ecx
cmpl $0x7, %ecx
ja 0xe3c5
leaq 0x1e904(%rip), %rax     # 0x2ccb8
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x6, %r14d
jmp 0xe3f0
cmpl $0x42, %eax
jne 0xe3f5
movl $0x3, %r14d
jmp 0xe3f0
movl $0x4, %r14d
jmp 0xe3f0
movl $0x1, %r14d
jmp 0xe3f0
movl $0x2, %r14d
jmp 0xe3f0
movl $0x5, %r14d
incq %r15
xorl %ebp, %ebp
movq %rsi, 0x28(%rsp)
movq %rdx, 0x8(%rsp)
movq %r15, %rdi
callq 0x2080
cmpb $0x78, -0x1(%rax,%r15)
jne 0xe415
movb $0x0, -0x1(%r15,%rax)
movb (%r15), %al
testb %al, %al
je 0xe44b
addb $-0x61, %al
cmpb $0x8, %al
adcq $0x0, %r15
cmpb $0x8, %al
movzbl %al, %r12d
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmovael %r13d, %r12d
movzbl (%r15), %eax
leal -0x31(%rax), %ecx
cmpb $0x7, %cl
ja 0xe457
movl $0x38, %r13d
subl %eax, %r13d
jmp 0xe457
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testb %bpl, %bpl
je 0xe47d
movl $0x1, %r14d
testl %r13d, %r13d
js 0xe47d
testl %r12d, %r12d
js 0xe47d
leal (%r12,%r13,8), %eax
movl (%rbx,%rax,4), %eax
movl %eax, %r14d
negl %r14d
cmovsl %eax, %r14d
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x78e0
cmpq %rax, %r15
jae 0xe366
movq %rax, %rbx
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rax
shll $0x3, %eax
movq 0x28(%rsp), %rcx
addb $-0x61, %cl
andl $0x78, %eax
movzbl %cl, %ebp
subl %eax, %ebp
addl $0x40, %ebp
movq $0x0, 0x8(%rsp)
testl %r13d, %r13d
js 0xe4e9
movq (%r15), %rdi
callq 0x7854
movl %eax, %edi
callq 0xca69
cmpl %eax, %r13d
jne 0xe53d
testl %r12d, %r12d
js 0xe502
movq (%r15), %rdi
callq 0x7854
movl %eax, %edi
callq 0xca3e
cmpl %eax, %r12d
jne 0xe53d
movq (%r15), %rdi
callq 0x785b
cmpl %ebp, %eax
jne 0xe53d
movq (%r15), %rdi
callq 0x7865
cmpl %r14d, %eax
jne 0xe53d
movq (%r15), %rdi
callq 0x786f
cmpl 0x24(%rsp), %eax
jne 0xe53d
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movq (%r15), %rax
movq %rax, 0x8(%rsp)
addq $0x8, %r15
cmpq %rbx, %r15
jb 0xe4d0
xorl %r14d, %r14d
cmpl $0x1, 0x10(%rsp)
cmoveq 0x8(%rsp), %r14
jmp 0xe369