// File: /workspace/github_c_makefile_star2_O0/kernfel[P]fNEAT/neat.c
// Function: get_population_fertility
int get_population_fertility( Population *pop, struct NEAT_Params *params, int *num_offspring ) {
	int i, j;
	double species_scores[pop->num_species];
	for ( i=0; i<pop->num_species; i++ ) {
		species_scores[i] = 0.0;
	}

	// Add up all scores within a species
	for ( i=0; i<pop->num_members; i++ ) {
		for ( j=0; j<pop->num_species; j++ ) {
			if ( pop->species[j].id == pop->members[i].species_id ) {
				species_scores[j] += pop->members[i].score;
				#if defined VERBOSE && VERBOSE > 2
					printf( "Member #%5x in species #%3x, score: %2.2f\n", pop->members[i].id, pop->species[j].id, pop->members[i].score );
				#endif
				break;
			}
		}
	}

	double sum_average_scores = 0.0;
	int species_scoresort[pop->num_species];
	for ( i=0; i<pop->num_species; i++ ) {

		// Determine average score
		double mean_score = species_scores[i] / pop->species[i].size;

		// Increase stagnation counter, where appropriate
		if ( fabs( mean_score - pop->species[i].mean_score ) > params->stagnation_score_threshold )
			pop->species[i].stagnating_for = 0;
		else
			pop->species[i].stagnating_for++;

		pop->species[i].mean_score = mean_score;
		sum_average_scores += mean_score;

		// Sort species by average score, descending
		for ( j=0; j<i; j++ ) {
			if ( pop->species[i].mean_score > pop->species[species_scoresort[j]].mean_score ) {
				memmove( species_scoresort+j+1, species_scoresort+j, (i-j)*sizeof *species_scoresort );
				break;
			}
		}
		species_scoresort[j] = i;
	}

	// Assign offspring in proportion to a species' average score
	int unassigned_offspring = params->population_size, living_species=0;
	for ( i=0; i<pop->num_species; i++ ) {
		// Penalise stagnation
		if ( pop->species[i].stagnating_for < params->stagnation_age_threshold )
			num_offspring[i] = (int)(params->population_size * pop->species[i].mean_score/sum_average_scores);
		else
			num_offspring[i] = (int)(params->population_size * (1-params->stagnation_penalty) * pop->species[i].mean_score/sum_average_scores);
		// Kill insular species
		if ( num_offspring[i] < params->extinction_threshold )
			num_offspring[i] = 0;
		
		if ( num_offspring[i] ) {
			living_species++;
			unassigned_offspring -= num_offspring[i];
		}
	}

	if ( ! living_species )
		return E_NO_OFFSPRING;

	// Distribute leftovers
	int shared=unassigned_offspring/living_species, stolen=unassigned_offspring%living_species;
	if ( shared || stolen ) {
		for ( i=0; i<pop->num_species; i++ ) {
			if ( num_offspring[i] ) {
				num_offspring[i] += shared;
				if ( stolen ) {
					num_offspring[i]++;
					stolen--;
				}
			}
		}
	}
	
	#ifdef VERBOSE
		double sd[pop->num_species], dev;
		for ( j=0; j<pop->num_species; j++ ) {
			sd[j] = 0;
			for ( i=0; i<pop->num_members; i++ ) {
				if ( pop->species[j].id == pop->members[i].species_id ) {
					dev = pop->species[j].mean_score - pop->members[i].score;
					sd[j] += dev*dev;
				}
			}
			sd[j] = sqrt(sd[j]/pop->species[j].size);
		}
		
		for ( i=0; i<pop->num_species; i++ ) {
			printf( "Species #%3x (idx %2d) - %3d members, %3d offspring - total score: %8.2f | avg score: %8.2f | sd: %4.2f\n",
				pop->species[i].id,
				i,
				pop->species[i].size,
				num_offspring[i],
				species_scores[i],
				pop->species[i].mean_score,
				sd[i]
			);
		}
	#endif
	
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x30(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x80(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x5b00
movq -0x80(%rbp), %rax
movslq -0x24(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x5ad9
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x5b9e
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x5b8e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movslq -0x24(%rbp), %rdx
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x30(%rcx), %eax
jne 0x5b81
movq -0x80(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movslq -0x24(%rbp), %rdx
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
movsd 0x28(%rcx), %xmm0
movslq -0x28(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x5b8e
jmp 0x5b83
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x5b1e
jmp 0x5b90
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x5b07
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x88(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x5d63
movq -0x80(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cvtsi2sdl 0x4(%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
subsd 0x10(%rax), %xmm0
movaps 0x457a(%rip), %xmm1     # 0xa1b0
pand %xmm1, %xmm0
movq -0x18(%rbp), %rax
ucomisd 0xa0(%rax), %xmm0
jbe 0x5c64
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
jmp 0x5c80
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
movsd -0x50(%rbp), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x10(%rax)
movsd -0x50(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x5d44
movq -0x88(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movsd 0x10(%rax), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
ucomisd 0x10(%rax), %xmm0
jbe 0x5d34
movq -0x88(%rbp), %rsi
movslq -0x28(%rbp), %rax
shlq $0x2, %rax
movq %rsi, %rdi
addq %rax, %rdi
addq $0x4, %rdi
movslq -0x28(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movl -0x24(%rbp), %eax
subl -0x28(%rbp), %eax
movslq %eax, %rdx
shlq $0x2, %rdx
callq 0x1120
jmp 0x5d44
jmp 0x5d36
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x5cb3
movq -0x88(%rbp), %rax
movl -0x24(%rbp), %edx
movslq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x5bd4
movq -0x18(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x5e8e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x98(%rcx), %eax
jge 0x5de4
movq -0x18(%rbp), %rax
cvtsi2sdl 0x50(%rax), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
mulsd 0x10(%rax), %xmm0
divsd -0x40(%rbp), %xmm0
cvttsd2si %xmm0, %edx
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x5e31
movq -0x18(%rbp), %rax
cvtsi2sdl 0x50(%rax), %xmm0
movq -0x18(%rbp), %rax
movsd 0x426f(%rip), %xmm1     # 0xa068
subsd 0xa8(%rax), %xmm1
mulsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
mulsd 0x10(%rax), %xmm0
divsd -0x40(%rbp), %xmm0
cvttsd2si %xmm0, %edx
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x18(%rbp), %rcx
cmpl 0x54(%rcx), %eax
jge 0x5e54
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x5e7e
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movl -0x54(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x54(%rbp)
jmp 0x5e80
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x5d7b
cmpl $0x0, -0x58(%rbp)
jne 0x5ea7
movl $0xfffffff6, -0x4(%rbp) # imm = 0xFFFFFFF6
movl $0x1, -0x5c(%rbp)
jmp 0x6129
movl -0x54(%rbp), %eax
cltd
idivl -0x58(%rbp)
movl %eax, -0x60(%rbp)
movl -0x54(%rbp), %eax
cltd
idivl -0x58(%rbp)
movl %edx, -0x64(%rbp)
cmpl $0x0, -0x60(%rbp)
jne 0x5ec7
cmpl $0x0, -0x64(%rbp)
je 0x5f2a
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x5f28
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x5f1b
movl -0x60(%rbp), %edx
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
cmpl $0x0, -0x64(%rbp)
je 0x5f19
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x64(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x5f1b
jmp 0x5f1d
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x5ece
jmp 0x5f2a
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rax, -0x70(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x6078
movq -0x90(%rbp), %rax
movslq -0x28(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x6029
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movslq -0x24(%rbp), %rdx
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x30(%rcx), %eax
jne 0x6019
movq -0x90(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x28(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movsd 0x10(%rcx), %xmm0
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movslq -0x24(%rbp), %rdx
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
subsd 0x28(%rcx), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x78(%rbp), %xmm0
movsd -0x78(%rbp), %xmm2
movslq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x601b
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x5f82
movq -0x90(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cvtsi2sdl 0x4(%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x1190
movq -0x90(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x5f58
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x611b
movq -0x90(%rbp), %rax
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x24(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %esi
movl -0x24(%rbp), %edx
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x24(%rbp), %r8
imulq $0x18, %r8, %r8
addq %r8, %rcx
movl 0x4(%rcx), %ecx
movq -0x20(%rbp), %r8
movslq -0x24(%rbp), %r9
movl (%r8,%r9,4), %r8d
movslq -0x24(%rbp), %r9
movsd (%rdi,%r9,8), %xmm0
movq -0x10(%rbp), %rdi
movq 0x10(%rdi), %rdi
movslq -0x24(%rbp), %r9
imulq $0x18, %r9, %r9
addq %r9, %rdi
movsd 0x10(%rdi), %xmm1
movslq -0x24(%rbp), %rdi
movsd (%rax,%rdi,8), %xmm2
leaq 0x413a(%rip), %rdi      # 0xa240
movb $0x3, %al
callq 0x1060
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x607f
movl $0x0, -0x4(%rbp)
movl $0x1, -0x5c(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax,%rax)