// File: /workspace/github_cpp_makefile_star2_O3/Doug0855[P]TigerZone/Board.cpp
// Function: Board::getOptimalPlacement(Tile&, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>)
std::pair<int,int> Board::getOptimalPlacement(Tile &tile, std::vector< std::pair<int, int> > availableMoves)
{
	// Check that there's more than 1 possible place to put the tile
	if (availableMoves.size() > 1)
	{
		// Keep track of the orientation of the tile for when we find the best location
		int bestRotation = 0;
		std::pair<int, int> bestSpot;
		int mostPoints = -1; // set this to -1 otherwise if points is never > mostPoints bestSpot won't be selected
		for (size_t z = 0; z < availableMoves.size(); z++)
		{
			// set the row and col (i and j)
			int i = availableMoves[z].first;
			int j = availableMoves[z].second;
			// Check the available location with all possible tile rotations
			for (int q = 0; q < 4; q++)
			{
				int points = 0;
				// double check that the tile can connect to all neighbors and run the positionPoints method if it can
				if ((m_board[i + 1][j] == NULL || m_board[i + 1][j]->getUpFace()->faceEquals(*tile.getDownFace())) &&
				(m_board[i - 1][j] == NULL || m_board[i - 1][j]->getDownFace()->faceEquals(*tile.getUpFace())) &&
				(m_board[i][j + 1] == NULL || m_board[i][j + 1]->getLeftFace()->faceEquals(*tile.getRightFace())) &&
				(m_board[i][j - 1] == NULL || m_board[i][j - 1]->getRightFace()->faceEquals(*tile.getLeftFace())))
				{
					points = positionPoints(i, j);
				}
				else
				{
					points = -1;
				}
				if (points > mostPoints)
				{
					mostPoints = points;
					bestSpot = availableMoves[z];
					bestRotation = q;
				}
				tile.rotate();
			}
		}
		for (int i = 0; i < bestRotation; i++)
		{
			tile.rotate();
		}
		return bestSpot;
	}
	// If there was only 1 available location to place the tile
	else
	{
		int i = availableMoves[0].first;
		int j = availableMoves[0].second;
		for (int q = 0; q < 4; q++)
		{
			if ((m_board[i + 1][j] == NULL || m_board[i + 1][j]->getUpFace()->faceEquals(*tile.getDownFace())) &&
			(m_board[i - 1][j] == NULL || m_board[i - 1][j]->getDownFace()->faceEquals(*tile.getUpFace())) &&
			(m_board[i][j + 1] == NULL || m_board[i][j + 1]->getLeftFace()->faceEquals(*tile.getRightFace())) &&
			(m_board[i][j - 1] == NULL || m_board[i][j - 1]->getRightFace()->faceEquals(*tile.getLeftFace())))
			{
				//debugging std::cout << "Location " << i << ' ' << j << " passed optimalPlacement checks of connecting to all surrounding tiles" << std::endl;
				break;
			}
			else
			{
				tile.rotate();
			}
		}
		return availableMoves[0];
	}
	return availableMoves[0];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, -0x50(%rbp)
movq (%rdx), %rax
movq %rdx, -0x80(%rbp)
movq 0x8(%rdx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
cmpq $0x9, %rdx
movq %rsi, -0x60(%rbp)
jb 0x107ed
cmpq %rax, %rcx
je 0x10a97
movq %rsi, %r15
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, -0x90(%rbp)
leaq -0x138(%rbp), %rcx
movq %rcx, -0x88(%rbp)
leaq -0xe8(%rbp), %r14
xorl %edx, %edx
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movslq (%rax,%rdx,8), %rcx
movq %rdx, -0x40(%rbp)
movslq 0x4(%rax,%rdx,8), %rdx
movq %rcx, -0x98(%rbp)
leaq (%rcx,%rcx,2), %rax
movq %rax, -0x58(%rbp)
xorl %ebx, %ebx
movq %rdx, -0x38(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq 0x18(%rax,%rcx,8), %rcx
movq (%rcx,%rdx,8), %rcx
testq %rcx, %rcx
je 0x10567
movl %ebx, %r13d
movq %rcx, %rbx
leaq 0x10(%rcx), %rax
movq %rax, -0x30(%rbp)
leaq -0x128(%rbp), %rax
movq %rax, -0x138(%rbp)
movq 0x38(%r15), %rsi
movq 0x40(%r15), %rdx
addq %rsi, %rdx
leaq -0x138(%rbp), %r14
movq %r14, %rdi
callq 0x9cc4
movq 0x58(%r15), %rax
movq %rax, -0x118(%rbp)
movq -0x30(%rbp), %rdi
movq %r14, %rsi
callq 0x173e0
testb %al, %al
movq -0x60(%rbp), %r15
leaq -0xe8(%rbp), %r14
je 0x10671
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rdx
movq %rbx, %rcx
movl %r13d, %ebx
movq %rcx, -0x48(%rbp)
movq -0x58(%rbp), %rcx
movq -0x18(%rax,%rcx,8), %rcx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x30(%rbp)
testq %rcx, %rcx
je 0x105d0
leaq -0xd8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x9cc4
movq -0x30(%rbp), %rax
leaq 0x38(%rax), %rdi
movq 0x30(%r15), %rax
movq %rax, -0xc8(%rbp)
movq %r14, %rsi
callq 0x173e0
testb %al, %al
je 0x1065f
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax,%rdx,8), %r13
testq %r13, %r13
je 0x1068a
leaq -0x178(%rbp), %rax
movq %rax, -0x188(%rbp)
movq 0x88(%r15), %rsi
movq 0x90(%r15), %rdx
addq %rsi, %rdx
leaq -0x188(%rbp), %r14
movq %r14, %rdi
callq 0x9cc4
leaq 0x60(%r13), %rdi
movq 0xa8(%r15), %rax
movq %rax, -0x168(%rbp)
movq %r14, %rsi
callq 0x173e0
testb %al, %al
je 0x1070a
movq %r13, -0x68(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x38(%rbp), %rcx
movq -0x8(%rax,%rcx,8), %r13
testq %r13, %r13
jne 0x10698
movb $0x1, %r13b
jmp 0x106fc
movl %ebx, %r13d
movq %r14, %rdi
callq 0x173b0
cmpq $0x0, -0x48(%rbp)
je 0x1067d
leaq -0x138(%rbp), %rdi
callq 0x173b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %r13d, %ebx
jmp 0x10765
movq %r13, -0x68(%rbp)
movq -0x8(%rax,%rdx,8), %r13
testq %r13, %r13
je 0x10717
movl %r12d, %r14d
leaq -0x1c8(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x60(%rbp), %r15
movq 0x60(%r15), %rsi
movq 0x68(%r15), %rdx
addq %rsi, %rdx
leaq -0x1d8(%rbp), %r12
movq %r12, %rdi
callq 0x9cc4
addq $0x88, %r13
movq 0x80(%r15), %rax
movq %rax, -0x1b8(%rbp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x173e0
movl %eax, %r13d
movq %r12, %rdi
callq 0x173b0
cmpq $0x0, -0x68(%rbp)
movl %r14d, %r12d
je 0x1071a
leaq -0x188(%rbp), %rdi
callq 0x173b0
jmp 0x1071a
movq %r14, %rdi
callq 0x173b0
xorl %r13d, %r13d
jmp 0x1071a
movb $0x1, %r13b
cmpq $0x0, -0x30(%rbp)
leaq -0xe8(%rbp), %r14
je 0x10730
movq %r14, %rdi
callq 0x173b0
cmpq $0x0, -0x48(%rbp)
je 0x10743
leaq -0x138(%rbp), %rdi
callq 0x173b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %r13b, %r13b
movq -0x60(%rbp), %r15
je 0x10765
movq -0x50(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x10b8e
cmpl %r12d, %eax
jle 0x10790
movq -0x80(%rbp), %rcx
movq (%rcx), %rcx
movq -0x40(%rbp), %rsi
movl (%rcx,%rsi,8), %edx
movq %rdx, -0x70(%rbp)
movl 0x4(%rcx,%rsi,8), %ecx
movq %rcx, -0x78(%rbp)
movl %eax, %r12d
movl %ebx, %eax
movq %rax, -0x90(%rbp)
movq %r15, %rdi
callq 0x1679e
incl %ebx
cmpl $0x4, %ebx
movq -0x38(%rbp), %rdx
jne 0x104dd
movq -0x40(%rbp), %rdx
incq %rdx
movq -0x80(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %rdx
jb 0x104bb
movq -0x90(%rbp), %rbx
testl %ebx, %ebx
jle 0x107e0
movq %r15, %rdi
callq 0x1679e
decl %ebx
jne 0x107d4
movq -0x78(%rbp), %rcx
movq -0x70(%rbp), %rax
jmp 0x10a9b
movslq (%rax), %rcx
movslq 0x4(%rax), %r12
movl $0x4, %r15d
leaq (%rcx,%rcx,2), %rax
movq %rax, -0x58(%rbp)
leaq -0x110(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0xc0(%rbp), %r14
movq %r12, -0x48(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq 0x18(%rax,%rcx,8), %rcx
movq (%rcx,%r12,8), %rbx
testq %rbx, %rbx
je 0x10897
leaq 0x10(%rbx), %r13
leaq -0x100(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x60(%rbp), %r12
movq 0x38(%r12), %rsi
movq 0x40(%r12), %rdx
addq %rsi, %rdx
leaq -0x110(%rbp), %r14
movq %r14, %rdi
callq 0x9cc4
movq 0x58(%r12), %rax
movq %rax, -0xf0(%rbp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x173e0
testb %al, %al
movq -0x48(%rbp), %r12
leaq -0xc0(%rbp), %r14
je 0x1099c
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq -0x18(%rax,%rcx,8), %rcx
movq (%rcx,%r12,8), %rcx
movq %rcx, -0x38(%rbp)
testq %rcx, %rcx
movq -0x60(%rbp), %r13
je 0x108fc
leaq -0xb0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x9cc4
movq -0x38(%rbp), %rax
leaq 0x38(%rax), %rdi
movq 0x30(%r13), %rax
movq %rax, -0xa0(%rbp)
movq %r14, %rsi
callq 0x173e0
testb %al, %al
je 0x1098b
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax,%r12,8), %rcx
movq %rcx, -0x30(%rbp)
testq %rcx, %rcx
je 0x109ad
leaq -0x150(%rbp), %rax
movq %rax, -0x160(%rbp)
movq 0x88(%r13), %rsi
movq 0x90(%r13), %rdx
addq %rsi, %rdx
leaq -0x160(%rbp), %r14
movq %r14, %rdi
callq 0x9cc4
movq -0x30(%rbp), %rax
leaq 0x60(%rax), %rdi
movq 0xa8(%r13), %rax
movq %rax, -0x140(%rbp)
movq %r14, %rsi
callq 0x173e0
testb %al, %al
je 0x10a36
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x8(%rax,%r12,8), %r13
testq %r13, %r13
jne 0x109bb
movb $0x1, %r13b
jmp 0x10a28
movq %r14, %rdi
callq 0x173b0
testq %rbx, %rbx
je 0x10a72
leaq -0x110(%rbp), %rdi
callq 0x173b0
jmp 0x10a72
movq -0x8(%rax,%r12,8), %r13
testq %r13, %r13
je 0x10a43
movq %rbx, -0x40(%rbp)
leaq -0x1a0(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x60(%rbp), %r12
movq 0x60(%r12), %rsi
movq 0x68(%r12), %rdx
addq %rsi, %rdx
leaq -0x1b0(%rbp), %rbx
movq %rbx, %rdi
callq 0x9cc4
addq $0x88, %r13
movq 0x80(%r12), %rax
movq %rax, -0x190(%rbp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x173e0
movl %eax, %r13d
movq %rbx, %rdi
callq 0x173b0
cmpq $0x0, -0x30(%rbp)
movq -0x48(%rbp), %r12
movq -0x40(%rbp), %rbx
je 0x10a46
leaq -0x160(%rbp), %rdi
callq 0x173b0
jmp 0x10a46
movq %r14, %rdi
callq 0x173b0
xorl %r13d, %r13d
jmp 0x10a46
movb $0x1, %r13b
cmpq $0x0, -0x38(%rbp)
leaq -0xc0(%rbp), %r14
je 0x10a5c
movq %r14, %rdi
callq 0x173b0
testq %rbx, %rbx
je 0x10a6d
leaq -0x110(%rbp), %rdi
callq 0x173b0
testb %r13b, %r13b
jne 0x10a84
movq -0x60(%rbp), %rdi
callq 0x1679e
decl %r15d
jne 0x1081b
movq -0x80(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq %rax, %rcx
shrq $0x20, %rcx
jmp 0x10a9b
xorl %ecx, %ecx
xorl %eax, %eax
shlq $0x20, %rcx
movl %eax, %eax
orq %rcx, %rax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r13
leaq -0x1b0(%rbp), %rdi
callq 0x173b0
jmp 0x10aca
movq %rax, %r13
cmpq $0x0, -0x30(%rbp)
jne 0x10ada
jmp 0x10aef
movq %rbx, -0x40(%rbp)
movq %rax, %r13
leaq -0x160(%rbp), %rdi
callq 0x173b0
jmp 0x10aef
movq %rbx, -0x40(%rbp)
movq %rax, %r13
cmpq $0x0, -0x38(%rbp)
jne 0x10aff
jmp 0x10b0b
movq %rbx, -0x40(%rbp)
movq %rax, %r13
leaq -0xc0(%rbp), %rdi
callq 0x173b0
cmpq $0x0, -0x40(%rbp)
jne 0x10b7a
jmp 0x10b86
movq %rax, %r13
testq %rbx, %rbx
jne 0x10b7a
jmp 0x10b86
jmp 0x10b77
movq %rax, %r13
leaq -0x1d8(%rbp), %rdi
callq 0x173b0
jmp 0x10b34
movq %rax, %r13
cmpq $0x0, -0x68(%rbp)
jne 0x10b45
jmp 0x10b51
movq %rax, %r13
jmp 0x10b51
movq %rax, %r13
leaq -0x188(%rbp), %rdi
callq 0x173b0
cmpq $0x0, -0x30(%rbp)
jne 0x10b62
jmp 0x10b6e
movq %rax, %r13
jmp 0x10b6e
movq %rax, %r13
leaq -0xe8(%rbp), %rdi
callq 0x173b0
cmpq $0x0, -0x48(%rbp)
jne 0x10b7a
jmp 0x10b86
movq %rax, %r13
movq -0x88(%rbp), %rdi
callq 0x173b0
movq %r13, %rdi
callq 0x33f0