// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0x990a
jmp 0x9726
leaq 0x119f1(%rip), %rdi     # 0x1b11e
movb $0x0, %al
callq 0x8040
movq 0x1785d(%rip), %rax     # 0x20f98
movq (%rax), %rdi
callq 0x8660
leaq -0x3a(%rbp), %rdi
movq 0x1786a(%rip), %rax     # 0x20fb8
movq (%rax), %rdx
movl $0xa, %esi
callq 0x8920
leaq -0x3a(%rbp), %rdi
leaq 0x119c5(%rip), %rsi     # 0x1b12b
callq 0x1a3b0
cmpl $0x0, %eax
je 0x9798
callq 0x9660
leaq 0x119b2(%rip), %rdi     # 0x1b12e
movb $0x0, %al
callq 0x8040
movq -0x10(%rbp), %rdi
callq 0x8680
movl $0x0, -0x4(%rbp)
jmp 0x9a32
leaq -0x3a(%rbp), %rdi
callq 0x9320
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x9871
movl $0x0, -0x40(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x97fd
movq -0x18(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x18080
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x9d20
cmpl $0x0, %eax
je 0x97f0
movl $0x1, -0x40(%rbp)
jmp 0x97fd
jmp 0x97f2
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x97be
cmpl $0x1, -0x40(%rbp)
jne 0x9861
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $-0x2, %eax
jne 0x9819
movq -0x30(%rbp), %rax
movb $0x6, (%rax)
jmp 0x983a
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %esi
movq -0x30(%rbp), %rax
movsbl 0x2(%rax), %edx
callq 0xeab0
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x17c80
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x8(%rax)
leaq 0x1184c(%rip), %rdi     # 0x1b0a4
movb $0x0, %al
callq 0x8040
jmp 0x9884
leaq 0x118db(%rip), %rdi     # 0x1b143
movb $0x0, %al
callq 0x8040
jmp 0x987f
leaq 0x118da(%rip), %rdi     # 0x1b152
movb $0x0, %al
callq 0x8040
jmp 0x9726
xorl %eax, %eax
movl %eax, %edi
callq 0x84e0
movl %eax, %edi
callq 0x8540
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq 0x17d45(%rip), %rdx     # 0x215e8
callq 0x180a0
movq 0x17d39(%rip), %rdi     # 0x215e8
callq 0x8680
movq -0x30(%rbp), %rdi
callq 0x19c00
movq %rax, 0x17d24(%rip)     # 0x215e8
movq -0x10(%rbp), %rdi
callq 0xec20
leaq 0x117d0(%rip), %rdi     # 0x1b0a4
movb $0x0, %al
callq 0x8040
movq -0x30(%rbp), %rdi
callq 0x185d0
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x17c80
movl %eax, %edi
callq 0x9da0
leaq 0x117a6(%rip), %rdi     # 0x1b0a4
movb $0x0, %al
callq 0x8040
jmp 0x9a2b
movq -0x10(%rbp), %rdi
callq 0xebd0
cmpl $0x9, %eax
jle 0x993a
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq 0x17cc2(%rip), %rdx     # 0x215e8
movl -0x20(%rbp), %r8d
movl $0x4, %ecx
callq 0x18900
movq %rax, -0x48(%rbp)
jmp 0x998c
movq -0x10(%rbp), %rdi
callq 0xebd0
cmpl $0x5, %eax
jle 0x996a
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq 0x17c92(%rip), %rdx     # 0x215e8
movl -0x20(%rbp), %r8d
movl $0x5, %ecx
callq 0x18900
movq %rax, -0x48(%rbp)
jmp 0x998a
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq 0x17c70(%rip), %rdx     # 0x215e8
movl -0x20(%rbp), %r8d
movl $0x6, %ecx
callq 0x18900
movq %rax, -0x48(%rbp)
jmp 0x998c
cmpq $0x0, -0x48(%rbp)
jne 0x99b8
leaq 0x1173d(%rip), %rdi     # 0x1b0d7
movb $0x0, %al
callq 0x8040
callq 0x9660
movq -0x10(%rbp), %rdi
callq 0x8680
movl $0x0, -0x4(%rbp)
jmp 0x9a32
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq 0x17c21(%rip), %rdx     # 0x215e8
callq 0x180a0
movq 0x17c15(%rip), %rdi     # 0x215e8
callq 0x8680
movq -0x48(%rbp), %rdi
callq 0x19c00
movq %rax, 0x17c00(%rip)     # 0x215e8
movq -0x10(%rbp), %rdi
callq 0xec20
leaq 0x116ac(%rip), %rdi     # 0x1b0a4
movb $0x0, %al
callq 0x8040
movq -0x48(%rbp), %rdi
callq 0x185d0
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x17c80
movl %eax, %edi
callq 0x9da0
leaq 0x11682(%rip), %rdi     # 0x1b0a4
movb $0x0, %al
callq 0x8040
jmp 0x9a2b
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)