// File: /workspace/github_cpp_makefile_star2_O3/tyhsu[P]Ewn/minimax.cpp
// Function: Minimax::evaluate_feature(Game&)
int Minimax::evaluate_feature(Game& cur_game)
{
	int ai_val = 0, opp_val = 0;
	bool cur_side = this->ai_side;
	Heuristic_map *feature_map = this->ai_side? &this->feature_map_B: &this->feature_map_A;
	int *val_ptr = &ai_val;

	for (int side=0; side<2; side++) {
		for (int chs_index = 0; chs_index < 6; chs_index++) {
			// calculate the score of the side
			Chess chs = cur_game.get_chs_list(cur_side, chs_index);
			if (chs.exist) {
				// [exist] chess calculating function.
				Pos temp;
				temp.first = chs.x;
				temp.second = chs.y;
				*val_ptr += (*feature_map)[temp];
			}
			else {
				// [non-exist] chess calculating function.
				// prev_chs_val: find closest, previous chess num's value.
				// next_chs_val: find closest, following chess num's value.
				int prev_chs_val = 0, next_chs_val = 0;
				// get small part of chess list.
				for (int prev_chs_index = chs_index; prev_chs_index > 0; prev_chs_index--) {
					Chess ctmp = cur_game.get_chs_list(cur_side, prev_chs_index);
					if (ctmp.exist) {
						Pos temp;
						temp.first = ctmp.x;
						temp.second = ctmp.y;
						prev_chs_val = (*feature_map)[temp];
						break;
					}
				}

				// get big part of chess list.
				for (int next_chs_index = chs_index; next_chs_index < 6; next_chs_index++) {
					Chess ctmp = cur_game.get_chs_list(cur_side, next_chs_index);
					if (ctmp.exist) {
						Pos temp;
						temp.first = ctmp.x;
						temp.second = ctmp.y;
						next_chs_val = (*feature_map)[temp];
						break;
					}
				}

				// select one.
				prev_chs_val = (prev_chs_val > next_chs_val ? prev_chs_val : next_chs_val);
				*val_ptr += prev_chs_val;
			}
		}

		// calculate the score of opponent's side
		cur_side = !this->ai_side;
		feature_map = this->ai_side? &this->feature_map_A: &this->feature_map_B;
		val_ptr = &opp_val;
	}

	return ai_val - (int)(opp_val*1.2);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
xorl %eax, %eax
leaq -0x50(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movl %eax, (%rcx)
leaq -0x3c(%rbp), %rcx
movl %eax, (%rcx)
movb 0x10(%rdi), %al
leaq 0x48(%rdi), %rcx
movq %rdi, -0x68(%rbp)
leaq 0x18(%rdi), %r14
testb %al, %al
movq %r14, -0x58(%rbp)
movq %rcx, -0x60(%rbp)
cmovneq %rcx, %r14
movb $0x1, %cl
leaq -0x30(%rbp), %r15
leaq -0x2c(%rbp), %r12
movl %ecx, -0x4c(%rbp)
movl $0x0, -0x30(%rbp)
movzbl %al, %r13d
andl $0x1, %r13d
movq %rbx, %rdi
movl %r13d, %esi
movq %r15, %rdx
callq 0x852a
btl $0x8, %eax
jb 0x6e44
movl -0x30(%rbp), %eax
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jle 0x6e64
movq %rbx, %rdi
movl %r13d, %esi
movq %r12, %rdx
callq 0x852a
btl $0x8, %eax
jb 0x6e6c
movl -0x2c(%rbp), %eax
leal -0x1(%rax), %ecx
movl %ecx, -0x2c(%rbp)
cmpl $0x1, %eax
jg 0x6e1d
xorl %r15d, %r15d
jmp 0x6e85
shrq $0x20, %rax
movl %eax, -0x38(%rbp)
movl %edx, -0x34(%rbp)
movq %r14, %rdi
leaq -0x38(%rbp), %rsi
callq 0x7324
movl (%rax), %eax
movq -0x48(%rbp), %rcx
addl %eax, (%rcx)
jmp 0x6edf
movl %eax, -0x2c(%rbp)
xorl %r15d, %r15d
jmp 0x6e90
shrq $0x20, %rax
movl %eax, -0x38(%rbp)
movl %edx, -0x34(%rbp)
movq %r14, %rdi
leaq -0x38(%rbp), %rsi
callq 0x7324
movl (%rax), %r15d
movl -0x30(%rbp), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x5, %eax
jg 0x6eb2
movq %rbx, %rdi
movl %r13d, %esi
movq %r12, %rdx
callq 0x852a
btl $0x8, %eax
jb 0x6eb6
movl -0x2c(%rbp), %eax
leal 0x1(%rax), %ecx
movl %ecx, -0x2c(%rbp)
cmpl $0x5, %eax
jl 0x6e90
xorl %eax, %eax
jmp 0x6ece
shrq $0x20, %rax
movl %eax, -0x38(%rbp)
movl %edx, -0x34(%rbp)
movq %r14, %rdi
leaq -0x38(%rbp), %rsi
callq 0x7324
movl (%rax), %eax
cmpl %eax, %r15d
cmovgl %r15d, %eax
movq -0x48(%rbp), %rcx
addl %eax, (%rcx)
leaq -0x30(%rbp), %r15
movl -0x30(%rbp), %eax
leal 0x1(%rax), %ecx
movl %ecx, -0x30(%rbp)
cmpl $0x5, %eax
jl 0x6dff
movq -0x68(%rbp), %rax
movb 0x10(%rax), %cl
movl %ecx, %eax
xorb $0x1, %al
testb %cl, %cl
movq -0x60(%rbp), %r14
cmovneq -0x58(%rbp), %r14
leaq -0x3c(%rbp), %rcx
movq %rcx, -0x48(%rbp)
testb $0x1, -0x4c(%rbp)
movl $0x0, %ecx
jne 0x6ded
movl -0x50(%rbp), %eax
cvtsi2sdl -0x3c(%rbp), %xmm0
mulsd 0x28ba(%rip), %xmm0     # 0x97e8
cvttsd2si %xmm0, %ecx
subl %ecx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop