// File: /workspace/github_c_makefile_star2_O3/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 0x5149
movups 0x8(%r14), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x10(%rbx), %rax
jmp 0x5156
movups 0x10(%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x8(%r14), %rax
movdqu (%rax), %xmm0
movdqa %xmm0, 0x20(%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movq 0x30(%r14), %rdi
movl (%rdi), %ecx
movl $0x0, %r15d
testl %ecx, %ecx
je 0x5208
movq 0x10(%rdi), %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
xorl %r8d, %r8d
movzbl %al, %r10d
cmpl $0x1, %r10d
adcl $0x0, %esi
movslq %esi, %r11
movq (%rdx,%r11,8), %r11
leaq (%r10,%r10,2), %r10
leaq (%r11,%r10,8), %r15
movdqu (%r15), %xmm0
pcmpeqb 0x30(%rsp), %xmm0
pmovmskb %xmm0, %r10d
cmpl $0xffff, %r10d          # imm = 0xFFFF
jne 0x51cf
movq %r15, 0x10(%rsp)
movq %r15, %r9
testq %r8, %r8
jne 0x51fd
movdqu (%r15), %xmm0
pcmpeqb 0x20(%rsp), %xmm0
pmovmskb %xmm0, %r10d
cmpl $0xffff, %r10d          # imm = 0xFFFF
jne 0x51f5
movq %r15, 0x8(%rsp)
movq %r15, %r8
testq %r9, %r9
jne 0x522a
incl %eax
cmpl %eax, %ecx
jne 0x518f
jmp 0x5200
movq %r15, %r9
movq %r8, %r15
testq %r9, %r9
jne 0x522a
cmpb $0x0, 0x19(%r14)
je 0x532a
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x5001
testl %eax, %eax
jne 0x532c
testq %r15, %r15
je 0x5268
cmpb $0x0, 0x18(%r14)
je 0x5290
movq 0x30(%r14), %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0x5087
testl %eax, %eax
jne 0x532c
movq 0x18(%rsp), %rax
movq 0x8(%rbx), %rcx
movq %rcx, 0x10(%rax)
jmp 0x532a
cmpb $0x0, 0x19(%r14)
je 0x532a
movq 0x30(%r14), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x5001
testl %eax, %eax
jne 0x532c
jmp 0x522f
leaq 0x28(%rsp), %rax
leaq 0x20(%rsp), %rdx
movb $0x1, %cl
movapd 0x1fec(%rip), %xmm0     # 0x7290
movsd 0x1e9c(%rip), %xmm1     # 0x7148
movsd (%rdx), %xmm2
andpd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x5236
movq %rax, %rdx
testb $0x1, %cl
movl $0x0, %ecx
jne 0x52ac
movq 0x30(%r14), %rdi
movl 0x20(%rdi), %eax
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
testl %eax, %eax
je 0x5244
movq 0x30(%rdi), %rcx
xorl %r8d, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %r8d, %r10d
andl $0x3ff, %r10d           # imm = 0x3FF
cmpl $0x1, %r10d
adcl $0x0, %r9d
movslq %r9d, %r11
movq (%rcx,%r11,8), %r11
shll $0x5, %r10d
cmpq %rsi, (%r11,%r10)
jne 0x531d
addq %r10, %r11
cmpq %rdx, 0x8(%r11)
je 0x532a
incl %r8d
cmpl %r8d, %eax
jne 0x52f1
jmp 0x5244
xorl %eax, %eax
addq $0x40, %rsp
popq %rbx
popq %r14
popq %r15
retq