// File: /workspace/github_c_makefile_star2_O2/kernfel[P]fNEAT/network.c
// Function: connect_pNet
int connect_pNet( BinLeaf *leaf, struct Extraction_Params *eparams ) {
	int err=0;

	// Determine source and target coordinates
	double source[DIMENSIONS], target[DIMENSIONS];
	if ( eparams->outgoing ) {
		memcpy( source, eparams->ref, DIMENSIONS*sizeof *source );
		memcpy( target, leaf->x, DIMENSIONS*sizeof *target );
	} else {
		memcpy( source, leaf->x, DIMENSIONS*sizeof *source );
		memcpy( target, eparams->ref, DIMENSIONS*sizeof *target );
	}

	// Find the source and target nodes
	unsigned int i;
	int block=-1, index;
	pNode *source_pnode=0, *target_pnode=0, *n;
	for ( i=0; i<eparams->net->num_nodes; i++ ) {
		index = i % BLOCKSIZE_NODES;
		if ( ! index )
			block++;
		n = &eparams->net->p_nodes[block][index];
		if ( ! memcmp( source, n->x, DIMENSIONS*sizeof *source ) ) {
			source_pnode = n;
			if ( target_pnode )
				break;
		}
		if ( ! memcmp( target, n->x, DIMENSIONS*sizeof *source ) ) {
			target_pnode = n;
			if ( source_pnode )
				break;
		}
	}

	// Create what does not exist
	if ( ! source_pnode ) {
		if ( ! eparams->create_nodes )
			return 0;
		if (( err = add_pNode( eparams->net, source, &source_pnode ) ))
			return err;
	}
	if ( ! target_pnode ) {
		if ( ! eparams->create_nodes )
			return 0;
		if (( err = add_pNode( eparams->net, target, &target_pnode ) ))
			return err;
	}

	pLink *plink;
	if ( eparams->outgoing ) {
		// Unless the extraction algorithm is out of whack, no link comparison should be necessary, so skip it
		if (( err = add_pLink( eparams->net, source_pnode, target_pnode, &plink ) ))
			return err;
	} else {
		// Check whether the targeted node lies within the substrate space
		unsigned char out_of_bounds=0;
		for ( i=0; i<DIMENSIONS; i++ ) {
			if ( target[i] <= -0.5 || target[i] >= 0.5 ) {
				out_of_bounds = 1;
				break;
			}
		}
		if ( out_of_bounds ) {
			// There's no way extraction could have produced links here, skip the check
			if (( err = add_pLink( eparams->net, source_pnode, target_pnode, &plink ) ))
				return err;
		} else {
			// Target node lies within substrate space, there may already be links to it
			block=-1;
			pLink *l;
			for ( i=0; i<eparams->net->num_links; i++ ) {
				index = i % BLOCKSIZE_LINKS;
				if ( ! index )
					block++;
				l = &eparams->net->p_links[block][index];
				if ( l->from == source_pnode && l->to == target_pnode ) {
					return 0;
				}
			}

			// No such luck, add a new one
			if (( err = add_pLink( eparams->net, source_pnode, target_pnode, &plink ) ))
				return err;
		}
	}

	memcpy( plink->r, leaf->r, N_OUTPUTS*sizeof *plink->r );

	return err;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x40, %rsp
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x0, 0x18(%rsi)
je 0x4afc
movups 0x8(%r14), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x10(%rbx), %rax
jmp 0x4b09
movups 0x10(%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x8(%r14), %rax
movdqu (%rax), %xmm0
andq $0x0, 0x10(%rsp)
andq $0x0, 0x8(%rsp)
movdqa %xmm0, 0x20(%rsp)
movq 0x30(%r14), %rdi
movl (%rdi), %eax
pushq $-0x1
popq %rcx
xorl %r15d, %r15d
xorl %edx, %edx
xorl %esi, %esi
cmpl %esi, %eax
je 0x4ba5
movzbl %sil, %r8d
cmpl $0x1, %r8d
adcl $0x0, %ecx
movq 0x10(%rdi), %r9
movslq %ecx, %r10
movq (%r9,%r10,8), %r9
imulq $0x18, %r8, %r10
leaq (%r9,%r10), %r8
movdqu (%r9,%r10), %xmm0
pcmpeqb 0x30(%rsp), %xmm0
pmovmskb %xmm0, %r9d
cmpl $0xffff, %r9d           # imm = 0xFFFF
jne 0x4b78
movq %r8, 0x10(%rsp)
movq %r8, %rdx
testq %r15, %r15
jne 0x4ba2
movdqu (%r8), %xmm0
pcmpeqb 0x20(%rsp), %xmm0
pmovmskb %xmm0, %r9d
cmpl $0xffff, %r9d           # imm = 0xFFFF
jne 0x4b9e
movq %r8, 0x8(%rsp)
movq %r8, %r15
testq %rdx, %rdx
jne 0x4bad
incl %esi
jmp 0x4b2f
movq %r8, %rdx
testq %rdx, %rdx
je 0x4c1e
movq %r15, %r8
testq %r8, %r8
je 0x4c44
cmpb $0x0, 0x18(%r14)
jne 0x4bec
xorl %eax, %eax
movapd 0x1679(%rip), %xmm0     # 0x6240
movsd 0x1551(%rip), %xmm1     # 0x6120
cmpq $0x2, %rax
je 0x4c67
movsd 0x20(%rsp,%rax,8), %xmm2
andpd %xmm0, %xmm2
incq %rax
ucomisd %xmm1, %xmm2
jb 0x4bcf
movq 0x30(%r14), %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0x4a3a
testl %eax, %eax
jne 0x4cbd
movq 0x18(%rsp), %rax
movq 0x8(%rbx), %rcx
movq %rcx, 0x10(%rax)
jmp 0x4cbb
cmpb $0x0, 0x19(%r14)
je 0x4cbb
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x49b5
movq %r15, %r8
testl %eax, %eax
jne 0x4cbd
jmp 0x4bad
cmpb $0x0, 0x19(%r14)
je 0x4cbb
movq 0x30(%r14), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x49b5
testl %eax, %eax
jne 0x4cbd
jmp 0x4bb6
movq 0x30(%r14), %rdi
movl 0x20(%rdi), %eax
movq 0x10(%rsp), %rsi
pushq $-0x1
popq %rcx
xorl %r8d, %r8d
movq 0x8(%rsp), %rdx
cmpl %r8d, %eax
je 0x4bfa
movl %r8d, %r9d
andl $0x3ff, %r9d            # imm = 0x3FF
cmpl $0x1, %r9d
adcl $0x0, %ecx
movq 0x30(%rdi), %r10
movslq %ecx, %r11
movq (%r10,%r11,8), %r10
shll $0x5, %r9d
cmpq %rsi, (%r10,%r9)
jne 0x4cb6
addq %r9, %r10
cmpq %rdx, 0x8(%r10)
je 0x4cbb
incl %r8d
jmp 0x4c7e
xorl %eax, %eax
addq $0x40, %rsp
popq %rbx
popq %r14
popq %r15
retq