// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1c8, %rsp            # imm = 0x1C8
movq %rsi, %r14
movq %rdi, -0x40(%rbp)
movq (%rdx), %rax
movq %rdx, -0x80(%rbp)
movq 0x8(%rdx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
cmpq $0x9, %rdx
movq %rsi, -0x78(%rbp)
jb 0xc020
leaq 0x38(%r14), %rdx
movq %rdx, -0x88(%rbp)
leaq 0x10(%r14), %rdx
movq %rdx, -0xa8(%rbp)
leaq 0x88(%r14), %rdx
movq %rdx, -0xa0(%rbp)
leaq 0x60(%r14), %rdx
movq %rdx, -0x98(%rbp)
pushq $-0x1
popq %rsi
movq $0x0, -0x68(%rbp)
leaq -0xf8(%rbp), %rdx
movq %rdx, -0x90(%rbp)
movq $0x0, -0x60(%rbp)
xorl %edx, %edx
movl %esi, -0x48(%rbp)
movl $0x0, -0x54(%rbp)
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %rdx
jae 0xc225
movslq (%rax,%rdx,8), %rcx
movq %rcx, -0x50(%rbp)
movq %rdx, -0x70(%rbp)
movslq 0x4(%rax,%rdx,8), %rax
movq %rax, -0x38(%rbp)
xorl %r12d, %r12d
cmpl $0x4, %r12d
je 0xc009
movq -0x40(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0x50(%rbp), %r13
movq 0x18(%rax,%r13), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %r14
testq %r14, %r14
je 0xbe78
leaq 0x10(%r14), %r15
leaq -0xf8(%rbp), %rbx
movq %rbx, %rdi
movq -0x88(%rbp), %rsi
callq 0x659a
movq %r15, %rdi
movq %rbx, %rsi
callq 0x11786
testb %al, %al
je 0xbf19
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rax,%r13), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x30(%rbp)
testq %rcx, %rcx
je 0xbebf
leaq -0x170(%rbp), %rbx
movq %rbx, %rdi
movq -0xa8(%rbp), %rsi
callq 0x659a
movq -0x30(%rbp), %rax
leaq 0x38(%rax), %rdi
movq %rbx, %rsi
callq 0x11786
testb %al, %al
je 0xbf31
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq (%rax,%r13), %rax
movq -0x38(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rbx
testq %rbx, %rbx
je 0xbf36
leaq -0x148(%rbp), %r15
movq %r15, %rdi
movq -0xa0(%rbp), %rsi
callq 0x659a
leaq 0x60(%rbx), %rdi
movq %r15, %rsi
callq 0x11786
testb %al, %al
je 0xbf7e
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq (%rax,%r13), %rax
movq -0x38(%rbp), %rcx
movq -0x8(%rax,%rcx,8), %r15
testq %r15, %r15
jne 0xbf44
movb $0x1, %r15b
jmp 0xbf81
leaq -0xf8(%rbp), %rdi
callq 0x11768
pushq $-0x1
popq %rax
movq -0x78(%rbp), %r14
jmp 0xbfce
xorl %r15d, %r15d
jmp 0xbf94
movq -0x8(%rax,%rcx,8), %r15
testq %r15, %r15
je 0xc004
leaq -0x1e8(%rbp), %r13
movq %r13, %rdi
movq -0x98(%rbp), %rsi
callq 0x659a
addq $0x88, %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x11786
movl %eax, %r15d
movq %r13, %rdi
callq 0x11768
testq %rbx, %rbx
jne 0xbf81
jmp 0xbf8d
xorl %r15d, %r15d
leaq -0x148(%rbp), %rdi
callq 0x11768
cmpq $0x0, -0x30(%rbp)
je 0xbfa0
leaq -0x170(%rbp), %rdi
callq 0x11768
testq %r14, %r14
je 0xbfb1
leaq -0xf8(%rbp), %rdi
callq 0x11768
pushq $-0x1
popq %rax
testb %r15b, %r15b
movq -0x78(%rbp), %r14
je 0xbfce
movq -0x40(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0xc322
cmpl -0x48(%rbp), %eax
jle 0xbff4
movq -0x80(%rbp), %rcx
movq (%rcx), %rcx
movq -0x70(%rbp), %rsi
movl (%rcx,%rsi,8), %edx
movq %rdx, -0x60(%rbp)
movl 0x4(%rcx,%rsi,8), %ecx
movq %rcx, -0x68(%rbp)
movl %eax, -0x48(%rbp)
movl %r12d, -0x54(%rbp)
movq %r14, %rdi
callq 0x10e82
incl %r12d
jmp 0xbe1c
movb $0x1, %r15b
jmp 0xbf8d
movq -0x70(%rbp), %rdx
incq %rdx
movq -0x80(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
jmp 0xbdf4
movslq (%rax), %rcx
movslq 0x4(%rax), %r13
leaq 0x38(%r14), %rax
movq %rax, -0x88(%rbp)
leaq 0x10(%r14), %rax
movq %rax, -0x70(%rbp)
leaq 0x88(%r14), %rax
movq %rax, -0x68(%rbp)
leaq 0x60(%r14), %rax
movq %rax, -0x60(%rbp)
pushq $0x4
popq %r14
imulq $0x18, %rcx, %rax
movq %rax, -0x38(%rbp)
leaq -0xd0(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x1c0(%rbp), %r12
movq %r13, -0x50(%rbp)
subl $0x1, %r14d
jb 0xc212
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x18(%rax,%rcx), %rcx
movq (%rcx,%r13,8), %rcx
testq %rcx, %rcx
je 0xc0d3
movq %rcx, %r15
leaq 0x10(%rcx), %r13
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdi
movq -0x88(%rbp), %rsi
callq 0x659a
movq %r13, %rdi
movq %rbx, %rsi
callq 0x11786
testb %al, %al
je 0xc16a
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x50(%rbp), %r13
movq %r15, %rcx
movq %rcx, -0x48(%rbp)
movq -0x38(%rbp), %rcx
movq -0x18(%rax,%rcx), %rcx
movq (%rcx,%r13,8), %rcx
movq %rcx, -0x30(%rbp)
testq %rcx, %rcx
je 0xc117
movq %r12, %rbx
movq %r12, %rdi
movq -0x70(%rbp), %rsi
callq 0x659a
movq -0x30(%rbp), %rax
leaq 0x38(%rax), %rdi
movq %rbx, %rsi
callq 0x11786
testb %al, %al
je 0xc17f
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx), %rax
movq 0x8(%rax,%r13,8), %rbx
testq %rbx, %rbx
je 0xc184
leaq -0x120(%rbp), %r15
movq %r15, %rdi
movq -0x68(%rbp), %rsi
callq 0x659a
leaq 0x60(%rbx), %rdi
movq %r15, %rsi
callq 0x11786
testb %al, %al
je 0xc1c5
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx), %rax
movq -0x8(%rax,%r13,8), %r13
testq %r13, %r13
jne 0xc18e
movb $0x1, %r13b
jmp 0xc1c8
leaq -0xd0(%rbp), %rdi
callq 0x11768
movq -0x50(%rbp), %r13
jmp 0xc1ff
xorl %r13d, %r13d
jmp 0xc1db
movq -0x8(%rax,%r13,8), %r13
testq %r13, %r13
je 0xc20d
leaq -0x198(%rbp), %r15
movq %r15, %rdi
movq -0x60(%rbp), %rsi
callq 0x659a
addq $0x88, %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x11786
movl %eax, %r13d
movq %r15, %rdi
callq 0x11768
testq %rbx, %rbx
jne 0xc1c8
jmp 0xc1d4
xorl %r13d, %r13d
leaq -0x120(%rbp), %rdi
callq 0x11768
cmpq $0x0, -0x30(%rbp)
je 0xc1e3
movq %r12, %rdi
callq 0x11768
cmpq $0x0, -0x48(%rbp)
je 0xc1f6
leaq -0xd0(%rbp), %rdi
callq 0x11768
testb %r13b, %r13b
movq -0x50(%rbp), %r13
jne 0xc212
movq -0x78(%rbp), %rdi
callq 0x10e82
jmp 0xc072
movb $0x1, %r13b
jmp 0xc1d4
movq -0x80(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq %rax, %rcx
shrq $0x20, %rcx
jmp 0xc246
xorl %ebx, %ebx
movl -0x54(%rbp), %eax
testl %eax, %eax
cmovgl %eax, %ebx
subl $0x1, %ebx
jb 0xc23e
movq %r14, %rdi
callq 0x10e82
jmp 0xc22f
movq -0x68(%rbp), %rcx
movq -0x60(%rbp), %rax
shlq $0x20, %rcx
movl %eax, %eax
orq %rcx, %rax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r13
leaq -0x198(%rbp), %rdi
callq 0x11768
jmp 0xc275
movq %rax, %r13
testq %rbx, %rbx
jne 0xc27f
jmp 0xc290
movq %rax, %r13
leaq -0x120(%rbp), %rdi
callq 0x11768
jmp 0xc290
movq %rax, %r13
cmpq $0x0, -0x30(%rbp)
jne 0xc29c
jmp 0xc2ad
movq %rax, %r13
leaq -0x1c0(%rbp), %rdi
callq 0x11768
jmp 0xc2ad
movq %rax, %r13
cmpq $0x0, -0x48(%rbp)
jne 0xc30e
jmp 0xc31a
jmp 0xc30b
movq %rax, %r13
leaq -0x1e8(%rbp), %rdi
callq 0x11768
jmp 0xc2cc
movq %rax, %r13
testq %rbx, %rbx
jne 0xc2db
jmp 0xc2e7
movq %rax, %r13
jmp 0xc2e7
movq %rax, %r13
leaq -0x148(%rbp), %rdi
callq 0x11768
cmpq $0x0, -0x30(%rbp)
jne 0xc2f3
jmp 0xc304
movq %rax, %r13
leaq -0x170(%rbp), %rdi
callq 0x11768
jmp 0xc304
movq %rax, %r13
testq %r14, %r14
jne 0xc30e
jmp 0xc31a
movq %rax, %r13
movq -0x90(%rbp), %rdi
callq 0x11768
movq %r13, %rdi
callq 0x33c0