// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %rsp
movq %rcx, -0xa8(%rbp)
movq %rdx, %r15
movq %rsi, -0x48(%rbp)
movq %rdi, %r12
movq %rsp, %r14
movl $0x0, -0x2c(%rbp)
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
jle 0x3565
shlq $0x2, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1060
movq %r12, %rdi
movq -0x48(%rbp), %rsi
movq %rbx, -0xa0(%rbp)
movq %rbx, %rdx
callq 0x40e8
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x3a5e
leaq -0x2c(%rbp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x44be
movq %rax, %rcx
movl (%rbx), %eax
testl %eax, %eax
jne 0x3a5e
movq %rcx, -0x40(%rbp)
movq %r14, -0x78(%rbp)
movl 0x18(%r12), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movl 0x8(%r12), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x88(%rbp)
movq %rcx, %rsp
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, 0x18(%r12)
movq %r15, -0x80(%rbp)
jle 0x3770
xorl %esi, %esi
xorl %r15d, %r15d
movq %r12, %r8
movq %r12, -0x60(%rbp)
movq -0xa0(%rbp), %rax
movl (%rax,%rsi,4), %eax
testl %eax, %eax
je 0x36ba
movq 0x10(%r8), %rcx
leaq (%rsi,%rsi,2), %r14
xorps %xmm0, %xmm0
cvtsi2sdl 0x4(%rcx,%r14,8), %xmm0
movq -0x48(%rbp), %r12
mulsd 0x60(%r12), %xmm0
cvttsd2si %xmm0, %ecx
leal 0x1(%rcx), %r13d
cmpl %ecx, %eax
cmovlel %eax, %r13d
movq -0x50(%rbp), %rax
movl %r13d, (%rax,%rsi,4)
imulq $0x38, %rsi, %rbx
addq -0x80(%rbp), %rbx
movq -0x40(%rbp), %rax
movq %rsi, -0x58(%rbp)
movq (%rax,%rsi,8), %rax
movq %rax, -0x70(%rbp)
callq 0x11a0
cltd
idivl %r13d
movslq %edx, %rax
movq -0x70(%rbp), %rcx
movq (%rcx,%rax,8), %rsi
movq %rbx, %rdi
callq 0x1e84
movl %eax, -0x30(%rbp)
testl %eax, %eax
jne 0x3a03
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax,%r14,8), %ecx
movl %ecx, 0x30(%rbx)
movl 0x4(%rax,%r14,8), %eax
cmpl 0x58(%r12), %eax
jge 0x36d3
xorl %eax, %eax
movq -0x60(%rbp), %r8
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rsi
jmp 0x36ea
movq -0x50(%rbp), %rdx
xorl %edi, %edi
movl %edi, (%rdx,%rsi,4)
imulq $0x38, %rsi, %rax
movq -0x80(%rbp), %rcx
movl %edi, 0x30(%rcx,%rax)
xorl %eax, %eax
jmp 0x36ea
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rsi
movq (%rax,%rsi,8), %rax
movq (%rax), %rax
movq -0x60(%rbp), %r8
movq -0x50(%rbp), %rdx
movq -0xa8(%rbp), %rcx
movq %rax, (%rcx,%rsi,8)
movslq (%rdx,%rsi,4), %rbx
movq 0x10(%r8), %rax
movq %rsi, %r12
leaq (%rsi,%rsi,2), %r14
cmpl 0x4(%rax,%r14,8), %ebx
jge 0x374a
movslq %r15d, %rax
movq -0x88(%rbp), %rcx
leaq (%rcx,%rax,8), %r13
movq -0x40(%rbp), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%rbx,8), %rdi
movq %rdi, (%r13)
callq 0x1eec
incq %rbx
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movslq 0x4(%rax,%r14,8), %rax
addq $0x8, %r13
incl %r15d
cmpq %rax, %rbx
jl 0x3719
incq %r12
movq -0x60(%rbp), %rcx
movslq 0x18(%rcx), %rax
cmpq %rax, %r12
movq %rcx, %r8
movq %r12, %rsi
jl 0x360b
movq %rsi, -0x58(%rbp)
movslq %r15d, %rax
movq %r8, %r12
jmp 0x377a
xorl %eax, %eax
movq $0x0, -0x58(%rbp)
movl -0x30(%rbp), %edx
movl %edx, -0x2c(%rbp)
movq -0x88(%rbp), %rcx
movq $0x0, (%rcx,%rax,8)
cmpl $0x0, 0x18(%r12)
jle 0x39f2
xorl %ebx, %ebx
xorl %r8d, %r8d
movq -0xa0(%rbp), %rax
movl (%rax,%rbx,4), %eax
movl %eax, -0x90(%rbp)
testl %eax, %eax
jle 0x39d1
movq -0x50(%rbp), %rax
movl (%rax,%rbx,4), %eax
movq %rax, -0x70(%rbp)
decl %eax
movl %eax, -0x8c(%rbp)
xorl %r15d, %r15d
movl -0x30(%rbp), %eax
movl %eax, -0x34(%rbp)
movq %rbx, -0x98(%rbp)
movq -0x40(%rbp), %rax
movq (%rax,%rbx,8), %rcx
movl %r15d, %eax
cltd
movq -0x70(%rbp), %rdi
idivl %edi
movq (%rcx,%rdx,8), %r14
cmpl %edi, %r15d
jge 0x3808
movq -0xa8(%rbp), %rax
cmpq (%rax,%rbx,8), %r14
jne 0x3863
jmp 0x39bf
movq 0x10(%r12), %rax
leaq (%rbx,%rbx,2), %rcx
movl (%rax,%rcx,8), %eax
movslq %r8d, %rcx
movq -0x88(%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, %r13d
movq %r14, %rsi
callq 0x1e84
movl %eax, -0x34(%rbp)
testl %eax, %eax
jne 0x3a11
incl %r13d
movq %rbx, %r14
movq -0x98(%rbp), %rbx
movl %r13d, %r8d
cmpl $0x2, -0x70(%rbp)
jl 0x38e2
movq -0x48(%rbp), %rax
movsd 0xd0(%rax), %xmm0
mulsd 0x38db(%rip), %xmm0     # 0x7158
movsd %xmm0, -0x68(%rbp)
movl %r8d, %r13d
callq 0x11a0
movl %r13d, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd -0x68(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x38e2
callq 0x11a0
cltd
idivl -0x8c(%rbp)
xorl %eax, %eax
cmpl %r15d, %edx
setge %al
addl %edx, %eax
movq -0x40(%rbp), %rcx
movq (%rcx,%rbx,8), %rcx
cltq
movq (%rcx,%rax,8), %rsi
movq %r14, %rdi
movq -0x48(%rbp), %rdx
callq 0x2d3f
movl %r13d, %r8d
movl %eax, -0x34(%rbp)
testl %eax, %eax
je 0x39bf
jmp 0x3a11
cmpl $0x2, 0x18(%r12)
jl 0x39bf
movq -0x48(%rbp), %rax
movsd 0xd8(%rax), %xmm0
ucomisd 0x3876(%rip), %xmm0     # 0x7178
jbe 0x39bf
movq %r14, -0xb0(%rbp)
movl %r8d, -0x68(%rbp)
mulsd 0x383d(%rip), %xmm0     # 0x7158
movsd %xmm0, -0xb8(%rbp)
callq 0x11a0
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd -0xb8(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x39a0
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 -0x50(%rbp), %rax
movl (%rax,%rbx,4), %r13d
testl %r13d, %r13d
je 0x39a6
callq 0x11a0
cltd
idivl %r13d
movq -0x40(%rbp), %rax
movq (%rax,%rbx,8), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rsi
movq -0xb0(%rbp), %rdi
movq -0x48(%rbp), %rdx
callq 0x2d3f
xorl %ecx, %ecx
movl %eax, -0x34(%rbp)
testl %eax, %eax
setne %cl
shll $0x3, %ecx
jmp 0x39ab
movl -0x68(%rbp), %r8d
jmp 0x39bf
movl $0x12, %ecx
testl %ecx, %ecx
movq -0x98(%rbp), %rbx
movl -0x68(%rbp), %r8d
je 0x39bf
cmpl $0x12, %ecx
jne 0x3a0c
incl %r15d
cmpl %r15d, -0x90(%rbp)
jne 0x37db
jmp 0x39d7
movl -0x30(%rbp), %eax
movl %eax, -0x34(%rbp)
incq %rbx
movslq 0x18(%r12), %rax
movl -0x34(%rbp), %r14d
movl %r14d, -0x30(%rbp)
cmpq %rax, %rbx
jl 0x37a0
jmp 0x39f5
movl %edx, %r14d
movl %r14d, -0x2c(%rbp)
movq -0x78(%rbp), %r14
movq -0x40(%rbp), %r12
jmp 0x3a53
movq -0x40(%rbp), %r12
movl -0x30(%rbp), %eax
jmp 0x3a1e
cmpl $0x8, %ecx
jne 0x3a70
movl -0x30(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
movq -0x40(%rbp), %r12
movl %eax, -0x2c(%rbp)
movq -0x58(%rbp), %rax
testl %eax, %eax
movq -0x78(%rbp), %r14
movq -0x80(%rbp), %r15
je 0x3a53
movl %eax, %ebx
cmpl $0x0, 0x30(%r15)
je 0x3a4a
movq %r15, %rdi
callq 0x1eec
movl $0x0, 0x30(%r15)
addq $0x38, %r15
decq %rbx
jne 0x3a33
movq %r12, %rdi
callq 0x1030
movl -0x2c(%rbp), %eax
movq %r14, %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x34(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x78(%rbp), %r14
jmp 0x3a5e