// File: /workspace/github_c_makefile_star2_O3/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)
movq %rdi, %r13
movl 0xc(%rdi), %eax
addl 0x8(%rdi), %eax
addl 0x10(%rdi), %eax
movq %rax, -0x38(%rbp)
movq %rsi, %r12
movl 0xc(%rsi), %eax
addl 0x8(%rsi), %eax
addl 0x10(%rsi), %eax
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
movq %rsp, %r15
subq %rcx, %r15
movq %r15, %rsp
testl %eax, %eax
jle 0x2ef9
decl %eax
leaq 0x4(,%rax,4), %rdx
movq %r15, %rdi
movl $0xff, %esi
callq 0x1060
movl 0x20(%r12), %eax
movl %eax, %ecx
movq %rsp, %rdx
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x30(%rbp)
movq %rdx, %rsp
movl 0x20(%r13), %ecx
testl %ecx, %ecx
setg %dl
testl %eax, %eax
setg %sil
andb %dl, %sil
xorl %edx, %edx
movl $0x0, %r14d
cmpb $0x1, %sil
jne 0x2fa8
movq 0x18(%r13), %rsi
movq 0x18(%r12), %rdi
xorl %r14d, %r14d
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), %ebx
cmpl %ebx, (%rsi,%r9)
jne 0x2f88
leaq (%rsi,%r9), %rbx
movl 0x4(%rbx), %ebx
movslq 0x4(%r10), %r10
movl %ebx, (%r15,%r10,4)
movl 0x8(%rsi,%r9), %r9d
movslq 0x8(%rdi,%r11), %r10
movl %r9d, (%r15,%r10,4)
incl %r8d
jmp 0x2f98
jbe 0x2f9c
movslq %r14d, %r9
incl %r14d
movq -0x30(%rbp), %r11
movq %r10, (%r11,%r9,8)
incl %edx
jmp 0x2f9f
incl %r8d
cmpl %ecx, %r8d
jge 0x2fa8
cmpl %eax, %edx
jl 0x2f4a
cmpl %eax, %edx
jge 0x2fdd
cltq
movslq %r14d, %rsi
movslq %edx, %rcx
subq %rcx, %rax
shlq $0x5, %rcx
addq 0x18(%r12), %rcx
movq -0x30(%rbp), %rdx
leaq (%rdx,%rsi,8), %rsi
xorl %edx, %edx
movq %rcx, (%rsi,%rdx,8)
addq $0x20, %rcx
incq %rdx
cmpq %rdx, %rax
jne 0x2fca
addl %edx, %r14d
movq -0x38(%rbp), %rdi
testl %r14d, %r14d
je 0x3346
movl %r14d, %r10d
movl %r14d, %ecx
movq %rsp, %r14
leaq 0xf(,%r10,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movl %ecx, -0x54(%rbp)
testl %ecx, %ecx
movq %r10, -0x48(%rbp)
jle 0x30d7
xorl %eax, %eax
xorl %ebx, %ebx
movq -0x30(%rbp), %rcx
movq (%rcx,%rax,8), %rcx
movslq 0x4(%rcx), %rdx
cmpl $-0x1, (%r15,%rdx,4)
jne 0x3046
movq %rdx, %rsi
shlq $0x4, %rsi
addq (%r12), %rsi
movslq %ebx, %rbx
movq %rsi, (%r14,%rbx,8)
leal (%rbx,%rdi), %esi
movl %esi, (%r15,%rdx,4)
incl %ebx
movslq 0x8(%rcx), %rcx
cmpl $-0x1, (%r15,%rcx,4)
jne 0x306c
movq %rcx, %rdx
shlq $0x4, %rdx
addq (%r12), %rdx
movslq %ebx, %rbx
movq %rdx, (%r14,%rbx,8)
leal (%rbx,%rdi), %edx
movl %edx, (%r15,%rcx,4)
incl %ebx
incq %rax
cmpq %rax, %r10
jne 0x3018
testl %ebx, %ebx
je 0x30d7
leal (%rbx,%rdi), %r12d
movslq %r12d, %rsi
shlq $0x4, %rsi
movq %r13, %rdi
callq 0x480d
movq %r13, %rcx
movl %eax, %r13d
testl %eax, %eax
jne 0x3360
testl %ebx, %ebx
movq %rcx, %r13
jle 0x30cd
movslq -0x38(%rbp), %rax
movl %ebx, %ecx
shlq $0x4, %rax
shlq $0x3, %rcx
xorl %edx, %edx
movq (%r13), %rsi
addq %rax, %rsi
movq (%r14,%rdx), %rdi
movdqu (%rdi), %xmm0
movdqu %xmm0, (%rsi,%rdx,2)
addq $0x8, %rdx
cmpq %rdx, %rcx
jne 0x30b0
addl %ebx, 0x10(%r13)
movq -0x48(%rbp), %r10
jmp 0x30da
movl %edi, %r12d
movq -0x40(%rbp), %rax
testb $0x8, (%rax)
movl -0x54(%rbp), %r14d
jne 0x322d
movq %r13, %rbx
leal (%r12,%r12), %eax
movslq %eax, %rsi
shlq $0x2, %rsi
leaq -0x50(%rbp), %rdi
callq 0x47e4
movl %eax, %r13d
testl %eax, %eax
jne 0x3360
movq -0x50(%rbp), %rax
testl %r12d, %r12d
movq %rbx, %r13
movq -0x48(%rbp), %r10
jle 0x3222
movl %r12d, %edx
leal 0x3(%r12), %ecx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl $0x2, %ecx
shlq $0x4, %rcx
movdqa 0x410b(%rip), %xmm1     # 0x7250
movdqa 0x4113(%rip), %xmm2     # 0x7260
xorl %edx, %edx
movdqa 0x4119(%rip), %xmm3     # 0x7270
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x4119(%rip), %xmm5     # 0x7280
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %esi
notl %esi
testb $0x1, %sil
je 0x31a3
movl $0x1, (%rax,%rdx)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0x31ba
movl $0x1, 0x4(%rax,%rdx)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %esi
testb $0x1, %sil
je 0x31fa
movl $0x1, 0x8(%rax,%rdx)
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0x320d
movl $0x1, 0xc(%rax,%rdx)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x3167
movslq %r12d, %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, -0x60(%rbp)
testl %r14d, %r14d
jle 0x3341
movslq %r12d, %rax
shlq $0x2, %rax
movq %rax, -0x38(%rbp)
xorl %ebx, %ebx
movslq 0x20(%r13), %rax
testq %rax, %rax
jle 0x3292
movq 0x18(%r13), %rcx
movq -0x30(%rbp), %rdx
movq (%rdx,%rbx,8), %rdx
movslq 0x4(%rdx), %rsi
movl (%r15,%rsi,4), %esi
shlq $0x5, %rax
xorl %edi, %edi
cmpl %esi, 0x4(%rcx,%rdi)
jne 0x327b
movl 0x8(%rcx,%rdi), %r8d
movslq 0x8(%rdx), %r9
cmpl (%r15,%r9,4), %r8d
je 0x3286
addq $0x20, %rdi
cmpq %rdi, %rax
jne 0x3266
jmp 0x3292
movq -0x30(%rbp), %rax
movq $0x0, (%rax,%rbx,8)
movq %r13, %r12
movq -0x40(%rbp), %rax
testb $0x8, (%rax)
movq -0x30(%rbp), %rax
movq (%rax,%rbx,8), %r14
jne 0x32fc
testq %r14, %r14
je 0x3332
movq -0x50(%rbp), %rsi
movq -0x60(%rbp), %r13
movq %r13, %rdi
movq -0x38(%rbp), %rdx
callq 0x10c0
movslq 0x8(%r14), %rax
movl (%r15,%rax,4), %edx
movq %r12, %rdi
movq -0x40(%rbp), %rsi
movq %r13, %rcx
callq 0x2788
movq -0x48(%rbp), %r10
movslq 0x4(%r14), %rax
movslq (%r15,%rax,4), %rax
cmpl $0x0, (%r13,%rax,4)
jne 0x3301
movq -0x30(%rbp), %rax
movq $0x0, (%rax,%rbx,8)
jmp 0x3332
testq %r14, %r14
je 0x3332
movslq 0x4(%r14), %rax
movl (%r15,%rax,4), %edx
movslq 0x8(%r14), %rax
movl (%r15,%rax,4), %ecx
movq 0x10(%r14), %xmm0
movl 0x18(%r14), %r8d
movl (%r14), %r9d
movq %r12, %rdi
movq -0x40(%rbp), %rsi
callq 0x2696
movq -0x48(%rbp), %r10
testl %eax, %eax
jne 0x334b
incq %rbx
cmpq %r10, %rbx
movq %r12, %r13
jne 0x3243
xorl %r13d, %r13d
jmp 0x334e
xorl %r13d, %r13d
jmp 0x3360
movl %eax, %r13d
movq -0x40(%rbp), %rax
testb $0x8, (%rax)
jne 0x3360
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