// File: /workspace/github_c_makefile_star2_O1/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, %r12d
movq %rsi, %rbx
testl %edi, %edi
je 0x1965
movl %ecx, %r14d
movq %rbx, %rdi
callq 0x2004
cmpl $0xa, %eax
jl 0x19eb
movq 0xa7b3(%rip), %rdx      # 0xc108
movq %rbx, %rdi
movl %r14d, %esi
movl $0x4, %ecx
jmp 0x1a11
movq %rdx, %r15
movq %rbx, -0x38(%rbp)
leaq 0x76a1(%rip), %rdi      # 0x9014
xorl %eax, %eax
callq 0x1030
movq 0xa627(%rip), %rbx      # 0xbfa8
movq (%rbx), %rdi
callq 0x1110
movq 0xa638(%rip), %r13      # 0xbfc8
movq (%r13), %rdx
leaq -0x46(%rbp), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x11b0
leaq 0x7675(%rip), %rsi      # 0x9021
movq %r14, %rdi
callq 0x8a64
testl %eax, %eax
sete %al
je 0x1abc
movl %eax, %ebx
callq 0x18b8
leaq 0x777f(%rip), %rdi      # 0x914c
callq 0x1190
movq -0x38(%rbp), %rdi
callq 0x1130
testb $0x1, %bl
jne 0x1aa8
xorl %eax, %eax
jmp 0x1aad
movq %rbx, %rdi
callq 0x2004
movq 0xa70e(%rip), %rdx      # 0xc108
movq %rbx, %rdi
movl %r14d, %esi
cmpl $0x6, %eax
jl 0x1a0c
movl $0x5, %ecx
jmp 0x1a11
movl $0x6, %ecx
movl %r12d, %r8d
callq 0x7df6
movq %rax, %r14
testq %rax, %rax
je 0x1a86
movq 0xa6e0(%rip), %rdx      # 0xc108
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7824
movq 0xa6ce(%rip), %rdi      # 0xc108
callq 0x1130
movq %r14, %rdi
callq 0x8754
movq %rax, 0xa6ba(%rip)      # 0xc108
movq %rbx, %rdi
callq 0x201e
movl $0xa, %edi
callq 0x1150
movq %r14, %rdi
callq 0x7c2c
movq %rbx, %rdi
movl %r12d, %esi
callq 0x7532
movl %eax, %edi
callq 0x1e6a
movl $0xa, %edi
callq 0x1150
jmp 0x1a9f
leaq 0x768b(%rip), %rdi      # 0x9118
callq 0x1190
callq 0x18b8
movq %rbx, %rdi
callq 0x1130
testq %r14, %r14
je 0x19e4
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r12d, -0x3c(%rbp)
leaq -0x46(%rbp), %rcx
movq %rbx, %r14
movl %eax, %ebx
movq %r13, %r12
movq %rcx, %r13
movq %r13, %rdi
callq 0x1781
testq %rax, %rax
je 0x1b56
movq %rax, %r13
movb %bl, -0x29(%rbp)
movq %r14, %rbx
cmpl $0x0, 0x4(%r15)
setg %r12b
jle 0x1b35
xorl %r14d, %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0x7813
movsbl 0x1(%r13), %esi
cmpb %sil, 0x1(%rax)
jne 0x1b28
movb 0x2(%rax), %cl
cmpb 0x2(%r13), %cl
jne 0x1b28
movb 0x3(%rax), %cl
cmpb 0x3(%r13), %cl
jne 0x1b28
movb 0x4(%rax), %al
cmpb 0x4(%r13), %al
je 0x1bac
incl %r14d
cmpl 0x4(%r15), %r14d
setl %r12b
jl 0x1af3
leaq 0x7602(%rip), %rdi      # 0x913e
callq 0x1190
movq %rbx, %r14
testb $0x1, %r12b
movq 0xa479(%rip), %r12      # 0xbfc8
je 0x1b62
jmp 0x1bea
leaq 0x75d2(%rip), %rdi      # 0x912f
callq 0x1190
leaq 0x74ab(%rip), %rdi      # 0x9014
xorl %eax, %eax
callq 0x1030
movq (%r14), %rdi
callq 0x1110
movq (%r12), %rdx
leaq -0x46(%rbp), %r13
movq %r13, %rdi
movl $0xa, %esi
callq 0x11b0
movq %r13, %rdi
leaq 0x748a(%rip), %rsi      # 0x9021
callq 0x8a64
testl %eax, %eax
sete %bl
je 0x1acf
jmp 0x19c1
movb $0x6, %al
cmpb $-0x2, %sil
movq %rbx, %r14
je 0x1bc5
movsbl 0x2(%r13), %edx
movq -0x38(%rbp), %rdi
callq 0x1f8b
movb %al, (%r13)
movq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x7532
movl %eax, 0x8(%r13)
movl $0xa, %edi
callq 0x1150
jmp 0x1b44
xorl %edi, %edi
callq 0x10c0
movl %eax, %edi
callq 0x10d0
movq 0xa509(%rip), %rdx      # 0xc108
movq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x7824
movq 0xa4f3(%rip), %rdi      # 0xc108
callq 0x1130
movq %r13, %rdi
callq 0x8754
movq %rax, 0xa4df(%rip)      # 0xc108
movq %r14, %rdi
callq 0x201e
movl $0xa, %edi
callq 0x1150
movq %r13, %rdi
callq 0x7c2c
movq %r14, %rdi
movl -0x3c(%rbp), %esi
callq 0x7532
movl %eax, %edi
callq 0x1e6a
movl $0xa, %edi
callq 0x1150
movb -0x29(%rbp), %bl
jmp 0x19db