// File: /workspace/github_cpp_makefile_star2_O1/vygr[P]C-Chess/chess.cpp
// Function: piece_moves(std::vector<score_board, std::allocator<score_board>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int, int, std::vector<move, std::allocator<move>> const&, unsigned long&)
auto piece_moves(score_boards &yield, board brd, unsigned int index, int colour, const moves &moves, std::size_t &king_index)
{
	auto piece = brd[index];
	auto promote = std::string("qrbn");
	if (colour == white) promote = "QRBN";
	auto cx = int(index % 8);
	auto cy = int(index / 8);
	for (auto &move : moves)
 	{
		auto dx = move.dx;
		auto dy = move.dy;
		auto length = move.length;
		auto flag = move.flag;
		auto x = cx;
		auto y = cy;
		//special length for pawns so we can adjust for starting 2 hop
		if (length == 0)
		{
			length = 1;
			if (piece == 'p')
			{
				if (y == 1) length = 2;
			}
			else
			{
				if (y == 6) length = 2;
			}
		}
		for (;length > 0; --length)
		{
			x += dx;
			y += dy;
			if ((x < 0) || (x >= 8) || (y < 0) || (y >= 8))
			{
				//gone off the board
				break;
			}
			auto newindex = y*8 + x;
			auto newpiece = brd[newindex];
			auto newtype = piece_type[newpiece];
			if (newtype == colour)
			{
				//hit one of our own piece type (black hit black etc)
				break;
			}
			if ((flag == no_capture) && (newtype != empty))
			{
				//not suposed to capture and not empty square
				break;
			}
			if ((flag == must_capture) && (newtype == empty))
			{
				//must capture and got empty square
				break;
			}
			brd[index] = ' ';
			if ((y == 0 || y == 7) && (piece == 'P' || piece == 'p'))
			{
				//try all the pawn promotion possibilities
				for (auto &promote_piece : promote)
				{
					brd[newindex] = promote_piece;
					if (!in_check(brd, colour, king_index)) yield.push_back(score_board{evaluate(brd, colour), 0, brd});
				}
			}
			else
			{
				//generate this as a possible move
				brd[newindex] = piece;
				if (!in_check(brd, colour, king_index)) yield.push_back(score_board{evaluate(brd, colour), 0, brd});
			}
			brd[index] = piece;
			brd[newindex] = newpiece;
			if ((flag == may_capture) && (newtype != empty))
			{
				//may capture and we did so !
				break;
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, 0x68(%rsp)
movq %r8, %r14
movl %ecx, %ebx
movq %rsi, %r15
movq %rdi, 0x60(%rsp)
movl %edx, %ebp
movl %edx, %r13d
movq (%rsi), %rax
movb (%rax,%r13), %al
movb %al, 0x5(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x670b(%rip), %rsi      # 0x9c1e
leaq 0x6708(%rip), %rdx      # 0x9c22
leaq 0x70(%rsp), %rdi
callq 0x6c98
movl %ebx, 0x8(%rsp)
cmpl $0x1, %ebx
jne 0x354a
leaq 0x70(%rsp), %rdi
movq 0x8(%rdi), %rdx
leaq 0x66e6(%rip), %rcx      # 0x9c23
movl $0x4, %r8d
xorl %esi, %esi
callq 0x2250
movq (%r14), %rdx
movq 0x8(%r14), %rax
movq %rax, 0x90(%rsp)
cmpq %rax, %rdx
je 0x3821
movl %ebp, %eax
andl $0x7, %eax
movl %eax, 0x38(%rsp)
shrl $0x3, %ebp
xorl %eax, %eax
cmpl $0x6, %ebp
sete %al
xorl %esi, %esi
movl %ebp, 0x3c(%rsp)
cmpl $0x1, %ebp
sete %sil
movb 0x5(%rsp), %cl
movl %ecx, %edi
andb $-0x21, %dil
movb %dil, 0x7(%rsp)
cmpb $0x70, %cl
cmovnel %eax, %esi
incl %esi
movl %esi, 0x40(%rsp)
movq %r13, 0x98(%rsp)
movq %rdx, 0x58(%rsp)
movl 0x8(%rdx), %r14d
testl %r14d, %r14d
cmovel 0x40(%rsp), %r14d
testl %r14d, %r14d
jle 0x380a
movq 0x58(%rsp), %rax
movl (%rax), %ecx
movl %ecx, 0x50(%rsp)
movl 0x4(%rax), %ecx
movl %ecx, 0x4c(%rsp)
movl 0xc(%rax), %ebp
movl %ebp, %eax
xorl $0x2, %eax
movl %eax, 0x48(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, %r12d
movl 0x38(%rsp), %edx
movl %ebp, 0x44(%rsp)
addl 0x50(%rsp), %edx
cmpl $0x7, %edx
ja 0x380a
addl 0x4c(%rsp), %r12d
cmpl $0x7, %r12d
ja 0x380a
leal (,%r12,8), %ebx
movl %edx, 0x54(%rsp)
orl %edx, %ebx
movq (%r15), %rax
movb (%rax,%rbx), %al
movb %al, 0x6(%rsp)
leaq 0xab7f(%rip), %rdi      # 0xe1a8
leaq 0x6(%rsp), %rsi
callq 0x6372
movl (%rax), %eax
movl %eax, 0xc(%rsp)
cmpl 0x8(%rsp), %eax
je 0x380a
testl %ebp, %ebp
sete %cl
movl 0xc(%rsp), %esi
testl %esi, %esi
setne %dl
movl 0x48(%rsp), %eax
orl %esi, %eax
sete %al
testb %dl, %cl
jne 0x380a
testb %al, %al
jne 0x380a
movq (%r15), %rax
movb $0x20, (%rax,%r13)
cmpl $0x7, %r12d
movq %r14, 0xa0(%rsp)
je 0x3689
testl %r12d, %r12d
jne 0x373b
cmpb $0x50, 0x7(%rsp)
jne 0x373b
movq 0x78(%rsp), %rbp
testq %rbp, %rbp
je 0x37bb
movq 0x70(%rsp), %r14
xorl %r13d, %r13d
movb (%r14,%r13), %al
movq (%r15), %rcx
movb %al, (%rcx,%rbx)
movq %r15, %rdi
movl 0x8(%rsp), %esi
movq 0x68(%rsp), %rdx
callq 0x3324
testb %al, %al
jne 0x372a
movq %r15, %rdi
movl 0x8(%rsp), %esi
callq 0x3416
movl %eax, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x18(%rsp), %rdi
callq 0x6a42
movq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x7cf8
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x372a
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
incq %r13
cmpq %r13, %rbp
jne 0x36aa
jmp 0x37bb
movq (%r15), %rax
movb 0x5(%rsp), %cl
movb %cl, (%rax,%rbx)
movq %r15, %rdi
movl 0x8(%rsp), %esi
movq 0x68(%rsp), %rdx
callq 0x3324
testb %al, %al
jne 0x37bb
movq %r15, %rdi
movl 0x8(%rsp), %esi
callq 0x3416
movl %eax, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x18(%rsp), %rdi
callq 0x6a42
movq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x7cf8
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x37bb
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
cmpl $0x0, 0xc(%rsp)
setne %al
movl 0x44(%rsp), %ebp
cmpl $0x1, %ebp
sete %cl
movq (%r15), %rdx
movq 0x98(%rsp), %r13
movb 0x5(%rsp), %sil
movb %sil, (%rdx,%r13)
movb 0x6(%rsp), %dl
movq (%r15), %rsi
movb %dl, (%rsi,%rbx)
testb %al, %cl
movq 0xa0(%rsp), %rcx
movl 0x54(%rsp), %edx
jne 0x380a
leal -0x1(%rcx), %eax
cmpl $0x2, %ecx
movl %eax, %r14d
jge 0x35ee
movq 0x58(%rsp), %rdx
addq $0x10, %rdx
cmpq 0x90(%rsp), %rdx
jne 0x35a6
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x3843
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21a0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3880
jmp 0x385d
jmp 0x3880
jmp 0x3880
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x3883
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x3883
jmp 0x3880
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x38a5
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22c0