// File: /workspace/github_cpp_makefile_star2_O2/ATOMIC-LICHESS[P]ATOMKRAFT/create_book.cpp
// Function: san_to_move(Position&, char*)
Move san_to_move(Position& pos, char* s) {
	char str[10];
	strcpy(str, s);

	// generate legal moves
	MoveStack mlist[MAX_MOVES];
	MoveStack* last = generate<MV_LEGAL>(pos, mlist);


	// get rid of 'x' chars
	char* x = strchr(str, 'x');
	if (x) while (*x) *x++ = *(x+1);


	// get piece type
	int p = 1;
	PieceType pt = getPiece(str[0]);
	if (pt == PIECE_TYPE_NONE) {
		pt = PAWN;
		p = 0;
	}

	// there may be additional info regarding the file/rank
	File fromFile = getFile(str[p+0]);
	Rank fromRank = getRank(str[p+0]);
	Square to;

	// TODO: san can be like Bf3e4

	if (fromRank != RANK_NONE || getFile(str[p+1]) != FILE_NONE) {
		// san begins with a number e.g. B8d6 or with two letters e.g. Bfd6
		to = make_square(File(str[p+1] - 'a'), Rank(str[p+2] - '1'));
	} else {
		// san is like Bd6
		to = make_square(File(str[p+0] - 'a'), Rank(str[p+1] - '1'));
		fromFile = FILE_NONE;
	}


	// traverse legal moves and pick the right one
	Move m = MOVE_NONE;
	int possible_move_count = 0; // used to detect errors

	for (MoveStack* cur = mlist; cur != last; cur++) {
		Square sqfrom = move_from(cur->move);
		Square sqto = move_to(cur->move);

		if (strcmp(str, "O-O") == 0) {
			if (move_is_short_castle(cur->move)) {
				m = cur->move;
				possible_move_count = 1;
				break;
			}
		} else if (strcmp(str, "O-O-O") == 0) {
			if (move_is_long_castle(cur->move)) {
				m = cur->move;
				possible_move_count = 1;
				break;
			}
		} else {

			if (sqto == to && pt == pos.type_of_piece_on(sqfrom)) {
				if (fromFile != FILE_NONE) {
					if (square_file(sqfrom) == fromFile) {
						m = cur->move;
						++possible_move_count;
						//break;
					}
				} else if (fromRank != RANK_NONE) {
					if (square_rank(sqfrom) == fromRank) {
						m = cur->move;
						++possible_move_count;
						//break;
					}
				} else {
					m = cur->move;
					++possible_move_count;
					//break;
				}
			}
		}
	}


	if (possible_move_count != 1) {
		m = MOVE_NONE;
		cout << "incorrect SAN! (it may be ambiguous )" << endl;
	}


	return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x848, %rsp            # imm = 0x848
movq %rdi, %rbx
leaq 0x16(%rsp), %r12
movq %r12, %rdi
callq 0x5370
leaq 0x40(%rsp), %r14
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1543a
movq %rax, 0x38(%rsp)
pushq $0x78
popq %rsi
movq %r12, %rdi
callq 0x5150
testq %rax, %rax
je 0x8e64
movb (%rax), %cl
incq %rax
testb %cl, %cl
je 0x8e64
movb (%rax), %cl
movb %cl, -0x1(%rax)
jmp 0x8e56
movsbl 0x16(%rsp), %edi
callq 0x8dd5
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
cmpl $0x1, %eax
movl %eax, %edx
adcl $0x0, %edx
movl %edx, 0x28(%rsp)
movzbl 0x16(%rsp,%rcx), %edx
leal -0x61(%rdx), %edi
leal -0x61(%rdx), %esi
cmpb $0x8, %dil
pushq $-0x1
popq %r8
cmovael %r8d, %esi
leal -0x31(%rdx), %edi
movl %edx, %r9d
addl $-0x31, %r9d
cmpb $0x7, %dil
movl %r8d, 0x10(%rsp)
cmovael %r8d, %r9d
cmpl $-0x1, %r9d
jne 0x8ecd
cmpl $0x1, %eax
movq %r12, %rdi
sbbq $-0x1, %rdi
movb 0x1(%rdi), %dil
leal -0x69(%rdi), %r8d
cmpb $-0x8, %r8b
jb 0x8ee2
cmpl $0x1, %eax
sbbq $-0x1, %r12
movb 0x1(%r12), %dl
movb 0x18(%rsp,%rcx), %dil
movl %esi, 0x10(%rsp)
movsbl %dl, %eax
addl $-0x61, %eax
movsbl %dil, %ecx
leal -0x188(,%rcx,8), %ecx
orl %eax, %ecx
movl %ecx, 0x2c(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
cmpq 0x38(%rsp), %r14
je 0x9000
movl (%r14), %r15d
movl %r15d, %ebx
shrl $0x6, %ebx
movl %ebx, %r13d
andl $0x3f, %r13d
movl %r15d, %ebp
andl $0x3f, %ebp
cmpl $0x4f2d4f, 0x16(%rsp)   # imm = 0x4F2D4F
movl %r15d, %r12d
notl %r12d
je 0x8f8e
movq %r14, 0x20(%rsp)
movl %r9d, %r14d
leaq 0x16(%rsp), %rdi
leaq 0x1f5e2(%rip), %rsi     # 0x28530
pushq $0x6
popq %rdx
callq 0x53f0
testl %eax, %eax
je 0x8fa6
cmpl 0x2c(%rsp), %ebp
movl %r14d, %r9d
jne 0x8fc3
movq 0x30(%rsp), %rax
movl (%rax,%r13,4), %eax
andl $0x7, %eax
cmpl %eax, 0x28(%rsp)
movq 0x20(%rsp), %r14
jne 0x8fec
movl 0x10(%rsp), %eax
cmpl $-0x1, %eax
je 0x8fca
andl $0x7, %ebx
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %ebx
jmp 0x8fd9
testw $0xc000, %r12w          # imm = 0xC000
sete %al
cmpl %r13d, %ebp
seta %cl
testb %cl, %al
je 0x8fec
jmp 0x902a
testw $0xc000, %r12w          # imm = 0xC000
sete %al
cmpl %r13d, %ebp
setb %cl
testb %cl, %al
movl %r14d, %r9d
movq 0x20(%rsp), %r14
je 0x8fec
jmp 0x902a
movq 0x20(%rsp), %r14
jmp 0x8fec
cmpl $-0x1, %r9d
je 0x8ff5
shrl $0x3, %r13d
xorl %eax, %eax
cmpl %r9d, %r13d
sete %al
movl 0x8(%rsp), %ecx
cmovel %r15d, %ecx
movl %ecx, 0x8(%rsp)
addl %eax, 0xc(%rsp)
addq $0x8, %r14
jmp 0x8f09
incl 0xc(%rsp)
movl %r15d, 0x8(%rsp)
jmp 0x8fec
movl 0x8(%rsp), %r15d
cmpl $0x1, 0xc(%rsp)
je 0x902a
movq 0x2bf8d(%rip), %rdi     # 0x34fa0
leaq 0x1f51c(%rip), %rsi     # 0x28536
callq 0x53b0
movq %rax, %rdi
callq 0x51d0
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq