// File: /workspace/github_cpp_makefile_star2_O1/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 $0x9a8, %rsp            # imm = 0x9A8
movq %rsi, %rbx
callq 0x2310
movq %rax, %r14
movq %rax, %rdi
movl $0x23, %esi
callq 0x2070
testq %rax, %rax
je 0xe1c6
movb $0x0, (%rax)
movq %r14, %rdi
movl $0x2b, %esi
callq 0x2070
testq %rax, %rax
je 0xe1db
movb $0x0, (%rax)
leaq 0x1ec7d(%rip), %rsi     # 0x2ce5f
movq %r14, %rdi
callq 0x2260
testl %eax, %eax
je 0xe2a0
leaq 0x1ec6c(%rip), %rsi     # 0x2ce65
movq %r14, %rdi
callq 0x2260
testl %eax, %eax
je 0xe2a0
leaq 0x1ec5b(%rip), %rsi     # 0x2ce6b
movq %r14, %rdi
callq 0x2260
testl %eax, %eax
je 0xe2a0
leaq 0x1ec36(%rip), %rsi     # 0x2ce5d
movq %r14, %rdi
callq 0x2260
testl %eax, %eax
je 0xe31b
leaq 0x1ec25(%rip), %rsi     # 0x2ce63
movq %r14, %rdi
callq 0x2260
testl %eax, %eax
je 0xe31b
leaq 0x1ec14(%rip), %rsi     # 0x2ce69
movq %r14, %rdi
callq 0x2260
testl %eax, %eax
je 0xe31b
movq %r14, %rdi
callq 0x2080
movzbl -0x1(%r14,%rax), %edx
xorl %ebp, %ebp
movb $0x1, %cl
cmpl $0x61, %edx
jg 0xe346
cmpl $0x50, %edx
jg 0xe35c
cmpl $0x42, %edx
je 0xe379
cmpl $0x4e, %edx
je 0xe355
jmp 0xe387
cmpl $0x1, 0x138(%rbx)
jne 0xe2ba
movl $0x6, %edi
movl $0x3c, %esi
movl $0x3e, %edx
jmp 0xe2c9
movl $0x6, %edi
movl $0x4, %esi
movl $0x6, %edx
callq 0x76b8
leaq 0x18(%rsp), %r14
movq %rax, (%r14)
movq %r14, %rdi
callq 0x77ab
movq (%r14), %r14
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x7840
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xe727
testb %al, %al
je 0xe3db
movq 0x18(%rsp), %rbx
jmp 0xe3dd
cmpl $0x1, 0x138(%rbx)
jne 0xe335
movl $0x6, %edi
movl $0x3c, %esi
movl $0x3a, %edx
jmp 0xe2c9
movl $0x6, %edi
movl $0x4, %esi
movl $0x2, %edx
jmp 0xe2c9
cmpl $0x70, %edx
jg 0xe368
cmpl $0x62, %edx
je 0xe379
cmpl $0x6e, %edx
jne 0xe387
movl $0x2, %ebp
jmp 0xe385
cmpl $0x51, %edx
je 0xe380
cmpl $0x52, %edx
je 0xe372
jmp 0xe387
cmpl $0x71, %edx
je 0xe380
cmpl $0x72, %edx
jne 0xe387
movl $0x4, %ebp
jmp 0xe385
movl $0x3, %ebp
jmp 0xe385
movl $0x5, %ebp
xorl %ecx, %ecx
testb %cl, %cl
jne 0xe391
movb $0x0, -0x1(%r14,%rax)
movq %r14, %rdi
movl $0x3d, %esi
callq 0x2070
testq %rax, %rax
je 0xe3a6
movb $0x0, (%rax)
movq %r14, %rdi
callq 0x2080
cmpq $0x2, %rax
jb 0xe3db
movzbl -0x1(%r14,%rax), %edx
leal -0x39(%rdx), %ecx
cmpb $-0x8, %cl
jb 0xe3db
movb $0x0, -0x1(%r14,%rax)
movsbl -0x2(%r14,%rax), %ecx
movq %rcx, 0x10(%rsp)
addl $-0x69, %ecx
cmpb $-0x8, %cl
jae 0xe3f2
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x9a8, %rsp            # imm = 0x9A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, -0x2(%r14,%rax)
movzbl (%r14), %ecx
leal -0x4b(%rcx), %eax
cmpl $0x7, %eax
movq %rdx, 0x38(%rsp)
ja 0xe423
leaq 0x1e828(%rip), %rcx     # 0x2cc38
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x6, 0xc(%rsp)
jmp 0xe465
cmpl $0x42, %ecx
jne 0xe432
movl $0x3, 0xc(%rsp)
jmp 0xe465
movl $0x0, 0xc(%rsp)
movb $0x1, %r15b
jmp 0xe468
movl $0x4, 0xc(%rsp)
jmp 0xe465
movl $0x1, 0xc(%rsp)
jmp 0xe465
movl $0x2, 0xc(%rsp)
jmp 0xe465
movl $0x5, 0xc(%rsp)
xorl %r15d, %r15d
movl %ebp, 0x24(%rsp)
movl %r15d, %eax
xorb $0x1, %al
movzbl %al, %eax
addq %rax, %r14
movq %r14, %rdi
callq 0x2080
cmpb $0x78, -0x1(%rax,%r14)
jne 0xe48d
movb $0x0, -0x1(%r14,%rax)
movb (%r14), %al
addb $-0x61, %al
cmpb $0x8, %al
adcq $0x0, %r14
cmpb $0x8, %al
movzbl %al, %r13d
movl $0xff, %eax
cmovael %eax, %r13d
movsbl %r13b, %ecx
movq %rcx, 0x28(%rsp)
movb (%r14), %cl
movb $0x38, %dl
subb %cl, %dl
addb $-0x31, %cl
cmpb $0x8, %cl
movzbl %dl, %r12d
cmovael %eax, %r12d
movsbl %r12b, %eax
movq %rax, 0x30(%rsp)
testb %r15b, %r15b
je 0xe500
movl $0x1, 0xc(%rsp)
testb %r12b, %r12b
js 0xe500
testb %r13b, %r13b
js 0xe500
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
leal (%rax,%rcx,8), %eax
movl (%rbx,%rax,4), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movl %ecx, 0xc(%rsp)
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x7840
xorl %ebx, %ebx
cmpq %rax, %r14
jae 0xe3dd
movq %rax, %r15
movq 0x38(%rsp), %rax
shll $0x3, %eax
andl $0x78, %eax
movq 0x10(%rsp), %rbp
subl %eax, %ebp
addl $-0x21, %ebp
movq $0x0, 0x10(%rsp)
testb %r12b, %r12b
js 0xe560
movq (%r14), %rdi
callq 0x77b0
movl %eax, %edi
callq 0xca75
cmpl 0x30(%rsp), %eax
jne 0xe5ac
testb %r13b, %r13b
js 0xe57a
movq (%r14), %rdi
callq 0x77b0
movl %eax, %edi
callq 0xca4a
cmpl 0x28(%rsp), %eax
jne 0xe5ac
movq (%r14), %rdi
callq 0x77b7
cmpl %ebp, %eax
jne 0xe5ac
movq (%r14), %rdi
callq 0x77c1
cmpl 0xc(%rsp), %eax
jne 0xe5ac
movq (%r14), %rdi
callq 0x77cb
cmpl 0x24(%rsp), %eax
jne 0xe5ac
incl %ebx
movq (%r14), %rax
movq %rax, 0x10(%rsp)
addq $0x8, %r14
cmpq %r15, %r14
jb 0xe546
xorl %eax, %eax
cmpl $0x1, %ebx
cmoveq 0x10(%rsp), %rax
movq %rax, %rbx
jmp 0xe3dd