// File: /workspace/github_c_makefile_star2_O3/kernfel[P]fNEAT/neat.c
// Function: mutate_population
int mutate_population( Population *pop, struct NEAT_Params *params, Individual **champions ) {
	int i, k, l, err=0;
	Node_Innovation ni[pop->num_members];
	Link_Innovation li[pop->num_members];
	int num_ni=0, num_li=0;
	for ( i=0; i<pop->num_members; i++ ) {
		// Don't mutate champions
		int is_champ=0;
		for ( k=0; k<pop->num_species; k++ ) {
			if ( &pop->members[i] == champions[k] ) {
				is_champ = 1;
				break;
			}
		}
		if ( is_champ )
			continue;
		
		// Shortcut
		CPPN *genotype =& pop->members[i].genotype;

		// Prepare for new innovations
		ni[num_ni].replaced_link = 0;
		li[num_li].innov_id = 0;

		// Mutate
		if (( err = mutate_CPPN( genotype, params, ni+num_ni, li+num_li ) ))
			return err;

		// Identify and collapse parallel node innovations
		if ( ni[num_ni].replaced_link ) {
			int collapsed=0;
			// Check through the previous innovations
			for ( k=0; k<num_ni; k++ ) {
				if ( ni[k].replaced_link == ni[num_ni].replaced_link ) {
					// An equivalent innovation is found. Find the relevant links and collapse their innov_id's
					// Note that updating moves the links within their containing array, hence the l++ as a security blanket
					for ( l=genotype->num_links-1; l>=0 && collapsed<2; l-- ) {
						if ( genotype->links[l].innov_id == ni[num_ni].link_out ) {
							CPPN_update_innov_id( genotype, genotype->links[l].innov_id, ni[k].link_out );
							collapsed++;
							l++;
						} else if ( genotype->links[l].innov_id == ni[num_ni].link_in ) {
							CPPN_update_innov_id( genotype, genotype->links[l].innov_id, ni[k].link_in );
							collapsed++;
							l++;
						}
					}
					ASSERT( collapsed==2 );
					break;
				}
			}
			
			// A truly new innovation
			if ( ! collapsed )
				num_ni++;
		}

		// Identify and collapse parallel link innovations
		if ( li[num_li].innov_id ) {
			int collapsed=0;
			// Check through the previous innovations
			for ( k=0; k<num_li; k++ ) {
				if ( li[k].type == li[num_li].type && li[k].from == li[num_li].from && li[k].to == li[num_li].to ) {
					// An equivalent innovation is found. Find the relevant link and collapse its innov_id
					for ( l=genotype->num_links-1; l>=0; l-- ) {
						if ( genotype->links[l].innov_id == li[num_li].innov_id ) {
							CPPN_update_innov_id( genotype, genotype->links[l].innov_id, li[k].innov_id );
							collapsed = 1;
							break;
						}
					}
					ASSERT( collapsed );
					break;
				}
			}
			
			// A truly new innovation
			if ( ! collapsed )
				num_li++;
		}
	}

	return err;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movl 0x8(%rdi), %eax
movq %rsp, %rsi
leaq (%rax,%rax,2), %rcx
leaq 0xf(,%rcx,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rsi
movq %rsi, %rsp
movq %rsp, %r14
movq %rax, %rcx
shlq $0x4, %rcx
subq %rcx, %r14
movq %r14, %rsp
testl %eax, %eax
jle 0x3e8e
movq %rdx, %r15
leaq 0x8(%rsi), %rax
movq %rax, -0x60(%rbp)
leaq 0x4(%rsi), %rax
movq %rax, -0x58(%rbp)
xorl %r8d, %r8d
movl $0x0, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movq %rdx, -0x68(%rbp)
movq %rbx, -0x70(%rbp)
movq %rdi, -0x40(%rbp)
movq %rsi, -0x38(%rbp)
movq %r14, -0x48(%rbp)
movslq 0x18(%rdi), %rax
movq (%rdi), %r12
testq %rax, %rax
jle 0x3cf2
imulq $0x38, %r8, %rcx
addq %r12, %rcx
xorl %edx, %edx
cmpq (%r15,%rdx,8), %rcx
je 0x3e46
incq %rdx
cmpq %rdx, %rax
jne 0x3ce0
movq %r8, -0x50(%rbp)
imulq $0x38, %r8, %rax
addq %rax, %r12
movslq -0x2c(%rbp), %rax
leaq (%rax,%rax,2), %rax
movq %r14, %rcx
leaq (%rsi,%rax,4), %r14
xorl %edx, %edx
movl %edx, (%r14)
movslq -0x30(%rbp), %rax
shlq $0x4, %rax
leaq (%rcx,%rax), %r13
movl %edx, (%rcx,%rax)
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x1f6c
testl %eax, %eax
jne 0x3e90
movl (%r14), %eax
testl %eax, %eax
je 0x3df2
cmpl $0x0, -0x2c(%rbp)
jle 0x3d70
movl -0x2c(%rbp), %ecx
shlq $0x2, %rcx
leaq (%rcx,%rcx,2), %rdx
movq -0x58(%rbp), %rsi
xorl %ecx, %ecx
cmpl %eax, -0x4(%rsi)
je 0x3d75
addq $-0xc, %rcx
addq $0xc, %rsi
movq %rdx, %rdi
addq %rcx, %rdi
jne 0x3d5b
incl -0x2c(%rbp)
jmp 0x3df2
movl 0x20(%r12), %ebx
testl %ebx, %ebx
jle 0x3de6
movq -0x58(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x60(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x80(%rbp)
xorl %r15d, %r15d
leal -0x1(%rbx), %eax
movq 0x18(%r12), %rcx
movq %rax, %rdx
shlq $0x5, %rdx
movl (%rcx,%rdx), %esi
movq -0x80(%rbp), %rcx
cmpl 0x8(%r14), %esi
je 0x3dbd
movq -0x78(%rbp), %rcx
cmpl 0x4(%r14), %esi
jne 0x3dcc
movl (%rcx), %edx
movq %r12, %rdi
callq 0x2812
incl %r15d
jmp 0x3dce
movl %eax, %ebx
testl %ebx, %ebx
jle 0x3dd8
cmpl $0x2, %r15d
jl 0x3d97
xorl %eax, %eax
testl %r15d, %r15d
sete %al
movq -0x68(%rbp), %r15
jmp 0x3deb
movl $0x1, %eax
addl %eax, -0x2c(%rbp)
movq -0x70(%rbp), %rbx
movq -0x48(%rbp), %r14
movl (%r13), %esi
testl %esi, %esi
je 0x3e3a
cmpl $0x0, -0x30(%rbp)
jle 0x3e37
movl 0x4(%r13), %ecx
movl -0x30(%rbp), %edx
shlq $0x4, %rdx
xorl %eax, %eax
cmpl %ecx, 0x4(%r14,%rax)
jne 0x3e2e
movl 0x8(%r14,%rax), %edi
cmpl 0x8(%r13), %edi
jne 0x3e2e
movl 0xc(%r14,%rax), %edi
cmpl 0xc(%r13), %edi
je 0x3e58
addq $0x10, %rax
cmpq %rax, %rdx
jne 0x3e11
incl -0x30(%rbp)
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %r8
incq %r8
movslq 0x8(%rdi), %rax
cmpq %rax, %r8
jl 0x3ccb
jmp 0x3e8e
movl 0x20(%r12), %ecx
leal 0x1(%rcx), %edx
shlq $0x5, %rcx
addq $-0x20, %rcx
decl %edx
testl %edx, %edx
jle 0x3e37
leaq -0x20(%rcx), %rdi
movq 0x18(%r12), %r8
cmpl %esi, (%r8,%rcx)
movq %rdi, %rcx
jne 0x3e68
movl (%r14,%rax), %edx
movq %r12, %rdi
callq 0x2812
jmp 0x3e3a
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq