// File: /workspace/github_cpp_makefile_star2_O2/big-data-lab-team[P]OrpailleCC/./src/mondrian_coarse.hpp
// Function: CoarseMondrianForest<double, funct, KappaMetrics<2, false>, 4, 2, 6000>::extend_block4(int, int, double const*, int, int)
void extend_block4(int const node_id, int const tree_id, feature_type const* features, int const label, int const forced_extend_sum=0){
	//e_lower and e_upper are used to compute probabilities
	feature_type e_lower[feature_count], e_upper[feature_count];
	double probabilities[feature_count];
	Node& node = nodes()[node_id];
	int const parent_id = node.parent;
	double const parent_tau = node.parent >= 0 ? nodes()[node.parent].tau : 0; //The tau value of the parent of the root is 0
	//sum is used as a parameter to pick random numbers following exponential law
	feature_type sum = 0;
	//compute e_lower, e_upper and sum
	for(int i = 0; i < feature_count; ++i){
		e_lower[i] = node.bound_lower[i] - features[i] > 0 ? node.bound_lower[i] - features[i] : 0;
		e_upper[i] = features[i] - node.bound_upper[i] > 0 ? features[i] - node.bound_upper[i] : 0;
		probabilities[i] = e_lower[i] + e_upper[i];
		sum += e_lower[i] + e_upper[i];
	}
	//Pick a random number following an exponential law of parameter *sum* (except if sum is 0)
	double const E = sum == 0 ?  -1 : Utils::rand_exponential<func>(sum);
	bool update_box = false;

	bool pause_expension = false;
	if((tree_management == COBBLE_MANAGEMENT || tree_management == OPTIMISTIC_COBBLE_MANAGEMENT)){
		int const remaining_depth = node_depth(node_id, nullptr);
		int const distance_to_root = unravel(node_id);
		pause_expension = remaining_depth + distance_to_root + 1 > tree_bases()[tree_id].node_count_limit;
	}
	else{
		pause_expension = tree_bases()[tree_id].is_paused(tree_management);
		//PAUSE
		//pause_expension = ((total_count >= 20 && total_count <= 150));
	}

	if(E >= 0 && parent_tau + E < node.tau && node_available >= 2 && !pause_expension){//Introduce a new parent and a new sibling
		bool split_instead = false;
		//If forced_extend_sum is higher than 0, then we consider spliting
		if(forced_extend_sum > 0){
			double const probability = static_cast<double>(node.forced_extend) / static_cast<double>(forced_extend_sum);
			split_instead = (func::rand_uniform() <  probability);
		}

		if(fe_split_trigger == SPLIT_TRIGGER_NONE){
			extend_node(node_id, tree_id, features, label, parent_id, parent_tau, probabilities, E, sum);
		}
		else if(fe_split_trigger == SPLIT_TRIGGER_POSITIVE && node.forced_extend > 0){
			split_node(node_id, tree_id, features, label, parent_id, parent_tau, probabilities, E);
		}
		else if((fe_split_trigger == SPLIT_TRIGGER_TOTAL || fe_split_trigger == SPLIT_TRIGGER_SFE) && split_instead){
			split_node(node_id, tree_id, features, label, parent_id, parent_tau, probabilities, E);
		}
		else{
			extend_node(node_id, tree_id, features, label, parent_id, parent_tau, probabilities, E, sum);
		}
	}
	else{ //Otherwise, just update the box
		//update lower bound and upper bound of this node
		for(int i = 0; i < feature_count; ++i){
			if(node.bound_lower[i] > features[i]){
				double const difference = (node.bound_lower[i] - features[i]) * fe_parameter;
				node.bound_lower[i] -= difference;
			}
			if(node.bound_upper[i] < features[i]){
				double const difference = (features[i] - node.bound_upper[i]) * fe_parameter;
				node.bound_upper[i] += difference;
			}
		}
		//Check if it's a forced extend
		if(E >= 0 && (parent_tau + E < node.tau)){
			node.forced_extend += 1;
		}
		//if not leaf, recurse on the node that contains the data point
		if(!node.is_leaf()){
			if(features[node.split_dimension] <= node.split_value)
				extend_block4(node.child_left, tree_id, features, label, forced_extend_sum);
			else if(features[node.split_dimension] > node.split_value)
				extend_block4(node.child_right, tree_id, features, label, forced_extend_sum);
			//NOTE: we don't update the counters of labels here because the counting will be done when prediction is required.
			//We can optimize that.
		}
		else{
			bool has_split_bary = false;
			//PAUSE
			//if(!((total_count >= 20 && total_count <= 150)) && E < 0 && node_available >= 2){//Data point in the box
			if(has_been_full > 20 && E < 0 && node_available >= 2){//Data point in the box
					has_split_bary = split_barycenter(node_id, tree_id, features, label, parent_id, parent_tau);
			}
			if(!has_split_bary){

				//Update the counter of label
				node.counters[label] += 1;
				node.fading_score += 1; //fading_score is decreased later
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, %r15d
movl %r8d, 0xc(%rsp)
movq %rcx, %r14
movl %edx, %r8d
movq %rdi, %r12
movslq %esi, %rax
shlq $0x7, %rax
movslq 0x9c(%rdi,%rax), %r13
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
testq %r13, %r13
js 0x357b
leaq 0x44(%r12), %rcx
movq %r13, %rdx
shlq $0x7, %rdx
movsd 0x60(%rcx,%rdx), %xmm0
leaq (%r12,%rax), %rbp
addq $0x44, %rbp
xorl %eax, %eax
xorpd %xmm3, %xmm3
cmpq $0x4, %rax
je 0x35c8
movsd (%r14,%rax,8), %xmm1
movsd 0x10(%rbp,%rax,8), %xmm4
unpcklpd %xmm1, %xmm4            # xmm4 = xmm4[0],xmm1[0]
movhpd 0x30(%rbp,%rax,8), %xmm1 # xmm1 = xmm1[0],mem[0]
subpd %xmm1, %xmm4
maxpd %xmm3, %xmm4
movapd %xmm4, %xmm1
unpckhpd %xmm4, %xmm1            # xmm1 = xmm1[1],xmm4[1]
addsd %xmm4, %xmm1
movsd %xmm1, 0x30(%rsp,%rax,8)
addsd %xmm1, %xmm2
incq %rax
jmp 0x3589
movsd 0x2a58(%rip), %xmm3     # 0x6028
divsd %xmm2, %xmm3
xorpd %xmm1, %xmm1
cmpeqsd %xmm2, %xmm1
movsd 0x2a4b(%rip), %xmm5     # 0x6030
andpd %xmm1, %xmm5
andnpd %xmm3, %xmm1
orpd %xmm1, %xmm5
movl (%r12), %eax
movslq %r8d, %rcx
cmpl $0x2, %eax
jae 0x3671
movq %r12, %rdi
movl %esi, %ebx
xorl %edx, %edx
movl %r8d, 0x14(%rsp)
movsd %xmm0, 0x28(%rsp)
movapd %xmm5, 0x50(%rsp)
movsd %xmm2, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
callq 0x38ac
movl %eax, 0x10(%rsp)
movq %r12, %rdi
movl %ebx, %esi
callq 0x396e
movsd 0x20(%rsp), %xmm2
movapd 0x50(%rsp), %xmm5
movl %ebx, %esi
movsd 0x28(%rsp), %xmm0
movl 0x14(%rsp), %r8d
addl 0x10(%rsp), %eax
movslq 0x17dc(%r12), %rcx
imulq $-0x38, %rcx, %rcx
addq %r12, %rcx
imulq $0x38, 0x18(%rsp), %rdx
addq %rdx, %rcx
addq $0x17b4, %rcx           # imm = 0x17B4
jmp 0x369f
cmpl $0x4, %eax
je 0x367f
cmpl $0x2, %eax
jne 0x3828
movslq 0x17dc(%r12), %rax
imulq $-0x38, %rax, %rax
addq %r12, %rax
imulq $0x38, %rcx, %rcx
addq %rax, %rcx
addq $0x17b4, %rcx           # imm = 0x17B4
movl 0x4(%rcx), %eax
cmpl (%rcx), %eax
setge %al
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm5
movapd %xmm0, %xmm4
addsd %xmm5, %xmm4
jb 0x36db
movsd 0x60(%rbp), %xmm1
ucomisd %xmm4, %xmm1
setbe %cl
cmpl $0x2, 0x17d8(%r12)
setl %dl
orb %al, %dl
orb %cl, %dl
testb $0x1, %dl
je 0x37e1
xorl %eax, %eax
cmpq $0x4, %rax
je 0x373a
movsd 0x10(%rbp,%rax,8), %xmm2
movsd (%r14,%rax,8), %xmm1
ucomisd %xmm1, %xmm2
jbe 0x3714
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
mulsd 0x30(%r12), %xmm3
subsd %xmm3, %xmm2
movsd %xmm2, 0x10(%rbp,%rax,8)
movsd (%r14,%rax,8), %xmm1
movsd 0x30(%rbp,%rax,8), %xmm2
ucomisd %xmm2, %xmm1
jbe 0x3735
subsd %xmm2, %xmm1
mulsd 0x30(%r12), %xmm1
addsd %xmm2, %xmm1
movsd %xmm1, 0x30(%rbp,%rax,8)
incq %rax
jmp 0x36dd
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm5
jb 0x3752
movsd 0x60(%rbp), %xmm1
ucomisd %xmm4, %xmm1
jbe 0x3752
incl 0x70(%rbp)
movslq (%rbp), %rax
cmpq $-0x1, %rax
je 0x3783
movsd (%r14,%rax,8), %xmm0
movsd 0x8(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x3801
ucomisd %xmm1, %xmm0
jbe 0x389d
movl 0x54(%rbp), %esi
jmp 0x3804
xorpd %xmm1, %xmm1
ucomisd %xmm5, %xmm1
jbe 0x37c1
cmpl $0x15, 0x1840(%r12)
jl 0x37c1
cmpl $0x2, 0x17d8(%r12)
jl 0x37c1
movq %r12, %rdi
movl %r8d, %edx
movq %r14, %rcx
movl 0xc(%rsp), %r8d
movl %r13d, %r9d
callq 0x4010
testb %al, %al
jne 0x389d
movslq 0xc(%rsp), %rax
incl 0x68(%rbp,%rax,4)
movsd 0x78(%rbp), %xmm0
addsd 0x2841(%rip), %xmm0     # 0x6018
movsd %xmm0, 0x78(%rbp)
jmp 0x389d
testl %r15d, %r15d
jle 0x382f
cvtsi2sdl 0x70(%rbp), %xmm3
cvtsi2sd %r15d, %xmm1
divsd %xmm1, %xmm3
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm3
setbe %al
jmp 0x3831
movl 0x50(%rbp), %esi
movq %r12, %rdi
movl %r8d, %edx
movq %r14, %rcx
movl 0xc(%rsp), %r8d
movl %r15d, %r9d
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x352e
xorl %eax, %eax
jmp 0x36a4
movb $0x1, %al
movl 0x18(%r12), %ecx
cmpl $0x1, %ecx
je 0x3874
testl %ecx, %ecx
je 0x384f
andl $-0x2, %ecx
cmpl $0x2, %ecx
setne %cl
orb %al, %cl
testb $0x1, %cl
je 0x387a
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movl %r8d, %edx
movq %r14, %rcx
movl 0xc(%rsp), %r8d
movl %r13d, %r9d
movapd %xmm5, %xmm1
callq 0x3a1e
jmp 0x389d
cmpl $0x0, 0x70(%rbp)
jle 0x384f
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movl %r8d, %edx
movq %r14, %rcx
movl 0xc(%rsp), %r8d
movl %r13d, %r9d
movapd %xmm5, %xmm1
callq 0x3c2e
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq