// File: /workspace/github_cpp_makefile_star2_O3/miklhh[P]tetris/src/game.cpp
// Function: test_movable(block_t*, int)
collision_t test_movable(block_t* block, direction_t direction)
{
	/* Make a copy of the block and move it. */
	block_t block_copy = *block;
	block_move(&block_copy, direction);

	/* Test if the block interfears with any other placed box, or wall. */
	collision_t collision = NO_COLLISION;

	/* Test collision with other boxes. */
	for (int y = 0; y < MAX_BLOCK_WIDTH; y++)
	{
		for (int x = 0; x < MAX_BLOCK_HEIGHT; x++)
		{
			if (block_copy.structure.tile[y][x] == FILLED   &&
				field[y + block_copy.position.y][x + block_copy.position.x].tile == TAKEN)
			{
				collision = COLLISION_BLOCK;
			}
		}
	}

	/* Test collision with wall. */
	switch (direction)
	{
	case DIRECTION_LEFT:
		/* Test for collision with left side wall. */
		for (int y = 0; y < MAX_BLOCK_HEIGHT; y++)
		{
			for (int x = 0; x < MAX_BLOCK_WIDTH; x++)
			{
				if (block_copy.structure.tile[y][x] == FILLED   &&
					x + block_copy.position.x < 0)
				{
					collision = COLLISION_WALL_R;
				}
			}
		}
		break;

	case DIRECTION_RIGHT:
		/* Test for collision with the right side wall. */
		for (int y = 0; y < MAX_BLOCK_HEIGHT; y++)
		{
			for (int x = 0; x < MAX_BLOCK_WIDTH; x++)
			{
				if (block_copy.structure.tile[y][x] == FILLED   &&
					x + block_copy.position.x > GRID_WIDTH - 1)
				{
					collision = COLLISION_WALL_R;
				}
			}
		}
		break;
		
	case DIRECTION_UP:
		/* Test for collision with the upper side wall. */
		for (int y = 0; y < MAX_BLOCK_HEIGHT; y++)
		{
			for (int x = 0; x < MAX_BLOCK_WIDTH; x++)
			{
				if (block_copy.structure.tile[y][x] == FILLED   &&
					y + block_copy.position.y < 0)
				{
					collision = COLLISION_WALL_U;
				}
			}
		}
		break;

	case DIRECTION_DOWN:
		/* Test for collision the the lower side wall. */
		for (int y = 0; y < MAX_BLOCK_HEIGHT; y++)
		{
			for (int x = 0; x < MAX_BLOCK_WIDTH; x++)
			{
				if (block_copy.structure.tile[y][x] == FILLED   &&
					y + block_copy.position.y > GRID_HEIGHT - 1)
				{
					collision = COLLISION_WALL_D;
				}
			}
		}
		break;

	default:
		break;
	}
	return collision;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x58, %rsp
movl %esi, %ebx
movq %rdi, %rsi
leaq 0x8(%rsp), %rax
movl $0x14, %ecx
movq %rax, %rdi
rep  movsl (%rsi), %es:(%rdi)
movq %rax, %rdi
movl %ebx, %esi
callq 0x29d6
leaq 0x18(%rsp), %rcx
movslq -0x4(%rcx), %rdx
movslq -0x8(%rcx), %rsi
imulq $0x78, %rdx, %rax
leaq (%rsi,%rsi,2), %rdi
leaq (%rax,%rdi,4), %rax
leaq 0x34fe(%rip), %rdi      # 0x6170
addq %rax, %rdi
addq $0x8, %rdi
xorl %r8d, %r8d
movl $0x5, %r9d
movq %rcx, %r10
xorl %eax, %eax
movq %rdi, %r11
xorl %r14d, %r14d
cmpl $0x1, (%r10,%r14,4)
jne 0x2c9c
cmpl $0x1, (%r11)
cmovel %r9d, %eax
incq %r14
addq $0xc, %r11
cmpq $0x4, %r14
jne 0x2c8d
incq %r8
addq $0x78, %rdi
addq $0x10, %r10
cmpq $0x4, %r8
jne 0x2c87
cmpl $0x3, %ebx
ja 0x2dbc
movl %ebx, %edi
leaq 0x13ac(%rip), %r8       # 0x4078
movslq (%r8,%rdi,4), %rdi
addq %r8, %rdi
jmpq *%rdi
negq %rsi
xorl %edx, %edx
movl $0x2, %edi
xorl %r8d, %r8d
movl %eax, %r9d
cmpq %rsi, %r8
cmovll %edi, %eax
cmpl $0x1, (%rcx,%r8,4)
cmovnel %r9d, %eax
incq %r8
cmpq $0x4, %r8
jne 0x2ce2
incq %rdx
addq $0x10, %rcx
cmpq $0x4, %rdx
jne 0x2cdf
jmp 0x2dbc
negq %rdx
xorl %esi, %esi
movl $0x3, %edi
xorl %r8d, %r8d
movl %eax, %r9d
cmpq %rdx, %rsi
cmovll %edi, %eax
cmpl $0x1, (%rcx,%r8,4)
cmovnel %r9d, %eax
incq %r8
cmpq $0x4, %r8
jne 0x2d1c
incq %rsi
addq $0x10, %rcx
cmpq $0x4, %rsi
jne 0x2d19
jmp 0x2dbc
movl $0x13, %esi
subq %rdx, %rsi
xorl %edx, %edx
movl $0x4, %edi
xorl %r8d, %r8d
movl %eax, %r9d
cmpq %rsi, %rdx
cmovgl %edi, %eax
cmpl $0x1, (%rcx,%r8,4)
cmovnel %r9d, %eax
incq %r8
cmpq $0x4, %r8
jne 0x2d58
incq %rdx
addq $0x10, %rcx
cmpq $0x4, %rdx
jne 0x2d55
jmp 0x2dbc
movl $0x9, %edx
subq %rsi, %rdx
xorl %esi, %esi
movl $0x2, %edi
xorl %r8d, %r8d
movl %eax, %r9d
cmpq %rdx, %r8
cmovgl %edi, %eax
cmpl $0x1, (%rcx,%r8,4)
cmovnel %r9d, %eax
incq %r8
cmpq $0x4, %r8
jne 0x2d94
incq %rsi
addq $0x10, %rcx
cmpq $0x4, %rsi
jne 0x2d91
addq $0x58, %rsp
popq %rbx
popq %r14
retq