// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movq %rcx, -0x98(%rbp)
movq %rdx, %r14
movq %rsi, -0x38(%rbp)
movq %rdi, %r15
movq %rsp, %r12
movl 0x18(%rdi), %eax
movq %rsp, %rbx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movslq 0x18(%rdi), %rdx
testq %rdx, %rdx
movq %rdi, -0x40(%rbp)
jle 0x37b7
shlq $0x2, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1060
movq -0x40(%rbp), %r15
movq %r15, %rdi
movq -0x38(%rbp), %rsi
movq %rbx, -0x90(%rbp)
movq %rbx, %rdx
callq 0x42ce
movl %eax, %ebx
movl %eax, -0x88(%rbp)
testl %eax, %eax
jne 0x3c37
leaq -0x88(%rbp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x468c
movl (%rbx), %ebx
testl %ebx, %ebx
jne 0x3c37
movq %rax, -0x50(%rbp)
movl 0x18(%r15), %eax
movq %rsp, %rdx
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x70(%rbp)
movq %rdx, %rsp
movl 0x8(%r15), %ecx
movq %rsp, %rdx
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x80(%rbp)
movq %rdx, %rsp
testl %eax, %eax
jle 0x3bdf
movq %r15, %rsi
movq %r14, -0x30(%rbp)
movq %r12, -0x48(%rbp)
xorl %edi, %edi
xorl %r15d, %r15d
movq -0x90(%rbp), %rax
movl (%rax,%rdi,4), %eax
testl %eax, %eax
movq -0x38(%rbp), %rdx
je 0x38f5
movq 0x10(%rsi), %rcx
leaq (%rdi,%rdi,2), %r8
movq %r8, -0x68(%rbp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x4(%rcx,%r8,8), %xmm0
mulsd 0x60(%rdx), %xmm0
cvttsd2si %xmm0, %ecx
leal 0x1(%rcx), %r13d
cmpl %ecx, %eax
cmovlel %eax, %r13d
movq -0x70(%rbp), %rax
movl %r13d, (%rax,%rdi,4)
imulq $0x38, %rdi, %r12
addq -0x30(%rbp), %r12
movq -0x50(%rbp), %rax
movq %rdi, -0x78(%rbp)
movq (%rax,%rdi,8), %rbx
movq %rsi, %r14
callq 0x11a0
cltd
idivl %r13d
movslq %edx, %rax
movq (%rbx,%rax,8), %rsi
movq %r12, %rdi
callq 0x1e6f
testl %eax, %eax
jne 0x3be3
movq 0x10(%r14), %rax
movq -0x68(%rbp), %rdx
movl (%rax,%rdx,8), %ecx
movl %ecx, 0x30(%r12)
movl 0x4(%rax,%rdx,8), %eax
movq -0x38(%rbp), %rcx
cmpl 0x58(%rcx), %eax
movq %r14, %rsi
jge 0x390f
xorl %eax, %eax
movq -0x78(%rbp), %rdi
jmp 0x391e
xorl %eax, %eax
movq -0x70(%rbp), %rcx
movl %eax, (%rcx,%rdi,4)
imulq $0x38, %rdi, %rcx
movq -0x30(%rbp), %rdx
movl %eax, 0x30(%rdx,%rcx)
xorl %r13d, %r13d
jmp 0x391e
movq -0x50(%rbp), %rax
movq -0x78(%rbp), %rdi
movq (%rax,%rdi,8), %rax
movq (%rax), %rax
movq -0x98(%rbp), %rcx
movq %rax, (%rcx,%rdi,8)
movq 0x10(%rsi), %rax
movq %rdi, %rbx
leaq (%rdi,%rdi,2), %r14
cmpl 0x4(%rax,%r14,8), %r13d
jge 0x397a
movslq %r13d, %r12
movslq %r15d, %rax
movq -0x80(%rbp), %rcx
leaq (%rcx,%rax,8), %r13
movq -0x50(%rbp), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r12,8), %rdi
movq %rdi, (%r13)
callq 0x1ed7
movq -0x40(%rbp), %rsi
incq %r12
movq 0x10(%rsi), %rax
movslq 0x4(%rax,%r14,8), %rax
addq $0x8, %r13
incl %r15d
cmpq %rax, %r12
jl 0x3949
incq %rbx
movslq 0x18(%rsi), %rax
cmpq %rax, %rbx
movq %rbx, %rdi
jl 0x384f
movq %rdi, -0x78(%rbp)
movslq %r15d, %rcx
movq -0x80(%rbp), %rdx
movq $0x0, (%rdx,%rcx,8)
testl %eax, %eax
jle 0x3bf7
xorl %edi, %edi
xorl %edx, %edx
movq -0x48(%rbp), %r12
movq -0x30(%rbp), %r15
movq -0x90(%rbp), %rcx
movl (%rcx,%rdi,4), %r13d
testl %r13d, %r13d
jle 0x3bcc
movl %edx, -0x54(%rbp)
movq -0x70(%rbp), %rax
movl (%rax,%rdi,4), %r14d
leal -0x1(%r14), %eax
movl %eax, -0x84(%rbp)
xorl %r12d, %r12d
movq %rdi, -0x68(%rbp)
movq %r14, -0xa0(%rbp)
movq -0x50(%rbp), %rax
movq (%rax,%rdi,8), %rcx
movl %r12d, %eax
cltd
idivl %r14d
movq (%rcx,%rdx,8), %rbx
cmpl %r14d, %r12d
jge 0x3a15
movq -0x98(%rbp), %rax
cmpq (%rax,%rdi,8), %rbx
jne 0x3a7b
jmp 0x3bb2
movl %r13d, %r14d
movq 0x10(%rsi), %rax
leaq (%rdi,%rdi,2), %rcx
movl (%rax,%rcx,8), %eax
movl -0x54(%rbp), %r13d
movslq %r13d, %rcx
movq -0x80(%rbp), %rdx
movq (%rdx,%rcx,8), %r15
movl %eax, 0x30(%r15)
movq -0x38(%rbp), %rcx
movl 0x110(%rcx), %eax
incl %eax
movl %eax, 0x110(%rcx)
movl %eax, 0x34(%r15)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1e6f
testl %eax, %eax
jne 0x3bef
incl %r13d
movl %r13d, -0x54(%rbp)
movq %r15, %rbx
movq -0x30(%rbp), %r15
movq -0x40(%rbp), %rsi
movl %r14d, %r13d
movq -0xa0(%rbp), %r14
cmpl $0x2, %r14d
jl 0x3afa
movq -0x38(%rbp), %rax
movsd 0xd0(%rax), %xmm0
mulsd 0x380b(%rip), %xmm0     # 0x72a0
movsd %xmm0, -0x60(%rbp)
callq 0x11a0
movq -0x40(%rbp), %rsi
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd -0x60(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3afa
callq 0x11a0
cltd
idivl -0x84(%rbp)
xorl %eax, %eax
cmpl %r12d, %edx
setge %al
addl %edx, %eax
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cltq
movq (%rcx,%rax,8), %rsi
movq %rbx, %rdi
movq -0x38(%rbp), %rdx
callq 0x2e98
movq -0x40(%rbp), %rsi
testl %eax, %eax
je 0x3bb2
jmp 0x3bff
cmpl $0x2, 0x18(%rsi)
jl 0x3bb2
movq -0x38(%rbp), %rax
movsd 0xd8(%rax), %xmm0
ucomisd 0x37a0(%rip), %xmm0     # 0x72b8
jbe 0x3bb2
mulsd 0x377a(%rip), %xmm0     # 0x72a0
movsd %xmm0, -0x60(%rbp)
callq 0x11a0
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd -0x60(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3ba8
movq %rbx, -0x60(%rbp)
callq 0x11a0
movq -0x40(%rbp), %rsi
movl 0x18(%rsi), %ecx
decl %ecx
cltd
idivl %ecx
movslq %edx, %rax
xorl %ecx, %ecx
cmpq %rax, -0x68(%rbp)
setle %cl
addl %ecx, %eax
movslq %eax, %rbx
movq -0x70(%rbp), %rax
movl (%rax,%rbx,4), %r15d
testl %r15d, %r15d
je 0x3bae
callq 0x11a0
cltd
idivl %r15d
movq -0x50(%rbp), %rax
movq (%rax,%rbx,8), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rsi
movq -0x60(%rbp), %rdi
movq -0x38(%rbp), %rdx
callq 0x2e98
movq -0x40(%rbp), %rsi
testl %eax, %eax
movq -0x30(%rbp), %r15
je 0x3bb2
jmp 0x3bff
movq -0x40(%rbp), %rsi
jmp 0x3bb2
movq -0x30(%rbp), %r15
incl %r12d
cmpl %r12d, %r13d
movq -0x68(%rbp), %rdi
jne 0x39eb
movl 0x18(%rsi), %eax
movq -0x48(%rbp), %r12
movl -0x54(%rbp), %edx
incq %rdi
movslq %eax, %rcx
xorl %ebx, %ebx
cmpq %rcx, %rdi
jl 0x39b4
jmp 0x3c2e
xorl %ebx, %ebx
jmp 0x3c2e
movl %eax, %ebx
movq -0x48(%rbp), %r12
movq -0x30(%rbp), %r15
jmp 0x3c05
movl %eax, %ebx
movq -0x30(%rbp), %r15
jmp 0x3c01
xorl %ebx, %ebx
movq -0x48(%rbp), %r12
jmp 0x3c2e
movl %eax, %ebx
movq -0x48(%rbp), %r12
movq -0x78(%rbp), %r14
testl %r14d, %r14d
je 0x3c2e
cmpl $0x0, 0x30(%r15)
je 0x3c25
movq %r15, %rdi
callq 0x1ed7
movl $0x0, 0x30(%r15)
addq $0x38, %r15
decq %r14
jne 0x3c0e
movq -0x50(%rbp), %rdi
callq 0x1030
movq %r12, %rsp
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq