// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rdx, -0x58(%rbp)
movq %rsi, %r15
movq %rdi, %r8
movslq 0x18(%rdi), %rax
testq %rax, %rax
jle 0x3fcd
movq 0x10(%r8), %rcx
leal 0x3(%rax), %edx
leaq -0x1(%rax), %rsi
movq %rsi, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x2, %edx
shlq $0x5, %rdx
leaq (%rdx,%rdx,2), %rdx
movdqa 0x3361(%rip), %xmm1     # 0x7250
movdqa 0x3369(%rip), %xmm2     # 0x7260
xorl %esi, %esi
movdqa 0x336f(%rip), %xmm3     # 0x7270
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x336f(%rip), %xmm5     # 0x7280
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %edi
notl %edi
testb $0x1, %dil
je 0x3f4e
movl $0x0, 0x4(%rcx,%rsi)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %edi
testb $0x1, %dil
je 0x3f65
movl $0x0, 0x1c(%rcx,%rsi)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %edi
testb $0x1, %dil
je 0x3fa5
movl $0x0, 0x34(%rcx,%rsi)
pextrw $0x4, %xmm6, %edi
testb $0x1, %dil
je 0x3fb8
movl $0x0, 0x4c(%rcx,%rsi)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x60, %rsi
cmpq %rsi, %rdx
jne 0x3f11
movl 0x8(%r8), %ecx
movq %rsp, %rsi
leaq 0xf(,%rcx,4), %rdx
andq $-0x10, %rdx
subq %rdx, %rsi
movq %rsi, -0x40(%rbp)
movq %rsi, %rsp
movq %rsp, %r13
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
subq %rdx, %r13
movq %r13, %rsp
xorl %edi, %edi
testl %ecx, %ecx
movq %r8, -0x38(%rbp)
jle 0x41ab
xorl %ecx, %ecx
movl $0x0, -0x2c(%rbp)
movq %r13, -0x48(%rbp)
movq (%r8), %rax
movq %rcx, -0x60(%rbp)
imulq $0x38, %rcx, %r14
cmpl $0x0, 0x30(%rax,%r14)
movl 0x18(%r8), %eax
je 0x408d
testl %eax, %eax
jle 0x40e9
movl $0x4, %r13d
movq -0x58(%rbp), %r12
xorl %ebx, %ebx
movl 0x30(%r12), %ecx
movq (%r8), %rdi
cmpl 0x30(%rdi,%r14), %ecx
jne 0x407a
addq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2d3f
movsd 0x68(%r15), %xmm1
ucomisd %xmm0, %xmm1
ja 0x416c
movq -0x38(%rbp), %r8
movl 0x18(%r8), %eax
incq %rbx
movslq %eax, %rcx
addq $0x18, %r13
addq $0x38, %r12
cmpq %rcx, %rbx
jl 0x4045
testl %eax, %eax
jle 0x40e9
movl $0x4, %r13d
movq -0x58(%rbp), %r12
xorl %ebx, %ebx
movl 0x30(%r12), %ecx
testl %ecx, %ecx
je 0x40d6
movq (%r8), %rdi
cmpl 0x30(%rdi,%r14), %ecx
je 0x40d6
addq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2d3f
movsd 0x68(%r15), %xmm1
ucomisd %xmm0, %xmm1
ja 0x4172
movq -0x38(%rbp), %r8
movl 0x18(%r8), %eax
incq %rbx
movslq %eax, %rcx
addq $0x18, %r13
addq $0x38, %r12
cmpq %rcx, %rbx
jl 0x409d
movl -0x2c(%rbp), %ebx
movq -0x48(%rbp), %r13
testl %ebx, %ebx
jle 0x4133
movq (%r8), %rdi
addq %r14, %rdi
movq -0x8(%r13,%rbx,8), %r12
decq %rbx
movq %r12, %rsi
movq %r15, %rdx
callq 0x2d3f
movq -0x38(%rbp), %r8
movsd 0x68(%r15), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x40f0
movl 0x30(%r12), %eax
movq (%r8), %rcx
movl %eax, 0x30(%rcx,%r14)
movq -0x40(%rbp), %rax
incl (%rax,%rbx,4)
jmp 0x418f
movl 0x10c(%r15), %eax
incl %eax
movl %eax, 0x10c(%r15)
movq (%r8), %rcx
leaq (%rcx,%r14), %rdx
movl %eax, 0x30(%rcx,%r14)
movl -0x2c(%rbp), %ecx
movslq %ecx, %rax
movq %rdx, (%r13,%rax,8)
incl %ecx
movl %ecx, -0x2c(%rbp)
movq -0x40(%rbp), %rcx
movl $0x1, (%rcx,%rax,4)
jmp 0x418f
movq -0x38(%rbp), %r8
jmp 0x4183
movl 0x30(%r12), %eax
movq -0x38(%rbp), %r8
movq (%r8), %rcx
movl %eax, 0x30(%rcx,%r14)
movq 0x10(%r8), %rax
incl (%rax,%r13)
movq -0x48(%rbp), %r13
movq -0x60(%rbp), %rcx
incq %rcx
movslq 0x8(%r8), %rax
cmpq %rax, %rcx
jl 0x401a
movl 0x18(%r8), %eax
xorl %edi, %edi
jmp 0x41b2
movl $0x0, -0x2c(%rbp)
testl %eax, %eax
jle 0x41d9
movq 0x10(%r8), %rcx
movl %eax, %edx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpl $0x1, 0x4(%rcx,%rsi)
adcl $0x0, %edi
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x41c8
xorl %r15d, %r15d
movl -0x2c(%rbp), %r14d
movl %r14d, %ecx
orl %edi, %ecx
je 0x42bc
subl %edi, %r14d
addl %eax, %r14d
movslq %r14d, %rsi
leaq -0x50(%rbp), %rdi
movl $0x18, %edx
callq 0x47f7
movl %eax, %r15d
testl %eax, %eax
jne 0x42bc
movq -0x38(%rbp), %r10
movl 0x18(%r10), %edi
xorl %eax, %eax
testl %edi, %edi
jle 0x4269
movl $0x4, %ecx
movq -0x50(%rbp), %rdx
xorl %esi, %esi
movq 0x10(%r10), %r8
cmpl $0x0, (%r8,%rcx)
je 0x4258
leaq (%r8,%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
movl 0x18(%r10), %edi
incq %rsi
movslq %edi, %r8
addq $0x18, %rcx
cmpq %r8, %rsi
jl 0x4226
cltq
movl -0x2c(%rbp), %ecx
testl %ecx, %ecx
movq -0x40(%rbp), %rdi
jle 0x42a4
movq -0x50(%rbp), %rdx
movl %ecx, %ecx
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0x4, %rax
xorl %edx, %edx
movq (%r13,%rdx,8), %rsi
movl 0x30(%rsi), %esi
movl %esi, -0x4(%rax)
movl (%rdi,%rdx,4), %esi
movl %esi, (%rax)
incq %rdx
addq $0x18, %rax
cmpq %rdx, %rcx
jne 0x4288
movq 0x10(%r10), %rdi
movq %r10, %rbx
callq 0x1030
movq -0x50(%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