// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movl 0x8(%rdi), %r9d
movq %rsp, %rcx
imulq $0xc, %r9, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rcx, %r8
subq %rdx, %r8
movq %r8, %rsp
movq %rsp, %r12
movq %r9, %rsi
shlq $0x4, %rsi
subq %rsi, %r12
movq %r12, %rsp
negq %rdx
leaq (%rcx,%rdx), %rax
addq $-0x4, %rax
movq %rax, -0x58(%rbp)
movq %r8, -0x48(%rbp)
leaq -0x8(%r8), %rax
movq %rax, -0x50(%rbp)
xorl %eax, %eax
xorl %r8d, %r8d
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq %rbx, -0x68(%rbp)
movq %r15, -0x60(%rbp)
movq %rdi, -0x38(%rbp)
movq %r12, -0x78(%rbp)
movslq %r9d, %rcx
cmpq %rcx, %r8
jge 0x3a3c
movl 0x18(%rdi), %esi
testl %esi, %esi
cmovlel %eax, %esi
imulq $0x38, %r8, %r13
addq (%rdi), %r13
xorl %ecx, %ecx
cmpq %rcx, %rsi
je 0x388a
leaq 0x1(%rcx), %rdx
cmpq (%rbx,%rcx,8), %r13
movq %rdx, %rcx
jne 0x3873
jmp 0x3a30
movq %r8, -0x40(%rbp)
movslq -0x2c(%rbp), %rax
imulq $0xc, %rax, %rax
movq -0x48(%rbp), %rcx
andl $0x0, (%rcx,%rax)
movq %r12, %rdx
leaq (%rcx,%rax), %r12
movslq -0x30(%rbp), %rax
shlq $0x4, %rax
leaq (%rdx,%rax), %r14
andl $0x0, (%rdx,%rax)
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1dbd
testl %eax, %eax
jne 0x3a3c
movl (%r12), %eax
testl %eax, %eax
je 0x39a5
movl -0x2c(%rbp), %edx
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
imulq $0xc, %rcx, %rdx
addq $0xc, %rdx
movq -0x48(%rbp), %rsi
xorl %ecx, %ecx
addq $-0xc, %rcx
movq %rdx, %rdi
addq %rcx, %rdi
je 0x3991
leaq 0xc(%rsi), %rdi
cmpl %eax, (%rsi)
movq %rdi, %rsi
jne 0x38f5
movl 0x20(%r13), %edx
movq -0x50(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x88(%rbp)
xorl %ebx, %ebx
leal -0x1(%rdx), %eax
movq %rax, -0x70(%rbp)
shlq $0x5, %rax
movq %rax, -0x90(%rbp)
testl %edx, %edx
jle 0x3996
cmpl $0x1, %ebx
jg 0x3996
movq 0x18(%r13), %rax
movq -0x90(%rbp), %rcx
movl (%rax,%rcx), %esi
movq -0x88(%rbp), %rax
cmpl 0x8(%r12), %esi
je 0x3971
movq -0x80(%rbp), %rax
cmpl 0x4(%r12), %esi
jne 0x398b
movq %r12, %r15
movq %rdx, %r12
movl (%rax), %edx
movq %r13, %rdi
callq 0x252b
movq %r12, %rdx
movq %r15, %r12
incl %ebx
jmp 0x3941
movq -0x70(%rbp), %rdx
jmp 0x392f
incl -0x2c(%rbp)
jmp 0x39a5
cmpl $0x1, %ebx
adcl $0x0, -0x2c(%rbp)
movq -0x68(%rbp), %rbx
movq -0x60(%rbp), %r15
movl (%r14), %esi
testl %esi, %esi
movq -0x78(%rbp), %r12
je 0x3a26
movl -0x30(%rbp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
shlq $0x4, %rcx
xorl %eax, %eax
cmpq %rax, %rcx
je 0x3a23
movl 0x4(%r12,%rax), %edx
cmpl 0x4(%r14), %edx
jne 0x39e9
movl 0x8(%r12,%rax), %edx
cmpl 0x8(%r14), %edx
jne 0x39e9
movl 0xc(%r12,%rax), %edx
cmpl 0xc(%r14), %edx
je 0x39ef
addq $0x10, %rax
jmp 0x39c3
movl 0x20(%r13), %ecx
leal 0x1(%rcx), %edx
shlq $0x5, %rcx
addq $-0x20, %rcx
decl %edx
testl %edx, %edx
jle 0x3a23
leaq -0x20(%rcx), %rdi
movq 0x18(%r13), %r8
cmpl %esi, (%r8,%rcx)
movq %rdi, %rcx
jne 0x39fe
movl (%r12,%rax), %edx
movq %r13, %rdi
callq 0x252b
jmp 0x3a26
incl -0x30(%rbp)
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movq -0x40(%rbp), %r8
incq %r8
movl 0x8(%rdi), %r9d
jmp 0x3856
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq