// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rsi, %r15
movq %rdi, %r14
callq 0x5f30
leaq 0x158(%r14), %rax
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x158(%r14)
movq $0x0, 0x168(%r14)
leaq 0x90(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x80(%rsp), %rdi
movq %r14, 0x30(%rsp)
callq 0x6f6c
leaq 0x10(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl $0x20, %edx
callq 0x16006
movq 0x80(%rsp), %rdi
cmpq %rbx, %rdi
je 0x8afe
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x42b0
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
cmpq $0xc0, %rcx
jne 0x8e73
leaq 0x70(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %rdi
callq 0x6f6c
leaq 0xc0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x5f6a
leaq 0xc0(%rsp), %rsi
movl $0x158, %edx            # imm = 0x158
movq %r14, %rdi
callq 0x4230
movq 0x60(%rsp), %rdi
cmpq %rbx, %rdi
je 0x8b73
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x42b0
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movsbl (%rax), %edi
callq 0x8546
movl %eax, 0x170(%r14)
leaq 0x178(%r14), %rdi
xorl %ebp, %ebp
movl $0x8000, %edx           # imm = 0x8000
xorl %esi, %esi
callq 0x4150
leaq 0x22ff1(%rip), %rcx     # 0x2bb98
movl 0x8(%rcx), %eax
movl %eax, 0xc8(%rsp)
movq (%rcx), %rax
movq %rax, 0xc0(%rsp)
movaps 0x10fcd(%rip), %xmm0    # 0x19b90
movups %xmm0, 0xd4(%rsp)
movl %ebp, 0xe4(%rsp)
movw $0x0, 0xe8(%rsp)
movq $0x0, 0xf0(%rsp)
movl %ebp, 0xd0(%rsp)
movq 0x10(%rsp), %rax
movq 0x48(%rax), %rcx
testq %rcx, %rcx
je 0x8c4d
movq 0x40(%rax), %rdx
xorl %esi, %esi
xorl %ebp, %ebp
movzbl (%rdx,%rsi), %edi
cmpl $0x6a, %edi
jg 0x8c1f
cmpl $0x4b, %edi
je 0x8c30
cmpl $0x51, %edi
jne 0x8c45
movl $0x2, %edi
jmp 0x8c3c
cmpl $0x6b, %edi
je 0x8c37
cmpl $0x71, %edi
jne 0x8c45
movl $0x8, %edi
jmp 0x8c3c
movl $0x1, %edi
jmp 0x8c3c
movl $0x4, %edi
orl %edi, %ebp
movl %ebp, 0xd0(%rsp)
incq %rsi
cmpq %rsi, %rcx
jne 0x8c05
leaq 0x50(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x6f6c
leaq 0x40(%rsp), %rdi
callq 0x12144
movl %eax, %r15d
movl %eax, 0xcc(%rsp)
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x8c96
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x42b0
movq 0x10(%rsp), %rax
movq 0x80(%rax), %r13
callq 0x4040
movq %rax, %r12
movl (%rax), %eax
movl %eax, 0xc(%rsp)
movl $0x0, (%r12)
leaq 0x38(%rsp), %r14
movq %r13, %rdi
movq %r14, %rsi
movl $0xa, %edx
callq 0x43a0
cmpq %r13, (%r14)
je 0x8ee0
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x8eec
movl (%r12), %ebx
cmpl $0x22, %ebx
je 0x8eec
testl %ebx, %ebx
jne 0x8cfb
movl 0xc(%rsp), %ebx
movl %ebx, (%r12)
movl %eax, 0xdc(%rsp)
movq 0x10(%rsp), %rax
movq 0xa0(%rax), %r14
movl $0x0, (%r12)
leaq 0x38(%rsp), %r13
movq %r14, %rdi
movq %r13, %rsi
movl $0xa, %edx
callq 0x43a0
cmpq %r14, (%r13)
je 0x8ef8
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
leaq -0x80000000(%rax), %rdx
cmpq %rcx, %rdx
jb 0x8f04
movl (%r12), %ecx
cmpl $0x22, %ecx
je 0x8f04
testl %ecx, %ecx
jne 0x8d64
movl %ebx, (%r12)
movl %eax, 0xe0(%rsp)
movq $-0x1, 0xd4(%rsp)
leaq 0x22e1a(%rip), %rcx     # 0x2bb98
movq (%rcx), %rax
movq %rax, 0xc0(%rsp)
movl 0x8(%rcx), %eax
movl %eax, 0xc8(%rsp)
movq 0x30(%rsp), %r14
movq 0x150(%r14), %rbx
movl %r15d, %edi
callq 0x16ac0
movq %rax, %r15
movl %ebp, %edi
callq 0x16a88
xorq %r15, %rbx
xorq %rax, %rbx
movq %rbx, 0xf0(%rsp)
movw $0x0, 0xe8(%rsp)
movl 0x170(%r14), %esi
movq %r14, %rdi
callq 0xfba6
movzbl %al, %eax
movl %eax, 0xe4(%rsp)
cmpl $0x1, 0x170(%r14)
jne 0x8dfc
callq 0x16b26
xorq %rax, %rbx
movq %rbx, 0xf0(%rsp)
movq 0x160(%r14), %rsi
cmpq 0x168(%r14), %rsi
je 0x8e45
movq 0xf0(%rsp), %rax
movq %rax, 0x30(%rsi)
movups 0xc0(%rsp), %xmm0
movups 0xd0(%rsp), %xmm1
movups 0xe0(%rsp), %xmm2
movups %xmm2, 0x20(%rsi)
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x38, 0x160(%r14)
jmp 0x8e57
leaq 0xc0(%rsp), %rdx
movq 0x28(%rsp), %rdi
callq 0xfc68
leaq 0x10(%rsp), %rdi
callq 0x6f0e
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x4110
movq %rax, %r12
leaq 0xb0(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x10d9d(%rip), %rsi     # 0x19c30
leaq 0x10dbf(%rip), %rdx     # 0x19c59
leaq 0xa0(%rsp), %rdi
callq 0x70d8
movq 0x18(%rsp), %rdx
subq 0x10(%rsp), %rdx
sarq $0x5, %rdx
movb $0x1, %bpl
leaq 0xa0(%rsp), %rsi
movq %r12, %rdi
callq 0x6d30
xorl %ebp, %ebp
movq 0x190f7(%rip), %rsi     # 0x21fc8
movq 0x19110(%rip), %rdx     # 0x21fe8
movq %r12, %rdi
callq 0x44c0
leaq 0x1135d(%rip), %rdi     # 0x1a244
callq 0x4160
leaq 0x11351(%rip), %rdi     # 0x1a244
callq 0x43d0
leaq 0x11345(%rip), %rdi     # 0x1a244
callq 0x4160
leaq 0x11339(%rip), %rdi     # 0x1a244
callq 0x43d0
movq %rax, %r15
movq 0xa0(%rsp), %rdi
cmpq %rbx, %rdi
je 0x8f30
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x42b0
testb %bpl, %bpl
jne 0x8f3d
jmp 0x8fcc
movq %rax, %r15
movq %r12, %rdi
callq 0x41e0
jmp 0x8fcc
movq %rax, %r15
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x8fcc
movq 0x50(%rsp), %rsi
jmp 0x8f72
jmp 0x8fc9
movq %rax, %r15
movq 0x60(%rsp), %rdi
cmpq %rbx, %rdi
je 0x8fcc
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x42b0
jmp 0x8fcc
jmp 0x8fc9
movq %rax, %r15
movq 0x80(%rsp), %rdi
cmpq %rbx, %rdi
je 0x8fd6
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x42b0
jmp 0x8fd6
movq %rax, %r15
jmp 0x8fd6
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x8fcc
movl %ebx, (%r12)
jmp 0x8fcc
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x8fcc
movl 0xc(%rsp), %eax
movl %eax, (%r12)
jmp 0x8fcc
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x6f0e
movq 0x28(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8ff7
movq 0x30(%rsp), %rax
movq 0x168(%rax), %rsi
subq %rdi, %rsi
callq 0x42b0
movq %r15, %rdi
callq 0x44f0
nop