// File: /workspace/github_cpp_makefile_star2_O1/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 $0x208, %rsp            # imm = 0x208
movq %rsi, %r15
movq %rdi, %rbx
callq 0x5e1c
leaq 0x158(%rbx), %rax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x158(%rbx)
movq $0x0, 0x168(%rbx)
leaq 0x80(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x6e64
leaq 0x8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl $0x20, %edx
callq 0x15d76
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0x8a39
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x42b0
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
cmpq $0xc0, %rcx
jne 0x8dd0
leaq 0x60(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x50(%rsp), %rdi
callq 0x6e64
leaq 0xb0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x5e56
leaq 0xb0(%rsp), %rsi
movl $0x158, %edx            # imm = 0x158
movq %rbx, %rdi
callq 0x4230
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
je 0x8aae
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x42b0
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movsbl (%rax), %edi
callq 0x8442
movl %eax, 0x170(%rbx)
leaq 0x178(%rbx), %rdi
movl $0x8000, %edx           # imm = 0x8000
xorl %esi, %esi
callq 0x4150
leaq 0x220b9(%rip), %rbp     # 0x2ab98
movl 0x8(%rbp), %eax
movl %eax, 0xb8(%rsp)
movq (%rbp), %rax
movq %rax, 0xb0(%rsp)
movl $0xffffffff, 0xbc(%rsp) # imm = 0xFFFFFFFF
movaps 0x10049(%rip), %xmm0    # 0x18b50
movups %xmm0, 0xc8(%rsp)
movw $0x0, 0xd8(%rsp)
movq $0x0, 0xe0(%rsp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0xc0(%rsp)
movq 0x8(%rsp), %rax
movq 0x48(%rax), %rcx
testq %rcx, %rcx
je 0x8ba5
movq 0x40(%rax), %rdx
movl 0xc0(%rsp), %esi
xorl %edi, %edi
movzbl (%rdx,%rdi), %r8d
cmpl $0x6a, %r8d
jg 0x8b71
cmpl $0x4b, %r8d
je 0x8b85
cmpl $0x51, %r8d
jne 0x8b9d
movl $0x2, %r8d
jmp 0x8b93
cmpl $0x6b, %r8d
je 0x8b8d
cmpl $0x71, %r8d
jne 0x8b9d
movl $0x8, %r8d
jmp 0x8b93
movl $0x1, %r8d
jmp 0x8b93
movl $0x4, %r8d
orl %r8d, %esi
movl %esi, 0xc0(%rsp)
incq %rdi
cmpq %rdi, %rcx
jne 0x8b52
leaq 0x40(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rdi
callq 0x6e64
leaq 0x30(%rsp), %rdi
callq 0x11eb0
movl %eax, 0xbc(%rsp)
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x8beb
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42b0
movq 0x8(%rsp), %rax
movq 0x80(%rax), %r15
callq 0x4040
movq %rax, %r12
movl (%rax), %r14d
movl $0x0, (%rax)
leaq 0x28(%rsp), %r13
movq %r15, %rdi
movq %r13, %rsi
movl $0xa, %edx
callq 0x43a0
cmpq %r15, (%r13)
je 0x8e3d
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x8e49
movl (%r12), %ecx
cmpl $0x22, %ecx
je 0x8e49
testl %ecx, %ecx
jne 0x8c48
movl %r14d, (%r12)
movl %eax, 0xcc(%rsp)
movq 0x8(%rsp), %rax
movq 0xa0(%rax), %r15
movl (%r12), %r14d
movl $0x0, (%r12)
leaq 0x28(%rsp), %r13
movq %r15, %rdi
movq %r13, %rsi
movl $0xa, %edx
callq 0x43a0
cmpq %r15, (%r13)
je 0x8e55
leaq -0x80000000(%rax), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
cmpq %rdx, %rcx
jb 0x8e61
movl (%r12), %ecx
cmpl $0x22, %ecx
je 0x8e61
testl %ecx, %ecx
jne 0x8cb5
movl %r14d, (%r12)
movl %eax, 0xd0(%rsp)
movq $-0x1, 0xc4(%rsp)
movq (%rbp), %rax
movq %rax, 0xb0(%rsp)
movl 0x8(%rbp), %eax
movl %eax, 0xb8(%rsp)
movq $0x0, 0xe0(%rsp)
movq 0x150(%rbx), %rax
movq %rax, 0xe0(%rsp)
movl 0xbc(%rsp), %edi
callq 0x167f0
xorq %rax, 0xe0(%rsp)
movl 0xc0(%rsp), %edi
callq 0x167b8
xorq %rax, 0xe0(%rsp)
movw $0x0, 0xd8(%rsp)
movl 0x170(%rbx), %esi
movq %rbx, %rdi
callq 0xf818
movzbl %al, %eax
movl %eax, 0xd4(%rsp)
cmpl $0x1, 0x170(%rbx)
jne 0x8d59
callq 0x16856
xorq %rax, 0xe0(%rsp)
movq 0x160(%rbx), %rsi
cmpq 0x168(%rbx), %rsi
je 0x8da2
movq 0xe0(%rsp), %rax
movq %rax, 0x30(%rsi)
movups 0xb0(%rsp), %xmm0
movups 0xc0(%rsp), %xmm1
movups 0xd0(%rsp), %xmm2
movups %xmm2, 0x20(%rsi)
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x38, 0x160(%rbx)
jmp 0x8db4
leaq 0xb0(%rsp), %rdx
movq 0x20(%rsp), %rdi
callq 0xf8da
leaq 0x8(%rsp), %rdi
callq 0x6e08
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x4110
movq %rax, %r12
leaq 0xa0(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0xfd80(%rip), %rsi      # 0x18b70
leaq 0xfda2(%rip), %rdx      # 0x18b99
leaq 0x90(%rsp), %rdi
callq 0x6fca
movq 0x10(%rsp), %rdx
subq 0x8(%rsp), %rdx
sarq $0x5, %rdx
movb $0x1, %bpl
leaq 0x90(%rsp), %rsi
movq %r12, %rdi
callq 0x6c2a
xorl %ebp, %ebp
movq 0x1819a(%rip), %rsi     # 0x20fc8
movq 0x181b3(%rip), %rdx     # 0x20fe8
movq %r12, %rdi
callq 0x44c0
leaq 0x10340(%rip), %rdi     # 0x19184
callq 0x4160
leaq 0x10334(%rip), %rdi     # 0x19184
callq 0x43d0
leaq 0x10328(%rip), %rdi     # 0x19184
callq 0x4160
leaq 0x1031c(%rip), %rdi     # 0x19184
callq 0x43d0
movq %rax, %r15
movq 0x90(%rsp), %rdi
cmpq %r14, %rdi
je 0x8e95
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x42b0
jmp 0x8e95
movq %rax, %r15
movb $0x1, %bpl
testb %bpl, %bpl
je 0x8f11
movq %r12, %rdi
callq 0x41e0
jmp 0x8f11
movq %rax, %r15
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x8f11
movq 0x40(%rsp), %rsi
jmp 0x8ecc
jmp 0x8f0e
movq %rax, %r15
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
je 0x8f11
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x42b0
jmp 0x8f11
jmp 0x8f0e
movq %rax, %r15
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0x8f1b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x42b0
jmp 0x8f1b
movq %rax, %r15
jmp 0x8f1b
jmp 0x8efe
movq %rax, %r15
cmpl $0x0, (%r12)
jne 0x8f11
movl %r14d, (%r12)
jmp 0x8f11
movq %rax, %r15
leaq 0x8(%rsp), %rdi
callq 0x6e08
movq 0x20(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8f37
movq 0x168(%rbx), %rsi
subq %rdi, %rsi
callq 0x42b0
movq %r15, %rdi
callq 0x44f0
nop