// File: 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
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x138(%rbp)
movq %rdi, -0x10(%rbp)
addq $0x18, %rdi
movq %rdi, -0x140(%rbp)
callq 0x4130
movq -0x140(%rbp), %rdi
movl %eax, %edx
xorl %esi, %esi
callq 0x22f0
movq -0x140(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x44c0
movq -0x138(%rbp), %rsi
addq $0x48, %rsi
leaq -0x40(%rbp), %rdi
callq 0x4710
jmp 0x315c
movl $0x0, -0x50(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x44c0
jmp 0x3172
leaq -0x70(%rbp), %rdi
callq 0x2ad0
movb %al, -0x141(%rbp)
jmp 0x3183
leaq -0x70(%rbp), %rdi
callq 0x41e0
movb -0x141(%rbp), %al
testb $0x1, %al
jne 0x3198
jmp 0x31e7
movl $0x64, -0x4(%rbp)
movl $0x1, -0x74(%rbp)
jmp 0x399a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x39c1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x39b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x41e0
jmp 0x39b8
movl $0x0, -0x78(%rbp)
movslq -0x78(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4130
movq %rax, %rcx
movq -0x150(%rbp), %rax
subq $0x3, %rcx
cmpq %rcx, %rax
jae 0x3366
movslq -0x78(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
movl $0xd, %ecx
cltd
idivl %ecx
movl %edx, -0x54(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
movslq -0x80(%rbp), %rax
movq %rax, -0x158(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4130
movq %rax, %rcx
movq -0x158(%rbp), %rax
cmpq %rcx, %rax
jae 0x32ac
movl -0x54(%rbp), %eax
movl %eax, -0x15c(%rbp)
movslq -0x80(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movl (%rax), %eax
movl $0xd, %ecx
cltd
idivl %ecx
movl -0x15c(%rbp), %eax
cmpl %edx, %eax
jne 0x329f
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x32a1
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x3249
cmpl $0x3, -0x7c(%rbp)
jne 0x32c2
movl -0x50(%rbp), %eax
addl $0xf, %eax
movl %eax, -0x50(%rbp)
movl $0x0, -0x7c(%rbp)
cmpl $0xa, -0x58(%rbp)
jge 0x3356
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x160(%rbp)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movq %rax, %rcx
movl -0x160(%rbp), %eax
cmpl (%rcx), %eax
jne 0x3356
movl -0x58(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x164(%rbp)
movl -0x78(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movq %rax, %rcx
movl -0x164(%rbp), %eax
cmpl (%rcx), %eax
jne 0x3356
movl -0x58(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x168(%rbp)
movl -0x78(%rbp), %eax
addl $0x3, %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movq %rax, %rcx
movl -0x168(%rbp), %eax
cmpl (%rcx), %eax
jne 0x3356
movl -0x50(%rbp), %eax
addl $0xf, %eax
movl %eax, -0x50(%rbp)
jmp 0x3358
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x31ee
movl $0x0, -0x84(%rbp)
movslq -0x84(%rbp), %rax
movq %rax, -0x170(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4130
movq %rax, %rcx
movq -0x170(%rbp), %rax
subq $0x2, %rcx
cmpq %rcx, %rax
jae 0x3617
movslq -0x84(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
movl $0xd, %ecx
cltd
idivl %ecx
movl %edx, -0x54(%rbp)
movl $0x0, -0x88(%rbp)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
movslq -0x8c(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4130
movq %rax, %rcx
movq -0x178(%rbp), %rax
cmpq %rcx, %rax
jae 0x344f
movl -0x54(%rbp), %eax
movl %eax, -0x17c(%rbp)
movslq -0x8c(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movl (%rax), %eax
movl $0xd, %ecx
cltd
idivl %ecx
movl -0x17c(%rbp), %eax
cmpl %edx, %eax
jne 0x343c
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x343e
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x33da
cmpl $0x2, -0x88(%rbp)
jne 0x34e5
movl -0x50(%rbp), %eax
addl $0x9, %eax
movl %eax, -0x50(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x47d0
movq %rax, -0x98(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4830
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x4800
testb $0x1, %al
jne 0x349e
jmp 0x34db
leaq -0x98(%rbp), %rdi
callq 0x4860
movl (%rax), %eax
movl $0xd, %ecx
cltd
idivl %ecx
cmpl -0x54(%rbp), %edx
jne 0x34c2
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x34c4
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
callq 0x4880
movq %rax, -0xa8(%rbp)
jmp 0x3475
movl $0x0, -0x88(%rbp)
cmpl $0xb, -0x54(%rbp)
jge 0x3601
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x180(%rbp)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movq %rax, %rcx
movl -0x180(%rbp), %eax
cmpl (%rcx), %eax
jne 0x3601
movl -0x58(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x184(%rbp)
movl -0x84(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movq %rax, %rcx
movl -0x184(%rbp), %eax
cmpl (%rcx), %eax
jne 0x3601
movl -0x50(%rbp), %eax
addl $0x9, %eax
movl %eax, -0x50(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x47d0
movq %rax, -0xb0(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4830
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x4800
testb $0x1, %al
jne 0x3595
jmp 0x35ff
cmpl $0x0, -0x54(%rbp)
jle 0x35bc
leaq -0xb0(%rbp), %rdi
callq 0x4860
movl (%rax), %eax
movl -0x58(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x35bc
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
cmpl $0xa, -0x54(%rbp)
jge 0x35e3
leaq -0xb0(%rbp), %rdi
callq 0x4860
movl (%rax), %eax
movl -0x58(%rbp), %ecx
addl $0x3, %ecx
cmpl %ecx, %eax
jne 0x35e3
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x35e5
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
callq 0x4880
movq %rax, -0xc0(%rbp)
jmp 0x356c
jmp 0x3601
jmp 0x3603
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x3370
movl $0x0, -0xc4(%rbp)
movslq -0xc4(%rbp), %rax
movq %rax, -0x190(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4130
movq %rax, %rcx
movq -0x190(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x398d
movslq -0xc4(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movl (%rax), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
movl $0xd, %ecx
cltd
idivl %ecx
movl %edx, -0x54(%rbp)
movl $0x0, -0xc8(%rbp)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
movslq -0xcc(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4130
movq %rax, %rcx
movq -0x198(%rbp), %rax
cmpq %rcx, %rax
jae 0x3700
movl -0x54(%rbp), %eax
movl %eax, -0x19c(%rbp)
movslq -0xcc(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movl (%rax), %eax
movl $0xd, %ecx
cltd
idivl %ecx
movl -0x19c(%rbp), %eax
cmpl %edx, %eax
jne 0x36ed
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x36ef
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x368b
cmpl $0x1, -0xc8(%rbp)
jne 0x378d
leaq -0x40(%rbp), %rdi
callq 0x47d0
movq %rax, -0xd8(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4830
movq %rax, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x4800
testb $0x1, %al
jne 0x3746
jmp 0x3783
leaq -0xd8(%rbp), %rdi
callq 0x4860
movl (%rax), %eax
movl $0xd, %ecx
cltd
idivl %ecx
cmpl -0x54(%rbp), %edx
jne 0x376a
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x376c
leaq -0xd8(%rbp), %rdi
xorl %esi, %esi
callq 0x4880
movq %rax, -0xe8(%rbp)
jmp 0x371d
movl $0x0, -0xc8(%rbp)
cmpl $0xc, -0x54(%rbp)
jge 0x3977
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1a0(%rbp)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x4110
movq %rax, %rcx
movl -0x1a0(%rbp), %eax
cmpl (%rcx), %eax
jne 0x3977
leaq -0x40(%rbp), %rdi
callq 0x47d0
movq %rax, -0xf0(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4830
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x4800
testb $0x1, %al
jne 0x3805
jmp 0x3975
cmpl $0x0, -0x54(%rbp)
jle 0x38af
leaq -0xf0(%rbp), %rdi
callq 0x4860
movl (%rax), %eax
movl -0x58(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x38af
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x47d0
movq %rax, -0x100(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4830
movq %rax, -0x108(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x4800
testb $0x1, %al
jne 0x386d
jmp 0x38ad
cmpl $0x1, -0x54(%rbp)
jle 0x3894
leaq -0x100(%rbp), %rdi
callq 0x4860
movl (%rax), %eax
movl -0x58(%rbp), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jne 0x3894
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3896
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
callq 0x4880
movq %rax, -0x110(%rbp)
jmp 0x3844
jmp 0x38af
cmpl $0xb, -0x54(%rbp)
jge 0x3959
leaq -0xf0(%rbp), %rdi
callq 0x4860
movl (%rax), %eax
movl -0x58(%rbp), %ecx
addl $0x2, %ecx
cmpl %ecx, %eax
jne 0x3959
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x47d0
movq %rax, -0x118(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4830
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x4800
testb $0x1, %al
jne 0x3917
jmp 0x3957
cmpl $0xa, -0x54(%rbp)
jge 0x393e
leaq -0x118(%rbp), %rdi
callq 0x4860
movl (%rax), %eax
movl -0x58(%rbp), %ecx
addl $0x3, %ecx
cmpl %ecx, %eax
jne 0x393e
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3940
leaq -0x118(%rbp), %rdi
xorl %esi, %esi
callq 0x4880
movq %rax, -0x128(%rbp)
jmp 0x38ee
jmp 0x3959
jmp 0x395b
leaq -0xf0(%rbp), %rdi
xorl %esi, %esi
callq 0x4880
movq %rax, -0x130(%rbp)
jmp 0x37d9
jmp 0x3977
jmp 0x3979
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x3621
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x74(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x42a0
leaq -0x28(%rbp), %rdi
callq 0x41e0
movl -0x4(%rbp), %eax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x42a0
leaq -0x28(%rbp), %rdi
callq 0x41e0
movq -0x48(%rbp), %rdi
callq 0x1170
nopw %cs:(%rax,%rax)