// File: /workspace/github_c_makefile_star2_O2/upcrob[P]garry/moves.c
// Function: evaluateBoard(char*, int)
int evaluateBoard(char* board, int functionId)
{
	int count;
	char i, j;
	count = 0;

	int pawnValue, rookValue, knightValue, bishopValue, queenValue;
	int pawnAdvanceValue, safetyRatio;
	switch (functionId)
	{
		case ROB:			//Rob's values
			pawnValue = 100;
			rookValue = 500;
			knightValue = 300;
			bishopValue = 350;
			queenValue = 900;
			pawnAdvanceValue = 4;
			safetyRatio = 30;
			break;
		case TURING:		//Turing's values
			pawnValue = 100;
			rookValue = 500;
			knightValue = 300;
			bishopValue = 350;
			queenValue = 1000;
			pawnAdvanceValue = 0;
			safetyRatio = 0;
			break;
		case BERLINER:
			pawnValue = 100;
			rookValue = 510;
			knightValue = 320;
			bishopValue = 333;
			queenValue = 880;
			pawnAdvanceValue = 6;
			safetyRatio = 0;
			break;
	}

	for (i = 0; i < 8; i++)
	{
		for (j = 0; j < 8; j++)
		{
			char p = getPieceAt(board, i, j);
			if (p != EMPTY)
			{
				if (!isBlack(p))
				{
					//white piece
					switch(p)
					{
						case PAWN:
							count += pawnValue;
							count += pawnAdvanceValue * i;
							if (safetyRatio > 0 && pieceSafe(board, i, j))
								count += pawnValue / safetyRatio;
							break;
						case ROOK:
							count += rookValue;
							if (safetyRatio > 0 && pieceSafe(board, i, j))
								count += rookValue / safetyRatio;
							break;
						case KNIGHT:
							count += knightValue;
							if (safetyRatio > 0 && pieceSafe(board, i, j))
								count += knightValue / safetyRatio;
							break;
						case BISHOP:
							count += bishopValue;
							if (safetyRatio > 0 && pieceSafe(board, i, j))
								count += bishopValue / safetyRatio;
							break;
						case QUEEN:
							count += queenValue;
							if (safetyRatio > 0 && pieceSafe(board, i, j))
								count += queenValue / safetyRatio;
							break;
					}
				} else {
					//black piece
					switch(p)
					{
						case BLACK + PAWN:
							count -= pawnAdvanceValue * (7 - i);
							count -= pawnValue;
							if (safetyRatio > 0 && pieceSafe(board, i, j))
								count -= pawnValue / safetyRatio;
							break;
						case BLACK + ROOK:
							count -= rookValue;
							if (safetyRatio > 0 && pieceSafe(board, i, j))
								count -= rookValue / safetyRatio;
							break;
						case BLACK + KNIGHT:
							count -= knightValue;
							if (safetyRatio > 0 && pieceSafe(board, i, j))
								count -= knightValue / safetyRatio;
							break;
						case BLACK + BISHOP:
							count -= bishopValue;
							if (safetyRatio > 0 && pieceSafe(board, i, j))
								count -= bishopValue / safetyRatio;
							break;
						case BLACK + QUEEN:
							count -= queenValue;
							if (safetyRatio > 0 && pieceSafe(board, i, j))
								count -= queenValue / safetyRatio;
							break;
					}
				}
			}
		}
	}

	return count;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
cmpl $0x3, %esi
je 0x8639
movl $0x15e, -0x40(%rbp)     # imm = 0x15E
movl $0x12c, -0x3c(%rbp)     # imm = 0x12C
movl $0x1f4, -0x38(%rbp)     # imm = 0x1F4
cmpl $0x1, %esi
jne 0x8664
pushq $0x1e
popq %rax
movl %eax, -0x2c(%rbp)
movb $0x1, %al
movl %eax, -0x30(%rbp)
pushq $0x4
popq %rax
movl %eax, -0x44(%rbp)
movl $0x384, -0x34(%rbp)     # imm = 0x384
jmp 0x8680
movl $0x0, -0x30(%rbp)
pushq $0x6
popq %rax
movl %eax, -0x44(%rbp)
movl $0x370, -0x34(%rbp)     # imm = 0x370
movl $0x14d, -0x40(%rbp)     # imm = 0x14D
movl $0x140, -0x3c(%rbp)     # imm = 0x140
movl $0x1fe, -0x38(%rbp)     # imm = 0x1FE
jmp 0x8679
movl $0x0, -0x44(%rbp)
movl $0x3e8, -0x34(%rbp)     # imm = 0x3E8
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpl $0x8, %r14d
je 0x88bc
leal -0x7(%r14), %ecx
movl -0x44(%rbp), %eax
imull %eax, %ecx
addl $-0x64, %ecx
movl %ecx, -0x4c(%rbp)
imull %r14d, %eax
addl $0x64, %eax
movl %eax, -0x48(%rbp)
xorl %r12d, %r12d
cmpl $0x8, %r12d
je 0x88b4
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x3534
testb %al, %al
je 0x88ac
movl %eax, %r13d
movsbl %al, %edi
callq 0x3565
movzbl %r13b, %ecx
testb %al, %al
je 0x8728
addl $-0x33, %ecx
cmpl $0x4, %ecx
ja 0x88ac
leaq 0x1dad(%rip), %rdx      # 0xa4a0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
addl -0x4c(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x88ac
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x37c7
testb %al, %al
je 0x88ac
pushq $0x64
popq %rax
jmp 0x8811
decl %ecx
cmpl $0x4, %ecx
ja 0x88ac
leaq 0x1d7a(%rip), %rdx      # 0xa4b4
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
addl -0x48(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x88ac
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x37c7
testb %al, %al
je 0x88ac
pushq $0x64
popq %rax
jmp 0x88a4
subl -0x34(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x88ac
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x37c7
testb %al, %al
je 0x88ac
movl -0x34(%rbp), %eax
jmp 0x8811
subl -0x3c(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x88ac
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x37c7
testb %al, %al
je 0x88ac
movl -0x3c(%rbp), %eax
jmp 0x8811
subl -0x40(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x88ac
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x37c7
testb %al, %al
je 0x88ac
movl -0x40(%rbp), %eax
jmp 0x8811
subl -0x38(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x88ac
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x37c7
testb %al, %al
je 0x88ac
movl -0x38(%rbp), %eax
xorl %edx, %edx
divl -0x2c(%rbp)
subl %eax, %r15d
jmp 0x88ac
addl -0x34(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x88ac
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x37c7
testb %al, %al
je 0x88ac
movl -0x34(%rbp), %eax
jmp 0x88a4
addl -0x3c(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x88ac
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x37c7
testb %al, %al
je 0x88ac
movl -0x3c(%rbp), %eax
jmp 0x88a4
addl -0x40(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x88ac
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x37c7
testb %al, %al
je 0x88ac
movl -0x40(%rbp), %eax
jmp 0x88a4
addl -0x38(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x88ac
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x37c7
testb %al, %al
je 0x88ac
movl -0x38(%rbp), %eax
xorl %edx, %edx
divl -0x2c(%rbp)
addl %eax, %r15d
incl %r12d
jmp 0x86ad
incl %r14d
jmp 0x8686
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq