// File: /workspace/github_c_makefile_star2_O1/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 %rsi, -0x88(%rbp)
movl 0x8(%rdi), %eax
movq %rsp, %r12
leaq (%rax,%rax,2), %rcx
leaq 0xf(,%rcx,4), %rcx
andq $-0x10, %rcx
subq %rcx, %r12
movq %r12, %rsp
movq %rsp, %r9
movq %rax, %rcx
shlq $0x4, %rcx
subq %rcx, %r9
movq %r9, %rsp
testl %eax, %eax
jle 0x3d29
movq %rdx, %rbx
movq %rdi, %r14
leaq 0x8(%r12), %rax
movq %rax, -0x78(%rbp)
leaq 0x4(%r12), %rax
movq %rax, -0x68(%rbp)
xorl %r15d, %r15d
xorl %esi, %esi
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x34(%rbp)
movq %rdi, -0x50(%rbp)
movq %r12, -0x70(%rbp)
movq %r9, -0x40(%rbp)
movslq 0x18(%r14), %rax
testq %rax, %rax
jle 0x3b1f
imulq $0x38, %r15, %rcx
addq (%r14), %rcx
xorl %edx, %edx
cmpq (%rbx,%rdx,8), %rcx
je 0x3b82
incq %rdx
cmpq %rdx, %rax
jne 0x3b11
imulq $0x38, %r15, %r13
addq (%r14), %r13
movslq -0x2c(%rbp), %rax
leaq (%rax,%rax,2), %rax
leaq (%r12,%rax,4), %r12
xorl %ecx, %ecx
movl %ecx, (%r12)
movl %esi, -0x30(%rbp)
movslq %esi, %rax
shlq $0x4, %rax
leaq (%r9,%rax), %r14
movl %ecx, (%r9,%rax)
movq %r13, %rdi
movq -0x88(%rbp), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1f81
movl $0x1, %edx
testl %eax, %eax
movl %eax, -0x34(%rbp)
je 0x3b8c
movl %eax, -0x5c(%rbp)
movq -0x70(%rbp), %r12
movq -0x40(%rbp), %r9
movl -0x30(%rbp), %esi
movq -0x50(%rbp), %r14
jmp 0x3cd9
movl $0x4, %edx
jmp 0x3cd9
movq %r14, -0x48(%rbp)
movq %r13, -0x58(%rbp)
movl (%r12), %ecx
testl %ecx, %ecx
je 0x3be6
movl $0x1, %eax
movl -0x2c(%rbp), %esi
testl %esi, %esi
movq -0x40(%rbp), %r9
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %r11
jle 0x3c7b
movl %esi, %edx
shlq $0x2, %rdx
leaq (%rdx,%rdx,2), %rsi
movq -0x68(%rbp), %r10
xorl %edx, %edx
cmpl %ecx, -0x4(%r10)
je 0x3bf7
addq $-0xc, %rdx
addq $0xc, %r10
movq %rsi, %r8
addq %rdx, %r8
jne 0x3bc8
movl -0x2c(%rbp), %esi
jmp 0x3c7b
movq -0x40(%rbp), %r9
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %r11
jmp 0x3c80
movl 0x20(%rdi), %r14d
testl %r14d, %r14d
movl -0x2c(%rbp), %esi
jle 0x3c7b
movq -0x68(%rbp), %rax
subq %rdx, %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rax
subq %rdx, %rax
movq %rax, -0x90(%rbp)
xorl %r13d, %r13d
leal -0x1(%r14), %eax
movq 0x18(%rdi), %rcx
movq %rax, %rdx
shlq $0x5, %rdx
movl (%rcx,%rdx), %esi
movq -0x90(%rbp), %rcx
cmpl 0x8(%r12), %esi
je 0x3c4a
movq -0x80(%rbp), %rcx
cmpl 0x4(%r12), %esi
jne 0x3c5e
movl (%rcx), %edx
callq 0x276b
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %r9
incl %r13d
jmp 0x3c61
movl %eax, %r14d
testl %r14d, %r14d
jle 0x3c6c
cmpl $0x2, %r13d
jl 0x3c1f
xorl %eax, %eax
testl %r13d, %r13d
sete %al
movl -0x2c(%rbp), %esi
movq -0x48(%rbp), %r11
addl %eax, %esi
movl %esi, -0x2c(%rbp)
movq -0x50(%rbp), %r14
movl (%r11), %ecx
xorl %edx, %edx
testl %ecx, %ecx
movq -0x70(%rbp), %r12
movl -0x30(%rbp), %esi
je 0x3cd9
movl $0x1, %eax
testl %esi, %esi
jle 0x3cd7
movl %esi, %edx
movl 0x4(%r11), %esi
movl %edx, %r10d
shlq $0x4, %r10
xorl %edx, %edx
cmpl %esi, 0x4(%r9,%rdx)
jne 0x3cc9
movl 0x8(%r9,%rdx), %r8d
cmpl 0x8(%r11), %r8d
jne 0x3cc9
movl 0xc(%r9,%rdx), %r8d
cmpl 0xc(%r11), %r8d
je 0x3cf0
addq $0x10, %rdx
cmpq %rdx, %r10
jne 0x3cac
movl -0x30(%rbp), %esi
xorl %edx, %edx
addl %eax, %esi
testb $0x3, %dl
jne 0x3d32
incq %r15
movslq 0x8(%r14), %rax
cmpq %rax, %r15
jl 0x3aff
jmp 0x3d38
movl 0x20(%rdi), %r8d
leal 0x1(%r8), %r10d
shlq $0x5, %r8
addq $-0x20, %r8
decl %r10d
testl %r10d, %r10d
jle 0x3cd2
movq 0x18(%rdi), %rsi
movl (%rsi,%r8), %esi
addq $-0x20, %r8
cmpl %ecx, %esi
jne 0x3d00
movl (%r9,%rdx), %edx
callq 0x276b
movq -0x40(%rbp), %r9
xorl %eax, %eax
jmp 0x3cd2
movl $0x0, -0x34(%rbp)
jmp 0x3d38
movl -0x5c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq