// File: /workspace/github_c_makefile_star2_O2/kernfel[P]fNEAT/robot-simplistic.c
// Function: get_sensor_readings_in_tilemaze
void get_sensor_readings_in_tilemaze( Robot *bot, TileMaze *maze, struct Robot_Params *params, double *sensor_inputs ) {
	int i;
	for ( i=0; i<params->num_dist_sensors; i++ ) {
		double s_cos = cos(bot->heading + params->dist_sensor_pos[i]);
		double s_sin = sin(bot->heading + params->dist_sensor_pos[i]);
		double s_x = s_cos * params->dist_sensor_length;
		double s_y = s_sin * params->dist_sensor_length;
		double root_x = bot->x + s_cos * params->radius;
		double root_y = bot->y + s_sin * params->radius;
		int root_tile_x = (int)(root_x / maze->tile_width);
		int root_tile_y = (int)(root_y / maze->tile_width);
		int cross_x = (int)((root_x + s_x) / maze->tile_width) - root_tile_x;
		int cross_y = (int)((root_y + s_y) / maze->tile_width) - root_tile_y;
		
		sensor_inputs[i] = params->invert_dist_sensors ? 1 : 0;

		if ( ! cross_x && ! cross_y )
			continue;

		double reading=0;
		
		// Penetration of the sensor stub into the space outside of the root tile
		double depth_x = (root_x + s_x - (cross_x>0 ? root_tile_x+1 : root_tile_x)*maze->tile_width) / s_x;
		double depth_y = (root_y + s_y - (cross_y>0 ? root_tile_y+1 : root_tile_y)*maze->tile_width) / s_y;
		
		// X crossing only, solid/invalid target tile
		if ( cross_x && ! cross_y
		 && (  root_tile_x+cross_x == maze->x || root_tile_x+cross_x == -1
		    || ! maze->tiles[root_tile_x+cross_x + maze->x*root_tile_y]
		    )
		) {
			reading = depth_x;

		// Y crossing only, solid/invalid target tile
		} else if ( cross_y && ! cross_x
		 && (  root_tile_y+cross_y == maze->y || root_tile_y+cross_y == -1
		    || ! maze->tiles[root_tile_x + maze->x*(root_tile_y+cross_y)]
		    )
		) {
			reading = depth_y;

		// Diagonal crossing
		} else if ( cross_x && cross_y ) {
			// Larger proportion of the x vector outside the tile => sensor stub reaches into diagonal tile via x-adjacent neighbour
			if ( depth_x > depth_y ) {
				// x-adjacent neighbour is solid/invalid target
				if ( root_tile_x+cross_x == -1 || root_tile_x+cross_x == maze->x || ! maze->tiles[root_tile_x+cross_x + maze->x*root_tile_y] )
					reading = depth_x;
				// Diagonally adjacent neighbour is solid/invalid target
				else if ( root_tile_y+cross_y == -1 || root_tile_y+cross_y == maze->y \
				 || ! maze->tiles[root_tile_x+cross_x + maze->x*(root_tile_y+cross_y)] )
					reading = depth_y;
			} else {
				if ( root_tile_y+cross_y == -1 || root_tile_y+cross_y == maze->y || ! maze->tiles[root_tile_x + maze->x*(root_tile_y+cross_y)] )
					reading = depth_y;
				else if ( root_tile_x+cross_x == -1 || root_tile_x+cross_x == maze->x \
				 || ! maze->tiles[root_tile_x+cross_x + maze->x*(root_tile_y+cross_y)] )
					reading = depth_x;
			}
		}

		sensor_inputs[i] = params->invert_dist_sensors ? 1-reading : reading;
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
xorl %r13d, %r13d
movslq 0x28(%r14), %rax
cmpq %rax, %r13
jge 0x5b13
movsd 0x10(%r12), %xmm0
movq 0x38(%r14), %rax
addsd (%rax,%r13,8), %xmm0
callq 0x1080
movapd %xmm0, (%rsp)
movsd 0x10(%r12), %xmm0
movq 0x38(%r14), %rax
addsd (%rax,%r13,8), %xmm0
callq 0x1130
movb 0x40(%r14), %al
movsd 0x823(%rip), %xmm1      # 0x6110
testb %al, %al
jne 0x58f5
xorpd %xmm1, %xmm1
movapd (%rsp), %xmm4
unpcklpd %xmm0, %xmm4            # xmm4 = xmm4[0],xmm0[0]
movsd (%r14), %xmm3
movsd 0x30(%r14), %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm4, %xmm2
movupd (%r12), %xmm0
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
mulpd %xmm4, %xmm3
addpd %xmm0, %xmm3
movsd (%r15), %xmm4
unpcklpd %xmm4, %xmm4            # xmm4 = xmm4[0,0]
movapd %xmm2, %xmm0
addpd %xmm3, %xmm0
divpd %xmm4, %xmm3
cvttpd2dq %xmm3, %xmm3
pshufd $0x55, %xmm3, %xmm5     # xmm5 = xmm3[1,1,1,1]
movd %xmm5, %esi
movd %xmm3, %edi
movapd %xmm0, %xmm5
divpd %xmm4, %xmm5
cvttpd2dq %xmm5, %xmm4
pshufd $0x55, %xmm4, %xmm5     # xmm5 = xmm4[1,1,1,1]
movd %xmm5, %edx
movd %xmm4, %ecx
movsd %xmm1, (%rbx,%r13,8)
cmpl %edi, %ecx
jne 0x5974
cmpl %esi, %edx
je 0x5b0b
pcmpgtd %xmm3, %xmm4
pshufd $0x50, %xmm4, %xmm1     # xmm1 = xmm4[0,0,1,1]
movmskpd %xmm1, %r8d
movl %r8d, %r9d
andl $0x1, %r9d
addl %edi, %r9d
cvtsi2sd %r9d, %xmm4
movsd (%r15), %xmm3
mulsd %xmm3, %xmm4
movapd %xmm0, %xmm1
subsd %xmm4, %xmm1
divsd %xmm2, %xmm1
cmpl %edi, %ecx
je 0x59e2
cmpl %esi, %edx
jne 0x59e2
movl 0x8(%r15), %r9d
cmpl %ecx, %r9d
sete %r10b
cmpl $-0x1, %ecx
sete %r11b
orb %r10b, %r11b
jne 0x5aed
movq 0x10(%r15), %r10
imull %esi, %r9d
addl %ecx, %r9d
movslq %r9d, %r9
cmpb $0x0, (%r10,%r9)
je 0x5aed
shrb %r8b
movzbl %r8b, %r8d
addl %esi, %r8d
cvtsi2sd %r8d, %xmm4
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
mulsd %xmm3, %xmm4
subsd %xmm4, %xmm0
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
divsd %xmm2, %xmm0
cmpl %edi, %ecx
jne 0x5a39
cmpl %esi, %edx
je 0x5a39
cmpl $-0x1, %edx
je 0x5aaa
cmpl %edx, 0xc(%r15)
je 0x5aaa
movq 0x10(%r15), %r8
movl 0x8(%r15), %r9d
imull %edx, %r9d
addl %edi, %r9d
movslq %r9d, %r9
cmpb $0x0, (%r8,%r9)
je 0x5aaa
xorpd %xmm2, %xmm2
cmpl %edi, %ecx
je 0x5af1
cmpl %esi, %edx
je 0x5af1
ucomisd %xmm0, %xmm1
jbe 0x5a9f
cmpl $-0x1, %ecx
je 0x5aed
movl 0x8(%r15), %edi
cmpl %ecx, %edi
je 0x5aed
movq 0x10(%r15), %r8
imull %edi, %esi
addl %ecx, %esi
movslq %esi, %rsi
cmpb $0x0, (%r8,%rsi)
movapd %xmm1, %xmm2
je 0x5af1
cmpl $-0x1, %edx
je 0x5aaa
cmpl %edx, 0xc(%r15)
je 0x5aaa
imull %edx, %edi
addl %ecx, %edi
movslq %edi, %rcx
cmpb $0x0, (%r8,%rcx)
xorpd %xmm2, %xmm2
jne 0x5af1
jmp 0x5aaa
cmpl $-0x1, %edx
je 0x5aaa
cmpl %edx, 0xc(%r15)
jne 0x5ab0
movapd %xmm0, %xmm2
jmp 0x5af1
movq 0x10(%r15), %rsi
movl 0x8(%r15), %r8d
imull %r8d, %edx
addl %edx, %edi
movslq %edi, %rdi
cmpb $0x0, (%rsi,%rdi)
movapd %xmm0, %xmm2
je 0x5af1
cmpl $-0x1, %ecx
sete %dil
cmpl %ecx, %r8d
sete %r8b
orb %dil, %r8b
jne 0x5aed
addl %ecx, %edx
movslq %edx, %rcx
cmpb $0x0, (%rsi,%rcx)
xorpd %xmm2, %xmm2
jne 0x5af1
movapd %xmm1, %xmm2
testb %al, %al
je 0x5b05
movsd 0x613(%rip), %xmm0      # 0x6110
subsd %xmm2, %xmm0
movapd %xmm0, %xmm2
movsd %xmm2, (%rbx,%r13,8)
incq %r13
jmp 0x58a3
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nopl (%rax)