// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rsi, %r15
movq %rdi, -0x58(%rbp)
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
subq %rax, %rcx
cmpq $0x9, %rcx
jb 0x10878
movq (%rdx), %rax
cmpq %rax, 0x8(%rdx)
je 0x10b33
movq %rdx, -0x70(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x78(%rbp)
leaq -0x1d0(%rbp), %rcx
movq %rcx, -0x80(%rbp)
xorl %edx, %edx
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movslq (%rax,%rdx,8), %rcx
movq %rdx, -0x88(%rbp)
movslq 0x4(%rax,%rdx,8), %rax
movq %rax, -0x48(%rbp)
movq %rcx, -0x90(%rbp)
leaq (%rcx,%rcx,2), %rax
movq %rax, -0x50(%rbp)
xorl %r12d, %r12d
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x18(%rax,%rcx,8), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %r13
testq %r13, %r13
setne %r14b
movb %r14b, -0x38(%rbp)
je 0x10601
addq $0x10, %r13
leaq -0x1c0(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq 0x38(%r15), %rsi
movq 0x40(%r15), %rdx
addq %rsi, %rdx
leaq -0x1d0(%rbp), %r14
movq %r14, %rdi
callq 0x9da8
movq 0x58(%r15), %rax
movq %rax, -0x1b0(%rbp)
movb $0x1, %bl
movq %r13, %rdi
movq %r14, %rsi
callq 0x17728
testb %al, %al
movb -0x38(%rbp), %r14b
je 0x10784
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x50(%rbp), %rcx
movq -0x18(%rax,%rcx,8), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %r13
testq %r13, %r13
setne %al
movl %eax, -0x40(%rbp)
je 0x1067f
leaq -0x170(%rbp), %rax
movq %rax, -0x180(%rbp)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
addq %rsi, %rdx
movl %r14d, %ebx
leaq -0x180(%rbp), %rdi
callq 0x9da8
addq $0x38, %r13
movq 0x30(%r15), %rax
movq %rax, -0x160(%rbp)
movq %r15, %rbx
movb $0x1, %r15b
movq %r13, %rdi
leaq -0x180(%rbp), %rsi
callq 0x17728
testb %al, %al
movq %rbx, %r15
movb -0x38(%rbp), %r14b
je 0x10790
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x48(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %r13
testq %r13, %r13
setne %bl
je 0x10706
leaq -0x148(%rbp), %rax
movq %rax, -0x158(%rbp)
movq 0x88(%r15), %rsi
movq 0x90(%r15), %rdx
addq %rsi, %rdx
movq %r15, %r14
movl -0x40(%rbp), %eax
movl %eax, %r15d
leaq -0x158(%rbp), %rdi
callq 0x9da8
addq $0x60, %r13
movq 0xa8(%r14), %rax
movq %rax, -0x138(%rbp)
movb $0x1, %r15b
movq %r13, %rdi
leaq -0x158(%rbp), %rsi
callq 0x17728
testb %al, %al
movq %r14, %r15
movb -0x38(%rbp), %r14b
je 0x10799
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x48(%rbp), %rcx
movq -0x8(%rax,%rcx,8), %r13
testq %r13, %r13
je 0x10844
leaq -0x120(%rbp), %rax
movq %rax, -0x130(%rbp)
movq 0x60(%r15), %rsi
movq 0x68(%r15), %rdx
addq %rsi, %rdx
movq %r15, %r14
movl %ebx, %r15d
leaq -0x130(%rbp), %rdi
callq 0x9da8
addq $0x88, %r13
movq 0x80(%r14), %rax
movq %rax, -0x110(%rbp)
movq %r13, %rdi
leaq -0x130(%rbp), %rsi
callq 0x17728
movl %eax, %r13d
movq %r14, %r15
movb $0x1, %al
movb -0x38(%rbp), %r14b
jmp 0x107a0
movb $0x1, %r14b
movl $0x0, -0x40(%rbp)
jmp 0x10795
movb $0x1, %al
movl %eax, -0x40(%rbp)
xorl %ebx, %ebx
jmp 0x1079b
movb $0x1, %bl
xorl %eax, %eax
xorl %r13d, %r13d
testb %al, %al
je 0x107b0
leaq -0x130(%rbp), %rdi
callq 0x176f8
testb %bl, %bl
je 0x107c0
leaq -0x158(%rbp), %rdi
callq 0x176f8
cmpb $0x0, -0x40(%rbp)
je 0x107d2
leaq -0x180(%rbp), %rdi
callq 0x176f8
testb %r14b, %r14b
je 0x107e3
leaq -0x1d0(%rbp), %rdi
callq 0x176f8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %r13b, %r13b
je 0x10801
movq -0x58(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x10c3a
cmpl -0x2c(%rbp), %eax
jle 0x1082d
movq -0x70(%rbp), %rcx
movq (%rcx), %rcx
movq -0x88(%rbp), %rsi
movl (%rcx,%rsi,8), %edx
movq %rdx, -0x60(%rbp)
movl 0x4(%rcx,%rsi,8), %ecx
movq %rcx, -0x68(%rbp)
movl %eax, -0x2c(%rbp)
movl %r12d, %eax
movq %rax, -0x78(%rbp)
movq %r15, %rdi
callq 0x16ae6
incl %r12d
cmpl $0x4, %r12d
jne 0x1058c
jmp 0x1084e
movb $0x1, %r13b
xorl %eax, %eax
jmp 0x107a0
movq -0x88(%rbp), %rdx
incq %rdx
movq -0x70(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %rdx
jb 0x10566
jmp 0x10b4b
movq %rdx, -0x70(%rbp)
movq %r15, -0x38(%rbp)
movslq (%rax), %rcx
movslq 0x4(%rax), %rax
movq %rax, -0x50(%rbp)
movl $0x4, %r15d
leaq (%rcx,%rcx,2), %rax
movq %rax, -0x40(%rbp)
leaq -0x1a8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x40(%rbp), %rcx
movq 0x18(%rax,%rcx,8), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %r13
testq %r13, %r13
setne -0x48(%rbp)
je 0x10916
addq $0x10, %r13
leaq -0x198(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x38(%rbp), %r14
movq 0x38(%r14), %rsi
movq 0x40(%r14), %rdx
addq %rsi, %rdx
leaq -0x1a8(%rbp), %rbx
movq %rbx, %rdi
callq 0x9da8
movq 0x58(%r14), %rax
movq %rax, -0x188(%rbp)
movb $0x1, %r14b
movq %r13, %rdi
movq %rbx, %rsi
callq 0x17728
testb %al, %al
je 0x10aa3
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x40(%rbp), %rcx
movq -0x18(%rax,%rcx,8), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %r13
testq %r13, %r13
setne %r14b
je 0x10996
movl %r14d, %ebx
leaq -0xf8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x38(%rbp), %r12
movq 0x10(%r12), %rsi
movq 0x18(%r12), %rdx
addq %rsi, %rdx
movb -0x48(%rbp), %r14b
leaq -0x108(%rbp), %rdi
callq 0x9da8
addq $0x38, %r13
movq 0x30(%r12), %rax
movq %rax, -0xe8(%rbp)
movb $0x1, %r12b
movq %r13, %rdi
leaq -0x108(%rbp), %rsi
callq 0x17728
testb %al, %al
movl %ebx, %r14d
je 0x10aac
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x50(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %r13
testq %r13, %r13
setne %r12b
je 0x10a26
movl %r15d, -0x2c(%rbp)
movl %r12d, %r15d
leaq -0xd0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl %r14d, %eax
movq -0x38(%rbp), %rbx
movq 0x88(%rbx), %rsi
movq 0x90(%rbx), %rdx
addq %rsi, %rdx
movl %eax, %r12d
leaq -0xe0(%rbp), %rdi
callq 0x9da8
addq $0x60, %r13
movq 0xa8(%rbx), %rax
movq %rax, -0xc0(%rbp)
movb $0x1, %r12b
movq %r13, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x17728
testb %al, %al
movl %r15d, %r12d
movl -0x2c(%rbp), %r15d
je 0x10ab4
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x50(%rbp), %rcx
movq -0x8(%rax,%rcx,8), %r13
testq %r13, %r13
je 0x10b19
leaq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rbx
movq 0x60(%rbx), %rsi
movq 0x68(%rbx), %rdx
addq %rsi, %rdx
movl %r12d, -0x2c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x9da8
addq $0x88, %r13
movq 0x80(%rbx), %rax
movq %rax, -0x98(%rbp)
movq %r13, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x17728
movl %eax, %r13d
movb $0x1, %al
movl -0x2c(%rbp), %r12d
jmp 0x10abc
movb $0x1, -0x48(%rbp)
xorl %r14d, %r14d
jmp 0x10aaf
movb $0x1, %r14b
xorl %r12d, %r12d
jmp 0x10ab7
movb $0x1, %r12b
xorl %eax, %eax
xorl %r13d, %r13d
testb %al, %al
je 0x10acc
leaq -0xb8(%rbp), %rdi
callq 0x176f8
testb %r12b, %r12b
je 0x10add
leaq -0xe0(%rbp), %rdi
callq 0x176f8
testb %r14b, %r14b
je 0x10aee
leaq -0x108(%rbp), %rdi
callq 0x176f8
cmpb $0x0, -0x48(%rbp)
je 0x10b00
leaq -0x1a8(%rbp), %rdi
callq 0x176f8
testb %r13b, %r13b
jne 0x10b20
movq -0x38(%rbp), %rdi
callq 0x16ae6
decl %r15d
jne 0x108a4
jmp 0x10b20
movb $0x1, %r13b
xorl %eax, %eax
jmp 0x10abc
movq -0x70(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq %rax, %rcx
shrq $0x20, %rcx
jmp 0x10b67
movq $0x0, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rbx
testl %ebx, %ebx
jle 0x10b5f
movq %r15, %rdi
callq 0x16ae6
decl %ebx
jne 0x10b53
movq -0x68(%rbp), %rcx
movq -0x60(%rbp), %rax
shlq $0x20, %rcx
movl %eax, %eax
orq %rcx, %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r13
leaq -0xb8(%rbp), %rdi
callq 0x176f8
jmp 0x10b9c
movq %rax, %r13
movl %r12d, %eax
movl %eax, -0x2c(%rbp)
cmpb $0x0, -0x2c(%rbp)
je 0x10bb6
leaq -0xe0(%rbp), %rdi
callq 0x176f8
jmp 0x10bb6
movq %rax, %r13
movl %r12d, %r14d
testb %r14b, %r14b
je 0x10bd0
leaq -0x108(%rbp), %rdi
callq 0x176f8
jmp 0x10bd0
movq %rax, %r13
movb %r14b, -0x48(%rbp)
cmpb $0x0, -0x48(%rbp)
jne 0x10c29
jmp 0x10c32
movq %rax, %r13
leaq -0x130(%rbp), %rdi
callq 0x176f8
jmp 0x10bef
movq %rax, %r13
movl %r15d, %ebx
testb %bl, %bl
movb -0x38(%rbp), %bl
je 0x10c02
leaq -0x158(%rbp), %rdi
callq 0x176f8
movl -0x40(%rbp), %eax
jmp 0x10c10
movq %rax, %r13
movl %r15d, %eax
movb -0x38(%rbp), %bl
testb %al, %al
je 0x10c25
leaq -0x180(%rbp), %rdi
callq 0x176f8
jmp 0x10c25
movq %rax, %r13
testb %bl, %bl
je 0x10c32
movq -0x80(%rbp), %rdi
callq 0x176f8
movq %r13, %rdi
callq 0x33f0