// File: /workspace/github_c_makefile_star2_O1/kernfel[P]fNEAT/neat.c
// Function: speciate_population
int speciate_population( Population *pop, struct NEAT_Params *params, const Individual *reps ) {
	int i, j, num_new_species=0, num_extinct=0;
	
	for ( i=0; i<pop->num_species; i++ ) {
		pop->species[i].size = 0;
	}
	
	int new_species_size[pop->num_members];
	Individual *new_reps[pop->num_members];
	for ( i=0; i<pop->num_members; i++ ) {
		int assigned=0;
		
		// Check for same species first...
		if ( pop->members[i].species_id ) {
			for ( j=0; j<pop->num_species; j++ ) {
				if ( reps[j].species_id == pop->members[i].species_id ) {
					double distance = get_genetic_distance( &pop->members[i].genotype, &reps[j].genotype, params );
					if ( distance < params->speciation_threshold ) {
						pop->species[j].size++;
						assigned = 1;
						break;
					}
				}
			}
		}
		
		// Check for other ancestor species...
		if ( ! assigned ) {
			for ( j=0; j<pop->num_species; j++ ) {
				if ( reps[j].species_id && reps[j].species_id != pop->members[i].species_id ) {
					double distance = get_genetic_distance( &pop->members[i].genotype, &reps[j].genotype, params );
					if ( distance < params->speciation_threshold ) {
						pop->members[i].species_id = reps[j].species_id;
						pop->species[j].size++;
						assigned = 1;
						break;
					}
				}
			}
		}
		
		// ... or to a newly created species...
		if ( ! assigned ) {
			for ( j=num_new_species-1; j>=0; j-- ) {
				double distance = get_genetic_distance( &pop->members[i].genotype, &new_reps[j]->genotype, params );
				if ( distance < params->speciation_threshold ) {
					pop->members[i].species_id = new_reps[j]->species_id;
					new_species_size[j]++;
					assigned = 1;
					break;
				}
			}
		}
		
		// ... or create a new species, proudly representing!
		if ( ! assigned ) {
			pop->members[i].species_id = ++params->species_counter;
			new_reps[num_new_species] = pop->members + i;
			new_species_size[num_new_species++] = 1;
		}
	}
	
	for ( i=0; i<pop->num_species; i++ ) {
		if ( ! pop->species[i].size )
			num_extinct++;
	}
	
	// No structural changes to species list, bail
	if ( ! num_new_species && ! num_extinct )
		return 0;
	
	// Compose a new list with all the (living) species and replace the old one with that
	int err=0;
	int num_all = pop->num_species + num_new_species - num_extinct;
	Species *all_species;
	if (( err = Calloc( all_species, num_all, sizeof *all_species ) ))
		return err;
	
	j=0;
	for ( i=0; i<pop->num_species; i++ ) {
		if ( pop->species[i].size ) {
			all_species[j] = pop->species[i];
			j++;
		}
	}
	for ( i=0; i<num_new_species; i++ ) {
		all_species[j].id = new_reps[i]->species_id;
		all_species[j].size = new_species_size[i];
		j++;
	}
	
	free( pop->species );
	pop->species = all_species;
	pop->num_species = num_all;
	
	return err;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, -0x68(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdi, %rbx
movslq 0x18(%rdi), %rcx
testq %rcx, %rcx
jle 0x3d8e
movq 0x10(%rbx), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
movl $0x0, 0x4(%rax,%rdx)
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x3d7d
movl 0x8(%rbx), %eax
movq %rsp, %r12
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
subq %rcx, %r12
movq %r12, %rsp
movq %rsp, %rdx
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x50(%rbp)
movq %rdx, %rsp
movl $0x0, %edi
testl %eax, %eax
jle 0x3fca
xorl %r8d, %r8d
xorl %edi, %edi
movq %rbx, -0x58(%rbp)
movq %r12, -0x38(%rbp)
movq (%rbx), %rax
movq %r8, -0x48(%rbp)
imulq $0x38, %r8, %r13
cmpl $0x0, 0x30(%rax,%r13)
movl %edi, -0x30(%rbp)
je 0x3e73
cmpl $0x0, 0x18(%rbx)
jle 0x3e73
xorl %r14d, %r14d
movl $0x4, %r15d
movq -0x68(%rbp), %r12
movl $0x0, -0x2c(%rbp)
movl 0x30(%r12), %eax
movq (%rbx), %rdi
cmpl 0x30(%rdi,%r13), %eax
jne 0x3e52
addq %r13, %rdi
movq %r12, %rsi
movq -0x40(%rbp), %rbx
movq %rbx, %rdx
callq 0x2bf2
movsd 0x68(%rbx), %xmm1
movq -0x58(%rbp), %rbx
ucomisd %xmm0, %xmm1
jbe 0x3e4c
movq 0x10(%rbx), %rax
incl (%rax,%r15)
movl $0x1, -0x2c(%rbp)
ucomisd %xmm0, %xmm1
ja 0x3e66
incq %r14
movslq 0x18(%rbx), %rax
addq $0x38, %r12
addq $0x18, %r15
cmpq %rax, %r14
jl 0x3e0d
cmpl $0x0, -0x2c(%rbp)
movl -0x30(%rbp), %edi
jne 0x3f04
cmpl $0x0, 0x18(%rbx)
jle 0x3f11
xorl %r14d, %r14d
movl $0x4, %r15d
movq -0x68(%rbp), %r12
movl $0x0, -0x2c(%rbp)
movl 0x30(%r12), %eax
testl %eax, %eax
je 0x3ee7
movq (%rbx), %rdi
cmpl 0x30(%rdi,%r13), %eax
je 0x3ee7
addq %r13, %rdi
movq %r12, %rsi
movq -0x40(%rbp), %rbx
movq %rbx, %rdx
callq 0x2bf2
movsd 0x68(%rbx), %xmm1
movq -0x58(%rbp), %rbx
ucomisd %xmm0, %xmm1
jbe 0x3ee1
movl 0x30(%r12), %eax
movq (%rbx), %rcx
movl %eax, 0x30(%rcx,%r13)
movq 0x10(%rbx), %rax
incl (%rax,%r15)
movl $0x1, -0x2c(%rbp)
ucomisd %xmm0, %xmm1
ja 0x3efb
incq %r14
movslq 0x18(%rbx), %rax
addq $0x38, %r12
addq $0x18, %r15
cmpq %rax, %r14
jl 0x3e91
cmpl $0x0, -0x2c(%rbp)
movl -0x30(%rbp), %edi
je 0x3f11
movq -0x38(%rbp), %r12
movq -0x48(%rbp), %r8
jmp 0x3fba
movl %edi, %r14d
xorl %r15d, %r15d
movq -0x50(%rbp), %rsi
testl %r14d, %r14d
jle 0x3f72
movq (%rbx), %rdi
addq %r13, %rdi
movq -0x8(%rsi,%r14,8), %r12
movq %r12, %rsi
movq -0x40(%rbp), %rbx
movq %rbx, %rdx
callq 0x2bf2
movsd 0x68(%rbx), %xmm1
movq -0x58(%rbp), %rbx
ucomisd %xmm0, %xmm1
jbe 0x3f65
movl 0x30(%r12), %eax
movq (%rbx), %rcx
movl %eax, 0x30(%rcx,%r13)
movq -0x38(%rbp), %rax
incl -0x4(%rax,%r14,4)
movl $0x1, %r15d
decq %r14
ucomisd %xmm0, %xmm1
movq -0x50(%rbp), %rsi
jbe 0x3f1b
testl %r15d, %r15d
movq -0x48(%rbp), %r8
je 0x3f84
movq -0x38(%rbp), %r12
movl -0x30(%rbp), %edi
jmp 0x3fba
movq -0x40(%rbp), %rcx
movl 0x10c(%rcx), %eax
incl %eax
movl %eax, 0x10c(%rcx)
movq (%rbx), %rcx
leaq (%rcx,%r13), %rdx
movl %eax, 0x30(%rcx,%r13)
movl -0x30(%rbp), %edi
movslq %edi, %rax
movq %rdx, (%rsi,%rax,8)
incl %edi
movq -0x38(%rbp), %r12
movl $0x1, (%r12,%rax,4)
incq %r8
movslq 0x8(%rbx), %rax
cmpq %rax, %r8
jl 0x3dd9
movslq 0x18(%rbx), %rax
testq %rax, %rax
movl $0x0, %r8d
jle 0x4000
movq 0x10(%rbx), %rcx
leaq (,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
xorl %r8d, %r8d
cmpl $0x1, 0x4(%rcx,%rsi)
adcl $0x0, %r8d
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x3fee
xorl %r15d, %r15d
movl %edi, %ecx
orl %r8d, %ecx
je 0x40d6
movl %edi, %r13d
movl %edi, %r14d
subl %r8d, %r14d
addl %eax, %r14d
movslq %r14d, %rsi
leaq -0x60(%rbp), %rdi
movl $0x18, %edx
callq 0x460f
movl %eax, %r15d
testl %eax, %eax
jne 0x40d6
xorl %eax, %eax
cmpl $0x0, 0x18(%rbx)
jle 0x4087
movl $0x4, %ecx
movq -0x60(%rbp), %rdx
xorl %esi, %esi
movq 0x10(%rbx), %rdi
cmpl $0x0, (%rdi,%rcx)
je 0x4075
addq %rcx, %rdi
addq $-0x4, %rdi
cltq
leaq (%rax,%rax,2), %r8
movq 0x10(%rdi), %r9
movq %r9, 0x10(%rdx,%r8,8)
movupd (%rdi), %xmm0
movupd %xmm0, (%rdx,%r8,8)
incl %eax
incq %rsi
movslq 0x18(%rbx), %rdi
addq $0x18, %rcx
cmpq %rdi, %rsi
jl 0x4049
cltq
testl %r13d, %r13d
movq -0x50(%rbp), %rdi
jle 0x40c1
movq -0x60(%rbp), %rdx
movl %r13d, %ecx
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0x4, %rax
xorl %edx, %edx
movq (%rdi,%rdx,8), %rsi
movl 0x30(%rsi), %esi
movl %esi, -0x4(%rax)
movl (%r12,%rdx,4), %esi
movl %esi, (%rax)
incq %rdx
addq $0x18, %rax
cmpq %rdx, %rcx
jne 0x40a5
movq 0x10(%rbx), %rdi
callq 0x1030
movq -0x60(%rbp), %rax
movq %rax, 0x10(%rbx)
movl %r14d, 0x18(%rbx)
movl %r15d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq