// File: 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:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rsi, 0x78(%rsp)
movq %rdi, %rax
movq 0x78(%rsp), %rdi
movq %rdi, 0x80(%rsp)
movq %rax, 0x1b0(%rsp)
movq %rdi, 0x1a8(%rsp)
movl %edx, 0x1a4(%rsp)
movl %ecx, 0x1a0(%rsp)
movq %r8, 0x198(%rsp)
movq %r9, 0x190(%rsp)
movl 0x1a4(%rsp), %eax
movl %eax, %esi
callq 0x23c0
movb (%rax), %al
movb %al, 0x18f(%rsp)
leaq 0x167(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0x2390
movq 0x88(%rsp), %rdx
leaq 0x160d5(%rip), %rsi     # 0x1ac4a
leaq 0x168(%rsp), %rdi
callq 0x22e0
jmp 0x4b84
leaq 0x167(%rsp), %rdi
callq 0x2260
cmpl $0x1, 0x1a0(%rsp)
jne 0x4bf2
leaq 0x160ad(%rip), %rsi     # 0x1ac4f
leaq 0x168(%rsp), %rdi
callq 0x22d0
jmp 0x4bb1
jmp 0x4bf2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0x167(%rsp), %rdi
callq 0x2260
jmp 0x521a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x520d
movl 0x1a4(%rsp), %eax
andl $0x7, %eax
movl %eax, 0x150(%rsp)
movl 0x1a4(%rsp), %eax
shrl $0x3, %eax
movl %eax, 0x14c(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rdi
callq 0xa440
movq %rax, 0x138(%rsp)
movq 0x140(%rsp), %rdi
callq 0xa470
movq %rax, 0x130(%rsp)
leaq 0x138(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0xa4a0
testb $0x1, %al
jne 0x4c6c
jmp 0x51f8
leaq 0x138(%rsp), %rdi
callq 0xa4e0
movq %rax, 0x128(%rsp)
movq 0x128(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x124(%rsp)
movq 0x128(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x120(%rsp)
movq 0x128(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x11c(%rsp)
movq 0x128(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x118(%rsp)
movl 0x150(%rsp), %eax
movl %eax, 0x114(%rsp)
movl 0x14c(%rsp), %eax
movl %eax, 0x110(%rsp)
cmpl $0x0, 0x11c(%rsp)
jne 0x4d36
movl $0x1, 0x11c(%rsp)
movsbl 0x18f(%rsp), %eax
cmpl $0x70, %eax
jne 0x4d1d
cmpl $0x1, 0x110(%rsp)
jne 0x4d1b
movl $0x2, 0x11c(%rsp)
jmp 0x4d34
cmpl $0x6, 0x110(%rsp)
jne 0x4d32
movl $0x2, 0x11c(%rsp)
jmp 0x4d34
jmp 0x4d36
jmp 0x4d38
cmpl $0x0, 0x11c(%rsp)
jle 0x51e4
movl 0x124(%rsp), %eax
addl 0x114(%rsp), %eax
movl %eax, 0x114(%rsp)
movl 0x120(%rsp), %eax
addl 0x110(%rsp), %eax
movl %eax, 0x110(%rsp)
cmpl $0x0, 0x114(%rsp)
jl 0x4d98
cmpl $0x8, 0x114(%rsp)
jge 0x4d98
cmpl $0x0, 0x110(%rsp)
jl 0x4d98
cmpl $0x8, 0x110(%rsp)
jl 0x4d9d
jmp 0x51e4
movq 0x80(%rsp), %rdi
movl 0x110(%rsp), %eax
movl 0x114(%rsp), %edx
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,8), %eax
movl %eax, 0x10c(%rsp)
movslq 0x10c(%rsp), %rsi
callq 0x23c0
movq %rax, 0x70(%rsp)
jmp 0x4dd5
movq 0x70(%rsp), %rax
movb (%rax), %al
movb %al, 0x10b(%rsp)
leaq 0x21426(%rip), %rdi     # 0x26210
leaq 0x10b(%rsp), %rsi
callq 0xa4f0
movq %rax, 0x68(%rsp)
jmp 0x4dfe
movq 0x68(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x104(%rsp)
movl 0x104(%rsp), %eax
cmpl 0x1a0(%rsp), %eax
jne 0x4e21
jmp 0x51e4
cmpl $0x0, 0x118(%rsp)
jne 0x4e3a
cmpl $0x0, 0x104(%rsp)
je 0x4e3a
jmp 0x51e4
cmpl $0x2, 0x118(%rsp)
jne 0x4e53
cmpl $0x0, 0x104(%rsp)
jne 0x4e53
jmp 0x51e4
movq 0x80(%rsp), %rdi
movl 0x1a4(%rsp), %eax
movl %eax, %esi
callq 0x23c0
movq %rax, 0x60(%rsp)
jmp 0x4e70
movq 0x60(%rsp), %rax
movb $0x20, (%rax)
cmpl $0x0, 0x110(%rsp)
je 0x4e90
cmpl $0x7, 0x110(%rsp)
jne 0x5043
movsbl 0x18f(%rsp), %eax
cmpl $0x50, %eax
je 0x4eae
movsbl 0x18f(%rsp), %eax
cmpl $0x70, %eax
jne 0x5043
leaq 0x168(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x21c0
movq %rax, 0xf0(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x2120
movq %rax, 0xe8(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0xa5e0
testb $0x1, %al
jne 0x4f06
jmp 0x503e
leaq 0xf0(%rsp), %rdi
callq 0xa620
movq 0x80(%rsp), %rdi
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
movb (%rax), %al
movb %al, 0x57(%rsp)
movslq 0x10c(%rsp), %rsi
callq 0x23c0
movq %rax, 0x58(%rsp)
jmp 0x4f45
movq 0x80(%rsp), %rdi
movq 0x58(%rsp), %rax
movb 0x57(%rsp), %cl
movb %cl, (%rax)
movl 0x1a0(%rsp), %esi
movq 0x190(%rsp), %rdx
callq 0x4800
movb %al, 0x56(%rsp)
jmp 0x4f72
movb 0x56(%rsp), %al
testb $0x1, %al
jne 0x502a
jmp 0x4f80
movq 0x80(%rsp), %rdi
movq 0x1b0(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0xb8(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0x1a0(%rsp), %esi
callq 0x49d0
movl %eax, 0x50(%rsp)
jmp 0x4fb4
movq 0x80(%rsp), %rsi
movq 0x48(%rsp), %rax
movl 0x50(%rsp), %ecx
movl %ecx, (%rax)
movl $0x0, 0xbc(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x20e0
jmp 0x4fe1
movq 0x40(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0xa630
jmp 0x4ff5
leaq 0xb8(%rsp), %rdi
callq 0xa660
jmp 0x502a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xa660
jmp 0x520d
jmp 0x502c
leaq 0xf0(%rsp), %rdi
callq 0xa680
jmp 0x4ee8
jmp 0x5151
movq 0x80(%rsp), %rdi
movb 0x18f(%rsp), %al
movb %al, 0x37(%rsp)
movslq 0x10c(%rsp), %rsi
callq 0x23c0
movq %rax, 0x38(%rsp)
jmp 0x506a
movq 0x80(%rsp), %rdi
movq 0x38(%rsp), %rax
movb 0x37(%rsp), %cl
movb %cl, (%rax)
movl 0x1a0(%rsp), %esi
movq 0x190(%rsp), %rdx
callq 0x4800
movb %al, 0x36(%rsp)
jmp 0x5097
movb 0x36(%rsp), %al
testb $0x1, %al
jne 0x514f
jmp 0x50a5
movq 0x80(%rsp), %rdi
movq 0x1b0(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x28(%rsp)
movl 0x1a0(%rsp), %esi
callq 0x49d0
movl %eax, 0x30(%rsp)
jmp 0x50d9
movq 0x80(%rsp), %rsi
movq 0x28(%rsp), %rax
movl 0x30(%rsp), %ecx
movl %ecx, (%rax)
movl $0x0, 0x94(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x20e0
jmp 0x5106
movq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xa630
jmp 0x511a
leaq 0x90(%rsp), %rdi
callq 0xa660
jmp 0x514f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0x90(%rsp), %rdi
callq 0xa660
jmp 0x520d
jmp 0x5151
movq 0x80(%rsp), %rdi
movb 0x18f(%rsp), %al
movb %al, 0x17(%rsp)
movl 0x1a4(%rsp), %eax
movl %eax, %esi
callq 0x23c0
movq %rax, 0x18(%rsp)
jmp 0x5179
movq 0x80(%rsp), %rdi
movq 0x18(%rsp), %rax
movb 0x17(%rsp), %cl
movb %cl, (%rax)
movb 0x10b(%rsp), %al
movb %al, 0x7(%rsp)
movslq 0x10c(%rsp), %rsi
callq 0x23c0
movq %rax, 0x8(%rsp)
jmp 0x51ab
movq 0x8(%rsp), %rax
movb 0x7(%rsp), %cl
movb %cl, (%rax)
cmpl $0x1, 0x118(%rsp)
jne 0x51cc
cmpl $0x0, 0x104(%rsp)
je 0x51cc
jmp 0x51e4
jmp 0x51ce
movl 0x11c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x4d38
jmp 0x51e6
leaq 0x138(%rsp), %rdi
callq 0xa6a0
jmp 0x4c4e
leaq 0x168(%rsp), %rdi
callq 0x2140
addq $0x1b8, %rsp            # imm = 0x1B8
retq
leaq 0x168(%rsp), %rdi
callq 0x2140
movq 0x158(%rsp), %rdi
callq 0x2380
nopw (%rax,%rax)