// File: /workspace/github_c_makefile_star2_O0/TurkeyMcMac[P]ts3d/src/d3d.c
// Function: hit_wall
static const d3d_block_s *hit_wall(
	const d3d_board *board,
	d3d_vec_s *pos,
	const d3d_vec_s *dpos,
	d3d_direction *dir,
	const d3d_texture **txtr)
{
	const d3d_block_s *block;
	for (;;) {
		size_t x, y;
		d3d_direction inverted;
		const d3d_block_s * const *blk = NULL;
		d3d_vec_s tonext = {0, 0};
		d3d_direction y_dir = D3D_DNEGY, x_dir = D3D_DNEGX;
		if (dpos->x < (d3d_scalar)0.0) {
			// The ray is going in the -x direction.
			tonext.x = -mod1(pos->x);
		} else if (dpos->x > (d3d_scalar)0.0) {
			// The ray is going in the +x direction.
			x_dir = D3D_DPOSX;
			tonext.x = revmod1(pos->x);
		}
		if (dpos->y < (d3d_scalar)0.0) {
			// The ray is going in the -y direction.
			tonext.y = -mod1(pos->y);
		} else if (dpos->y > (d3d_scalar)0.0) {
			// They ray is going in the +y direction.
			y_dir = D3D_DPOSY;
			tonext.y = revmod1(pos->y);
		}
		if (dpos->x == (d3d_scalar)0.0) {
			goto hit_y;
		} else if (dpos->y == (d3d_scalar)0.0) {
			goto hit_x;
		}
		if (tonext.x / dpos->x < tonext.y / dpos->y) {
			// The ray will hit a wall on the x-axis first
	hit_x:
			*dir = x_dir;
			pos->x += tonext.x;
			pos->y += tonext.x / dpos->x * dpos->y;
		} else {
			// The ray will hit a wall on the y-axis first
	hit_y:
			*dir = y_dir;
			pos->y += tonext.y;
			pos->x += tonext.y / dpos->y * dpos->x;
		}
		x = tocoord(pos->x, dpos->x > (d3d_scalar)0.0);
		y = tocoord(pos->y, dpos->y > (d3d_scalar)0.0);
		inverted = invert_dir(*dir);
		blk = GET(board, blocks, x, y);
		if (!blk) return NULL; // The ray left the board
		if ((*blk)->faces[*dir]) {
			block = *blk;
			*txtr = block->faces[*dir];
			*dir = inverted;
			return block;
		} else {
			// The face the ray hit is empty
			move_dir(*dir, &x, &y);
			blk = GET(board, blocks, x, y);
			if (!blk) return NULL; // The ray left the board
			if ((*blk)->faces[inverted]) {
				block = *blk;
				*txtr = block->faces[inverted];
				return block;
			} else {
				// The face the ray hit is empty
				// Nudge the ray past the wall:
				if (*dir == x_dir) {
					pos->x += copysign((d3d_scalar)0.0001,
						dpos->x);
				} else {
					pos->y += copysign((d3d_scalar)0.0001,
						dpos->y);
				}
			}
		}
		block = *blk;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq $0x0, -0x78(%rbp)
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2060
movl $0x3, -0x8c(%rbp)
movl $0x2, -0x90(%rbp)
movq -0x40(%rbp), %rax
xorps %xmm0, %xmm0
ucomisd (%rax), %xmm0
jbe 0x3e99
movq -0x38(%rbp), %rax
movsd (%rax), %xmm0
callq 0x4300
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x88(%rbp)
jmp 0x3ecb
movq -0x40(%rbp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3ec9
movl $0x0, -0x90(%rbp)
movq -0x38(%rbp), %rax
movsd (%rax), %xmm0
callq 0x42d0
movsd %xmm0, -0x88(%rbp)
jmp 0x3ecb
movq -0x40(%rbp), %rax
xorps %xmm0, %xmm0
ucomisd 0x8(%rax), %xmm0
jbe 0x3f05
movq -0x38(%rbp), %rax
movsd 0x8(%rax), %xmm0
callq 0x4300
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x80(%rbp)
jmp 0x3f36
movq -0x40(%rbp), %rax
movsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3f34
movl $0x1, -0x8c(%rbp)
movq -0x38(%rbp), %rax
movsd 0x8(%rax), %xmm0
callq 0x42d0
movsd %xmm0, -0x80(%rbp)
jmp 0x3f36
movq -0x40(%rbp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3f4e
jp 0x3f4e
jmp 0x3fdf
movq -0x40(%rbp), %rax
movsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3f64
jp 0x3f64
jmp 0x3f8c
jmp 0x3f66
movsd -0x88(%rbp), %xmm1
movq -0x40(%rbp), %rax
divsd (%rax), %xmm1
movsd -0x80(%rbp), %xmm0
movq -0x40(%rbp), %rax
divsd 0x8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3fdd
jmp 0x3f8c
movl -0x90(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, (%rax)
movsd -0x88(%rbp), %xmm0
movq -0x38(%rbp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movsd -0x88(%rbp), %xmm0
movq -0x40(%rbp), %rax
divsd (%rax), %xmm0
movq -0x40(%rbp), %rax
movsd 0x8(%rax), %xmm2
movq -0x38(%rbp), %rax
movsd 0x8(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
jmp 0x4028
jmp 0x3fdf
movl -0x8c(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, (%rax)
movsd -0x80(%rbp), %xmm0
movq -0x38(%rbp), %rax
addsd 0x8(%rax), %xmm0
movsd %xmm0, 0x8(%rax)
movsd -0x80(%rbp), %xmm0
movq -0x40(%rbp), %rax
divsd 0x8(%rax), %xmm0
movq -0x40(%rbp), %rax
movsd (%rax), %xmm2
movq -0x38(%rbp), %rax
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movq -0x38(%rbp), %rax
movsd (%rax), %xmm0
movq -0x40(%rbp), %rax
movsd (%rax), %xmm1
xorps %xmm2, %xmm2
ucomisd %xmm2, %xmm1
seta %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x4340
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq -0x40(%rbp), %rax
movsd 0x8(%rax), %xmm1
xorps %xmm2, %xmm2
ucomisd %xmm2, %xmm1
seta %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x4340
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
movl (%rax), %edi
callq 0x4410
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x40d1
movq -0x68(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x40d1
movq -0x30(%rbp), %rax
addq $0x10, %rax
movq -0x68(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq 0x8(%rdx), %rdx
imulq -0x60(%rbp), %rdx
addq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
jmp 0x40dc
xorl %eax, %eax
movq %rax, -0x98(%rbp)
jmp 0x40dc
movq -0x98(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x40fb
movq $0x0, -0x28(%rbp)
jmp 0x42bf
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
movl (%rcx), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x4145
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x48(%rbp), %rcx
movl (%rcx), %ecx
movq (%rax,%rcx,8), %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movl -0x6c(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, (%rax)
movq -0x58(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x42bf
movq -0x48(%rbp), %rax
movl (%rax), %edi
leaq -0x60(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0x4470
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x419f
movq -0x68(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x419f
movq -0x30(%rbp), %rax
addq $0x10, %rax
movq -0x68(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq 0x8(%rdx), %rdx
imulq -0x60(%rbp), %rdx
addq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
jmp 0x41aa
xorl %eax, %eax
movq %rax, -0xa0(%rbp)
jmp 0x41aa
movq -0xa0(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x41c9
movq $0x0, -0x28(%rbp)
jmp 0x42bf
movq -0x78(%rbp), %rax
movq (%rax), %rax
movl -0x6c(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x4204
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl -0x6c(%rbp), %ecx
movq (%rax,%rcx,8), %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x58(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x42bf
movq -0x48(%rbp), %rax
movl (%rax), %eax
cmpl -0x90(%rbp), %eax
jne 0x425e
movq -0x40(%rbp), %rax
movsd (%rax), %xmm0
movsd 0xce26(%rip), %xmm1     # 0x11048
movsd %xmm1, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd -0x8(%rbp), %xmm0
movsd -0x10(%rbp), %xmm1
movaps 0xce53(%rip), %xmm2     # 0x11090
pand %xmm2, %xmm1
movaps 0xcdc8(%rip), %xmm2     # 0x11010
pand %xmm2, %xmm0
por %xmm1, %xmm0
movq -0x38(%rbp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x42ab
movq -0x40(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd 0xcdd9(%rip), %xmm1     # 0x11048
movsd %xmm1, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
movaps 0xce06(%rip), %xmm2     # 0x11090
pand %xmm2, %xmm1
movaps 0xcd7b(%rip), %xmm2     # 0x11010
pand %xmm2, %xmm0
por %xmm1, %xmm0
movq -0x38(%rbp), %rax
addsd 0x8(%rax), %xmm0
movsd %xmm0, 0x8(%rax)
jmp 0x42ad
jmp 0x42af
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
jmp 0x3e2f
movq -0x28(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)