// File: /workspace/github_c_makefile_star2_O2/kernfel[P]fNEAT/cppn.c
// Function: crossover_CPPN
int crossover_CPPN( CPPN *net1, const CPPN *net2, struct NEAT_Params *params ) {
	int err=0, i, j;
	
	int num_nodes_net1 = net1->num_inputs+net1->num_outputs+net1->num_hidden;
	
	int nodemap[net2->num_inputs+net2->num_outputs+net2->num_hidden];
	for ( i=0; i<net2->num_inputs+net2->num_outputs+net2->num_hidden; i++ )
		nodemap[i] = -1;
	
	CPPN_Link *extra_links[net2->num_links];
	int num_extra=0;
	i=0;
	j=0;
	while ( i<net1->num_links && j<net2->num_links ) {
		if ( net1->links[i].innov_id == net2->links[j].innov_id ) {
			// Add matching nodes to the node mapping, then move on
			nodemap[net2->links[j].from] = net1->links[i].from;
			nodemap[net2->links[j].to] = net1->links[i].to;
			i++;
			j++;
		} else if ( net1->links[i].innov_id > net2->links[j].innov_id ) {
			// Disjoint in net2, track
			extra_links[num_extra++] = &net2->links[j];
			j++;
		} else {
			// Disjoint in net1, ignore
			i++;
		}
	}
	for ( ; j<net2->num_links; j++ ) {
		// Excess in net2
		extra_links[num_extra++] = &net2->links[j];
	}
	
	// Nothing to add, bail
	if ( ! num_extra )
		return 0;
	
	// Determine extra nodes - assume hidden for now
	CPPN_Node *extra_nodes[num_extra];
	int num_extra_nodes=0;
	for ( i=0; i<num_extra; i++ ) {
		if ( nodemap[extra_links[i]->from] == -1 ) {
			extra_nodes[num_extra_nodes] = &net2->nodes[extra_links[i]->from];
			nodemap[extra_links[i]->from] = num_nodes_net1 + num_extra_nodes;
			num_extra_nodes++;
		}
		if ( nodemap[extra_links[i]->to] == -1 ) {
			extra_nodes[num_extra_nodes] = &net2->nodes[extra_links[i]->to];
			nodemap[extra_links[i]->to] = num_nodes_net1 + num_extra_nodes;
			num_extra_nodes++;
		}
	}
	
	// Add extra nodes
	if ( num_extra_nodes ) {
		if (( err = Realloc( net1->nodes, (num_nodes_net1+num_extra_nodes)*sizeof *net1->nodes ) ))
			return err;
		for ( i=0; i<num_extra_nodes; i++ ) {
			memcpy( &net1->nodes[num_nodes_net1+i], extra_nodes[i], sizeof(CPPN_Node) );
		}
		net1->num_hidden += num_extra_nodes;
		num_nodes_net1 += num_extra_nodes;
	}

	// Eliminate duplicates and recursions, then add remaining links
	int *downstream_tpl, *downstream;
	if ( ! (params->flags & CFL_ALLOW_RECURRENCE) ) {
		if (( err = Malloc( downstream_tpl, num_nodes_net1*2*sizeof(int) ) ))
			return err;
		for ( i=0; i<num_nodes_net1; i++ )
			downstream_tpl[i] = 1;
		downstream = &downstream_tpl[num_nodes_net1];
	}
	for ( i=0; i<num_extra; i++ ) {
		
		// Exclude duplicates
		for ( j=0; j<net1->num_links; j++ ) {
			if ( net1->links[j].from == nodemap[extra_links[i]->from] \
			 && net1->links[j].to == nodemap[extra_links[i]->to] ) {
				extra_links[i] = 0;
				break;
			}
		}
		
		// Exclude recursions
		if ( ! (params->flags & CFL_ALLOW_RECURRENCE) && extra_links[i] ) {
			memcpy( downstream, downstream_tpl, num_nodes_net1*sizeof *downstream );
			CPPN_exclude_recurrent_links( net1, params, nodemap[extra_links[i]->to], downstream );
			if ( ! downstream[nodemap[extra_links[i]->from]] )
				extra_links[i] = 0;
		}

		if ( ! extra_links[i] )
			continue;

		// Add extra links
		if (( err = CPPN_insert_link(
			net1,
			params,
			nodemap[extra_links[i]->from],
			nodemap[extra_links[i]->to],
			extra_links[i]->weight,
			extra_links[i]->is_disabled,
			extra_links[i]->innov_id
		) ))
			goto cleanup;
	}

cleanup:
	if ( ! (params->flags & CFL_ALLOW_RECURRENCE) )
		free( downstream_tpl );
	
	return err;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, -0x40(%rbp)
movq %rdi, %r14
movl 0x8(%rdi), %eax
movl 0xc(%rdi), %edi
movl 0x10(%r14), %ecx
movl %ecx, -0x6c(%rbp)
movl 0xc(%rsi), %ecx
addl 0x8(%rsi), %ecx
addl 0x10(%rsi), %ecx
leaq 0xf(,%rcx,4), %rdx
andq $-0x10, %rdx
movq %rsp, %r12
subq %rdx, %r12
movq %r12, %rsp
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
cmpq %rdx, %rcx
je 0x2b57
orl $-0x1, (%r12,%rdx,4)
incq %rdx
jmp 0x2b48
addl %eax, %edi
movq %rdi, -0x68(%rbp)
movl 0x20(%rsi), %eax
movslq %eax, %rdx
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsp
movl 0x20(%r14), %eax
movl %eax, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
xorl %ecx, %ecx
xorl %r10d, %r10d
movl $0x0, -0x38(%rbp)
movslq %ecx, %rdi
shlq $0x5, %rdi
movl %ecx, -0x4c(%rbp)
movslq %ecx, %r15
movslq %r10d, %r9
shlq $0x5, %r9
orq $0x8, %r9
movq %r15, %r13
shlq $0x5, %r13
movl -0x38(%rbp), %eax
cmpl -0x48(%rbp), %r10d
jge 0x2c54
cmpl %edx, %r15d
jge 0x2c54
movq %r14, %rbx
movq 0x18(%r14), %r8
movq %rsi, %r14
movq 0x18(%rsi), %r11
leaq (%r11,%r13), %rcx
movl (%r11,%r13), %esi
cmpl %esi, -0x8(%r8,%r9)
je 0x2bfa
ja 0x2c2b
incl %r10d
addq $0x20, %r9
movq %r14, %rsi
movq %rbx, %r14
jmp 0x2bb6
movl -0x4(%r8,%r9), %eax
movslq 0x4(%rcx), %rcx
movl %eax, (%r12,%rcx,4)
movl (%r8,%r9), %eax
movslq 0x8(%r11,%r13), %rcx
movl %eax, (%r12,%rcx,4)
incl %r10d
movl -0x4c(%rbp), %ecx
incl %ecx
addq $0x20, %rdi
movq %r14, %rsi
movq %rbx, %r14
jmp 0x2b9e
incl -0x38(%rbp)
movq -0x30(%rbp), %rsi
movq %rcx, (%rsi,%rax,8)
movl -0x4c(%rbp), %ecx
incl %ecx
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, -0x58(%rbp)
movq %r14, %rsi
movq %rbx, %r14
jmp 0x2b97
movq -0x68(%rbp), %r8
addl -0x6c(%rbp), %r8d
movq -0x58(%rbp), %r13
movq -0x30(%rbp), %r10
movabsq $0x100000000, %r9       # imm = 0x100000000
cmpq %rdx, %r15
jge 0x2c8d
movq 0x18(%rsi), %rcx
addq %rdi, %rcx
movq %rcx, (%r10,%rax,8)
incq %rax
incq %r15
addq %r9, %r13
addq $0x20, %rdi
jmp 0x2c6e
testq %rax, %rax
je 0x2d82
movl %eax, %eax
movq %rsp, %r11
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r11
movq %r11, %rsp
sarq $0x20, %r13
xorl %eax, %eax
xorl %r15d, %r15d
cmpq %r13, %rax
jge 0x2d12
movq (%r10,%rax,8), %rcx
movslq 0x4(%rcx), %rdx
cmpl $-0x1, (%r12,%rdx,4)
jne 0x2ce6
movq %rdx, %r9
shlq $0x4, %r9
addq (%rsi), %r9
movslq %r15d, %rdi
movq %r9, (%r11,%rdi,8)
leal (%r15,%r8), %edi
movl %edi, (%r12,%rdx,4)
incl %r15d
movslq 0x8(%rcx), %rcx
cmpl $-0x1, (%r12,%rcx,4)
jne 0x2d0d
movq %rcx, %rdx
shlq $0x4, %rdx
addq (%rsi), %rdx
movslq %r15d, %r15
movq %rdx, (%r11,%r15,8)
leal (%r15,%r8), %edx
movl %edx, (%r12,%rcx,4)
incl %r15d
incq %rax
jmp 0x2cb6
testl %r15d, %r15d
je 0x2d89
movq %r11, -0x38(%rbp)
leal (%r15,%r8), %eax
movq %rax, -0x48(%rbp)
movslq %eax, %rsi
shlq $0x4, %rsi
movq %r14, %rdi
movq %r8, -0x68(%rbp)
callq 0x4231
movl %eax, %ebx
testl %eax, %eax
jne 0x2f13
movq -0x48(%rbp), %r9
movslq -0x68(%rbp), %rax
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
shlq $0x4, %rax
shlq $0x3, %rdx
movq -0x30(%rbp), %r10
movq -0x38(%rbp), %r8
cmpq %rcx, %rdx
je 0x2d8e
movq (%r14), %rsi
addq %rax, %rsi
movq (%r8,%rcx), %rdi
movups (%rdi), %xmm0
movups %xmm0, (%rsi,%rcx,2)
addq $0x8, %rcx
jmp 0x2d66
xorl %ebx, %ebx
jmp 0x2f13
movl %r8d, %r15d
jmp 0x2d95
addl %r15d, 0x10(%r14)
movq %r9, %r15
movq -0x40(%rbp), %rax
testb $0x8, (%rax)
jne 0x2de3
leal (%r15,%r15), %eax
movslq %eax, %rsi
shlq $0x2, %rsi
leaq -0x60(%rbp), %rdi
callq 0x4208
movl %eax, %ebx
testl %eax, %eax
jne 0x2f13
movq -0x60(%rbp), %rax
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
movq -0x30(%rbp), %r10
cmpq %rcx, %rdx
je 0x2de8
movl $0x1, (%rax,%rcx,4)
incq %rcx
jmp 0x2dd2
movslq %r15d, %rax
jmp 0x2dfb
movslq %r15d, %rax
leaq (,%rax,4), %rcx
addq -0x60(%rbp), %rcx
movq %rcx, -0x48(%rbp)
shlq $0x2, %rax
movq %rax, -0x38(%rbp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %r13, -0x58(%rbp)
cmpq %r13, %r15
jge 0x2f01
movl 0x20(%r14), %eax
testl %eax, %eax
cmovlel %ebx, %eax
shlq $0x5, %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x2e53
movq 0x18(%r14), %rdx
movl 0x4(%rdx,%rcx), %edi
movq (%r10,%r15,8), %rsi
movslq 0x4(%rsi), %r8
cmpl (%r12,%r8,4), %edi
jne 0x2e4d
movl 0x8(%rdx,%rcx), %edx
movslq 0x8(%rsi), %rsi
cmpl (%r12,%rsi,4), %edx
je 0x2e59
addq $0x20, %rcx
jmp 0x2e24
movq (%r10,%r15,8), %r13
jmp 0x2e61
andq $0x0, (%r10,%r15,8)
xorl %r13d, %r13d
movq -0x40(%rbp), %rax
testb $0x8, (%rax)
jne 0x2ebc
testq %r13, %r13
je 0x2ef3
movq -0x60(%rbp), %rsi
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
movq -0x38(%rbp), %rdx
callq 0x10c0
movslq 0x8(%r13), %rax
movl (%r12,%rax,4), %edx
movq %r14, %rdi
movq -0x40(%rbp), %rsi
movq %rbx, %rcx
callq 0x24af
movq -0x30(%rbp), %r10
movslq 0x4(%r13), %rax
movslq (%r12,%rax,4), %rax
cmpl $0x0, (%rbx,%rax,4)
movl $0x0, %ebx
jne 0x2ec1
andq $0x0, (%r10,%r15,8)
jmp 0x2ef3
testq %r13, %r13
je 0x2ef3
movslq 0x4(%r13), %rax
movl (%r12,%rax,4), %edx
movslq 0x8(%r13), %rax
movl (%r12,%rax,4), %ecx
movsd 0x10(%r13), %xmm0
movl 0x18(%r13), %r8d
movl (%r13), %r9d
movq %r14, %rdi
movq -0x40(%rbp), %rsi
callq 0x23a8
movq -0x30(%rbp), %r10
testl %eax, %eax
jne 0x2eff
incq %r15
movq -0x58(%rbp), %r13
jmp 0x2e0c
movl %eax, %ebx
movq -0x40(%rbp), %rax
testb $0x8, (%rax)
jne 0x2f13
movq -0x60(%rbp), %rdi
callq 0x1030
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq