// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, -0x98(%rbp)
movq %rdx, -0x58(%rbp)
movq %rsi, %rbx
movq %rdi, %r12
movq %rsp, %r14
movl 0x18(%rdi), %eax
movq %rsp, %r15
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movl 0x18(%rdi), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
cmpq %rcx, %rax
je 0x334b
andl $0x0, (%r15,%rcx,4)
incq %rcx
jmp 0x333c
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3d5f
movl %eax, %ecx
movl %eax, -0x7c(%rbp)
testl %eax, %eax
jne 0x37c1
movq %rbx, %r13
leaq -0x7c(%rbp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x40e0
movl (%rbx), %ecx
testl %ecx, %ecx
jne 0x37c1
movq %r13, %rsi
movq %r15, -0x90(%rbp)
movq %rax, -0x50(%rbp)
movq %r14, -0xa0(%rbp)
movl 0x18(%r12), %eax
movq %rsp, %rdx
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x60(%rbp)
movq %rdx, %rsp
movl 0x8(%r12), %ecx
movq %rsp, %rdx
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x78(%rbp)
movq %rdx, %rsp
xorl %edx, %edx
xorl %r14d, %r14d
movq %r13, -0x48(%rbp)
movq %r12, %r15
movslq %eax, %rcx
movslq %r14d, %rdi
cmpq %rcx, %rdx
movq %rdx, -0x38(%rbp)
jge 0x3525
movq -0x90(%rbp), %rax
movl (%rax,%rdx,4), %eax
testl %eax, %eax
je 0x3496
movq %rdi, -0x70(%rbp)
movq 0x10(%r12), %rcx
imulq $0x18, %rdx, %rdi
movq %rdi, -0x40(%rbp)
cvtsi2sdl 0x4(%rcx,%rdi), %xmm0
mulsd 0x60(%rsi), %xmm0
cvttsd2si %xmm0, %ecx
leal 0x1(%rcx), %r13d
cmpl %ecx, %eax
cmovlel %eax, %r13d
movq -0x60(%rbp), %rax
movl %r13d, (%rax,%rdx,4)
imulq $0x38, %rdx, %rbx
addq -0x58(%rbp), %rbx
movq -0x50(%rbp), %rax
movq (%rax,%rdx,8), %rax
movq %rax, -0x30(%rbp)
callq 0x11a0
cltd
idivl %r13d
movslq %edx, %rax
movq -0x30(%rbp), %rcx
movq (%rcx,%rax,8), %rsi
movq %rbx, %rdi
callq 0x1cc9
testl %eax, %eax
jne 0x376b
movq 0x10(%r12), %rax
movq -0x40(%rbp), %rdx
movl (%rax,%rdx), %ecx
movl %ecx, 0x30(%rbx)
movl 0x4(%rax,%rdx), %eax
movq -0x48(%rbp), %rcx
cmpl 0x58(%rcx), %eax
movq -0x70(%rbp), %rdi
jge 0x34b2
xorl %eax, %eax
movq -0x38(%rbp), %rdx
jmp 0x34c1
movq -0x60(%rbp), %rax
andl $0x0, (%rax,%rdx,4)
imulq $0x38, %rdx, %rax
movq -0x58(%rbp), %rcx
andl $0x0, 0x30(%rcx,%rax)
xorl %eax, %eax
xorl %r13d, %r13d
jmp 0x34c1
movq -0x50(%rbp), %rax
movq -0x38(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq (%rax), %rax
movq -0x98(%rbp), %rcx
movq %rax, (%rcx,%rdx,8)
movslq %r13d, %rbx
movq -0x78(%rbp), %rax
leaq (%rax,%rdi,8), %r13
imulq $0x18, %rdx, %r12
movq 0x10(%r15), %rax
movslq 0x4(%rax,%r12), %rax
cmpq %rax, %rbx
jge 0x350e
movq -0x50(%rbp), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax,%rbx,8), %rdi
movq %rdi, (%r13)
callq 0x1d31
incq %rbx
incl %r14d
addq $0x8, %r13
jmp 0x34db
movq -0x38(%rbp), %rdx
incq %rdx
movq %r15, %r12
movl 0x18(%r15), %eax
movq -0x48(%rbp), %rsi
jmp 0x33df
movq -0x78(%rbp), %rcx
andq $0x0, (%rcx,%rdi,8)
movq $0x0, -0x88(%rbp)
xorl %ebx, %ebx
xorl %r8d, %r8d
cltq
cmpq %rax, %rbx
jge 0x37aa
movq -0x90(%rbp), %rax
movl (%rax,%rbx,4), %eax
xorl %edi, %edi
testl %eax, %eax
cmovlel %edi, %eax
movl %eax, -0x70(%rbp)
movq %rbx, -0x68(%rbp)
cmpl %edi, -0x70(%rbp)
je 0x3751
movq -0x50(%rbp), %rax
movq (%rax,%rbx,8), %rcx
movq -0x60(%rbp), %rax
movl (%rax,%rbx,4), %r14d
movl %edi, %eax
cltd
idivl %r14d
movq (%rcx,%rdx,8), %rsi
cmpl %r14d, %edi
movl %edi, -0x30(%rbp)
jge 0x359e
movq -0x98(%rbp), %rax
cmpq (%rax,%rbx,8), %rsi
jne 0x35f0
jmp 0x374a
movq 0x10(%r12), %rax
imulq $0x18, %rbx, %rcx
movl (%rax,%rcx), %eax
movslq %r8d, %rcx
movq -0x78(%rbp), %rdx
movq (%rdx,%rcx,8), %rbx
movl %eax, 0x30(%rbx)
movq -0x48(%rbp), %rcx
movl 0x110(%rcx), %eax
incl %eax
movl %eax, 0x110(%rcx)
movl %eax, 0x34(%rbx)
movq %rbx, %rdi
movl %r8d, %r15d
callq 0x1cc9
testl %eax, %eax
jne 0x375e
incl %r15d
movq %rbx, %rsi
movq -0x68(%rbp), %rbx
movl %r15d, %r8d
movl -0x30(%rbp), %edi
cmpl $0x2, %r14d
jl 0x3686
movq -0x48(%rbp), %rax
movsd 0xd0(%rax), %xmm0
mulsd 0x2c12(%rip), %xmm0     # 0x6220
movsd %xmm0, -0x40(%rbp)
movl %r8d, %r15d
movq %r12, %r13
movq %rsi, %r12
callq 0x11a0
movq %r12, %rsi
movq %r13, %r12
movl -0x30(%rbp), %edi
movl %r15d, %r8d
cvtsi2sd %eax, %xmm0
movsd -0x40(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3686
movq %rsi, %r13
callq 0x11a0
decl %r14d
cltd
idivl %r14d
xorl %eax, %eax
cmpl -0x30(%rbp), %edx
setge %al
addl %edx, %eax
movq -0x50(%rbp), %rcx
movq (%rcx,%rbx,8), %rcx
cltq
movq (%rcx,%rax,8), %rsi
movq %r13, %rdi
movq -0x48(%rbp), %rdx
callq 0x2afe
movl -0x30(%rbp), %edi
movl %r15d, %r8d
movq -0x38(%rbp), %rcx
testl %eax, %eax
je 0x374a
jmp 0x3778
cmpl $0x2, 0x18(%r12)
jl 0x374a
movq -0x48(%rbp), %rax
movsd 0xd8(%rax), %xmm0
ucomisd 0x2b92(%rip), %xmm0     # 0x6238
jbe 0x374a
movq %rsi, %r15
movl %r8d, -0x40(%rbp)
mulsd 0x2b65(%rip), %xmm0     # 0x6220
movsd %xmm0, -0xa8(%rbp)
callq 0x11a0
cvtsi2sd %eax, %xmm0
movsd -0xa8(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3743
callq 0x11a0
movl 0x18(%r12), %ecx
decl %ecx
cltd
idivl %ecx
movslq %edx, %rax
xorl %ecx, %ecx
cmpq %rax, %rbx
setle %cl
addl %ecx, %eax
movslq %eax, %rbx
movq -0x60(%rbp), %rax
movl (%rax,%rbx,4), %r14d
testl %r14d, %r14d
je 0x373f
callq 0x11a0
cltd
idivl %r14d
movq -0x50(%rbp), %rax
movq (%rax,%rbx,8), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rsi
movq %r15, %rdi
movq -0x48(%rbp), %rdx
callq 0x2afe
testl %eax, %eax
movq -0x38(%rbp), %rcx
movq -0x68(%rbp), %rbx
movl -0x40(%rbp), %r8d
movl -0x30(%rbp), %edi
je 0x374a
jmp 0x3778
movq -0x68(%rbp), %rbx
movl -0x40(%rbp), %r8d
movl -0x30(%rbp), %edi
incl %edi
jmp 0x3561
incq %rbx
movl 0x18(%r12), %eax
jmp 0x353e
movl %eax, %r12d
movq -0x58(%rbp), %rbx
movq -0x38(%rbp), %rcx
jmp 0x379d
movq -0x58(%rbp), %rbx
movq -0x38(%rbp), %rcx
movq %rax, %r12
jmp 0x379d
movl %eax, %r12d
movq -0x58(%rbp), %rbx
jmp 0x379d
cmpl $0x0, 0x30(%rbx)
je 0x3799
movq %rbx, %rdi
movq %rcx, %r14
callq 0x1d31
movq %r14, %rcx
andl $0x0, 0x30(%rbx)
addq $0x38, %rbx
subq $0x1, %rcx
jae 0x3781
movq %r12, -0x88(%rbp)
movq -0x50(%rbp), %rdi
callq 0x1030
movq -0xa0(%rbp), %r14
movq -0x88(%rbp), %rcx
movq %r14, %rsp
movl %ecx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq