// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movl %r8d, %r13d
movq %rsi, %rbx
testl %edi, %edi
je 0x2a8d
movl %ecx, %r15d
movq %rbx, %rdi
callq 0x35b4
cmpl $0xa, %eax
jl 0x2bfe
movq 0xa76a(%rip), %rdx      # 0xd1f0
pushq $0x4
jmp 0x2c18
movq %rdx, %r12
movq %rbx, -0x38(%rbp)
movl %r13d, -0x2c(%rbp)
leaq 0x7575(%rip), %r14      # 0xa014
movq 0xa502(%rip), %rbx      # 0xcfa8
movq 0xa51b(%rip), %r13      # 0xcfc8
leaq -0x42(%rbp), %r15
movq %r14, %rdi
xorl %eax, %eax
callq 0x2040
movq (%rbx), %rdi
callq 0x21e0
movq (%r13), %rdx
movq %r15, %rdi
pushq $0xa
popq %rsi
callq 0x2300
movq %r15, %rdi
leaq 0x7545(%rip), %rsi      # 0xa021
callq 0x9c6c
testl %eax, %eax
jne 0x2cc6
movq %r15, %rdi
callq 0x28d9
leaq 0x7637(%rip), %rdi      # 0xa12f
testq %rax, %rax
je 0x2b43
movq %rax, %r15
movq %rbx, %r13
movq %r14, %rbx
xorl %r14d, %r14d
cmpl 0x4(%r12), %r14d
jge 0x2b2f
movq %r12, %rdi
movl %r14d, %esi
callq 0x88ce
movq %rax, %rdi
movq %r15, %rsi
callq 0x2e8c
incl %r14d
testl %eax, %eax
je 0x2b09
jmp 0x2b4d
leaq 0x7608(%rip), %rdi      # 0xa13e
movq %rbx, %r14
movq %r13, %rbx
movq 0xa485(%rip), %r13      # 0xcfc8
callq 0x22e0
jmp 0x2aad
movsbl 0x1(%r15), %esi
movb $0x6, %al
cmpl $-0x2, %esi
movq -0x38(%rbp), %rbx
je 0x2b6a
movsbl 0x2(%r15), %edx
movq %rbx, %rdi
callq 0x3534
movb %al, (%r15)
pushq $0x1
popq %r12
movq %rbx, %rdi
movl %r12d, %esi
callq 0x85ec
movl %eax, 0x8(%r15)
pushq $0xa
popq %r13
movl %r13d, %edi
callq 0x2280
xorl %edi, %edi
callq 0x2170
movl %eax, %edi
callq 0x2180
movq 0xa64f(%rip), %rdx      # 0xd1f0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x88df
movq 0xa63d(%rip), %rdi      # 0xd1f0
callq 0x2200
movq %r15, %rdi
callq 0x9898
movq %rax, 0xa629(%rip)      # 0xd1f0
movq %rbx, %rdi
callq 0x35d0
movl %r13d, %edi
callq 0x2280
movq %r15, %rdi
callq 0x8ca3
movq %rbx, %rdi
movl -0x2c(%rbp), %esi
callq 0x85ec
movl %eax, %edi
callq 0x2ebb
movl %r13d, %edi
callq 0x2280
jmp 0x2cb4
movq %rbx, %rdi
callq 0x35b4
movq 0xa5e3(%rip), %rdx      # 0xd1f0
cmpl $0x6, %eax
jl 0x2c16
pushq $0x5
jmp 0x2c18
pushq $0x6
popq %rcx
movq %rbx, %rdi
movl %r15d, %esi
movl %r13d, %r8d
callq 0x8e7a
movq %rax, %r15
testq %rax, %rax
je 0x2c98
movq 0xa5ba(%rip), %rdx      # 0xd1f0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x88df
movq 0xa5a8(%rip), %rdi      # 0xd1f0
callq 0x2200
movq %r15, %rdi
callq 0x9898
movq %rax, 0xa594(%rip)      # 0xd1f0
movq %rbx, %rdi
callq 0x35d0
pushq $0xa
popq %r14
movl %r14d, %edi
callq 0x2280
movq %r15, %rdi
callq 0x8ca3
movq %rbx, %rdi
movl %r13d, %esi
callq 0x85ec
movl %eax, %edi
callq 0x2ebb
movl %r14d, %edi
callq 0x2280
pushq $0x1
popq %r12
jmp 0x2cb4
leaq 0x7479(%rip), %rdi      # 0xa118
callq 0x22e0
callq 0x29db
movq %rbx, %rdi
callq 0x2200
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x29db
leaq 0x747a(%rip), %rdi      # 0xa14c
callq 0x22e0
movq -0x38(%rbp), %rdi
jmp 0x2cac