// File: /workspace/github_cpp_makefile_star2_O1/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 0x23c7
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x3bc8
addq $0x48, %r14
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3c48
leaq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x3bc8
leaq 0x28(%rsp), %rdi
callq 0x2b39
movl %eax, %ebp
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x2f68
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x1100
movl $0x64, %r14d
testb %bpl, %bpl
jne 0x3332
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq %rcx, %rdx
addq $-0x3, %rdx
je 0x3063
xorl %edi, %edi
movl $0x1, %esi
xorl %r10d, %r10d
movq %rdi, %r8
movslq (%rax,%rdi,4), %r9
incq %rdi
xorl %r11d, %r11d
cmpq %rdi, %rcx
jbe 0x3018
imulq $0x4ec4ec4f, %r9, %r14  # imm = 0x4EC4EC4F
movq %r14, %r15
shrq $0x3f, %r15
sarq $0x22, %r14
addl %r15d, %r14d
leal (%r14,%r14,2), %r15d
leal (%r14,%r15,4), %r14d
movl %r9d, %ebp
subl %r14d, %ebp
movq %rsi, %r14
movslq (%rax,%r14,4), %r15
imulq $0x4ec4ec4f, %r15, %r12 # imm = 0x4EC4EC4F
movq %r12, %r13
shrq $0x3f, %r13
sarq $0x22, %r12
addl %r13d, %r12d
leal (%r12,%r12,2), %r13d
leal (%r12,%r13,4), %r12d
subl %r12d, %r15d
xorl %r12d, %r12d
cmpl %r15d, %ebp
sete %r12b
addl %r12d, %r11d
incq %r14
cmpq %r14, %rcx
ja 0x2fd7
cmpl $0x3, %r11d
sete %r11b
leal 0xf(%r10), %r14d
testb %r11b, %r11b
cmovel %r10d, %r14d
cmpl $0xa, %r9d
jge 0x3052
leal 0x1(%r9), %r10d
cmpl (%rax,%rdi,4), %r10d
jne 0x3052
leal 0x2(%r9), %r10d
cmpl 0x8(%rax,%r8,4), %r10d
jne 0x3052
addl $0x3, %r9d
cmpl 0xc(%rax,%r8,4), %r9d
leal 0xf(%r14), %r8d
cmovnel %r14d, %r8d
movl %r8d, %r14d
incq %rsi
movl %r14d, %r10d
cmpq %rdx, %rdi
jne 0x2f9f
jmp 0x3066
xorl %r14d, %r14d
movq %rcx, %rdx
addq $-0x2, %rdx
je 0x31ab
movl $0x1, %esi
movq 0x40(%rsp), %rdi
movq %rdi, 0x8(%rsp)
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 0x3141
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 0x30c1
cmpl $0x2, %ebp
jne 0x3141
addl $0x9, %r14d
movq 0x8(%rsp), %r15
cmpq %rbx, %r15
je 0x3141
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
jmp 0x3108
cmpl $0xa, %r9d
jg 0x319f
leal 0x1(%r10), %edi
cmpl (%rax,%r8,4), %edi
jne 0x319f
leal 0x2(%r10), %edi
cmpl 0x8(%rax,%r11,4), %edi
jne 0x319f
addl $0x9, %r14d
movq 0x8(%rsp), %r15
cmpq %rbx, %r15
je 0x319f
leal -0x1(%r10), %r11d
addl $0x3, %r10d
testl %r9d, %r9d
jle 0x3184
xorl %edi, %edi
cmpl %r11d, 0x10(%r15)
sete %dil
addl %edi, %r14d
cmpl $0xa, %r9d
je 0x3197
xorl %edi, %edi
cmpl %r10d, 0x10(%r15)
sete %dil
addl %edi, %r14d
movq (%r15), %r15
cmpq %rbx, %r15
jne 0x3172
incq %rsi
cmpq %rdx, %r8
jne 0x3085
movq %rcx, %rdx
decq %rdx
movq %rdx, 0x8(%rsp)
je 0x3332
movl $0x1, %esi
movq 0x40(%rsp), %rdi
xorl %r8d, %r8d
movslq (%rax,%r8,4), %r10
imulq $0x4ec4ec4f, %r10, %r9  # imm = 0x4EC4EC4F
movq %r9, %r11
shrq $0x3f, %r11
sarq $0x22, %r9
addl %r11d, %r9d
leal (%r9,%r9,2), %r11d
leal (%r9,%r11,4), %r11d
movl %r10d, %r9d
subl %r11d, %r9d
incq %r8
cmpq %r8, %rcx
jbe 0x328b
xorl %r11d, %r11d
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, %r9d
sete %bpl
addl %ebp, %r11d
incq %r15
cmpq %r15, %rcx
ja 0x3202
cmpq %rbx, %rdi
sete %bpl
cmpl $0x1, %r11d
setne %r11b
orb %bpl, %r11b
movq %rdi, %r11
jne 0x328b
movslq 0x10(%r11), %r15
imulq $0x4ec4ec4f, %r15, %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, %r15d
xorl %ebp, %ebp
cmpl %r9d, %r15d
sete %bpl
addl %ebp, %r14d
movq (%r11), %r11
cmpq %rbx, %r11
jne 0x3253
cmpl $0xb, %r9d
jg 0x3324
cmpq %rbx, %rdi
sete %r11b
leal 0x1(%r10), %ebp
cmpl (%rax,%r8,4), %ebp
setne %bpl
orb %r11b, %bpl
jne 0x3324
leal -0x1(%r10), %r11d
leal -0x2(%r10), %ebp
leal 0x2(%r10), %r15d
addl $0x3, %r10d
movq %rdi, %r12
testl %r9d, %r9d
jle 0x32ed
cmpl %r11d, 0x10(%r12)
jne 0x32ed
incl %r14d
movq %rdi, %r13
cmpl $0x1, %r9d
je 0x32e4
xorl %edx, %edx
cmpl %ebp, 0x10(%r13)
sete %dl
addl %edx, %r14d
movq (%r13), %r13
cmpq %rbx, %r13
jne 0x32d2
cmpl $0xb, %r9d
je 0x331b
cmpl %r15d, 0x10(%r12)
jne 0x331b
incl %r14d
movq %rdi, %r13
cmpl $0x9, %r9d
jg 0x3312
xorl %edx, %edx
cmpl %r10d, 0x10(%r13)
sete %dl
addl %edx, %r14d
movq (%r13), %r13
cmpq %rbx, %r13
jne 0x3300
movq (%r12), %r12
cmpq %rbx, %r12
jne 0x32c0
incq %rsi
cmpq 0x8(%rsp), %r8
jne 0x31c9
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3351
movq (%rdi), %r15
movl $0x18, %esi
callq 0x1100
movq %r15, %rdi
cmpq %rbx, %r15
jne 0x333c
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3368
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 0x3399
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x1100
jmp 0x3399
movq %rax, %r14
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x33bd
movq (%rdi), %r15
movl $0x18, %esi
callq 0x1100
movq %r15, %rdi
cmpq %rbx, %r15
jne 0x33a3
jmp 0x33bd
movq %rax, %r14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x33d4
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x1100
movq %r14, %rdi
callq 0x1150