// File: /workspace/github_c_makefile_star2_O1/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 0x7581
movl $0x15e, -0x40(%rbp)     # imm = 0x15E
movl $0x12c, -0x3c(%rbp)     # imm = 0x12C
movl $0x1f4, -0x38(%rbp)     # imm = 0x1F4
cmpl $0x1, %esi
jne 0x75ad
movl $0x1e, -0x2c(%rbp)
movb $0x1, %al
movl %eax, -0x30(%rbp)
movl $0x4, -0x44(%rbp)
movl $0x384, -0x34(%rbp)     # imm = 0x384
jmp 0x75c9
movl $0x0, -0x30(%rbp)
movl $0x6, -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 0x75c2
movl $0x0, -0x44(%rbp)
movl $0x3e8, -0x34(%rbp)     # imm = 0x3E8
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
leal -0x7(%r14), %ecx
movl -0x44(%rbp), %eax
imull %eax, %ecx
addl $-0x64, %ecx
movl %ecx, -0x4c(%rbp)
movl %r14d, %ecx
imull %eax, %ecx
addl $0x64, %ecx
movl %ecx, -0x48(%rbp)
xorl %r12d, %r12d
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x1f8b
testb %al, %al
je 0x77e7
movl %eax, %r13d
movsbl %al, %edi
callq 0x1fbc
movzbl %r13b, %ecx
testb %al, %al
je 0x7661
addl $-0x33, %ecx
cmpl $0x4, %ecx
ja 0x77e7
leaq 0x1e66(%rip), %rdx      # 0x9490
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
addl -0x4c(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x77e7
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x220b
testb %al, %al
je 0x77e7
movl $0x64, %eax
jmp 0x774c
decl %ecx
cmpl $0x4, %ecx
ja 0x77e7
leaq 0x1e31(%rip), %rdx      # 0x94a4
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
addl -0x48(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x77e7
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x220b
testb %al, %al
je 0x77e7
movl $0x64, %eax
jmp 0x77df
subl -0x34(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x77e7
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x220b
testb %al, %al
je 0x77e7
movl -0x34(%rbp), %eax
jmp 0x774c
subl -0x3c(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x77e7
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x220b
testb %al, %al
je 0x77e7
movl -0x3c(%rbp), %eax
jmp 0x774c
subl -0x40(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x77e7
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x220b
testb %al, %al
je 0x77e7
movl -0x40(%rbp), %eax
jmp 0x774c
subl -0x38(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x77e7
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x220b
testb %al, %al
je 0x77e7
movl -0x38(%rbp), %eax
xorl %edx, %edx
divl -0x2c(%rbp)
subl %eax, %r15d
jmp 0x77e7
addl -0x34(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x77e7
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x220b
testb %al, %al
je 0x77e7
movl -0x34(%rbp), %eax
jmp 0x77df
addl -0x3c(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x77e7
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x220b
testb %al, %al
je 0x77e7
movl -0x3c(%rbp), %eax
jmp 0x77df
addl -0x40(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x77e7
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x220b
testb %al, %al
je 0x77e7
movl -0x40(%rbp), %eax
jmp 0x77df
addl -0x38(%rbp), %r15d
cmpb $0x0, -0x30(%rbp)
je 0x77e7
movq %rbx, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x220b
testb %al, %al
je 0x77e7
movl -0x38(%rbp), %eax
xorl %edx, %edx
divl -0x2c(%rbp)
addl %eax, %r15d
incl %r12d
cmpl $0x8, %r12d
jne 0x75ee
incl %r14d
cmpl $0x8, %r14d
jne 0x75cf
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq