// File: /workspace/github_c_makefile_star2_O3/kernfel[P]fNEAT/main.c
// Function: bots_in_a_maze
void bots_in_a_maze() {
	int err=0;

	struct NEAT_Params params;
	get_neat_params( &params );
	params.allowed_funcs = (enum CPPNFunc[]){CF_GAUSS, CF_SIGMOID, CF_SINE, CF_LINEAR};
	params.num_allowed_funcs = 4;

	struct Robot_Params bot_params;
	get_robot_params( &bot_params );
	bot_params.num_dist_sensors = 5;
	bot_params.dist_sensor_pos = (double[]){-M_PI/2, -M_PI/4, 0, M_PI/4, M_PI/2};

	TileMaze maze;
	maze.tile_width = 2;
	maze.x = 8;
	maze.y = 8;
	maze.tiles = (unsigned char []){
	1, 1, 1, 0, 1, 1, 1, 1,
	1, 0, 1, 0, 1, 0, 1, 0,
	1, 0, 1, 0, 1, 0, 1, 1,
	1, 0, 1, 0, 1, 1, 0, 1,
	1, 1, 1, 0, 0, 1, 1, 1,
	1, 0, 1, 1, 1, 1, 0, 1,
	1, 0, 0, 0, 1, 0, 0, 1,
	1, 0, 1, 1, 1, 1, 1, 1,
	};

	CPPN cppn;
	if (( err = create_CPPN( &cppn, &params ) ))
		exit(err);

	Population pop;
	if (( err = create_Population( &pop, &params, &cppn ) ))
		exit(err);
	randomise_population( &pop, &params );

	pNetwork net;
	if (( err = create_pNetwork( &net ) ))
		exit(err);

	eNetwork controller;
	if (( err = create_eNetwork( &controller ) ))
		exit(err);

	pNode sensors[NUM_SENSORS], motors[NUM_MOTORS];
/*	pNode sensors[NUM_SENSORS+1], motors[NUM_MOTORS];*/
/*	sensors[NUM_SENSORS].x[0] = 0;*/
/*	sensors[NUM_SENSORS].x[1] = -1;*/
	m_get_spread( sensors, NUM_SENSORS, -0.5 );
	m_get_spread( motors, NUM_MOTORS, 0.5 );

	FILE *fp = fopen( "logs/champions.txt", "w" );

	int i, gen;
	int behaviour[maze.x*maze.y], best_behaviour[maze.x*maze.y], best_score, winner, peak, peak_score=0;
	for ( gen=0; 1; gen++ ) {
		best_score = 0;
		for ( i=0; i<params.population_size; i++ ) {
			reset_pNetwork( &net );
			if (( err = build_pNetwork( &net, &pop.members[i].genotype, &params, NUM_SENSORS, sensors, NUM_MOTORS, motors ) ))
/*			if (( err = build_pNetwork( &net, &pop.members[i].genotype, &params, NUM_SENSORS+1, sensors, NUM_MOTORS, motors ) ))*/
				exit(err);

			int s=0;
			if ( ! net.num_used_links || ! net.num_used_nodes ) {
				pop.members[i].score = 0;
			} else {
				memset( behaviour, 0, maze.x*maze.y*sizeof *behaviour );
				if (( err = run_trial( &net, &params, &bot_params, &maze, behaviour ) ))
					exit(err);
				s = analyse_maze( &maze, behaviour, 0 );
				pop.members[i].score = s*s;

				if ( s > best_score ) {
					best_score = s;
					memcpy( best_behaviour, behaviour, maze.x*maze.y*sizeof *behaviour );
					winner = i;
				}

/*				printf( "indiv %5x | sp %3x | %2d tiles | %4d nodes | %6d links | inputs used: %d%d%d%d%d | bias used: %d\n",*/
/*					pop.members[i].id,*/
/*					pop.members[i].species_id,*/
/*					s,*/
/*					net.num_used_nodes,*/
/*					net.num_used_links,*/
/*					net.p_nodes[0][0].used,*/
/*					net.p_nodes[0][1].used,*/
/*					net.p_nodes[0][2].used,*/
/*					net.p_nodes[0][3].used,*/
/*					net.p_nodes[0][4].used,*/
/*					net.p_nodes[0][5].used*/
/*				*/
/*				);*/
			}

/*			printf( "%5x (%3x): ", pop.members[i].id, pop.members[i].species_id );*/
			printf( "%2d  ", s);
			fflush(stdout);
		}

		printf( "\n\nGeneration %d's champion explorer, #%5x of species #%3x, had a good look at %d tiles:",
			gen,
			pop.members[winner].id,
			pop.members[winner].species_id,
			best_score
		);
		analyse_maze( &maze, best_behaviour, 1 );
		
/*		if ( peak_score > best_score ) {*/
/*			fprintf( fp, "\n>>> Generation %d | #%5x | species #%3x | %2d tiles\n",*/
/*				gen,*/
/*				pop.members[peak].id,*/
/*				pop.members[peak].species_id,*/
/*				(int) sqrt(pop.members[peak].score)*/
/*			);*/
/*			dump_CPPN( &pop.members[peak].genotype, fp );*/

/*			reset_pNetwork( &net );*/
/*			if (( err = build_pNetwork( &net, &pop.members[peak].genotype, &params, NUM_SENSORS+1, sensors, NUM_MOTORS, motors ) ))*/
/*				exit(err);*/
/*			//dump_pNetwork( &net, fp );*/
/*			if (( err = build_eNetwork( &controller, &net, &params ) ))*/
/*				exit(err);*/
/*			dump_eNetwork( &controller, fp );*/
/*		} else {*/
/*			peak_score = best_score;*/
/*			peak = winner;*/
/*		}*/

/*		fprintf( fp, "\nGeneration %d | #%5x | species #%3x | %2d tiles\n",*/
/*			gen,*/
/*			pop.members[winner].id,*/
/*			pop.members[winner].species_id,*/
/*			best_score*/
/*		);*/
/*		dump_CPPN( &pop.members[winner].genotype, fp );*/
/*		reset_pNetwork( &net );*/
/*		if (( err = build_pNetwork( &net, &pop.members[winner].genotype, &params, NUM_SENSORS+1, sensors, NUM_MOTORS, motors ) ))*/
/*			exit(err);*/
/*		//dump_pNetwork( &net, fp );*/
/*		if (( err = build_eNetwork( &controller, &net, &params ) ))*/
/*			exit(err);*/
/*		dump_eNetwork( &controller, fp );*/

/*		if ( ! (gen%5) ) {*/
/*			fflush(fp);*/
/*			printf( "\nPaused... e=exit, anything else=continue: " );*/
/*			unsigned char c=getchar();*/
/*			if ( c == 'e' ) {*/
/*				fclose( fp );*/
/*				delete_eNetwork( &controller );*/
/*				delete_pNetwork( &net );*/
/*				delete_Population( &pop );*/
/*				delete_CPPN( &cppn );*/
/*				exit(0);*/
/*			}*/
/*		}*/

/*		while(!getchar());*/

		epoch( &pop, &params );
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
movaps 0x5938(%rip), %xmm0     # 0x7010
leaq -0x250(%rbp), %rsi
movaps %xmm0, (%rsi)
movaps 0x5937(%rip), %xmm0     # 0x7020
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, 0x20(%rsi)
movabsq $0x3fc999999999999a, %rax # imm = 0x3FC999999999999A
movq %rax, 0x30(%rsi)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x38(%rsi)
movabsq $0xf00000001, %rax      # imm = 0xF00000001
movq %rax, 0x58(%rsi)
movaps 0x591e(%rip), %xmm0     # 0x7040
movaps %xmm0, 0x60(%rsi)
movaps 0x5923(%rip), %xmm0     # 0x7050
movaps %xmm0, 0x70(%rsi)
movaps 0x5928(%rip), %xmm0     # 0x7060
movaps %xmm0, 0x80(%rsi)
movabsq $0x3fd999999999999a, %rax # imm = 0x3FD999999999999A
movq %rax, 0x90(%rsi)
movl $0xf, 0x98(%rsi)
movaps 0x590f(%rip), %xmm0     # 0x7070
movaps %xmm0, 0xa0(%rsi)
movaps 0x5911(%rip), %xmm0     # 0x7080
movaps %xmm0, 0xb0(%rsi)
movaps 0x5913(%rip), %xmm0     # 0x7090
movaps %xmm0, 0xc0(%rsi)
movaps 0x5915(%rip), %xmm0     # 0x70a0
movaps %xmm0, 0xd0(%rsi)
movaps 0x5917(%rip), %xmm0     # 0x70b0
movaps %xmm0, 0xe0(%rsi)
movaps 0x5919(%rip), %xmm0     # 0x70c0
movaps %xmm0, 0xf0(%rsi)
movabsq $0x4004000000000000, %rax # imm = 0x4004000000000000
movq %rax, 0x100(%rsi)
movq $0x0, 0x108(%rsi)
movl $0x0, 0x110(%rsi)
movaps 0x5915(%rip), %xmm0     # 0x70f0
leaq -0x110(%rbp), %rax
movaps %xmm0, (%rax)
movq %rax, 0x40(%rsi)
movaps 0x5910(%rip), %xmm0     # 0x7100
movups %xmm0, 0x48(%rsi)
movaps 0x58d5(%rip), %xmm0     # 0x70d0
movaps %xmm0, -0xc0(%rbp)
movaps 0x58d7(%rip), %xmm0     # 0x70e0
movaps %xmm0, -0xb0(%rbp)
movabsq $0x3fb999999999999a, %rax # imm = 0x3FB999999999999A
movq %rax, -0xa0(%rbp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0x90(%rbp)
movb $0x1, -0x80(%rbp)
movl $0x5, -0x98(%rbp)
movabsq $0x3ff921fb54442d18, %rax # imm = 0x3FF921FB54442D18
leaq -0x350(%rbp), %rcx
movq %rax, 0x20(%rcx)
movups 0x590c(%rip), %xmm0     # 0x7168
movaps %xmm0, (%rcx)
movups 0x5912(%rip), %xmm0     # 0x7178
movaps %xmm0, 0x10(%rcx)
movq %rcx, -0x88(%rbp)
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, -0x58(%rbp)
movabsq $0x800000008, %rax      # imm = 0x800000008
movq %rax, -0x50(%rbp)
movups 0x592c(%rip), %xmm0     # 0x71c0
leaq -0x3d0(%rbp), %rax
movaps %xmm0, 0x30(%rax)
movups 0x590a(%rip), %xmm0     # 0x71b0
movaps %xmm0, 0x20(%rax)
movups 0x58ef(%rip), %xmm0     # 0x71a0
movaps %xmm0, 0x10(%rax)
movupd 0x58d3(%rip), %xmm0     # 0x7190
movapd %xmm0, (%rax)
movq %rax, -0x48(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x1bc8
testl %eax, %eax
jne 0x1baa
leaq -0x78(%rbp), %rdi
leaq -0x250(%rbp), %rsi
leaq -0x138(%rbp), %rdx
callq 0x349c
testl %eax, %eax
jne 0x1baa
leaq -0x78(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x3605
leaq -0xf8(%rbp), %rdi
callq 0x4d28
testl %eax, %eax
jne 0x1baa
leaq -0x388(%rbp), %rdi
callq 0x6200
testl %eax, %eax
jne 0x1baa
movl $0x8, %eax
movl $0x1, %ecx
movabsq $-0x4020000000000000, %rdx # imm = 0xBFE0000000000000
movsd 0x57f4(%rip), %xmm0     # 0x7140
movsd 0x57e4(%rip), %xmm1     # 0x7138
movq %rdx, -0x328(%rbp,%rax)
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
divsd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, -0x320(%rbp,%rax)
addq $0x18, %rax
incl %ecx
cmpq $0x80, %rax
jne 0x1954
movaps 0x5787(%rip), %xmm0     # 0x7110
leaq -0x2a0(%rbp), %rax
movaps %xmm0, (%rax)
movsd 0x57ad(%rip), %xmm0     # 0x7148
movups %xmm0, 0x18(%rax)
movapd 0x5779(%rip), %xmm0     # 0x7120
movapd %xmm0, 0x30(%rax)
leaq 0x581d(%rip), %rdi      # 0x71d0
leaq 0x5829(%rip), %rsi      # 0x71e3
callq 0x1120
movq %rsp, %rbx
addq $-0x100, %rbx
movq %rbx, %rsp
movq %rsp, %rax
addq $-0x100, %rax
movq %rax, -0x40(%rbp)
movq %rax, %rsp
leaq -0xf8(%rbp), %r15
movl $0x0, -0x30(%rbp)
cmpl $0x0, -0x200(%rbp)
jle 0x1b48
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
movq %r15, %rdi
callq 0x4e3b
imulq $0x38, %r12, %r14
movq -0x78(%rbp), %rsi
addq %r14, %rsi
subq $0x8, %rsp
movq %r15, %rdi
leaq -0x250(%rbp), %rdx
movl $0x5, %ecx
leaq -0x320(%rbp), %r8
movl $0x3, %r9d
leaq -0x2a0(%rbp), %rax
pushq %rax
callq 0x5336
addq $0x10, %rsp
testl %eax, %eax
jne 0x1baa
cmpl $0x0, -0xd4(%rbp)
je 0x1ac7
cmpl $0x0, -0xf4(%rbp)
je 0x1ac7
movslq -0x50(%rbp), %rax
movslq -0x4c(%rbp), %rdx
imulq %rax, %rdx
shlq $0x2, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1060
movq %r15, %rdi
leaq -0x250(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
leaq -0x58(%rbp), %rcx
movq %rbx, %r8
callq 0x13fb
testl %eax, %eax
jne 0x1baa
movslq -0x50(%rbp), %rax
movslq -0x4c(%rbp), %rdx
imulq %rax, %rdx
testl %edx, %edx
jle 0x1ad9
movl %edx, %eax
xorl %ecx, %ecx
xorl %r13d, %r13d
cmpl $0x1, (%rbx,%rcx,4)
sbbl $-0x1, %r13d
incq %rcx
cmpq %rcx, %rax
jne 0x1ab5
jmp 0x1adc
movq -0x78(%rbp), %rax
movq $0x0, 0x28(%rax,%r14)
xorl %r13d, %r13d
jmp 0x1b13
xorl %r13d, %r13d
movl %r13d, %eax
imull %r13d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq -0x78(%rbp), %rax
movsd %xmm0, 0x28(%rax,%r14)
cmpl -0x2c(%rbp), %r13d
jle 0x1b13
shlq $0x2, %rdx
movq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x10c0
movl %r13d, -0x2c(%rbp)
movl %r12d, -0x34(%rbp)
leaq 0x56cb(%rip), %rdi      # 0x71e5
movl %r13d, %esi
xorl %eax, %eax
callq 0x1050
movq 0x84b5(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
callq 0x10f0
incq %r12
movslq -0x200(%rbp), %rax
cmpq %rax, %r12
jl 0x1a01
jmp 0x1b4f
movl $0x0, -0x2c(%rbp)
movq -0x78(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x38, %rcx, %rdx
movl 0x30(%rax,%rdx), %ecx
movl 0x34(%rax,%rdx), %edx
leaq 0x5681(%rip), %rdi      # 0x71eb
movl -0x30(%rbp), %r14d
movl %r14d, %esi
movl -0x2c(%rbp), %r8d
xorl %eax, %eax
callq 0x1050
leaq -0x58(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x1, %edx
callq 0x15c9
leaq -0x78(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x3649
incl %r14d
movl %r14d, -0x30(%rbp)
jmp 0x19ea
movl %eax, %edi
callq 0x1170