// File: /workspace/github_c_makefile_star2_O2/kernfel[P]fNEAT/network-static.c
// Function: build_eNetwork
int build_eNetwork( eNetwork *e, pNetwork *p, struct NEAT_Params *params, struct Implementation_Params *iparams ) {
	int err=0;

	if (( err = Realloc( e->nodes, p->num_used_nodes*sizeof *e->nodes ) )
	 || ( err = Realloc( e->inputs, p->num_inputs*sizeof *e->inputs ) )
	 || ( err = Realloc( e->outputs, p->num_outputs*sizeof *e->outputs ) )
	 || ( err = Realloc( e->links, p->num_used_links*sizeof *e->links ) )) {
		free( e->nodes );
		free( e->inputs );
		free( e->outputs );
		return err;
	}
	e->num_nodes = p->num_used_nodes;
	e->num_inputs = p->num_inputs;
	e->num_outputs = p->num_outputs;
	e->num_links = p->num_used_links;

	// Assign eNode indices to the pNodes
	int block=-1, index;
	unsigned int i, j, node_iter=0;
	pNode *n;
	for ( i=0; i<p->num_nodes; i++ ) {
		index = i % BLOCKSIZE_NODES;
		if ( ! index )
			block++;
		n = &p->p_nodes[block][index];

		if ( i < e->num_inputs ) {
			if ( n->used )
				e->inputs[i] = e->nodes + node_iter;
			else
				e->inputs[i] = 0;
		} else if ( i < e->num_inputs+e->num_outputs ) {
			if ( n->used )
				e->outputs[i-e->num_inputs] = e->nodes + node_iter;
			else
				e->outputs[i-e->num_inputs] = 0;
		}

		if ( n->used )
			n->index = node_iter++;
	}
	
	// Group links by target node
	pLink *l;
	eLink *next_link=e->links;
	eNode *next_node=e->nodes;
	double stretch = iparams->weight_range/(1-params->expression_thresholds[0]), offset = params->expression_thresholds[0] * stretch;
	int l_block, l_index;
	block = -1;
	for ( i=0; i<p->num_nodes; i++ ) {
		index = i % BLOCKSIZE_NODES;
		if ( ! index )
			block++;
		n = &p->p_nodes[block][index];
		if ( n->used ) {
			next_node->num_inputs = 0;
			l_block = -1;
			for ( j=0; j<p->num_links; j++ ) {
				l_index = j % BLOCKSIZE_LINKS;
				if ( ! l_index )
					l_block++;
				l = &p->p_links[l_block][l_index];
				if ( l->used && l->to == n) {
					next_link->w = l->r[0]*stretch - offset;
					next_link->from = e->nodes + l->from->index;
					next_node->num_inputs++;
					next_link++;
				}
			}
			next_node++;
		}
	}

	flush(e);

	return err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r12
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8(%rdi), %r13
movl 0x4(%rsi), %esi
shlq $0x4, %rsi
movq %r13, %rdi
callq 0x4231
testl %eax, %eax
jne 0x5c07
movq %rbp, 0x20(%rsp)
leaq 0x18(%rbx), %rbp
movslq 0x18(%r14), %rsi
shlq $0x3, %rsi
movq %rbp, %rdi
callq 0x4231
testl %eax, %eax
jne 0x5c07
movq %r12, 0x18(%rsp)
leaq 0x20(%rbx), %r12
movslq 0x1c(%r14), %rsi
shlq $0x3, %rsi
movq %r12, %rdi
callq 0x4231
testl %eax, %eax
jne 0x5c07
movq %r12, 0x10(%rsp)
leaq 0x30(%rbx), %r12
movl 0x24(%r14), %esi
shlq $0x4, %rsi
movq %r12, %rdi
callq 0x4231
testl %eax, %eax
je 0x5c37
movl %eax, %r15d
movq 0x8(%rbx), %rdi
callq 0x1030
movq 0x18(%rbx), %rdi
callq 0x1030
movq 0x20(%rbx), %rdi
callq 0x1030
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%r14), %eax
movl %eax, (%rbx)
movl 0x18(%r14), %ecx
movl %ecx, 0x10(%rbx)
movl 0x1c(%r14), %edx
movl %edx, 0x14(%rbx)
movl 0x24(%r14), %eax
movl %eax, 0x28(%rbx)
movl (%r14), %eax
movq %rax, 0x8(%rsp)
addl %ecx, %edx
movl %ecx, %eax
negl %eax
movq %rax, (%rsp)
pushq $-0x1
popq %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq 0x10(%rsp), %rax
cmpq %r8, 0x8(%rsp)
je 0x5d0e
cmpb $0x1, %r8b
adcl $0x0, %edi
movq 0x10(%r14), %r11
movslq %edi, %r15
movzbl %r8b, %r10d
imulq $0x18, %r10, %r10
addq (%r11,%r15,8), %r10
movl %r9d, %r11d
cmpq %rcx, %r8
jae 0x5cb8
cmpb $0x0, 0x10(%r10)
je 0x5cec
shlq $0x4, %r11
addq (%r13), %r11
movq (%rbp), %r15
movq %r11, (%r15,%r8,8)
jmp 0x5ce3
movb 0x10(%r10), %r15b
cmpq %rdx, %r8
jae 0x5cde
testb %r15b, %r15b
je 0x5cf7
shlq $0x4, %r11
addq (%r13), %r11
movq (%rax), %r15
movq (%rsp), %rsi
addl %r8d, %esi
movq %r11, (%r15,%rsi,8)
jmp 0x5ce3
testb %r15b, %r15b
je 0x5d06
movl %r9d, 0x14(%r10)
incl %r9d
jmp 0x5d06
movq (%rbp), %r10
andq $0x0, (%r10,%r8,8)
jmp 0x5d06
movq (%rax), %rsi
movq (%rsp), %r10
addl %r8d, %r10d
andq $0x0, (%rsi,%r10,8)
incq %r8
jmp 0x5c72
movq (%r12), %rcx
movq (%r13), %rdx
movq 0x18(%rsp), %rax
movsd (%rax), %xmm0
movq 0x20(%rsp), %rax
movsd 0x30(%rax), %xmm1
movsd 0x3df(%rip), %xmm2      # 0x6110
subsd %xmm1, %xmm2
divsd %xmm2, %xmm0
xorpd 0x67f(%rip), %xmm1      # 0x63c0
mulsd %xmm0, %xmm1
pushq $-0x1
popq %r8
xorl %edi, %edi
movq %rdx, %r9
cmpl 0x8(%rsp), %edi
je 0x5e06
movzbl %dil, %esi
cmpl $0x1, %esi
adcl $0x0, %r8d
movq 0x10(%r14), %r10
movslq %r8d, %r11
movq (%r10,%r11,8), %r10
imulq $0x18, %rsi, %rsi
cmpb $0x0, 0x10(%r10,%rsi)
je 0x5dff
addq %rsi, %r10
andl $0x0, 0x8(%r9)
movl 0x20(%r14), %r11d
xorl %ebp, %ebp
xorl %r15d, %r15d
pushq $-0x1
popq %r12
cmpl %r15d, %r11d
je 0x5dfb
movl %r15d, %esi
andl $0x3ff, %esi            # imm = 0x3FF
cmpl $0x1, %esi
adcl $0x0, %r12d
movq 0x30(%r14), %r13
movslq %r12d, %rax
movq (%r13,%rax,8), %r13
shll $0x5, %esi
cmpb $0x0, 0x18(%r13,%rsi)
je 0x5df6
addq %rsi, %r13
cmpq %r10, 0x8(%r13)
jne 0x5df6
movsd 0x10(%r13), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, (%rcx)
movq (%r13), %rax
movl 0x14(%rax), %eax
shlq $0x4, %rax
addq %rdx, %rax
movq %rax, 0x8(%rcx)
incl %ebp
movl %ebp, 0x8(%r9)
addq $0x10, %rcx
incl %r15d
jmp 0x5d93
addq $0x10, %r9
incl %edi
jmp 0x5d4e
movq %rbx, %rdi
callq 0x5e16
xorl %r15d, %r15d
jmp 0x5c25