// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x14
popq %rcx
leaq 0x8(%rsp), %rax
movq %rax, %rdi
rep  movsl (%rsi), %es:(%rdi)
movq %rax, %rdi
movl %ebx, %esi
callq 0x291f
leaq 0x18(%rsp), %rcx
movslq -0x4(%rcx), %rdx
movslq -0x8(%rcx), %rsi
imulq $0x78, %rdx, %rax
imulq $0xc, %rsi, %rdi
addq %rax, %rdi
leaq 0x35ab(%rip), %rax      # 0x6150
addq %rax, %rdi
addq $0x8, %rdi
xorl %r8d, %r8d
pushq $0x5
popq %r9
movq %rcx, %r10
xorl %eax, %eax
cmpq $0x4, %r8
je 0x2bef
movq %rdi, %r11
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x2be2
cmpl $0x1, (%r10,%r14,4)
jne 0x2bd9
cmpl $0x1, (%r11)
cmovel %r9d, %eax
incq %r14
addq $0xc, %r11
jmp 0x2bc4
incq %r8
addq $0x10, %r10
addq $0x78, %rdi
jmp 0x2bb8
cmpl $0x3, %ebx
ja 0x2cf8
movl %ebx, %edi
leaq 0x1477(%rip), %r8       # 0x4078
movslq (%r8,%rdi,4), %rdi
addq %r8, %rdi
jmpq *%rdi
negq %rsi
xorl %edx, %edx
pushq $0x2
popq %rdi
cmpq $0x4, %rdx
je 0x2cf8
xorl %r8d, %r8d
cmpq $0x4, %r8
je 0x2c3d
cmpq %rsi, %r8
movl %eax, %r9d
cmovll %edi, %r9d
cmpl $0x1, (%rcx,%r8,4)
cmovel %r9d, %eax
incq %r8
jmp 0x2c1f
incq %rdx
addq $0x10, %rcx
jmp 0x2c12
negq %rdx
xorl %esi, %esi
pushq $0x3
popq %rdi
cmpq $0x4, %rsi
je 0x2cf8
xorl %r8d, %r8d
cmpq $0x4, %r8
je 0x2c79
cmpq %rdx, %rsi
movl %eax, %r9d
cmovll %edi, %r9d
cmpl $0x1, (%rcx,%r8,4)
cmovel %r9d, %eax
incq %r8
jmp 0x2c5b
incq %rsi
addq $0x10, %rcx
jmp 0x2c4e
pushq $0x13
popq %rsi
subq %rdx, %rsi
xorl %edx, %edx
pushq $0x4
popq %rdi
cmpq $0x4, %rdx
je 0x2cf8
xorl %r8d, %r8d
cmpq $0x4, %r8
je 0x2cb4
cmpq %rsi, %rdx
movl %eax, %r9d
cmovgl %edi, %r9d
cmpl $0x1, (%rcx,%r8,4)
cmovel %r9d, %eax
incq %r8
jmp 0x2c96
incq %rdx
addq $0x10, %rcx
jmp 0x2c8d
pushq $0x9
popq %rdx
subq %rsi, %rdx
xorl %esi, %esi
pushq $0x2
popq %rdi
cmpq $0x4, %rsi
je 0x2cf8
xorl %r8d, %r8d
cmpq $0x4, %r8
je 0x2cef
cmpq %rdx, %r8
movl %eax, %r9d
cmovgl %edi, %r9d
cmpl $0x1, (%rcx,%r8,4)
cmovel %r9d, %eax
incq %r8
jmp 0x2cd1
incq %rsi
addq $0x10, %rcx
jmp 0x2cc8
addq $0x58, %rsp
popq %rbx
popq %r14
retq