// File: /workspace/github_cpp_makefile_star2_O2/ljc2154[P]Rummy/rummy.cpp
// Function: evaluate(State)
int evaluate(State state)
{
	mergeSort(state.agentHand, 0, state.agentHand.size());
	vector<int> hand = state.agentHand;
	list<int> remaining = state.remaining;
	int score = 0;					// score of hand
	int rank;								// rank of card
	int val;								// stored value of card

	/* Check if hand is a winner */
	if (isWinner(hand))
		return 100;

	/* Check for 4 of a kind or 4 in a row */
	for (int i = 0; i < hand.size() - 3; i++)
	{
		val = hand[i];
		rank = val % 13;
		/* Check for 4 of a kind */
		int matchCount = 0;
		for (int j = i + 1; j < hand.size(); j++)
		{
			if (rank == hand[j] % 13)
				matchCount++;
		}
		if (matchCount == 3)
		{
			score += 15;
			matchCount = 0;
		}

		/* Check for 4 in a row */
		if (val < 10 && val+1 == hand[i+1] && val+2 == hand[i+2]
				&& val+3 == hand[i+3])
			score += 15;
	}

	/* Check for 3 of a kind or 3 in a row */
	for (int i = 0; i < hand.size() - 2; i++)
	{
		val = hand[i];
		rank = val % 13;

		/* Check for 3 of a kind */
		int matchCount = 0;
		for (int j = i + 1; j < hand.size(); j++)
		{
			if (rank == hand[j] % 13)
				matchCount++;
		}
		if (matchCount == 2)
		{
			score += 9;
			/* Check for 1 of the same kind in remaining cards */
			for (list<int>::iterator it = remaining.begin();
						it != remaining.end(); it++)
				if (*it % 13 == rank)
					score ++;
			matchCount = 0;
		}	

		/* Check for 3 in a row */
		if (rank < 11 && val+1 == hand[i+1] && val+2 == hand[i+2])
		{
			score += 9;
			/* Check for 1 before and 1 after in remaining cards */
			for (list<int>::iterator it = remaining.begin();
						it != remaining.end(); it++)
			{
				if (rank > 0 && *it == val - 1)
					score++;
				if (rank < 10 && *it == val + 3)
					score++;
			}
		}
	}

	/* Check for 2 of a kind or 2 in a row */
	for (int i = 0; i < hand.size() - 1; i++)
	{
		val = hand[i];
		rank = val % 13;

		/* Check for 2 of a kind */
		int matchCount = 0;
		for (int j = i + 1; j < hand.size(); j++)
		{
			if (rank == hand[j] % 13)
				matchCount++;
		}
		if (matchCount == 1)
		{
			/* Check to see if any remain */
			for (list<int>::iterator it = remaining.begin();
						it != remaining.end(); it++)
				if (*it % 13 == rank)
					score++;
			matchCount = 0;
		}

		/* Check for 2 in a row */
		if (rank < 12 && val+1 == hand[i+1])
		{
			/* Check for 1 or 2 before and 1 or 2 after in remaining cards */
			for (list<int>::iterator it = remaining.begin();
						it != remaining.end(); it++)
			{
				if (rank > 0 && *it == val - 1)
				{
					score++;
					for (list<int>::iterator jt = remaining.begin();
						jt != remaining.end(); jt++)
						if (rank > 1 && *jt == val - 2)
							score++;
				}
				if (rank < 11 && *it == val + 2)
				{
					score++;
					for (list<int>::iterator jt = remaining.begin();
						jt != remaining.end(); jt++)
						if (rank < 10 && *jt == val + 3)
							score++;
				}
			}
		}			
	}

	return score;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
leaq 0x18(%rdi), %r14
movq 0x20(%rdi), %rdx
subq 0x18(%rdi), %rdx
shrq $0x2, %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1c0e
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x2b4e
addq $0x48, %rbx
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2bd6
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x2b4e
leaq 0x38(%rsp), %rdi
callq 0x21a9
movl %eax, %ebx
leaq 0x38(%rsp), %rdi
callq 0x2c46
testb %bl, %bl
je 0x244b
pushq $0x64
popq %rbx
leaq 0x50(%rsp), %rdi
callq 0x2c10
leaq 0x20(%rsp), %rdi
callq 0x2c46
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r8
subq %rdi, %r8
sarq $0x2, %r8
leaq -0x3(%r8), %r10
xorl %eax, %eax
pushq $0x1
popq %r11
pushq $0xd
popq %r9
xorl %ebx, %ebx
cmpq %r10, %rax
je 0x24da
movq %rax, %r15
movl (%rdi,%rax,4), %ecx
movl %ecx, %eax
cltd
idivl %r9d
movl %edx, %esi
xorl %ebp, %ebp
movq %r11, %r12
cmpq %r12, %r8
jbe 0x249f
movl (%rdi,%r12,4), %eax
cltd
idivl %r9d
xorl %eax, %eax
cmpl %edx, %esi
sete %al
addl %eax, %ebp
incq %r12
jmp 0x2484
leaq 0x1(%r15), %rax
leal 0xf(%rbx), %edx
cmpl $0x3, %ebp
cmovnel %ebx, %edx
cmpl $0xa, %ecx
jge 0x24d3
leal 0x1(%rcx), %esi
cmpl (%rdi,%rax,4), %esi
jne 0x24d3
leal 0x2(%rcx), %esi
cmpl 0x8(%rdi,%r15,4), %esi
jne 0x24d3
addl $0x3, %ecx
cmpl 0xc(%rdi,%r15,4), %ecx
leal 0xf(%rdx), %ecx
cmovnel %edx, %ecx
movl %ecx, %edx
incq %r11
movl %edx, %ebx
jmp 0x246c
leaq -0x2(%r8), %r10
pushq $0x1
popq %r11
xorl %eax, %eax
cmpq %r10, %rax
je 0x25a1
movq %rax, %r15
movl (%rdi,%rax,4), %ecx
movl %ecx, %eax
cltd
idivl %r9d
movl %edx, %esi
xorl %ebp, %ebp
movq %r11, %r12
cmpq %r12, %r8
jbe 0x251b
movl (%rdi,%r12,4), %eax
cltd
idivl %r9d
xorl %eax, %eax
cmpl %edx, %esi
sete %al
addl %eax, %ebp
incq %r12
jmp 0x2500
cmpl $0x2, %ebp
jne 0x2543
addl $0x9, %ebx
movq %r14, %r12
movq (%r12), %r12
cmpq %r14, %r12
je 0x2543
movl 0x10(%r12), %eax
cltd
idivl %r9d
xorl %eax, %eax
cmpl %esi, %edx
sete %al
addl %eax, %ebx
jmp 0x2526
leaq 0x1(%r15), %rax
cmpl $0xa, %esi
jg 0x2599
leal 0x1(%rcx), %edx
cmpl (%rdi,%rax,4), %edx
jne 0x2599
leal 0x2(%rcx), %edx
cmpl 0x8(%rdi,%r15,4), %edx
jne 0x2599
addl $0x9, %ebx
leal -0x1(%rcx), %edx
addl $0x3, %ecx
movq %r14, %r15
movq (%r15), %r15
cmpq %r14, %r15
je 0x2599
movl 0x10(%r15), %ebp
testl %esi, %esi
jle 0x258b
xorl %r12d, %r12d
cmpl %edx, %ebp
sete %r12b
addl %r12d, %ebx
cmpl $0xa, %esi
je 0x256a
xorl %r12d, %r12d
cmpl %ecx, %ebp
sete %r12b
addl %r12d, %ebx
jmp 0x256a
incq %r11
jmp 0x24e4
leaq -0x1(%r8), %r15
pushq $0x1
popq %r12
leaq 0x50(%rsp), %r14
movq (%r14), %r11
xorl %ebp, %ebp
movq %r15, 0x8(%rsp)
cmpq %r15, %rbp
je 0x2426
movl (%rdi,%rbp,4), %ecx
movl %ecx, %eax
cltd
idivl %r9d
movl %edx, %esi
xorl %r10d, %r10d
movq %r12, %r13
cmpq %r13, %r8
jbe 0x25ee
movl (%rdi,%r13,4), %eax
cltd
idivl %r9d
xorl %eax, %eax
cmpl %edx, %esi
sete %al
addl %eax, %r10d
incq %r13
jmp 0x25d2
cmpl $0x1, %r10d
jne 0x2612
movq %r14, %r10
movq (%r10), %r10
cmpq %r14, %r10
je 0x2612
movl 0x10(%r10), %eax
cltd
idivl %r9d
xorl %eax, %eax
cmpl %esi, %edx
sete %al
addl %eax, %ebx
jmp 0x25f7
incq %rbp
cmpl $0xb, %esi
jg 0x26b8
leal 0x1(%rcx), %eax
cmpl (%rdi,%rbp,4), %eax
jne 0x26b8
movq %rbp, 0x10(%rsp)
movq %r12, 0x18(%rsp)
leal -0x1(%rcx), %eax
leal -0x2(%rcx), %edx
leal 0x2(%rcx), %ebp
addl $0x3, %ecx
movq %r11, %r13
cmpq %r14, %r13
je 0x26a9
movl 0x10(%r13), %r10d
testl %esi, %esi
jle 0x267c
cmpl %eax, %r10d
jne 0x2677
incl %ebx
movq %r11, %r15
cmpq %r14, %r15
je 0x2677
cmpl $0x1, %esi
je 0x2672
xorl %r12d, %r12d
cmpl %edx, 0x10(%r15)
sete %r12b
addl %r12d, %ebx
movq (%r15), %r15
jmp 0x265a
cmpl $0xb, %esi
je 0x26a3
cmpl %ebp, %r10d
jne 0x26a3
incl %ebx
movq %r11, %r10
cmpq %r14, %r10
je 0x26a3
cmpl $0x9, %esi
jg 0x269e
xorl %r15d, %r15d
cmpl %ecx, 0x10(%r10)
sete %r15b
addl %r15d, %ebx
movq (%r10), %r10
jmp 0x2686
movq (%r13), %r13
jmp 0x2643
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %rbp
incq %r12
jmp 0x25b8
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x2c46
jmp 0x26d2
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2c10
jmp 0x26e1
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2c46
movq %rbx, %rdi
callq 0x1160