// File: /workspace/github_c_makefile_star2_O1/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 $0x398, %rsp            # imm = 0x398
movaps 0x58ab(%rip), %xmm0     # 0x7010
leaq -0x298(%rbp), %rsi
movups %xmm0, (%rsi)
movabsq $0x3fc999999999999a, %rdi # imm = 0x3FC999999999999A
movq %rdi, 0x10(%rsi)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x18(%rsi)
movq %rdi, 0x20(%rsi)
movq %rax, 0x28(%rsi)
movq %rdi, 0x30(%rsi)
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x38(%rsi)
xorl %edx, %edx
movq %rdx, 0x40(%rsi)
movaps 0x586e(%rip), %xmm0     # 0x7020
movups %xmm0, 0x48(%rsi)
movabsq $0xf00000001, %rcx      # imm = 0xF00000001
movq %rcx, 0x58(%rsi)
movq %rdi, 0x60(%rsi)
movabsq $0x4008000000000000, %rcx # imm = 0x4008000000000000
movq %rcx, 0x68(%rsi)
movabsq $0x3fb999999999999a, %rcx # imm = 0x3FB999999999999A
movq %rcx, 0x70(%rsi)
movabsq $0x4014000000000000, %r8 # imm = 0x4014000000000000
movq %r8, 0x78(%rsi)
movq %rax, 0x80(%rsi)
movq %rax, 0x88(%rsi)
movabsq $0x3fd999999999999a, %r8 # imm = 0x3FD999999999999A
movq %r8, 0x90(%rsi)
movl $0xf, 0x98(%rsi)
movq %rdi, 0xa0(%rsi)
movabsq $0x3fe999999999999a, %rdi # imm = 0x3FE999999999999A
movq %rdi, 0xa8(%rsi)
movq %r8, 0xb0(%rsi)
movabsq $0x3f947ae147ae147b, %r8 # imm = 0x3F947AE147AE147B
movq %r8, 0xb8(%rsi)
movabsq $0x3f9999999999999a, %r8 # imm = 0x3F9999999999999A
movq %r8, 0xc0(%rsi)
movabsq $0x3f847ae147ae147b, %r8 # imm = 0x3F847AE147AE147B
movq %r8, 0xc8(%rsi)
movabsq $0x3fe0000000000000, %rbx # imm = 0x3FE0000000000000
movq %rbx, 0xd0(%rsi)
movabsq $0x3f50624dd2f1a9fc, %r9 # imm = 0x3F50624DD2F1A9FC
movq %r9, 0xd8(%rsi)
movq %rdi, 0xe0(%rsi)
movabsq $0x3fe8000000000000, %rdi # imm = 0x3FE8000000000000
movq %rdi, 0xe8(%rsi)
movabsq $0x4012000000000000, %rdi # imm = 0x4012000000000000
movq %rdi, 0xf0(%rsi)
movabsq $0x3fd0000000000000, %rdi # imm = 0x3FD0000000000000
movq %rdi, 0xf8(%rsi)
movabsq $0x4004000000000000, %rdi # imm = 0x4004000000000000
movq %rdi, 0x100(%rsi)
movq %rdx, 0x108(%rsi)
movl $0x0, 0x110(%rsi)
movaps 0x573e(%rip), %xmm0     # 0x7030
leaq -0x108(%rbp), %rdx
movups %xmm0, (%rdx)
movq %rdx, 0x40(%rsi)
movl $0x4, 0x48(%rsi)
movq %rax, -0xc0(%rbp)
movq %r8, -0xb8(%rbp)
movq %rcx, -0xb0(%rbp)
movq %r8, -0xa8(%rbp)
movq %rcx, -0xa0(%rbp)
movq %rax, -0x90(%rbp)
movb $0x1, -0x80(%rbp)
movl $0x5, -0x98(%rbp)
movabsq $0x3ff921fb54442d18, %rax # imm = 0x3FF921FB54442D18
leaq -0x340(%rbp), %rcx
movq %rax, 0x20(%rcx)
movups 0x571d(%rip), %xmm0     # 0x7078
movaps %xmm0, (%rcx)
movups 0x5723(%rip), %xmm0     # 0x7088
movaps %xmm0, 0x10(%rcx)
movq %rcx, -0x88(%rbp)
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, -0x50(%rbp)
movabsq $0x800000008, %rax      # imm = 0x800000008
movq %rax, -0x48(%rbp)
movups 0x573d(%rip), %xmm0     # 0x70d0
leaq -0x3c0(%rbp), %rax
movaps %xmm0, 0x30(%rax)
movups 0x571b(%rip), %xmm0     # 0x70c0
movaps %xmm0, 0x20(%rax)
movups 0x5700(%rip), %xmm0     # 0x70b0
movaps %xmm0, 0x10(%rax)
movupd 0x56e4(%rip), %xmm0     # 0x70a0
movapd %xmm0, (%rax)
movq %rax, -0x40(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x1cdc
testl %eax, %eax
jne 0x1cc1
leaq -0x78(%rbp), %rdi
leaq -0x298(%rbp), %rsi
leaq -0x130(%rbp), %rdx
callq 0x3250
testl %eax, %eax
jne 0x1cc1
leaq -0x78(%rbp), %rdi
leaq -0x298(%rbp), %rsi
callq 0x33b1
leaq -0xf8(%rbp), %rdi
callq 0x4b34
testl %eax, %eax
jne 0x1cc1
leaq -0x378(%rbp), %rdi
callq 0x6164
testl %eax, %eax
jne 0x1cc1
movl $0x8, %eax
movl $0x1, %ecx
movabsq $-0x4020000000000000, %rdx # imm = 0xBFE0000000000000
movsd 0x5605(%rip), %xmm1     # 0x7050
movsd 0x55f5(%rip), %xmm0     # 0x7048
movq %rdx, -0x318(%rbp,%rax)
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
divsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, -0x310(%rbp,%rax)
addq $0x18, %rax
incl %ecx
cmpq $0x80, %rax
jne 0x1a53
movl $0x8, %eax
movl $0x1, %ecx
movsd 0x55c5(%rip), %xmm1     # 0x7058
movq %rbx, -0x188(%rbp,%rax)
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, -0x180(%rbp,%rax)
addq $0x18, %rax
incl %ecx
cmpq $0x50, %rax
jne 0x1a93
leaq 0x561a(%rip), %rdi      # 0x70e0
leaq 0x5626(%rip), %rsi      # 0x70f3
callq 0x1120
leaq -0x50(%rbp), %rcx
movl 0xc(%rcx), %eax
imull 0x8(%rcx), %eax
movq %rsp, %r14
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
leaq -0xf8(%rbp), %r12
movl $0x0, -0x30(%rbp)
cmpl $0x0, -0x248(%rbp)
jle 0x1c63
xorl %r13d, %r13d
movl $0x0, -0x2c(%rbp)
movq %r12, %rdi
callq 0x4c47
imulq $0x38, %r13, %r15
movq -0x78(%rbp), %rsi
addq %r15, %rsi
subq $0x8, %rsp
movq %r12, %rdi
leaq -0x298(%rbp), %rdx
movl $0x5, %ecx
leaq -0x310(%rbp), %r8
movl $0x3, %r9d
leaq -0x180(%rbp), %rax
pushq %rax
callq 0x516a
addq $0x10, %rsp
testl %eax, %eax
jne 0x1cc1
cmpl $0x0, -0xd4(%rbp)
je 0x1be9
cmpl $0x0, -0xf4(%rbp)
je 0x1be9
movslq -0x48(%rbp), %rax
movslq -0x44(%rbp), %rdx
imulq %rax, %rdx
shlq $0x2, %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1060
movq %r12, %rdi
leaq -0x298(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
leaq -0x50(%rbp), %rcx
movq %r14, %r8
callq 0x147c
testl %eax, %eax
jne 0x1cc1
movslq -0x48(%rbp), %rax
movslq -0x44(%rbp), %rdx
imulq %rax, %rdx
testl %edx, %edx
jle 0x1bfa
movl %edx, %eax
xorl %ecx, %ecx
xorl %ebx, %ebx
cmpl $0x1, (%r14,%rcx,4)
sbbl $-0x1, %ebx
incq %rcx
cmpq %rcx, %rax
jne 0x1bd7
jmp 0x1bfc
movq -0x78(%rbp), %rax
movq $0x0, 0x28(%rax,%r15)
xorl %ebx, %ebx
jmp 0x1c2f
xorl %ebx, %ebx
movl %ebx, %eax
imull %ebx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq -0x78(%rbp), %rax
movsd %xmm0, 0x28(%rax,%r15)
cmpl -0x2c(%rbp), %ebx
jle 0x1c2f
shlq $0x2, %rdx
movq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0x10c0
movl %ebx, -0x2c(%rbp)
movl %r13d, -0x34(%rbp)
leaq 0x54bf(%rip), %rdi      # 0x70f5
movl %ebx, %esi
xorl %eax, %eax
callq 0x1050
movq 0x839a(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
callq 0x10f0
incq %r13
movslq -0x248(%rbp), %rax
cmpq %rax, %r13
jl 0x1b24
jmp 0x1c6a
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 0x5476(%rip), %rdi      # 0x70fb
movl -0x30(%rbp), %ebx
movl %ebx, %esi
movl -0x2c(%rbp), %r8d
xorl %eax, %eax
callq 0x1050
leaq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl $0x1, %edx
callq 0x1655
leaq -0x78(%rbp), %rdi
leaq -0x298(%rbp), %rsi
callq 0x33f5
incl %ebx
movl %ebx, -0x30(%rbp)
jmp 0x1b0d
movl %eax, %edi
callq 0x1170