// File: /workspace/github_c_makefile_star2_O0/upcrob[P]garry/moves.c
// Function: bestMove(char*, int, Move*, int, int)
Move* bestMove(char* board, int color, Move* prevMove, int depth, int evaluator)
{
	MoveSet* legalMoves = getLegalMoves(board, color, prevMove, evaluator);
	
	if (legalMoves == NULL)
	{
		//No moves left
		return NULL;
	} else {
		//Divide up problem space
		int i, j;
		int proc = COMM_WORLD.Get_rank();
		int numProcs = COMM_WORLD.Get_size();
		int segSize, procCap;
		if (legalMoves->size >= numProcs - 1)
		{
			//There were more moves than processors
			segSize = legalMoves->size / (numProcs - 1);
			procCap = numProcs;
		} else {
			//There are more processors than legal moves
			segSize = 1;
			procCap = legalMoves->size + 1;
		}

		//Distribute board and move data to slave nodes
		MoveSet* sub;
		for (i = 1, j = 0; i < procCap - 1; i++, j++)
		{
			sub = subMoveSet(legalMoves, j * segSize, segSize);
			COMM_WORLD.Send(&color, 1, MPI_INT, i, 0);
			COMM_WORLD.Send(&depth, 1, MPI_INT, i, 0);
			COMM_WORLD.Send(&evaluator, 1, MPI_INT, i, 0);
			COMM_WORLD.Send(board, 64, MPI_CHAR, i, 0);
			sendMoveSet(i, sub);
			sendMove(i, prevMove);
			free(sub);
		}

		//Distribute last (potentially larger) segment
		sub = subMoveSet(legalMoves, j * segSize, segSize + legalMoves->size % (procCap - 1));
		COMM_WORLD.Send(&color, 1, MPI_INT, i, 0);
		COMM_WORLD.Send(&depth, 1, MPI_INT, i, 0);
		COMM_WORLD.Send(&evaluator, 1, MPI_INT, i, 0);
		COMM_WORLD.Send(board, 64, MPI_CHAR, i, 0);
		sendMoveSet(i, sub);
		sendMove(i, prevMove);
		free(sub);

		//Initialize "best" variable
		MoveDef* best = (MoveDef*) malloc(sizeof(MoveDef));
		best->index = 0;
		if (color == WHITE)
			best->value = -INFINITY;
		else
			best->value = INFINITY;

		//Retrieve other processed segments and determine best
		MoveDef* tmp;
		MoveDef* m;
		for (i = 1; i < procCap; i++)
		{
			m = receiveMoveDef(i);
			if (color == WHITE && m->value > best->value)
			{
				//New best found (white), update pointers
				tmp = best;
				best = m;
				
				//Make index relative to "legalMoves"
				best->index = (i - 1) * segSize + best->index;
			} else if (color == BLACK && m->value < best->value) {
				//New best found (black), update pointers
				tmp = best;
				best = m;
				
				//Make index relative to "legalMoves"
				best->index = (i - 1) * segSize + best->index;
			} else {
				tmp = m;
			}
			free(tmp);
		}
		
		//Return move
		Move* move = getMove(legalMoves, best->index);
		return move;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x28(%rbp), %ecx
callq 0xff20
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x18948
movq $0x0, -0x8(%rbp)
jmp 0x18f14
movq 0x8671(%rip), %rdi      # 0x20fc0
callq 0xa540
movl %eax, -0x3c(%rbp)
movq 0x8662(%rip), %rdi      # 0x20fc0
callq 0xa570
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jl 0x18992
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
cltd
idivl %ecx
movl %eax, -0x44(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x189a6
movl $0x1, -0x44(%rbp)
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl $0x1, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x34(%rbp), %eax
movl -0x48(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x18baf
movq -0x30(%rbp), %rdi
movl -0x38(%rbp), %esi
movl -0x44(%rbp), %edx
imull %edx, %esi
callq 0x18f30
movq %rax, -0x50(%rbp)
movq 0x85ee(%rip), %rsi      # 0x20fd0
leaq -0x60(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x9e80
movq -0x108(%rbp), %rcx
movl -0x34(%rbp), %r8d
movq 0x85bc(%rip), %rdi      # 0x20fc0
leaq -0x14(%rbp), %rsi
movl $0x1, %edx
xorl %r9d, %r9d
callq 0x9f20
jmp 0x18a17
leaq -0x60(%rbp), %rdi
callq 0xa620
movq 0x85a9(%rip), %rsi      # 0x20fd0
leaq -0x80(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x9e80
movq -0x110(%rbp), %rcx
movl -0x34(%rbp), %r8d
movq 0x8577(%rip), %rdi      # 0x20fc0
leaq -0x24(%rbp), %rsi
movl $0x1, %edx
xorl %r9d, %r9d
callq 0x9f20
jmp 0x18a5c
leaq -0x80(%rbp), %rdi
callq 0xa620
movq 0x8564(%rip), %rsi      # 0x20fd0
leaq -0x90(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x9e80
movq -0x118(%rbp), %rcx
movl -0x34(%rbp), %r8d
movq 0x852f(%rip), %rdi      # 0x20fc0
leaq -0x28(%rbp), %rsi
movl $0x1, %edx
xorl %r9d, %r9d
callq 0x9f20
jmp 0x18aa4
leaq -0x90(%rbp), %rdi
callq 0xa620
movq -0x10(%rbp), %rax
movq %rax, -0x128(%rbp)
movq 0x84de(%rip), %rsi      # 0x20fa0
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x9e80
movq -0x128(%rbp), %rsi
movq -0x120(%rbp), %rcx
movl -0x34(%rbp), %r8d
movq 0x84d2(%rip), %rdi      # 0x20fc0
movl $0x40, %edx
xorl %r9d, %r9d
callq 0x9f20
jmp 0x18afd
leaq -0xa0(%rbp), %rdi
callq 0xa620
movl -0x34(%rbp), %edi
movq -0x50(%rbp), %rsi
callq 0x18fd0
movl -0x34(%rbp), %edi
movq -0x20(%rbp), %rsi
callq 0x19370
movq -0x50(%rbp), %rdi
callq 0x8680
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x189b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0xa620
jmp 0x18f21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xa620
jmp 0x18f21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xa620
jmp 0x18f21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xa620
jmp 0x18f21
movq -0x30(%rbp), %rdi
movl -0x38(%rbp), %esi
movl -0x44(%rbp), %eax
movl %eax, -0x134(%rbp)
imull %eax, %esi
movl 0x4(%rdi), %eax
movl -0x48(%rbp), %ecx
decl %ecx
cltd
idivl %ecx
movl %edx, %eax
movl -0x134(%rbp), %edx
addl %eax, %edx
callq 0x18f30
movq %rax, -0x50(%rbp)
movq 0x83e9(%rip), %rsi      # 0x20fd0
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x9e80
movq -0x130(%rbp), %rcx
movl -0x34(%rbp), %r8d
movq 0x83b4(%rip), %rdi      # 0x20fc0
leaq -0x14(%rbp), %rsi
movl $0x1, %edx
xorl %r9d, %r9d
callq 0x9f20
jmp 0x18c1f
leaq -0xb0(%rbp), %rdi
callq 0xa620
movq 0x839e(%rip), %rsi      # 0x20fd0
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x9e80
movq -0x140(%rbp), %rcx
movl -0x34(%rbp), %r8d
movq 0x8369(%rip), %rdi      # 0x20fc0
leaq -0x24(%rbp), %rsi
movl $0x1, %edx
xorl %r9d, %r9d
callq 0x9f20
jmp 0x18c6a
leaq -0xc0(%rbp), %rdi
callq 0xa620
movq 0x8353(%rip), %rsi      # 0x20fd0
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x9e80
movq -0x148(%rbp), %rcx
movl -0x34(%rbp), %r8d
movq 0x831e(%rip), %rdi      # 0x20fc0
leaq -0x28(%rbp), %rsi
movl $0x1, %edx
xorl %r9d, %r9d
callq 0x9f20
jmp 0x18cb5
leaq -0xd0(%rbp), %rdi
callq 0xa620
movq -0x10(%rbp), %rax
movq %rax, -0x158(%rbp)
movq 0x82cd(%rip), %rsi      # 0x20fa0
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x9e80
movq -0x158(%rbp), %rsi
movq -0x150(%rbp), %rcx
movl -0x34(%rbp), %r8d
movq 0x82c1(%rip), %rdi      # 0x20fc0
movl $0x40, %edx
xorl %r9d, %r9d
callq 0x9f20
jmp 0x18d0e
leaq -0xe0(%rbp), %rdi
callq 0xa620
movl -0x34(%rbp), %edi
movq -0x50(%rbp), %rsi
callq 0x18fd0
movl -0x34(%rbp), %edi
movq -0x20(%rbp), %rsi
callq 0x19370
movq -0x50(%rbp), %rdi
callq 0x8680
movl $0x8, %edi
callq 0x8780
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movl $0x0, (%rax)
cmpl $0x0, -0x14(%rbp)
jne 0x18dea
movq -0xe8(%rbp), %rax
movl $0x82d48b00, 0x4(%rax)  # imm = 0x82D48B00
jmp 0x18df8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xa620
jmp 0x18f21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0xa620
jmp 0x18f21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0xa620
jmp 0x18f21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0xa620
jmp 0x18f21
movq -0xe8(%rbp), %rax
movl $0x7d2b7500, 0x4(%rax)  # imm = 0x7D2B7500
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0x18ef0
movl -0x34(%rbp), %edi
callq 0x19630
movq %rax, -0xf8(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x18e70
movq -0xf8(%rbp), %rax
movl 0x4(%rax), %eax
movq -0xe8(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jle 0x18e70
movq -0xe8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
imull -0x44(%rbp), %ecx
movq -0xe8(%rbp), %rax
addl (%rax), %ecx
movq -0xe8(%rbp), %rax
movl %ecx, (%rax)
jmp 0x18ed6
cmpl $0x32, -0x14(%rbp)
jne 0x18ec6
movq -0xf8(%rbp), %rax
movl 0x4(%rax), %eax
movq -0xe8(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x18ec6
movq -0xe8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
imull -0x44(%rbp), %ecx
movq -0xe8(%rbp), %rax
addl (%rax), %ecx
movq -0xe8(%rbp), %rax
movl %ecx, (%rax)
jmp 0x18ed4
movq -0xf8(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x18ed6
movq -0xf0(%rbp), %rdi
callq 0x8680
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x18dff
movq -0x30(%rbp), %rdi
movq -0xe8(%rbp), %rax
movl (%rax), %esi
callq 0x18080
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x8a50
nopw (%rax,%rax)