// File: /workspace/github_c_makefile_star2_O1/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 %r14
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x0, 0x18(%rsi)
je 0x4f4f
movups 0x8(%r14), %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0x10(%rbx), %rax
jmp 0x4f5c
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
testl %ecx, %ecx
je 0x4ffb
movq 0x8(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r8
movzbl %al, %r9d
cmpl $0x1, %r9d
adcl $0x0, %edx
movq 0x10(%rdi), %r10
movslq %edx, %r11
movq (%r10,%r11,8), %r10
leaq (%r9,%r9,2), %r9
leaq (%r10,%r9,8), %r9
movdqu (%r9), %xmm0
pcmpeqb 0x30(%rsp), %xmm0
pmovmskb %xmm0, %r10d
cmpl $0xffff, %r10d          # imm = 0xFFFF
jne 0x4fcf
movq %r9, 0x10(%rsp)
movq %r9, %r8
testq %rsi, %rsi
jne 0x4ffb
movdqu (%r9), %xmm0
pcmpeqb 0x20(%rsp), %xmm0
pmovmskb %xmm0, %r10d
cmpl $0xffff, %r10d          # imm = 0xFFFF
jne 0x4ff5
movq %r9, 0x8(%rsp)
movq %r9, %rsi
testq %r8, %r8
jne 0x4ffb
incl %eax
cmpl %eax, %ecx
jne 0x4f8b
cmpq $0x0, 0x10(%rsp)
je 0x5039
cmpq $0x0, 0x8(%rsp)
je 0x5059
cmpb $0x0, 0x18(%r14)
je 0x5084
movq 0x30(%r14), %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0x4e91
testl %eax, %eax
jne 0x515c
xorl %eax, %eax
jmp 0x514f
cmpb $0x0, 0x19(%r14)
je 0x507d
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x4e0d
testl %eax, %eax
jne 0x515c
jmp 0x5003
cmpb $0x0, 0x19(%r14)
je 0x507d
movq 0x30(%r14), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x4e0d
testl %eax, %eax
jne 0x515c
jmp 0x500b
xorl %eax, %eax
jmp 0x515c
xorl %eax, %eax
movapd 0x20f2(%rip), %xmm0     # 0x7180
movsd 0x20da(%rip), %xmm1     # 0x7170
movsd 0x20(%rsp,%rax,8), %xmm2
andpd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jae 0x5123
incq %rax
cmpq $0x1, %rax
je 0x5096
movq 0x30(%r14), %rdi
movl 0x20(%rdi), %eax
testl %eax, %eax
je 0x5102
movq 0x30(%rdi), %rcx
movq 0x10(%rsp), %rdx
xorl %esi, %esi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %r9
movl %esi, %r10d
andl $0x3ff, %r10d           # imm = 0x3FF
cmpl $0x1, %r10d
adcl $0x0, %r8d
movslq %r8d, %r11
movq (%rcx,%r11,8), %r11
shll $0x5, %r10d
cmpq %rdx, (%r11,%r10)
jne 0x50fc
addq %r10, %r11
cmpq %r9, 0x8(%r11)
je 0x5164
incl %esi
cmpl %esi, %eax
jne 0x50d0
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0x4e91
testl %eax, %eax
sete %cl
testb %cl, %cl
jne 0x5149
xorl %edx, %edx
jmp 0x514b
movq 0x30(%r14), %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
leaq 0x18(%rsp), %rcx
callq 0x4e91
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0x5149
xorl %edx, %edx
movl %ecx, %eax
jmp 0x514b
movb $0x1, %dl
testb %dl, %dl
je 0x515c
movq 0x18(%rsp), %rcx
movq 0x8(%rbx), %rdx
movq %rdx, 0x10(%rcx)
addq $0x48, %rsp
popq %rbx
popq %r14
retq
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x511b