// File: /workspace/github_cpp_makefile_star2_O2/codeandkey[P]neocortex/src/position.cpp
// Function: neocortex::Position::Position(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
Position::Position(std::string fen) {
	std::vector<std::string> fields = util::split(fen, ' ');

	if (fields.size() != 6) {
		throw util::fmterr("Invalid FEN: expected 6 fields, parsed %d", fields.size());
	}

	board = Board(fields[0]);
	color_to_move = piece::color_from_uci(fields[1][0]);

	reset_history_table();
	
	State first_state;

	first_state.castle_rights = 0;

	for (auto c : fields[2]) {
		switch (c) {
		case 'K':
			first_state.castle_rights |= CASTLE_WHITE_K;
			break;
		case 'Q':
			first_state.castle_rights |= CASTLE_WHITE_Q;
			break;
		case 'k':
			first_state.castle_rights |= CASTLE_BLACK_K;
			break;
		case 'q':
			first_state.castle_rights |= CASTLE_BLACK_Q;
			break;
		}
	}

	first_state.en_passant_square = square::from_uci(fields[3]);
	first_state.halfmove_clock = std::stoi(fields[4]);
	first_state.fullmove_number = std::stoi(fields[5]);

	first_state.captured_piece = piece::null;
	first_state.captured_square = piece::null;
	first_state.last_move = Move::null;

	first_state.key = 0;
	first_state.key ^= board.get_tt_key();
	first_state.key ^= zobrist::en_passant(first_state.en_passant_square);
	first_state.key ^= zobrist::castle(first_state.castle_rights);
	first_state.was_castle = false;
	first_state.was_en_passant = false;

	first_state.in_check = test_check(color_to_move);

	if (color_to_move == piece::BLACK) {
		first_state.key ^= zobrist::black_to_move();
	}

	ply.push_back(first_state);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rsi, %r15
movq %rdi, %r14
callq 0x56a4
leaq 0x158(%r14), %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x158(%r14)
andq $0x0, 0x168(%r14)
leaq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0x43e0
movq %rsp, %rdi
leaq 0x58(%rsp), %rsi
pushq $0x20
popq %rdx
callq 0x11f0e
leaq 0x58(%rsp), %rdi
callq 0x4120
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
subq %rsi, %rax
cmpq $0xc0, %rax
jne 0x7f58
leaq 0x38(%rsp), %rdi
callq 0x43e0
leaq 0x98(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x56e0
leaq 0x98(%rsp), %rsi
movl $0x158, %edx            # imm = 0x158
movq %r14, %rdi
callq 0x4290
leaq 0x38(%rsp), %rdi
callq 0x4120
movq (%rsp), %rax
movq 0x20(%rax), %rax
movsbl (%rax), %edi
callq 0x78a2
movl %eax, 0x170(%r14)
movq %r14, %rdi
callq 0x7cc8
andl $0x0, 0xbc(%rsp)
andl $0x0, 0xa8(%rsp)
movq (%rsp), %rsi
movq 0x40(%rsi), %rax
movq 0x48(%rsi), %rcx
xorl %edx, %edx
pushq $0x1
popq %rdi
pushq $0x8
popq %r8
pushq $0x4
popq %r9
pushq $0x2
popq %r10
xorl %ebp, %ebp
cmpq %rdx, %rcx
je 0x7e49
movzbl (%rax,%rdx), %r11d
movl %edi, %r15d
cmpl $0x4b, %r11d
je 0x7e3a
cmpl $0x71, %r11d
je 0x7e32
cmpl $0x6b, %r11d
je 0x7e37
cmpl $0x51, %r11d
jne 0x7e44
movl %r10d, %r15d
jmp 0x7e3a
movl %r8d, %r15d
jmp 0x7e3a
movl %r9d, %r15d
orl %r15d, %ebp
movl %ebp, 0xa8(%rsp)
incq %rdx
jmp 0x7e08
addq $0x60, %rsi
leaq 0x18(%rsp), %rdi
callq 0x43e0
leaq 0x18(%rsp), %rdi
callq 0xf330
movl %eax, %r15d
movl %eax, 0xa4(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x4120
movq (%rsp), %rdi
subq $-0x80, %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0xce79
movl %eax, 0xb4(%rsp)
movl $0xa0, %edi
addq (%rsp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0xce79
movl %eax, 0xb8(%rsp)
orq $-0x1, 0xac(%rsp)
leaq 0x1ed10(%rip), %rax     # 0x26bc8
movq (%rax), %rcx
movq %rcx, 0x98(%rsp)
movl 0x8(%rax), %eax
movl %eax, 0xa0(%rsp)
movq 0x150(%r14), %r12
movl %r15d, %edi
callq 0x1279d
movq %rax, %r15
movl %ebp, %edi
callq 0x12767
xorq %r15, %r12
xorq %rax, %r12
movq %r12, 0xc8(%rsp)
andw $0x0, 0xc0(%rsp)
movl 0x170(%r14), %esi
movq %r14, %rdi
callq 0xce96
movzbl %al, %eax
movl %eax, 0xbc(%rsp)
cmpl $0x1, 0x170(%r14)
jne 0x7f30
callq 0x127e1
xorq %rax, %r12
movq %r12, 0xc8(%rsp)
leaq 0x98(%rsp), %rsi
movq %rbx, %rdi
callq 0xce44
movq %rsp, %rdi
callq 0x63ce
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x4150
movq %rax, %r15
leaq 0xcd76(%rip), %rsi      # 0x14ce0
leaq 0x78(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x6302
movq 0x8(%rsp), %rdx
subq (%rsp), %rdx
sarq $0x5, %rdx
movb $0x1, %bpl
leaq 0x78(%rsp), %rsi
movq %r15, %rdi
callq 0x6271
xorl %ebp, %ebp
movq 0x15026(%rip), %rsi     # 0x1cfc8
movq 0x1503f(%rip), %rdx     # 0x1cfe8
movq %r15, %rdi
callq 0x4520
movq %rax, %r14
leaq 0x78(%rsp), %rdi
callq 0x4120
testb %bpl, %bpl
jne 0x7fc8
jmp 0x8006
movq %rax, %r14
movq %r15, %rdi
callq 0x4240
jmp 0x8006
movq %rax, %r14
leaq 0x18(%rsp), %rdi
jmp 0x7fe6
jmp 0x8003
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x4120
jmp 0x8006
jmp 0x8003
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x4120
jmp 0x800e
movq %rax, %r14
jmp 0x800e
movq %rax, %r14
movq %rsp, %rdi
callq 0x63ce
movq %rbx, %rdi
callq 0xd174
movq %r14, %rdi
callq 0x4550