// File: /workspace/github_c_makefile_star2_O3/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 0x1eef
movl %eax, %r15d
movzbl 0x1(%r14), %eax
cmpl $0xfe, %eax
jne 0x7972
movzbl 0x3(%r14), %ecx
cmpl $0xfc, %ecx
je 0x79ee
cmpl $0xfd, %ecx
jne 0x7972
movsbl (%r14), %edi
callq 0x1f20
movq %rbx, %rdi
cmpb $0x1, %al
jne 0x7b5e
movl $0x7, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1f12
movq %rbx, %rdi
movl $0x7, %esi
movl $0x7, %edx
xorl %ecx, %ecx
callq 0x1f12
movq %rbx, %rdi
movl $0x7, %esi
movl $0x6, %edx
movl $0x38, %ecx
callq 0x1f12
movq %rbx, %rdi
movl $0x7, %esi
movl $0x5, %edx
movl $0x34, %ecx
callq 0x1f12
movb $0x1, %al
movb %al, 0x47c4(%rip)       # 0xc12b
movb %al, 0x47bf(%rip)       # 0xc12c
jmp 0x7c56
movzbl %r15b, %ecx
cmpl $0x5, %ecx
jle 0x79a9
cmpl $0x38, %ecx
je 0x7a65
cmpl $0x33, %ecx
je 0x7a78
cmpl $0x6, %ecx
jne 0x7c2c
movb $0x1, %al
movb %al, 0x478b(%rip)       # 0xc129
movb %al, 0x4786(%rip)       # 0xc12a
jmp 0x7c2c
cmpl $0x1, %ecx
je 0x7ae6
cmpl $0x2, %ecx
jne 0x7c2c
cmpl $0x7, %eax
je 0x7c05
testl %eax, %eax
jne 0x7c2c
movzbl 0x2(%r14), %eax
cmpl $0x7, %eax
je 0x7c1c
testl %eax, %eax
jne 0x7c2c
movb $0x1, 0x4741(%rip)      # 0xc12a
jmp 0x7c2c
movsbl (%r14), %edi
callq 0x1f20
movq %rbx, %rdi
cmpb $0x1, %al
jne 0x7bb8
movl $0x7, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1f12
movq %rbx, %rdi
movl $0x7, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1f12
movq %rbx, %rdi
movl $0x7, %esi
movl $0x2, %edx
movl $0x38, %ecx
callq 0x1f12
movq %rbx, %rdi
movl $0x7, %esi
movl $0x3, %edx
movl $0x34, %ecx
callq 0x1f12
movb $0x1, %al
movb %al, 0x46d2(%rip)       # 0xc12c
movb %al, 0x46cb(%rip)       # 0xc12b
jmp 0x7c56
movb $0x1, %al
movb %al, 0x46be(%rip)       # 0xc12b
movb %al, 0x46b9(%rip)       # 0xc12c
jmp 0x7c2c
cmpl $0x3, %eax
jne 0x7c2c
cmpb $0x2, 0x3(%r14)
jne 0x7c2c
movsbl 0x2(%r14), %eax
cmpb 0x4(%r14), %al
je 0x7c2c
cmpb $0x1, (%r12)
jne 0x7c2c
movsbl 0x4(%r12), %edx
cmpb %dl, 0x2(%r12)
jne 0x7c2c
cmpb $0x3, 0x3(%r12)
jne 0x7c2c
leal 0x1(%rax), %ecx
cmpl %edx, %ecx
sete %cl
decl %eax
cmpl %edx, %eax
sete %al
orb %cl, %al
cmpb $0x1, %al
jne 0x7c2c
movq %rbx, %rdi
movl $0x3, %esi
jmp 0x7b52
cmpl $0x4, %eax
jne 0x7c2c
cmpb $0x5, 0x3(%r14)
jne 0x7c2c
movsbl 0x2(%r14), %eax
cmpb 0x4(%r14), %al
je 0x7c2c
cmpb $0x33, (%r12)
jne 0x7c2c
movsbl 0x4(%r12), %edx
cmpb %dl, 0x2(%r12)
jne 0x7c2c
cmpb $0x4, 0x3(%r12)
jne 0x7c2c
leal 0x1(%rax), %ecx
cmpl %edx, %ecx
sete %cl
decl %eax
cmpl %edx, %eax
sete %al
orb %cl, %al
cmpb $0x1, %al
jne 0x7c2c
movq %rbx, %rdi
movl $0x4, %esi
xorl %ecx, %ecx
callq 0x1f12
jmp 0x7c2c
xorl %esi, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1f12
movq %rbx, %rdi
xorl %esi, %esi
movl $0x7, %edx
xorl %ecx, %ecx
callq 0x1f12
movq %rbx, %rdi
xorl %esi, %esi
movl $0x6, %edx
movl $0x6, %ecx
callq 0x1f12
movq %rbx, %rdi
xorl %esi, %esi
movl $0x5, %edx
movl $0x2, %ecx
callq 0x1f12
movb $0x1, %al
movb %al, 0x457c(%rip)       # 0xc129
movb %al, 0x4577(%rip)       # 0xc12a
jmp 0x7c56
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1f12
movq %rbx, %rdi
xorl %esi, %esi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x1f12
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
movl $0x6, %ecx
callq 0x1f12
movq %rbx, %rdi
xorl %esi, %esi
movl $0x3, %edx
movl $0x2, %ecx
callq 0x1f12
movb $0x1, 0x4527(%rip)      # 0xc12a
jmp 0x7c56
movzbl 0x2(%r14), %eax
cmpl $0x7, %eax
je 0x7c25
testl %eax, %eax
jne 0x7c2c
movb $0x1, 0x4512(%rip)      # 0xc12c
jmp 0x7c2c
movb $0x1, 0x4506(%rip)      # 0xc129
jmp 0x7c2c
movb $0x1, 0x44ff(%rip)      # 0xc12b
movsbl 0x1(%r14), %esi
movsbl 0x2(%r14), %edx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x1f12
movsbl 0x3(%r14), %esi
movsbl 0x4(%r14), %edx
movsbl %r15b, %ecx
movq %rbx, %rdi
callq 0x1f12
movb 0x3(%r14), %al
movl %eax, %ecx
xorb $0x7, %cl
movl %r15d, %edx
xorb $0x1, %dl
orb %cl, %dl
jne 0x7c7d
movsbl 0x4(%r14), %edx
movq %rbx, %rdi
movl $0x7, %esi
movl $0x5, %ecx
jmp 0x7c95
xorb $0x33, %r15b
orb %al, %r15b
jne 0x7ca2
movsbl 0x4(%r14), %edx
movq %rbx, %rdi
xorl %esi, %esi
movl $0x37, %ecx
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1f12
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq