// File: /workspace/github_c_makefile_star2_O0/kernfel[P]fNEAT/neat.c
// Function: reproduce_population
int reproduce_population( Population *pop, struct NEAT_Params *params, Individual *reps, Individual **champions ) {
	int i, j, k=0, err=0;

	// Determine the number of offspring each species is allowed
	int num_offspring[pop->num_species];
	for ( i=0; i<pop->num_species; i++ )
		num_offspring[i]=0; // Appease valgrind at low cost
	if (( err = get_population_fertility( pop, params, num_offspring ) ))
		return err;

	// Get ranking
	Individual ***ranked = get_population_ranking( pop, &err );
	if ( err )
		return err;

	// Determine survivors, remove the rest
	int num_parents[pop->num_species];
	Individual *free_slots[pop->num_members];
	int species_processed=0;
	for ( i=0; i<pop->num_species; i++ ) {
		// Let subthreshold species go extinct
		if ( !num_offspring[i] ) {
			num_parents[i] = 0;
			reps[i].species_id = 0;
			champions[i] = 0;
		} else {
			// Find out how many old individuals get to reproduce
			num_parents[i] = 1 + (int)(params->survival_quota * pop->species[i].size);
			if ( num_parents[i] > num_offspring[i] )
				num_parents[i] = num_offspring[i];
			
			// Save a random parent as representative for speciation
			if (( err = clone_CPPN( &reps[i].genotype, &ranked[i][rand()%num_parents[i]]->genotype ) ))
				goto failure;
			reps[i].species_id = pop->species[i].id;
			
			// Make sure the champion survives unchanged
			if ( pop->species[i].size >= params->champion_threshold )
				champions[i] = ranked[i][0];
			else
				champions[i] = 0;
		}

		// Eliminate the infertile individuals and post job openings in free_slots
		for ( j=num_parents[i]; j<pop->species[i].size; j++ ) {
			free_slots[k] = ranked[i][j];
			delete_CPPN( &free_slots[k]->genotype );
			k++;
		}
		species_processed++;
	}

	// Error protection
	free_slots[k] = 0;

	// Repopulate from the survivors
	CPPN *g;
	k=0;
	for ( i=0; i<pop->num_species; i++ ) {
		for ( j=0; j<num_offspring[i]; j++ ) {
			// Retrieve a fertile individual
			g = &ranked[i][j%num_parents[i]]->genotype;
			
			// If necessary, spread its genes...
			if ( j >= num_parents[i] ) {
				free_slots[k]->species_id = pop->species[i].id;
				free_slots[k]->id = ++params->individual_counter;
				if (( err = clone_CPPN( &free_slots[k]->genotype, g ) ))
					goto failure;
				// ... and work with the result.
				g = &free_slots[k++]->genotype;

			// No further processing for champions
			} else if ( g == &champions[i]->genotype ) {
				continue;
			}

			// Perform crossover
			if ( num_parents[i] > 1 && params->crossover_prob * RAND_MAX > rand() ) {
				// Find a non-self partner in the same species
				int l=rand()%(num_parents[i]-1);
				if ( l >= j )
					l++;
				// Do the deed
				if (( err = crossover_CPPN( g, &ranked[i][l]->genotype, params ) ))
					goto failure;

			// Interspecies crossover
			}
			 else if ( pop->num_species > 1 \
			 && params->interspecies_mating_prob > 0 \
			 && params->interspecies_mating_prob * RAND_MAX > rand() ) {
				do {
					// Select a species to mate with
					int l=rand()%(pop->num_species-1);
					if ( l >= i )
						l++;
					// If that species doesn't do offspring, cancel the mating attempt
					if ( ! num_parents[l] )
						break;
					// Select a mate within the chosen species
					int m=rand()%num_parents[l];
					// Have at it
					if (( err = crossover_CPPN( g, &ranked[l][m]->genotype, params ) ))
						goto failure;
				} while (0);
			}
		}
	}

cleanup:
	free( ranked );

	return err;

failure:
	for ( i=0; i<species_processed; i++ ) {
		if ( reps[i].species_id ) {
			delete_CPPN( &reps[i].genotype );
			reps[i].species_id = 0;
		}
	}
	goto cleanup;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x40(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x4a07
movq -0x90(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x49de
movq -0x90(%rbp), %rdx
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x5a90
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
je 0x4a35
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x5041
movq -0x10(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x6140
movq %rax, -0x58(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x4a5e
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x5041
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa0(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x98(%rbp)
movq %rcx, %rsp
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x4ce5
movq -0x90(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x4b18
movq -0xa0(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x30(%rax)
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x4c5e
movq -0x90(%rbp), %rcx
movq -0xa0(%rbp), %rax
movq -0x18(%rbp), %rdx
movsd 0x60(%rdx), %xmm0
movq -0x10(%rbp), %rdx
movq 0x10(%rdx), %rdx
movslq -0x2c(%rbp), %rsi
imulq $0x18, %rsi, %rsi
addq %rsi, %rdx
cvtsi2sdl 0x4(%rdx), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %esi
addl $0x1, %esi
movslq -0x2c(%rbp), %rdx
movl %esi, (%rax,%rdx,4)
movslq -0x2c(%rbp), %rdx
movl (%rax,%rdx,4), %eax
movslq -0x2c(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jle 0x4b85
movq -0xa0(%rbp), %rax
movq -0x90(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
movl (%rcx,%rdx,4), %edx
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x58(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
callq 0x11b0
movq -0xa0(%rbp), %rcx
movq -0xb0(%rbp), %rdi
movslq -0x2c(%rbp), %rsi
cltd
idivl (%rcx,%rsi,4)
movq -0xa8(%rbp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rsi
callq 0x22f0
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
je 0x4be9
jmp 0x4fe4
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rdx
imulq $0x38, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jl 0x4c4c
movq -0x58(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdx
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x4c5c
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x4c5e
movq -0xa0(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x2c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jge 0x4cce
movq -0x98(%rbp), %rax
movq -0x58(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rdx
movslq -0x34(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x23a0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x4c6f
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x4aba
movq -0x98(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x4fca
movl $0x0, -0x30(%rbp)
movq -0x90(%rbp), %rcx
movl -0x30(%rbp), %eax
movslq -0x2c(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x4fba
movq -0xa0(%rbp), %rcx
movq -0x58(%rbp), %rax
movslq -0x2c(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rax, -0xb8(%rbp)
movl -0x30(%rbp), %eax
movslq -0x2c(%rbp), %rsi
cltd
idivl (%rcx,%rsi,4)
movq -0xb8(%rbp), %rax
movslq %edx, %rdx
movq (%rax,%rdx,8), %rax
movq %rax, -0x78(%rbp)
movl -0x30(%rbp), %eax
movslq -0x2c(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jl 0x4dfd
movq -0x98(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x2c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %edx
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movl %edx, 0x30(%rcx)
movq -0x18(%rbp), %rcx
movl 0x110(%rcx), %edx
addl $0x1, %edx
movl %edx, 0x110(%rcx)
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movl %edx, 0x34(%rcx)
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x78(%rbp), %rsi
callq 0x22f0
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
je 0x4dde
jmp 0x4fe4
movq -0x98(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x34(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x78(%rbp)
jmp 0x4e19
movq -0x78(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpq %rcx, %rax
jne 0x4e17
jmp 0x4fac
jmp 0x4e19
movq -0xa0(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpl $0x1, (%rax,%rcx,4)
jle 0x4ec1
movq -0x18(%rbp), %rax
movsd 0x5356(%rip), %xmm0     # 0xa190
mulsd 0xd0(%rax), %xmm0
movsd %xmm0, -0xc0(%rbp)
callq 0x11b0
movsd -0xc0(%rbp), %xmm0
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4ec1
callq 0x11b0
movq -0xa0(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x1, %ecx
cltd
idivl %ecx
movl %edx, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jl 0x4e8e
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movq -0x78(%rbp), %rdi
movq -0x58(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x7c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x18(%rbp), %rdx
callq 0x3cc0
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
je 0x4ebc
jmp 0x4fe4
jmp 0x4faa
movq -0x10(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jle 0x4fa8
movq -0x18(%rbp), %rax
movsd 0xd8(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4fa8
movq -0x18(%rbp), %rax
movsd 0x529c(%rip), %xmm0     # 0xa190
mulsd 0xd8(%rax), %xmm0
movsd %xmm0, -0xc8(%rbp)
callq 0x11b0
movsd -0xc8(%rbp), %xmm0
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4fa8
jmp 0x4f21
callq 0x11b0
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
subl $0x1, %ecx
cltd
idivl %ecx
movl %edx, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jl 0x4f47
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
movq -0xa0(%rbp), %rax
movslq -0x80(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x4f5a
jmp 0x4fa6
callq 0x11b0
movq -0xa0(%rbp), %rcx
movslq -0x80(%rbp), %rsi
cltd
idivl (%rcx,%rsi,4)
movl %edx, -0x84(%rbp)
movq -0x78(%rbp), %rdi
movq -0x58(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x84(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x18(%rbp), %rdx
callq 0x3cc0
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
je 0x4fa2
jmp 0x4fe4
jmp 0x4fa4
jmp 0x4fa6
jmp 0x4fa8
jmp 0x4faa
jmp 0x4fac
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x4d1d
jmp 0x4fbc
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x4d06
jmp 0x4fcc
movq -0x58(%rbp), %rdi
callq 0x1030
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x5041
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0x503f
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x30(%rax)
je 0x5032
movq -0x20(%rbp), %rdi
movslq -0x2c(%rbp), %rax
imulq $0x38, %rax, %rax
addq %rax, %rdi
callq 0x23a0
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x30(%rax)
jmp 0x5034
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x4feb
jmp 0x4fcc
movq -0x40(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq