// File: /workspace/github_c_makefile_star2_O1/kernfel[P]fNEAT/extract.c
// Function: extract_tree
int extract_tree( BinLeaf *p, struct Extraction_Params *eparams ) {
	int i, j, err=0;
	// If the children c of p do not have children gc, that means that either:
	// (a) c weren't recursed into because the variance threshold wasn't met
	//	=> c's are not different enough to warrant expressing
	//	=> express p and move on
	// (b) c were at highest resolution
	//	=> check variance over all c to decide whether to express them or not
	// Since variance is not calculated recursively, this process may leave a few fragments from min_resolution.
	if ( p->c \
	 && ( p->c[0].c \
	   || ( p->level == eparams->params->max_resolution-1 && get_binleaf_variance(p, eparams->params) > eparams->params->variance_threshold ) ) ) {
		for ( i=0; i<CLUSTERSIZE; i++ ) {
			if (( err = extract_tree( &p->c[i], eparams ) ))
				return err;
		}
	} else {
		// Check expression level
		for ( i=0; i<N_OUTPUTS; i++ )
			if ( fabs(p->r[i]) < eparams->params->expression_thresholds[i] )
				return err;

		// Band pruning: See whether I'm in a slope of some sort
		double w = 1.0 / (1<<p->level);
		double dband=0;
		for ( i=0; i<DIMENSIONS; i++ ) {
			double neighbour[DIMENSIONS], left[N_OUTPUTS], right[N_OUTPUTS];
			memcpy( neighbour, p->x, DIMENSIONS*sizeof *neighbour );

			// Left neighbour
			neighbour[i] = p->x[i] - w;
			get_activation_at( neighbour, left, eparams );
			// Right neighbour
			neighbour[i] = p->x[i] + w;
			get_activation_at( neighbour, right, eparams );
			
			// Calculate weighted difference
			double dleft=0, dright=0;
			for ( j=0; j<N_OUTPUTS; j++ ) {
				dleft += eparams->params->output_bandpruning_weight[j] * fabs(p->r[j] - left[j]);
				dright += eparams->params->output_bandpruning_weight[j] * fabs(p->r[j] - right[j]);
			}
			double ddim = dleft < dright ? dleft : dright;
			if ( ddim > dband )
				dband = ddim;
		}

		// Add a new connection
		if ( dband > eparams->params->band_threshold ) {
			if (( err = connect_pNet( p, eparams ) ))
				return err;
		}
	}

	return err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq (%rdi), %rax
testq %rax, %rax
je 0x4877
cmpq $0x0, (%rax)
je 0x4854
xorl %r15d, %r15d
movq (%r14), %rdi
addq %r15, %rdi
movq %rbx, %rsi
callq 0x4805
testl %eax, %eax
jne 0x4999
addq $0x28, %r15
cmpq $0xa0, %r15
jne 0x482a
xorl %ebp, %ebp
jmp 0x499b
movq 0x28(%rbx), %r15
movl 0x8(%r15), %eax
decl %eax
cmpl %eax, 0x20(%r14)
jne 0x4877
movq %r14, %rdi
movq %r15, %rsi
callq 0x49eb
ucomisd 0x10(%r15), %xmm0
ja 0x4827
movsd 0x8(%r14), %xmm0
andpd 0x28fb(%rip), %xmm0     # 0x7180
movq 0x28(%rbx), %rax
movsd 0x30(%rax), %xmm1
xorl %ebp, %ebp
ucomisd %xmm0, %xmm1
ja 0x499b
movb 0x20(%r14), %cl
movl $0x1, %eax
shll %cl, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x278c(%rip), %xmm1     # 0x7040
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
leaq 0x10(%r14), %r12
movl $0x2, %r13d
leaq 0x10(%rsp), %r15
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movups (%r12), %xmm0
movaps %xmm0, 0x10(%rsp)
movsd (%r14,%r13,8), %xmm0
subsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rsp,%r13,8)
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
movq %rbx, %rdx
callq 0x4a5a
movsd (%r14,%r13,8), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rsp,%r13,8)
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
movq %rbx, %rdx
callq 0x4a5a
movq 0x28(%rbx), %rax
movsd 0x28(%rax), %xmm1
movsd 0x8(%r14), %xmm2
movapd %xmm2, %xmm0
subsd 0x30(%rsp), %xmm0
movapd 0x283a(%rip), %xmm3     # 0x7180
andpd %xmm3, %xmm0
subsd 0x28(%rsp), %xmm2
andpd %xmm3, %xmm2
mulsd %xmm1, %xmm0
xorpd %xmm3, %xmm3
addsd %xmm3, %xmm0
mulsd %xmm1, %xmm2
addsd %xmm3, %xmm2
minsd %xmm2, %xmm0
maxsd 0x20(%rsp), %xmm0
incq %r13
cmpq $0x3, %r13
je 0x48d1
movq 0x28(%rbx), %rax
ucomisd 0x20(%rax), %xmm0
jbe 0x499b
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4f2c
testl %eax, %eax
je 0x499b
movl %eax, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq