// File: /workspace/github_cpp_makefile_star2_O1/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 0xa(%rsp), %r12
movq %r12, %rdi
callq 0x5380
leaq 0x20(%rsp), %r15
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1a0ce
movq %rax, %r14
movq %r12, %rdi
movl $0x78, %esi
callq 0x5160
testq %rax, %rax
je 0x9306
cmpb $0x0, (%rax)
je 0x9306
incq %rax
movb (%rax), %cl
movb %cl, -0x1(%rax)
incq %rax
testb %cl, %cl
jne 0x92fa
movzbl 0xa(%rsp), %ecx
leal -0x4b(%rcx), %eax
cmpl $0x7, %eax
ja 0x932a
leaq 0x272c2(%rip), %rcx     # 0x305dc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x6, %edx
jmp 0x934f
cmpl $0x42, %ecx
jne 0x9336
movl $0x3, %edx
jmp 0x934f
xorl %edx, %edx
movb $0x1, %al
jmp 0x9351
movl $0x5, %edx
jmp 0x934f
movl $0x4, %edx
jmp 0x934f
movl $0x2, %edx
xorl %eax, %eax
testb %al, %al
movl $0x1, %ecx
cmovel %edx, %ecx
movl %eax, %edx
xorb $0x1, %dl
movzbl %dl, %edi
movsbl 0xa(%rsp,%rdi), %edx
leal -0x61(%rdx), %esi
leal -0x61(%rdx), %r8d
cmpb $0x8, %sil
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
cmovbl %r8d, %r9d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leal -0x31(%rdx), %r10d
addl $-0x31, %edx
cmpb $0x7, %r10b
cmovael %esi, %edx
movzbl %al, %eax
cmpl $-0x1, %edx
jne 0x93ad
movq %rax, %r10
negq %r10
movb 0xc(%rsp,%r10), %r10b
leal -0x69(%r10), %r11d
cmpb $-0x8, %r11b
jb 0x93c2
negq %rax
movsbl 0xc(%rsp,%rax), %r8d
addl $-0x61, %r8d
movb 0xc(%rsp,%rdi), %r10b
movl %r9d, %esi
cmpq %r14, %r15
je 0x94e4
movl %esi, 0x14(%rsp)
movsbl %r10b, %eax
leal -0x188(,%rax,8), %edi
orl %r8d, %edi
xorl %r8d, %r8d
cmpl $0x4f2d4f, 0xa(%rsp)    # imm = 0x4F2D4F
setne %r8b
xorl %r11d, %r11d
leaq 0x28(%rsp), %r10
movl $0x2d4f2d4f, %r9d       # imm = 0x2D4F2D4F
xorl %eax, %eax
movl -0x8(%r10), %ebp
movl %ebp, %r12d
shrl $0x6, %r12d
movl %r12d, %r15d
andl $0x3f, %r15d
movl %ebp, %r13d
andl $0x3f, %r13d
testl %r8d, %r8d
je 0x945c
movl 0xa(%rsp), %ebx
xorl %r9d, %ebx
movzwl 0xe(%rsp), %esi
xorl $0x4f, %esi
orl %ebx, %esi
je 0x947b
cmpl %edi, %r13d
jne 0x94b9
movq 0x18(%rsp), %rsi
movl (%rsi,%r15,4), %esi
andl $0x7, %esi
cmpl %esi, %ecx
jne 0x94b9
movl 0x14(%rsp), %esi
cmpl $-0x1, %esi
je 0x94a1
andl $0x7, %r12d
movl %esi, %ebx
xorl %esi, %esi
cmpl %ebx, %r12d
jmp 0x94af
movl %ebp, %r12d
notl %r12d
testl $0xc000, %r12d          # imm = 0xC000
setne %r12b
cmpl %r15d, %r13d
setbe %r15b
orb %r12b, %r15b
je 0x9494
jmp 0x94b9
movl %ebp, %esi
notl %esi
testl $0xc000, %esi           # imm = 0xC000
setne %sil
cmpl %r15d, %r13d
setae %bl
orb %sil, %bl
jne 0x94b9
movb $0x1, %r15b
movl $0x1, %r11d
movl %ebp, %eax
jmp 0x94bc
cmpl $-0x1, %edx
je 0x94d3
shrl $0x3, %r15d
xorl %esi, %esi
cmpl %edx, %r15d
sete %sil
cmovel %ebp, %eax
addl %esi, %r11d
xorl %r15d, %r15d
testb %r15b, %r15b
jne 0x94db
leaq 0x8(%r10), %rsi
cmpq %r14, %r10
movq %rsi, %r10
jne 0x93fc
jmp 0x94db
incl %r11d
xorl %r15d, %r15d
jmp 0x949d
cmpl $0x1, %r11d
sete %cl
jmp 0x94e8
xorl %eax, %eax
xorl %ecx, %ecx
testb %cl, %cl
jne 0x9530
movq 0x32aad(%rip), %rbx     # 0x3bfa0
leaq 0x2711c(%rip), %rsi     # 0x30616
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