// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %rbp
leaq 0x8(%rdi), %r12
movl 0x4(%rsi), %esi
shlq $0x4, %rsi
movq %r12, %rdi
callq 0x4625
testl %eax, %eax
jne 0x624c
movq %r13, 0x20(%rsp)
leaq 0x18(%rbp), %r13
movslq 0x18(%rbx), %rsi
shlq $0x3, %rsi
movq %r13, %rdi
callq 0x4625
testl %eax, %eax
jne 0x624c
movq %r15, 0x18(%rsp)
leaq 0x20(%rbp), %r15
movslq 0x1c(%rbx), %rsi
shlq $0x3, %rsi
movq %r15, %rdi
callq 0x4625
testl %eax, %eax
jne 0x624c
movq %r15, 0x10(%rsp)
leaq 0x30(%rbp), %rdi
movl 0x24(%rbx), %esi
shlq $0x4, %rsi
movq %rdi, %r15
callq 0x4625
testl %eax, %eax
je 0x627c
movl %eax, %r14d
movq 0x8(%rbp), %rdi
callq 0x1030
movq 0x18(%rbp), %rdi
callq 0x1030
movq 0x20(%rbp), %rdi
callq 0x1030
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, (%rsp)
movl 0x4(%rbx), %eax
movq %rax, 0x8(%rsp)
movl %eax, (%rbp)
movl 0x18(%rbx), %edx
movl %edx, 0x10(%rbp)
movl 0x1c(%rbx), %esi
movl %esi, 0x14(%rbp)
movl 0x24(%rbx), %eax
movl %eax, 0x28(%rbp)
movl (%rbx), %ecx
testq %rcx, %rcx
je 0x635a
addl %edx, %esi
movl %edx, %edi
negl %edi
xorl %r8d, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movzbl %r8b, %eax
cmpq $0x1, %rax
adcl $0x0, %r9d
movq 0x10(%rbx), %r14
movslq %r9d, %rbp
leaq (%rax,%rax,2), %r11
shll $0x3, %r11d
addq (%r14,%rbp,8), %r11
cmpq %rdx, %r8
jae 0x62fa
cmpb $0x0, 0x10(%r11)
je 0x6323
movl %r10d, %eax
shlq $0x4, %rax
addq (%r12), %rax
movq (%r13), %r14
movq %rax, (%r14,%r8,8)
jmp 0x6340
cmpq %rsi, %r8
jae 0x6340
cmpb $0x0, 0x10(%r11)
movq 0x10(%rsp), %r14
je 0x6331
movl %r10d, %eax
shlq $0x4, %rax
addq (%r12), %rax
movq (%r14), %r14
leal (%rdi,%r8), %ebp
movq %rax, (%r14,%rbp,8)
jmp 0x6340
movq (%r13), %rax
movq $0x0, (%rax,%r8,8)
jmp 0x6340
movq (%r14), %rax
leal (%rdi,%r8), %r14d
movq $0x0, (%rax,%r14,8)
cmpb $0x0, 0x10(%r11)
je 0x634e
movl %r10d, 0x14(%r11)
incl %r10d
incq %r8
cmpq %r8, %rcx
jne 0x62ba
movq (%r12), %rdx
testq %rcx, %rcx
movq (%rsp), %rsi
je 0x645d
movq 0x18(%rsp), %rax
movsd (%rax), %xmm0
movq 0x20(%rsp), %rax
movsd 0x30(%rax), %xmm1
movsd 0xcba(%rip), %xmm2      # 0x7040
subsd %xmm1, %xmm2
divsd %xmm2, %xmm0
movq (%rsi), %rsi
movq 0x10(%rbx), %rdi
xorpd 0xf43(%rip), %xmm1      # 0x72e0
mulsd %xmm0, %xmm1
xorl %r8d, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movq %rdx, %r10
movzbl %r8b, %r11d
cmpl $0x1, %r11d
adcl $0x0, %r9d
movslq %r9d, %rax
movq (%rdi,%rax,8), %rax
leaq (%r11,%r11,2), %r14
cmpb $0x0, 0x10(%rax,%r14,8)
je 0x6451
movl $0x0, 0x8(%r10)
movl 0x20(%rbx), %r11d
testl %r11d, %r11d
je 0x644d
leaq (%rax,%r14,8), %r14
movq 0x30(%rbx), %r15
xorl %ebp, %ebp
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %ebp, %eax
andl $0x3ff, %eax            # imm = 0x3FF
cmpl $0x1, %eax
adcl $0x0, %r12d
movslq %r12d, %r13
movq (%r15,%r13,8), %r13
shll $0x5, %eax
cmpb $0x0, 0x18(%r13,%rax)
je 0x6446
addq %rax, %r13
cmpq %r14, 0x8(%r13)
jne 0x6446
movsd 0x10(%r13), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, (%rsi)
movq (%r13), %rax
movl 0x14(%rax), %eax
shlq $0x4, %rax
addq %rdx, %rax
movq %rax, 0x8(%rsi)
incl 0x8(%r10)
addq $0x10, %rsi
incl %ebp
cmpl %ebp, %r11d
jne 0x63f1
addq $0x10, %r10
incl %r8d
cmpl %ecx, %r8d
jne 0x63ad
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0x6486
shlq $0x4, %rcx
xorl %r14d, %r14d
xorl %eax, %eax
movq $0x0, (%rdx,%rax)
addq $0x10, %rax
cmpq %rax, %rcx
jne 0x6470
jmp 0x626a
xorl %r14d, %r14d
jmp 0x626a