// File: /workspace/github_c_makefile_star2_O0/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
subq $0x3d0, %rsp            # imm = 0x3D0
movl $0x0, -0x4(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x12c0
movl $0x0, -0x130(%rbp)
movl $0x1, -0x12c(%rbp)
movl $0x2, -0x128(%rbp)
movl $0x3, -0x124(%rbp)
leaq -0x130(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl $0x4, -0xd8(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x1570
movl $0x5, -0x150(%rbp)
leaq -0x1a0(%rbp), %rdi
leaq 0x859e(%rip), %rsi      # 0xa0b0
movl $0x28, %edx
callq 0x10d0
leaq -0x1a0(%rbp), %rax
movq %rax, -0x140(%rbp)
movsd 0x855e(%rip), %xmm0     # 0xa090
movsd %xmm0, -0x1b8(%rbp)
movl $0x8, -0x1b0(%rbp)
movl $0x8, -0x1ac(%rbp)
leaq -0x1f8(%rbp), %rdi
leaq 0x857c(%rip), %rsi      # 0xa0d8
movl $0x40, %edx
callq 0x10d0
leaq -0x1f8(%rbp), %rax
movq %rax, -0x1a8(%rbp)
leaq -0x220(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x1fc0
movl %eax, -0x4(%rbp)
cmpl $0x0, %eax
je 0x1b97
movl -0x4(%rbp), %edi
callq 0x1180
leaq -0x240(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x220(%rbp), %rdx
callq 0x4510
movl %eax, -0x4(%rbp)
cmpl $0x0, %eax
je 0x1bc1
movl -0x4(%rbp), %edi
callq 0x1180
leaq -0x240(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x4770
leaq -0x278(%rbp), %rdi
callq 0x6da0
movl %eax, -0x4(%rbp)
cmpl $0x0, %eax
je 0x1bf0
movl -0x4(%rbp), %edi
callq 0x1180
leaq -0x2b0(%rbp), %rdi
callq 0x9210
movl %eax, -0x4(%rbp)
cmpl $0x0, %eax
je 0x1c0c
movl -0x4(%rbp), %edi
callq 0x1180
leaq -0x330(%rbp), %rdi
movl $0x5, %esi
movsd 0x8468(%rip), %xmm0     # 0xa088
callq 0x1850
leaq -0x380(%rbp), %rdi
movl $0x3, %esi
movsd 0x83ff(%rip), %xmm0     # 0xa038
callq 0x1850
leaq 0x84d3(%rip), %rdi      # 0xa118
leaq 0x84df(%rip), %rsi      # 0xa12b
callq 0x1130
movq %rax, -0x388(%rbp)
movl -0x1b0(%rbp), %eax
movl -0x1ac(%rbp), %ecx
imull %ecx, %eax
movq %rsp, %rcx
movq %rcx, -0x398(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x3d0(%rbp)
movq %rcx, %rsp
movq %rax, -0x3a0(%rbp)
movl -0x1b0(%rbp), %eax
movl -0x1ac(%rbp), %ecx
imull %ecx, %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x3c8(%rbp)
movq %rcx, %rsp
movq %rax, -0x3a8(%rbp)
movl $0x0, -0x3b8(%rbp)
movl $0x0, -0x390(%rbp)
movb $0x1, %al
testb $0x1, %al
jne 0x1ce5
jmp 0x1f62
movl $0x0, -0x3ac(%rbp)
movl $0x0, -0x38c(%rbp)
movl -0x38c(%rbp), %eax
cmpl -0xd0(%rbp), %eax
jge 0x1ed8
leaq -0x278(%rbp), %rdi
callq 0x6f70
movq -0x240(%rbp), %rsi
movslq -0x38c(%rbp), %rax
imulq $0x38, %rax, %rax
addq %rax, %rsi
leaq -0x330(%rbp), %r8
leaq -0x380(%rbp), %rax
leaq -0x278(%rbp), %rdi
leaq -0x120(%rbp), %rdx
movl $0x5, %ecx
movl $0x3, %r9d
subq $0x10, %rsp
movq %rax, (%rsp)
callq 0x7850
addq $0x10, %rsp
movl %eax, -0x4(%rbp)
cmpl $0x0, %eax
je 0x1d74
movl -0x4(%rbp), %edi
callq 0x1180
movl $0x0, -0x3bc(%rbp)
cmpl $0x0, -0x254(%rbp)
je 0x1d90
cmpl $0x0, -0x274(%rbp)
jne 0x1db2
movq -0x240(%rbp), %rax
movslq -0x38c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rax)
jmp 0x1ea1
movq -0x3d0(%rbp), %rdi
movl -0x1b0(%rbp), %eax
imull -0x1ac(%rbp), %eax
movslq %eax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x1070
movq -0x3d0(%rbp), %r8
leaq -0x278(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x178(%rbp), %rdx
leaq -0x1b8(%rbp), %rcx
callq 0x1600
movl %eax, -0x4(%rbp)
cmpl $0x0, %eax
je 0x1e0c
movl -0x4(%rbp), %edi
callq 0x1180
movq -0x3d0(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
xorl %edx, %edx
callq 0x18e0
movl %eax, -0x3bc(%rbp)
movl -0x3bc(%rbp), %eax
imull -0x3bc(%rbp), %eax
cvtsi2sd %eax, %xmm0
movq -0x240(%rbp), %rax
movslq -0x38c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x28(%rax)
movl -0x3bc(%rbp), %eax
cmpl -0x3ac(%rbp), %eax
jle 0x1e9f
movq -0x3d0(%rbp), %rsi
movq -0x3c8(%rbp), %rdi
movl -0x3bc(%rbp), %eax
movl %eax, -0x3ac(%rbp)
movl -0x1b0(%rbp), %eax
imull -0x1ac(%rbp), %eax
movslq %eax, %rdx
shlq $0x2, %rdx
callq 0x10d0
movl -0x38c(%rbp), %eax
movl %eax, -0x3b0(%rbp)
jmp 0x1ea1
movl -0x3bc(%rbp), %esi
leaq 0x827f(%rip), %rdi      # 0xa12d
movb $0x0, %al
callq 0x1060
movq 0xa124(%rip), %rax      # 0xbfe0
movq (%rax), %rdi
callq 0x1100
movl -0x38c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38c(%rbp)
jmp 0x1cf9
movl -0x390(%rbp), %esi
movq -0x240(%rbp), %rax
movslq -0x3b0(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x34(%rax), %edx
movq -0x240(%rbp), %rax
movslq -0x3b0(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x30(%rax), %ecx
movl -0x3ac(%rbp), %r8d
leaq 0x8217(%rip), %rdi      # 0xa133
movb $0x0, %al
callq 0x1060
movq -0x3c8(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
movl $0x1, %edx
callq 0x18e0
leaq -0x240(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x47c0
movl -0x390(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x390(%rbp)
jmp 0x1cda
movq -0x398(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)