// File: /workspace/github_c_makefile_star2_O2/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, -0x58(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdi, %rbx
movl 0x18(%rdi), %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
imulq $0x18, %rcx, %rcx
cmpq %rax, %rcx
je 0x3a89
movq 0x10(%rbx), %rdx
andl $0x0, 0x4(%rdx,%rax)
addq $0x18, %rax
jmp 0x3a75
movl 0x8(%rbx), %eax
movq %rsp, %rdx
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x40(%rbp)
movq %rdx, %rsp
movq %rsp, %r13
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r13
movq %r13, %rsp
xorl %ecx, %ecx
xorl %r12d, %r12d
movq %rbx, -0x50(%rbp)
movq %r13, -0x48(%rbp)
cltq
cmpq %rax, %rcx
jge 0x3c57
movq %r12, -0x30(%rbp)
movq (%rbx), %rax
movq %rcx, -0x68(%rbp)
imulq $0x38, %rcx, %r14
cmpl $0x0, 0x30(%rax,%r14)
je 0x3b3e
movq -0x58(%rbp), %r12
pushq $0x4
popq %r15
xorl %r13d, %r13d
movslq 0x18(%rbx), %rax
cmpq %rax, %r13
jge 0x3b3e
movl 0x30(%r12), %eax
movq (%rbx), %rdi
cmpl 0x30(%rdi,%r14), %eax
jne 0x3b31
addq %r14, %rdi
movq %r12, %rsi
movq -0x38(%rbp), %rbx
movq %rbx, %rdx
callq 0x2997
movsd 0x68(%rbx), %xmm1
movq -0x50(%rbp), %rbx
ucomisd %xmm0, %xmm1
ja 0x3c21
incq %r13
addq $0x18, %r15
addq $0x38, %r12
jmp 0x3af4
movq -0x58(%rbp), %r12
pushq $0x4
popq %r13
xorl %r15d, %r15d
movslq 0x18(%rbx), %rax
cmpq %rax, %r15
jge 0x3b97
movl 0x30(%r12), %eax
testl %eax, %eax
je 0x3b8a
movq (%rbx), %rdi
cmpl 0x30(%rdi,%r14), %eax
je 0x3b8a
addq %r14, %rdi
movq %r12, %rsi
movq -0x38(%rbp), %rbx
movq %rbx, %rdx
callq 0x2997
movsd 0x68(%rbx), %xmm1
movq -0x50(%rbp), %rbx
ucomisd %xmm0, %xmm1
ja 0x3c2b
incq %r15
addq $0x18, %r13
addq $0x38, %r12
jmp 0x3b49
movl -0x30(%rbp), %r15d
movq -0x48(%rbp), %r13
testl %r15d, %r15d
jle 0x3be7
movq (%rbx), %rdi
addq %r14, %rdi
movq -0x8(%r13,%r15,8), %r12
decq %r15
movq %r12, %rsi
movq -0x38(%rbp), %rbx
movq %rbx, %rdx
callq 0x2997
movsd 0x68(%rbx), %xmm1
movq -0x50(%rbp), %rbx
ucomisd %xmm0, %xmm1
jbe 0x3b9f
movl 0x30(%r12), %eax
movq (%rbx), %rcx
movl %eax, 0x30(%rcx,%r14)
movq -0x40(%rbp), %rax
incl (%rax,%r15,4)
jmp 0x3c44
movq -0x38(%rbp), %rcx
movl 0x10c(%rcx), %eax
incl %eax
movl %eax, 0x10c(%rcx)
movq (%rbx), %rcx
leaq (%rcx,%r14), %rdx
movl %eax, 0x30(%rcx,%r14)
movq -0x30(%rbp), %r12
movslq %r12d, %rax
movq %rdx, (%r13,%rax,8)
incl %r12d
movq -0x40(%rbp), %rcx
movl $0x1, (%rcx,%rax,4)
jmp 0x3c48
movq 0x10(%rbx), %rax
incl (%rax,%r15)
jmp 0x3c40
movl 0x30(%r12), %eax
movq (%rbx), %rcx
movl %eax, 0x30(%rcx,%r14)
movq 0x10(%rbx), %rax
incl (%rax,%r13)
movq -0x48(%rbp), %r13
movq -0x30(%rbp), %r12
movq -0x68(%rbp), %rcx
incq %rcx
movl 0x8(%rbx), %eax
jmp 0x3ac7
movl 0x18(%rbx), %eax
xorl %edx, %edx
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
imulq $0x18, %rcx, %rsi
xorl %ecx, %ecx
cmpq %rdx, %rsi
je 0x3c83
movq 0x10(%rbx), %rdi
cmpl $0x1, 0x4(%rdi,%rdx)
adcl $0x0, %ecx
addq $0x18, %rdx
jmp 0x3c6c
xorl %r14d, %r14d
movl %r12d, %edx
orl %ecx, %edx
je 0x3d4d
movl %r12d, %r15d
subl %ecx, %r15d
addl %eax, %r15d
movslq %r15d, %rsi
leaq -0x60(%rbp), %rdi
pushq $0x18
popq %rdx
callq 0x421b
movl %eax, %r14d
testl %eax, %eax
jne 0x3d4d
xorl %ecx, %ecx
pushq $0x4
popq %rdx
movq -0x60(%rbp), %rax
xorl %esi, %esi
movslq 0x18(%rbx), %r8
movslq %esi, %rdi
cmpq %r8, %rcx
jge 0x3d02
movq 0x10(%rbx), %r8
cmpl $0x0, (%r8,%rdx)
je 0x3cf9
leaq (%r8,%rdx), %rsi
addq $-0x4, %rsi
imulq $0x18, %rdi, %r8
movq 0x10(%rsi), %r9
movq %r9, 0x10(%rax,%r8)
movupd (%rsi), %xmm0
movupd %xmm0, (%rax,%r8)
incl %edi
movl %edi, %esi
incq %rcx
addq $0x18, %rdx
jmp 0x3cbf
xorl %ecx, %ecx
testl %r12d, %r12d
cmovlel %ecx, %r12d
imulq $0x18, %rdi, %rdx
addq %rdx, %rax
addq $0x4, %rax
cmpq %rcx, %r12
je 0x3d38
movq (%r13,%rcx,8), %rdx
movl 0x30(%rdx), %edx
movl %edx, -0x4(%rax)
movq -0x40(%rbp), %rdx
movl (%rdx,%rcx,4), %edx
movl %edx, (%rax)
incq %rcx
addq $0x18, %rax
jmp 0x3d16
movq 0x10(%rbx), %rdi
callq 0x1030
movq -0x60(%rbp), %rax
movq %rax, 0x10(%rbx)
movl %r15d, 0x18(%rbx)
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq