// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rdx, -0x40(%rbp)
movl 0xc(%rdi), %eax
addl 0x8(%rdi), %eax
movq %rdi, -0x30(%rbp)
addl 0x10(%rdi), %eax
movq %rax, -0x38(%rbp)
movq %rsi, %r13
movl 0x8(%rsi), %eax
addl 0xc(%rsi), %eax
movl 0x10(%rsi), %ecx
leal (%rax,%rcx), %edx
leaq 0xf(,%rdx,4), %rsi
andq $-0x10, %rsi
movq %rsp, %r15
subq %rsi, %r15
movq %r15, %rsp
testl %edx, %edx
jle 0x2da6
addl %ecx, %eax
decl %eax
leaq 0x4(,%rax,4), %rdx
movq %r15, %rdi
movl $0xff, %esi
callq 0x1060
movl 0x20(%r13), %eax
movl %eax, %ecx
movq %rsp, %rbx
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rbx
movq %rbx, %rsp
movq -0x30(%rbp), %rcx
movl 0x20(%rcx), %ecx
testl %ecx, %ecx
setg %dl
testl %eax, %eax
setg %sil
andb %dl, %sil
xorl %edx, %edx
movl $0x0, %r12d
cmpb $0x1, %sil
jne 0x2e53
movq -0x30(%rbp), %rdx
movq 0x18(%rdx), %rsi
movq 0x18(%r13), %rdi
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %edx, %edx
movslq %r8d, %r9
shlq $0x5, %r9
movslq %edx, %r11
shlq $0x5, %r11
leaq (%rdi,%r11), %r10
movl (%rdi,%r11), %r14d
cmpl %r14d, (%rsi,%r9)
jne 0x2e37
leaq (%rsi,%r9), %r14
movl 0x4(%r14), %r14d
movslq 0x4(%r10), %r10
movl %r14d, (%r15,%r10,4)
movl 0x8(%rsi,%r9), %r9d
movslq 0x8(%rdi,%r11), %r10
movl %r9d, (%r15,%r10,4)
incl %r8d
jmp 0x2e43
jbe 0x2e47
movslq %r12d, %r9
incl %r12d
movq %r10, (%rbx,%r9,8)
incl %edx
jmp 0x2e4a
incl %r8d
cmpl %ecx, %r8d
jge 0x2e53
cmpl %eax, %edx
jl 0x2df8
cmpl %eax, %edx
jge 0x2e83
cltq
movslq %r12d, %rsi
movslq %edx, %rcx
subq %rcx, %rax
shlq $0x5, %rcx
addq 0x18(%r13), %rcx
leaq (%rbx,%rsi,8), %rsi
xorl %edx, %edx
movq %rcx, (%rsi,%rdx,8)
addq $0x20, %rcx
incq %rdx
cmpq %rdx, %rax
jne 0x2e70
addl %edx, %r12d
testl %r12d, %r12d
je 0x2f14
movl %r12d, %r10d
movq %rsp, %r14
leaq 0xf(,%r10,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movl %r12d, -0x54(%rbp)
testl %r12d, %r12d
jle 0x2f1c
xorl %eax, %eax
xorl %r12d, %r12d
movq -0x38(%rbp), %rdi
movq (%rbx,%rax,8), %rcx
movslq 0x4(%rcx), %rdx
cmpl $-0x1, (%r15,%rdx,4)
jne 0x2ee2
movq %rdx, %rsi
shlq $0x4, %rsi
addq (%r13), %rsi
movslq %r12d, %r12
movq %rsi, (%r14,%r12,8)
leal (%r12,%rdi), %esi
movl %esi, (%r15,%rdx,4)
incl %r12d
movslq 0x8(%rcx), %rcx
cmpl $-0x1, (%r15,%rcx,4)
jne 0x2f0a
movq %rcx, %rdx
shlq $0x4, %rdx
addq (%r13), %rdx
movslq %r12d, %r12
movq %rdx, (%r14,%r12,8)
leal (%r12,%rdi), %edx
movl %edx, (%r15,%rcx,4)
incl %r12d
incq %rax
cmpq %rax, %r10
jne 0x2eb6
jmp 0x2f23
xorl %r13d, %r13d
jmp 0x310b
xorl %r12d, %r12d
movq -0x38(%rbp), %rdi
testl %r12d, %r12d
movq %r10, -0x48(%rbp)
je 0x2f94
leal (%r12,%rdi), %eax
movq %rax, -0x60(%rbp)
movslq %eax, %rsi
shlq $0x4, %rsi
movq -0x30(%rbp), %rdi
callq 0x4625
movl %eax, %r13d
testl %eax, %eax
jne 0x310b
movq -0x60(%rbp), %r9
testl %r12d, %r12d
movq -0x30(%rbp), %r8
jle 0x2f87
movslq -0x38(%rbp), %rax
movl %r12d, %ecx
shlq $0x4, %rax
shlq $0x3, %rcx
xorl %edx, %edx
movq (%r8), %rsi
addq %rax, %rsi
movq (%r14,%rdx), %rdi
movups (%rdi), %xmm0
movups %xmm0, (%rsi,%rdx,2)
addq $0x8, %rdx
cmpq %rdx, %rcx
jne 0x2f6d
addl %r12d, 0x10(%r8)
movq -0x48(%rbp), %r10
movq %r9, %r14
jmp 0x2f9b
movl %edi, %r14d
movq -0x30(%rbp), %r8
movq -0x40(%rbp), %rax
testb $0x8, (%rax)
jne 0x2ff3
leal (%r14,%r14), %eax
movslq %eax, %rsi
shlq $0x2, %rsi
leaq -0x50(%rbp), %rdi
callq 0x45fc
movl %eax, %r13d
testl %eax, %eax
jne 0x310b
testl %r14d, %r14d
jle 0x2fe0
movq -0x50(%rbp), %rax
movl %r14d, %ecx
xorl %edx, %edx
movl $0x1, (%rax,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x2fd1
movslq %r14d, %r12
shlq $0x2, %r12
addq -0x50(%rbp), %r12
movq -0x30(%rbp), %r8
movq -0x48(%rbp), %r10
cmpl $0x0, -0x54(%rbp)
jle 0x30f1
movslq %r14d, %rax
shlq $0x2, %rax
movq %rax, -0x38(%rbp)
xorl %r14d, %r14d
movslq 0x20(%r8), %rax
testq %rax, %rax
jle 0x3052
movq 0x18(%r8), %rcx
movq (%rbx,%r14,8), %rdx
movslq 0x4(%rdx), %rsi
movl (%r15,%rsi,4), %esi
shlq $0x5, %rax
xorl %edi, %edi
cmpl %esi, 0x4(%rcx,%rdi)
jne 0x303f
movl 0x8(%rcx,%rdi), %r8d
movslq 0x8(%rdx), %r9
cmpl (%r15,%r9,4), %r8d
je 0x304a
addq $0x20, %rdi
cmpq %rdi, %rax
jne 0x302a
jmp 0x3052
movq $0x0, (%rbx,%r14,8)
movq -0x40(%rbp), %rax
testb $0x8, (%rax)
jne 0x30a7
movq (%rbx,%r14,8), %r13
testq %r13, %r13
je 0x30a7
movq -0x50(%rbp), %rsi
movq %r12, %rdi
movq -0x38(%rbp), %rdx
callq 0x10c0
movslq 0x8(%r13), %rax
movl (%r15,%rax,4), %edx
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %r12, %rcx
callq 0x26e5
movq -0x48(%rbp), %r10
movslq 0x4(%r13), %rax
movslq (%r15,%rax,4), %rax
cmpl $0x0, (%r12,%rax,4)
jne 0x30a7
movq $0x0, (%rbx,%r14,8)
movq (%rbx,%r14,8), %rax
testq %rax, %rax
je 0x30e1
movslq 0x4(%rax), %rcx
movl (%r15,%rcx,4), %edx
movslq 0x8(%rax), %rcx
movl (%r15,%rcx,4), %ecx
movsd 0x10(%rax), %xmm0
movl 0x18(%rax), %r8d
movl (%rax), %r9d
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x25dc
movq -0x48(%rbp), %r10
testl %eax, %eax
jne 0x30f6
incq %r14
cmpq %r10, %r14
movq -0x30(%rbp), %r8
jne 0x300b
xorl %r13d, %r13d
jmp 0x30f9
movl %eax, %r13d
movq -0x40(%rbp), %rax
testb $0x8, (%rax)
jne 0x310b
movq -0x50(%rbp), %rdi
callq 0x1030
movl %r13d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq