// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rdi, %r14
leaq 0x18(%rdi), %rbx
movq 0x20(%rdi), %rdx
subq 0x18(%rdi), %rdx
shrq $0x2, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x23f4
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c2e
addq $0x48, %r14
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3cae
leaq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x3c2e
leaq 0x28(%rsp), %rdi
callq 0x2b79
movl %eax, %ebp
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x2fb4
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x1100
testb %bpl, %bpl
je 0x2fc9
movl $0x64, %r14d
movq 0x40(%rsp), %rdx
jmp 0x339b
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq %rcx, %rdx
addq $-0x3, %rdx
je 0x30ce
xorl %edi, %edi
movl $0x1, %esi
xorl %r14d, %r14d
movq %rdi, %r8
movslq (%rax,%rdi,4), %r9
incq %rdi
cmpq %rdi, %rcx
jbe 0x3072
imulq $0x4ec4ec4f, %r9, %r10  # imm = 0x4EC4EC4F
movq %r10, %r11
shrq $0x3f, %r11
sarq $0x22, %r10
addl %r11d, %r10d
leal (%r10,%r10,2), %r11d
leal (%r10,%r11,4), %r10d
movl %r9d, %r11d
subl %r10d, %r11d
xorl %r10d, %r10d
movq %rsi, %r15
movslq (%rax,%r15,4), %r12
imulq $0x4ec4ec4f, %r12, %r13 # imm = 0x4EC4EC4F
movq %r13, %rbp
shrq $0x3f, %rbp
sarq $0x22, %r13
addl %ebp, %r13d
leal (%r13,%r13,2), %ebp
leal (%r13,%rbp,4), %ebp
subl %ebp, %r12d
xorl %ebp, %ebp
cmpl %r12d, %r11d
sete %bpl
addl %ebp, %r10d
incq %r15
cmpq %r15, %rcx
ja 0x3029
leal 0xf(%r14), %r11d
cmpl $0x3, %r10d
cmovnel %r14d, %r11d
movl %r11d, %r14d
cmpl $0xa, %r9d
jge 0x30a1
leal 0x1(%r9), %r10d
cmpl (%rax,%rdi,4), %r10d
jne 0x30a1
leal 0x2(%r9), %r10d
cmpl 0x8(%rax,%r8,4), %r10d
jne 0x30a1
addl $0x3, %r9d
cmpl 0xc(%rax,%r8,4), %r9d
leal 0xf(%r14), %r8d
cmovnel %r14d, %r8d
movl %r8d, %r14d
incq %rsi
cmpq %rdx, %rdi
jne 0x2ff1
movq %rcx, %rsi
movq 0x40(%rsp), %rdx
addq $-0x2, %rsi
movq %rsi, (%rsp)
jne 0x30df
movl $0x1, %esi
movq %rsi, 0x8(%rsp)
jmp 0x3220
xorl %r14d, %r14d
movl $0x1, %edx
movq %rdx, (%rsp)
movq 0x40(%rsp), %rdx
movl $0x1, %esi
xorl %r8d, %r8d
movq %r8, %r11
movslq (%rax,%r8,4), %r10
imulq $0x4ec4ec4f, %r10, %r8  # imm = 0x4EC4EC4F
movq %r8, %r9
shrq $0x3f, %r9
sarq $0x22, %r8
addl %r9d, %r8d
leal (%r8,%r8,2), %r9d
leal (%r8,%r9,4), %r8d
movl %r10d, %r9d
subl %r8d, %r9d
leaq 0x1(%r11), %r8
cmpq %r8, %rcx
jbe 0x31a4
xorl %ebp, %ebp
movq %rsi, %r15
movslq (%rax,%r15,4), %r12
imulq $0x4ec4ec4f, %r12, %r13 # imm = 0x4EC4EC4F
movq %r13, %rdi
shrq $0x3f, %rdi
sarq $0x22, %r13
addl %edi, %r13d
leal (%r13,%r13,2), %edi
leal (%r13,%rdi,4), %edi
subl %edi, %r12d
xorl %edi, %edi
cmpl %r12d, %r9d
sete %dil
addl %edi, %ebp
incq %r15
cmpq %r15, %rcx
ja 0x3123
cmpl $0x2, %ebp
jne 0x31a4
addl $0x9, %r14d
cmpq %rbx, %rdx
je 0x31a4
movq %rdx, %r15
movslq 0x10(%r15), %rdi
imulq $0x4ec4ec4f, %rdi, %r12 # imm = 0x4EC4EC4F
movq %r12, %r13
shrq $0x3f, %r13
sarq $0x22, %r12
addl %r13d, %r12d
leal (%r12,%r12,2), %r13d
leal (%r12,%r13,4), %ebp
subl %ebp, %edi
xorl %ebp, %ebp
cmpl %r9d, %edi
sete %bpl
addl %ebp, %r14d
movq (%r15), %r15
cmpq %rbx, %r15
jne 0x316d
cmpl $0xa, %r9d
jg 0x3202
leal 0x1(%r10), %edi
cmpl (%rax,%r8,4), %edi
jne 0x3202
leal 0x2(%r10), %edi
cmpl 0x8(%rax,%r11,4), %edi
jne 0x3202
addl $0x9, %r14d
cmpq %rbx, %rdx
je 0x3202
leal -0x1(%r10), %r11d
addl $0x3, %r10d
movq %rdx, %r15
movl 0x10(%r15), %ebp
testl %r9d, %r9d
jle 0x31ee
xorl %edi, %edi
cmpl %r11d, %ebp
sete %dil
addl %edi, %r14d
cmpl $0xa, %r9d
je 0x31fa
xorl %edi, %edi
cmpl %r10d, %ebp
sete %dil
addl %edi, %r14d
movq (%r15), %r15
cmpq %rbx, %r15
jne 0x31d3
incq %rsi
cmpq (%rsp), %r8
jne 0x30e7
movq %rcx, %rsi
decq %rsi
movq %rsi, 0x8(%rsp)
je 0x339b
movl $0x1, %esi
xorl %r8d, %r8d
movslq (%rax,%r8,4), %r10
imulq $0x4ec4ec4f, %r10, %rdi # imm = 0x4EC4EC4F
movq %rdi, %r9
shrq $0x3f, %r9
sarq $0x22, %rdi
addl %r9d, %edi
leal (%rdi,%rdi,2), %r9d
leal (%rdi,%r9,4), %edi
movl %r10d, %r9d
subl %edi, %r9d
incq %r8
movq %r8, (%rsp)
cmpq %r8, %rcx
jbe 0x32ea
xorl %r11d, %r11d
movq %rsi, %r15
movslq (%rax,%r15,4), %rdi
imulq $0x4ec4ec4f, %rdi, %r12 # imm = 0x4EC4EC4F
movq %r12, %r13
shrq $0x3f, %r13
sarq $0x22, %r12
addl %r13d, %r12d
leal (%r12,%r12,2), %r13d
leal (%r12,%r13,4), %ebp
subl %ebp, %edi
xorl %ebp, %ebp
cmpl %edi, %r9d
sete %bpl
addl %ebp, %r11d
incq %r15
cmpq %r15, %rcx
ja 0x3265
cmpq %rbx, %rdx
sete %dil
cmpl $0x1, %r11d
setne %r11b
orb %dil, %r11b
movq %rdx, %r11
jne 0x32ea
movslq 0x10(%r11), %rdi
imulq $0x4ec4ec4f, %rdi, %r15 # imm = 0x4EC4EC4F
movq %r15, %r12
shrq $0x3f, %r12
sarq $0x22, %r15
addl %r12d, %r15d
leal (%r15,%r15,2), %r12d
leal (%r15,%r12,4), %ebp
subl %ebp, %edi
xorl %ebp, %ebp
cmpl %r9d, %edi
sete %bpl
addl %ebp, %r14d
movq (%r11), %r11
cmpq %rbx, %r11
jne 0x32b3
cmpl $0xb, %r9d
jg 0x3389
cmpq %rbx, %rdx
sete %dil
leal 0x1(%r10), %r11d
movq (%rsp), %r8
cmpl (%rax,%r8,4), %r11d
setne %r11b
orb %dil, %r11b
jne 0x3389
leal -0x1(%r10), %r11d
leal -0x2(%r10), %ebp
leal 0x2(%r10), %r15d
addl $0x3, %r10d
movq %rdx, %r12
movl 0x10(%r12), %r13d
testl %r9d, %r9d
jle 0x3359
cmpl %r11d, %r13d
jne 0x3353
incl %r14d
movq %rdx, %rdi
cmpl $0x1, %r9d
je 0x334b
xorl %r8d, %r8d
cmpl %ebp, 0x10(%rdi)
sete %r8b
addl %r8d, %r14d
movq (%rdi), %rdi
cmpq %rbx, %rdi
jne 0x3338
cmpl $0xb, %r9d
je 0x3380
cmpl %r15d, %r13d
jne 0x3380
incl %r14d
movq %rdx, %rdi
cmpl $0x9, %r9d
jg 0x3378
xorl %r8d, %r8d
cmpl %r10d, 0x10(%rdi)
sete %r8b
addl %r8d, %r14d
movq (%rdi), %rdi
cmpq %rbx, %rdi
jne 0x3364
movq (%r12), %r12
cmpq %rbx, %r12
jne 0x3323
incq %rsi
movq (%rsp), %r8
cmpq 0x8(%rsp), %r8
jne 0x3228
cmpq %rbx, %rdx
je 0x33b8
movq (%rdx), %r15
movl $0x18, %esi
movq %rdx, %rdi
callq 0x1100
movq %r15, %rdx
cmpq %rbx, %r15
jne 0x33a0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x33cf
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1100
movl %r14d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x3400
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x1100
jmp 0x3400
movq %rax, %r14
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3424
movq (%rdi), %r15
movl $0x18, %esi
callq 0x1100
movq %r15, %rdi
cmpq %rbx, %r15
jne 0x340a
jmp 0x3424
movq %rax, %r14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x343b
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1100
movq %r14, %rdi
callq 0x1150