// File: /workspace/github_c_makefile_star2_O1/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:
cmpl $0x0, 0x28(%rdx)
jle 0x6163
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
xorl %r13d, %r13d
movsd 0x10(%r12), %xmm0
movq 0x38(%r14), %rax
addsd (%rax,%r13,8), %xmm0
callq 0x1080
movsd %xmm0, (%rsp)
movsd 0x10(%r12), %xmm0
movq 0x38(%r14), %rax
addsd (%rax,%r13,8), %xmm0
callq 0x1130
movb 0x40(%r14), %al
movsd 0x110d(%rip), %xmm5     # 0x7040
testb %al, %al
jne 0x5f3b
xorpd %xmm5, %xmm5
movsd (%r14), %xmm1
movsd 0x30(%r14), %xmm3
movsd (%rsp), %xmm2
movapd %xmm2, %xmm4
mulsd %xmm3, %xmm4
mulsd %xmm0, %xmm3
mulsd %xmm1, %xmm2
addsd (%r12), %xmm2
mulsd %xmm1, %xmm0
addsd 0x8(%r12), %xmm0
movsd (%r15), %xmm6
movapd %xmm4, %xmm1
addsd %xmm2, %xmm1
divsd %xmm6, %xmm2
cvttsd2si %xmm2, %esi
movapd %xmm3, %xmm2
addsd %xmm0, %xmm2
divsd %xmm6, %xmm0
cvttsd2si %xmm0, %edi
movapd %xmm1, %xmm0
divsd %xmm6, %xmm0
cvttsd2si %xmm0, %ecx
movapd %xmm2, %xmm0
divsd %xmm6, %xmm0
cvttsd2si %xmm0, %edx
movsd %xmm5, (%rbx,%r13,8)
cmpl %esi, %ecx
jne 0x5fbb
cmpl %edi, %edx
je 0x6145
xorl %r10d, %r10d
cmpl %esi, %ecx
setg %r10b
addl %esi, %r10d
xorl %r8d, %r8d
cmpl %edi, %edx
setg %r9b
xorps %xmm5, %xmm5
cvtsi2sd %r10d, %xmm5
movsd (%r15), %xmm0
mulsd %xmm0, %xmm5
subsd %xmm5, %xmm1
divsd %xmm4, %xmm1
cmpl %esi, %ecx
je 0x6025
cmpl %edi, %edx
jne 0x6025
movl 0x8(%r15), %r10d
cmpl %ecx, %r10d
sete %r11b
cmpl $-0x1, %ecx
sete %bpl
orb %r11b, %bpl
jne 0x6127
movq 0x10(%r15), %r11
imull %edi, %r10d
addl %ecx, %r10d
movslq %r10d, %r10
cmpb $0x0, (%r11,%r10)
je 0x6127
movb %r9b, %r8b
addl %edi, %r8d
xorps %xmm4, %xmm4
cvtsi2sd %r8d, %xmm4
mulsd %xmm0, %xmm4
subsd %xmm4, %xmm2
divsd %xmm3, %xmm2
cmpl %esi, %ecx
jne 0x6073
cmpl %edi, %edx
je 0x6073
cmpl $-0x1, %edx
je 0x60e4
cmpl %edx, 0xc(%r15)
je 0x60e4
movq 0x10(%r15), %r8
movl 0x8(%r15), %r9d
imull %edx, %r9d
addl %esi, %r9d
movslq %r9d, %r9
cmpb $0x0, (%r8,%r9)
je 0x60e4
xorpd %xmm0, %xmm0
cmpl %esi, %ecx
je 0x612b
cmpl %edi, %edx
je 0x612b
ucomisd %xmm2, %xmm1
jbe 0x60d9
cmpl $-0x1, %ecx
je 0x6127
movl 0x8(%r15), %esi
cmpl %ecx, %esi
je 0x6127
movq 0x10(%r15), %r8
imull %esi, %edi
addl %ecx, %edi
movslq %edi, %rdi
cmpb $0x0, (%r8,%rdi)
movapd %xmm1, %xmm0
je 0x612b
cmpl $-0x1, %edx
je 0x60e4
cmpl %edx, 0xc(%r15)
je 0x60e4
imull %edx, %esi
addl %ecx, %esi
movslq %esi, %rcx
cmpb $0x0, (%r8,%rcx)
xorpd %xmm0, %xmm0
jne 0x612b
jmp 0x60e4
cmpl $-0x1, %edx
je 0x60e4
cmpl %edx, 0xc(%r15)
jne 0x60ea
movapd %xmm2, %xmm0
jmp 0x612b
movq 0x10(%r15), %rdi
movl 0x8(%r15), %r8d
imull %r8d, %edx
addl %edx, %esi
movslq %esi, %rsi
cmpb $0x0, (%rdi,%rsi)
movapd %xmm2, %xmm0
je 0x612b
cmpl $-0x1, %ecx
sete %sil
cmpl %ecx, %r8d
sete %r8b
orb %sil, %r8b
jne 0x6127
addl %ecx, %edx
movslq %edx, %rcx
cmpb $0x0, (%rdi,%rcx)
xorpd %xmm0, %xmm0
jne 0x612b
movapd %xmm1, %xmm0
testb %al, %al
je 0x613f
movsd 0xf09(%rip), %xmm1      # 0x7040
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movsd %xmm0, (%rbx,%r13,8)
incq %r13
movslq 0x28(%r14), %rax
cmpq %rax, %r13
jl 0x5ef6
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq