// File: /workspace/github_c_makefile_star2_O3/upcrob[P]garry/chess.c
// Function: computeMove(int, char*, MoveSet*, int, int)
int computeMove(int userType, char* board, MoveSet* moves, int colorFlag, int evaluator)
{
	int i;
	if (userType == HUMAN)
	{
		//Human move
		Move* move;
		char text[10];
		
		while (TRUE)
		{
			printf("ENTER MOVE: ");
			fflush(stdout);
			fgets(text, sizeof(text), stdin);
			
			//Check for end program request
			if (streq(text, (char*) "q\n"))
			{
				//Send termination signal to slave nodes
				sendKillSig();
				
				//Exit
				printf("Program Terminated.\n");
				free(board);
				return 0;
			}
			
			move = strToMove(text);
			if (move != NULL)
			{
				int legalFlag = FALSE;
				//determine if move was legal
				for (i = 0; i < moves->size; i++)
				{
					if (movesEqual(getMove(moves, i), move))
					{
						legalFlag = TRUE;
						break;
					}
				}
				
				if (legalFlag == TRUE)
				{
					if (move->startRow == CASTLE)
						move->pieceId = KING;
					else
						move->pieceId = getPieceAt(board, move->startRow, move->startCol);
					move->boardScore = evaluateBoard(board, 1);
					printf("\n");
					break;
				} else {
					printf("ILLEGAL MOVE!\n");
				}
			} else {
				printf("INVALID INPUT!\n");
			}
		}
		srand((unsigned int) time(NULL));
		
		//execute move
		executeMove(board, move, prev);
		
		//set previous move to current
		free(prev);
		prev = cloneMove(move);
		
		//print output
		printBoard(board);
		printf("\n");
		printMove(move);
		printScore(evaluateBoard(board, evaluator));
		printf("\n");
	} else {
		//AI move
		Move* best;
		
		//determine best move, increase search depth if fewer pieces on board
		if (totalPieces(board) > 9)
			best = bestMove(board, colorFlag, prev, SEARCH_DEPTH, evaluator);
		else if (totalPieces(board) > 5)
			best = bestMove(board, colorFlag, prev, SEARCH_DEPTH + 1, evaluator);
		else
			best = bestMove(board, colorFlag, prev, SEARCH_DEPTH + 2, evaluator);
		
		if (best == NULL)
		{
			printf("CHECKMATE: WHITE WINS.\n");
			
			//End program
			sendKillSig();
			free(board);
			return 0;
		} else {
			//execute best move
			executeMove(board, best, prev);
			
			//set previous move to current
			free(prev);
			prev = cloneMove(best);
			
			//print output
			printBoard(board);
			printf("\n");
			printMove(best);
			printScore(evaluateBoard(board, evaluator));
			printf("\n");
		}
	}

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, %r15d
movq %rsi, %rbx
testl %edi, %edi
je 0x190a
movl %ecx, %r14d
movq %rbx, %rdi
callq 0x1f68
cmpl $0xa, %eax
jl 0x1a1a
movq 0xa80e(%rip), %rdx      # 0xc108
movq %rbx, %rdi
movl %r14d, %esi
movl $0x4, %ecx
jmp 0x1a5a
movq %rdx, %r12
movq %rbx, -0x30(%rbp)
movl %r15d, -0x34(%rbp)
leaq 0x76f8(%rip), %rdi      # 0x9014
xorl %eax, %eax
callq 0x1030
movq 0xa67e(%rip), %r15      # 0xbfa8
movq (%r15), %rdi
callq 0x1110
movq 0xa68f(%rip), %rbx      # 0xbfc8
movq (%rbx), %rdx
leaq -0x3e(%rbp), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x11b0
leaq 0x76cd(%rip), %rsi      # 0x9021
movq %r14, %rdi
callq 0x8af8
testl %eax, %eax
jne 0x1a3b
movq %r14, %rdi
callq 0x1756
leaq 0x77bc(%rip), %rdi      # 0x912f
testq %rax, %rax
je 0x19e0
cmpl $0x0, 0x4(%r12)
leaq 0x77b9(%rip), %rdi      # 0x913e
jle 0x19e0
movq %rax, %r13
movq %r15, %rbx
xorl %r15d, %r15d
movq %r12, %rdi
movl %r15d, %esi
callq 0x7899
movsbl 0x1(%rax), %esi
cmpb 0x1(%r13), %sil
jne 0x19c5
movsbl 0x2(%rax), %edx
cmpb 0x2(%r13), %dl
jne 0x19c5
movb 0x3(%rax), %cl
cmpb 0x3(%r13), %cl
jne 0x19c5
movb 0x4(%rax), %al
cmpb 0x4(%r13), %al
je 0x1b03
incl %r15d
cmpl 0x4(%r12), %r15d
jl 0x1990
leaq 0x7768(%rip), %rdi      # 0x913e
movq %rbx, %r15
movq 0xa5e8(%rip), %rbx      # 0xbfc8
callq 0x1190
leaq 0x7628(%rip), %rdi      # 0x9014
xorl %eax, %eax
callq 0x1030
movq (%r15), %rdi
callq 0x1110
movq (%rbx), %rdx
movq %r14, %rdi
movl $0xa, %esi
callq 0x11b0
movq %r14, %rdi
leaq 0x760c(%rip), %rsi      # 0x9021
jmp 0x1957
movq %rbx, %rdi
callq 0x1f68
movq 0xa6df(%rip), %rdx      # 0xc108
movq %rbx, %rdi
movl %r14d, %esi
cmpl $0x6, %eax
jl 0x1a55
movl $0x5, %ecx
jmp 0x1a5a
callq 0x185d
leaq 0x7705(%rip), %rdi      # 0x914c
callq 0x1190
movq -0x30(%rbp), %rdi
jmp 0x1ae9
movl $0x6, %ecx
movl %r15d, %r8d
callq 0x7e76
movq %rax, %r14
testq %rax, %rax
je 0x1ad5
movq 0xa697(%rip), %rdx      # 0xc108
movq %rbx, %rdi
movq %r14, %rsi
callq 0x78aa
movq 0xa685(%rip), %rdi      # 0xc108
callq 0x1130
movq %r14, %rdi
callq 0x87e5
movq %rax, 0xa671(%rip)      # 0xc108
movq %rbx, %rdi
callq 0x1f82
movl $0xa, %edi
callq 0x1150
movq %r14, %rdi
callq 0x7cab
movq %rbx, %rdi
movl %r15d, %esi
callq 0x75b8
movl %eax, %edi
callq 0x1dce
movl $0xa, %edi
callq 0x1150
movl $0x1, %r14d
jmp 0x1af1
leaq 0x763c(%rip), %rdi      # 0x9118
callq 0x1190
callq 0x185d
movq %rbx, %rdi
callq 0x1130
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x6, %al
cmpb $-0x2, %sil
je 0x1b14
movq -0x30(%rbp), %rdi
callq 0x1eef
movb %al, (%r13)
movl $0x1, %r14d
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x75b8
movl %eax, 0x8(%r13)
movl $0xa, %edi
callq 0x1150
xorl %edi, %edi
callq 0x10c0
movl %eax, %edi
callq 0x10d0
movq 0xa5b6(%rip), %rdx      # 0xc108
movq %rbx, %rdi
movq %r13, %rsi
callq 0x78aa
movq 0xa5a4(%rip), %rdi      # 0xc108
callq 0x1130
movq %r13, %rdi
callq 0x87e5
movq %rax, 0xa590(%rip)      # 0xc108
movq %rbx, %rdi
callq 0x1f82
movl $0xa, %edi
callq 0x1150
movq %r13, %rdi
callq 0x7cab
movq %rbx, %rdi
movl -0x34(%rbp), %esi
callq 0x75b8
movl %eax, %edi
callq 0x1dce
movl $0xa, %edi
callq 0x1150
jmp 0x1af1