// File: /workspace/github_c_makefile_star2_O1/upcrob[P]garry/moves.c
// Function: executeMove(char*, Move*, Move*)
void executeMove(char* board, Move* move, Move* prevMove)
{
	char pieceId = getPieceAt(board, move->startRow, move->startCol);
	if (move->startRow == CASTLE && move->endRow == KINGSIDE)
	{
		//castle kingside
		if (isBlack(move->pieceId) == TRUE)
		{
			//black
			setPieceAt(board, 7, 4, EMPTY);
			setPieceAt(board, 7, 7, EMPTY);
			setPieceAt(board, 7, 6, BLACK + KING);
			setPieceAt(board, 7, 5, BLACK + ROOK);
			blackKCastleInvalid = TRUE;
			blackQCastleInvalid = TRUE;
		} else {
			//white
			setPieceAt(board, 0, 4, EMPTY);
			setPieceAt(board, 0, 7, EMPTY);
			setPieceAt(board, 0, 6, KING);
			setPieceAt(board, 0, 5, ROOK);
			whiteKCastleInvalid = TRUE;
			whiteQCastleInvalid = TRUE;
		}
	} else if (move->startRow == CASTLE && move->endRow == QUEENSIDE) {
		//castle queenside
		if (isBlack(move->pieceId) == TRUE)
		{
			//black
			setPieceAt(board, 7, 0, EMPTY);
			setPieceAt(board, 7, 4, EMPTY);
			setPieceAt(board, 7, 2, BLACK + KING);
			setPieceAt(board, 7, 3, BLACK + ROOK);
			blackQCastleInvalid = TRUE;
			blackKCastleInvalid = TRUE;
		} else {
			//white
			setPieceAt(board, 0, 0, EMPTY);
			setPieceAt(board, 0, 4, EMPTY);
			setPieceAt(board, 0, 2, KING);
			setPieceAt(board, 0, 3, ROOK);
			whiteQCastleInvalid = TRUE;
			whiteQCastleInvalid = TRUE;
		}
	} else {
		if (pieceId == KING)
		{
			whiteKCastleInvalid = TRUE;
			whiteQCastleInvalid = TRUE;
		} else if (pieceId == BLACK + KING) {
			blackKCastleInvalid = TRUE;
			blackQCastleInvalid = TRUE;
		} else if (pieceId == ROOK) {
			if (move->startRow == 0 && move->startCol == 0)
			{
				whiteQCastleInvalid = TRUE;
			} else if (move->startRow == 0 && move->startCol == 7) {
				whiteKCastleInvalid = TRUE;
			} else if (move->startRow == 7 && move->startCol == 0) {
				blackQCastleInvalid = TRUE;
			} else if (move->startRow == 7 && move->startCol == 7) {
				blackKCastleInvalid = TRUE;
			}
		} else if (pieceId == PAWN) {
			//check for white en passant case
			if (move->startRow == 4 && move->endRow == 5 && move->startCol != move->endCol)
			{
				if (prevMove->pieceId == PAWN + BLACK && prevMove->startCol == prevMove->endCol &&
					prevMove->endRow == 4 && (prevMove->endCol == move->startCol + 1 ||
											  prevMove->endCol == move->startCol - 1))
				{
					//capture pawn
					setPieceAt(board, 4, prevMove->endCol, EMPTY);
				}
			}
		} else if (pieceId == PAWN + BLACK) {
			//check for black en passant case
			if (move->startRow == 3 && move->endRow == 2 && move->startCol != move->endCol)
			{
				if (prevMove->pieceId == PAWN && prevMove->startCol == prevMove->endCol &&
					prevMove->endRow == 3 && (prevMove->endCol == move->startCol + 1 ||
											  prevMove->endCol == move->startCol - 1))
				{
					//capture pawn
					setPieceAt(board, 3, prevMove->endCol, EMPTY);
				}
			}
		}
		
		setPieceAt(board, move->startRow, move->startCol, EMPTY);
		setPieceAt(board, move->endRow, move->endCol, pieceId);
	}

	//convert pawn to queen if necessary
	if (move->endRow == 7 && pieceId == PAWN)
		setPieceAt(board, move->endRow, move->endCol, QUEEN);

	if (move->endRow == 0 && pieceId == BLACK + PAWN)
		setPieceAt(board, move->endRow, move->endCol, BLACK + QUEEN);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movsbl 0x1(%rsi), %esi
movsbl 0x2(%r14), %edx
callq 0x1f8b
movl %eax, %r15d
movb 0x1(%r14), %al
cmpb $-0x2, %al
jne 0x7958
cmpb $-0x3, 0x3(%r14)
jne 0x78d6
movsbl (%r14), %edi
callq 0x1fbc
movq %rbx, %rdi
cmpb $0x1, %al
jne 0x79db
movl $0x7, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1fae
movq %rbx, %rdi
movl $0x7, %esi
movl $0x7, %edx
xorl %ecx, %ecx
callq 0x1fae
movq %rbx, %rdi
movl $0x7, %esi
movl $0x6, %edx
movl $0x38, %ecx
callq 0x1fae
movq %rbx, %rdi
movl $0x7, %esi
movl $0x5, %edx
movl $0x34, %ecx
callq 0x1fae
movb $0x1, %al
movb %al, 0x4860(%rip)       # 0xc12b
movb %al, 0x485b(%rip)       # 0xc12c
jmp 0x7bd6
cmpb $-0x2, %al
jne 0x7958
cmpb $-0x4, 0x3(%r14)
jne 0x7958
movsbl (%r14), %edi
callq 0x1fbc
movq %rbx, %rdi
cmpb $0x1, %al
jne 0x7b4f
movl $0x7, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1fae
movq %rbx, %rdi
movl $0x7, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1fae
movq %rbx, %rdi
movl $0x7, %esi
movl $0x2, %edx
movl $0x38, %ecx
callq 0x1fae
movq %rbx, %rdi
movl $0x7, %esi
movl $0x3, %edx
movl $0x34, %ecx
callq 0x1fae
movb $0x1, %al
movb %al, 0x47df(%rip)       # 0xc12c
movb %al, 0x47d8(%rip)       # 0xc12b
jmp 0x7bd6
movzbl %r15b, %ecx
cmpl $0x5, %ecx
jle 0x798f
cmpl $0x38, %ecx
je 0x7a35
cmpl $0x33, %ecx
je 0x7a48
cmpl $0x6, %ecx
jne 0x7bac
movb $0x1, %al
movb %al, 0x47a5(%rip)       # 0xc129
movb %al, 0x47a0(%rip)       # 0xc12a
jmp 0x7bac
cmpl $0x1, %ecx
je 0x7ab5
cmpl $0x2, %ecx
jne 0x7bac
testb %al, %al
je 0x7b2c
cmpb $0x7, %al
jne 0x7bac
cmpb $0x0, 0x2(%r14)
je 0x7b9c
cmpb $0x7, %al
jne 0x7bac
cmpb $0x7, 0x2(%r14)
jne 0x7bac
movb $0x1, 0x4755(%rip)      # 0xc12b
jmp 0x7bac
xorl %esi, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1fae
movq %rbx, %rdi
xorl %esi, %esi
movl $0x7, %edx
xorl %ecx, %ecx
callq 0x1fae
movq %rbx, %rdi
xorl %esi, %esi
movl $0x6, %edx
movl $0x6, %ecx
callq 0x1fae
movq %rbx, %rdi
xorl %esi, %esi
movl $0x5, %edx
movl $0x2, %ecx
callq 0x1fae
movb $0x1, %al
movb %al, 0x46ff(%rip)       # 0xc129
movb %al, 0x46fa(%rip)       # 0xc12a
jmp 0x7bd6
movb $0x1, %al
movb %al, 0x46ee(%rip)       # 0xc12b
movb %al, 0x46e9(%rip)       # 0xc12c
jmp 0x7bac
cmpb $0x3, %al
jne 0x7bac
cmpb $0x2, 0x3(%r14)
jne 0x7bac
movsbl 0x2(%r14), %eax
cmpb 0x4(%r14), %al
je 0x7bac
cmpb $0x1, (%r12)
jne 0x7bac
movsbl 0x4(%r12), %edx
cmpb %dl, 0x2(%r12)
jne 0x7bac
cmpb $0x3, 0x3(%r12)
jne 0x7bac
leal 0x1(%rax), %ecx
cmpl %edx, %ecx
sete %cl
decl %eax
cmpl %edx, %eax
sete %al
orb %cl, %al
cmpb $0x1, %al
jne 0x7bac
movq %rbx, %rdi
movl $0x3, %esi
jmp 0x7b20
cmpb $0x4, %al
jne 0x7bac
cmpb $0x5, 0x3(%r14)
jne 0x7bac
movsbl 0x2(%r14), %eax
cmpb 0x4(%r14), %al
je 0x7bac
cmpb $0x33, (%r12)
jne 0x7bac
movsbl 0x4(%r12), %edx
cmpb %dl, 0x2(%r12)
jne 0x7bac
cmpb $0x4, 0x3(%r12)
jne 0x7bac
leal 0x1(%rax), %ecx
cmpl %edx, %ecx
sete %cl
decl %eax
cmpl %edx, %eax
sete %al
orb %cl, %al
cmpb $0x1, %al
jne 0x7bac
movq %rbx, %rdi
movl $0x4, %esi
xorl %ecx, %ecx
callq 0x1fae
jmp 0x7bac
cmpb $0x0, 0x2(%r14)
je 0x7ba5
testb %al, %al
jne 0x79a9
cmpb $0x7, 0x2(%r14)
jne 0x79a9
movb $0x1, 0x45dc(%rip)      # 0xc129
jmp 0x7bac
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1fae
movq %rbx, %rdi
xorl %esi, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1fae
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
movl $0x6, %ecx
callq 0x1fae
movq %rbx, %rdi
xorl %esi, %esi
movl $0x3, %edx
movl $0x2, %ecx
callq 0x1fae
movb $0x1, 0x4590(%rip)      # 0xc12a
jmp 0x7bd6
movb $0x1, 0x4589(%rip)      # 0xc12c
jmp 0x7bac
movb $0x1, 0x457e(%rip)      # 0xc12a
movsbl 0x1(%r14), %esi
movsbl 0x2(%r14), %edx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x1fae
movsbl 0x3(%r14), %esi
movsbl 0x4(%r14), %edx
movsbl %r15b, %ecx
movq %rbx, %rdi
callq 0x1fae
cmpb $0x1, %r15b
jne 0x7bfa
cmpb $0x7, 0x3(%r14)
jne 0x7bfa
movsbl 0x4(%r14), %edx
movq %rbx, %rdi
movl $0x7, %esi
movl $0x5, %ecx
callq 0x1fae
cmpb $0x33, %r15b
jne 0x7c23
cmpb $0x0, 0x3(%r14)
jne 0x7c23
movsbl 0x4(%r14), %edx
movq %rbx, %rdi
xorl %esi, %esi
movl $0x37, %ecx
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1fae
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq