// File: /workspace/github_cpp_makefile_star2_O3/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 $0x828, %rsp            # imm = 0x828
movq %rdi, %rbx
leaq 0xe(%rsp), %r12
movq %r12, %rdi
callq 0x5380
leaq 0x20(%rsp), %r14
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a5c0
movq %rax, %r15
movq %r12, %rdi
movl $0x78, %esi
callq 0x5160
testq %rax, %rax
je 0x982e
cmpb $0x0, (%rax)
je 0x982e
incq %rax
movb (%rax), %cl
movb %cl, -0x1(%rax)
incq %rax
testb %cl, %cl
jne 0x9822
movzbl 0xe(%rsp), %edx
movl $0x1, 0x8(%rsp)
movb $0x1, %al
leal -0x4b(%rdx), %esi
cmpl $0x7, %esi
ja 0x985f
leaq 0x27e10(%rip), %rdx     # 0x3165c
movslq (%rdx,%rsi,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
movl $0x6, 0x8(%rsp)
jmp 0x988a
cmpl $0x42, %edx
jne 0x988c
movl $0x3, 0x8(%rsp)
jmp 0x988a
movl $0x5, 0x8(%rsp)
jmp 0x988a
movl $0x4, 0x8(%rsp)
jmp 0x988a
movl $0x2, 0x8(%rsp)
xorl %eax, %eax
movl %eax, %edx
xorb $0x1, %dl
movzbl %dl, %r8d
movzbl 0xe(%rsp,%r8), %edi
leal -0x61(%rdi), %edx
leal -0x61(%rdi), %r9d
cmpb $0x8, %dl
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmovael %esi, %r9d
leal -0x31(%rdi), %r10d
movl %edi, %edx
addl $-0x31, %edx
cmpb $0x7, %r10b
cmovael %esi, %edx
movzbl %al, %r10d
cmpl $-0x1, %edx
jne 0x98db
movq %r10, %rax
negq %rax
movb 0x10(%rsp,%rax), %al
leal -0x69(%rax), %r11d
cmpb $-0x8, %r11b
jb 0x98eb
negq %r10
movb 0x10(%rsp,%r10), %dil
movb 0x10(%rsp,%r8), %al
movl %r9d, %esi
cmpq %r15, %r14
je 0x99e7
movsbl %dil, %r8d
addl $-0x61, %r8d
movsbl %al, %eax
leal -0x188(,%rax,8), %edi
orl %r8d, %edi
xorl %r8d, %r8d
cmpl $0x4f2d4f, 0xe(%rsp)    # imm = 0x4F2D4F
setne %r8b
xorl %r9d, %r9d
movl $0x2d4f2d4f, %r10d      # imm = 0x2D4F2D4F
xorl %r11d, %r11d
movl (%r14), %eax
movl %eax, %ebp
shrl $0x6, %ebp
movl %ebp, %r12d
andl $0x3f, %r12d
movl %eax, %r13d
andl $0x3f, %r13d
testl %r8d, %r8d
je 0x9977
movl 0xe(%rsp), %ebx
xorl %r10d, %ebx
movzwl 0x12(%rsp), %ecx
xorl $0x4f, %ecx
orl %ebx, %ecx
je 0x9996
cmpl %edi, %r13d
jne 0x99c7
movq 0x18(%rsp), %rcx
movl (%rcx,%r12,4), %ecx
andl $0x7, %ecx
cmpl %ecx, 0x8(%rsp)
jne 0x99c7
cmpl $-0x1, %esi
je 0x99af
andl $0x7, %ebp
xorl %ecx, %ecx
cmpl %esi, %ebp
jmp 0x99bd
movl %eax, %ebp
notl %ebp
testl $0xc000, %ebp           # imm = 0xC000
sete %bpl
cmpl %r12d, %r13d
seta %r12b
testb %r12b, %bpl
je 0x99c7
jmp 0x9a2b
movl %eax, %ecx
notl %ecx
testl $0xc000, %ecx           # imm = 0xC000
sete %cl
cmpl %r12d, %r13d
setb %bl
testb %bl, %cl
je 0x99c7
jmp 0x9a2b
cmpl $-0x1, %edx
je 0x99d6
shrl $0x3, %r12d
xorl %ecx, %ecx
cmpl %edx, %r12d
sete %cl
cmovel %eax, %r11d
addl %ecx, %r9d
addq $0x8, %r14
cmpq %r15, %r14
jne 0x9924
jmp 0x99de
incl %r9d
movl %eax, %r11d
jmp 0x99c7
movl %r11d, %eax
cmpl $0x1, %r9d
je 0x9a2b
movq 0x335b2(%rip), %rbx     # 0x3cfa0
leaq 0x27ca1(%rip), %rsi     # 0x31696
movl $0x25, %edx
movq %rbx, %rdi
callq 0x5460
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5050
movq %rax, %rdi
callq 0x5270
xorl %eax, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq