// File: 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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x1b00
movslq -0xc(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x8c(%rbp)
movq -0x88(%rbp), %rax
cmpl $0x0, 0x58(%rax)
jl 0x693d
movq -0xf0(%rbp), %rdi
callq 0x1b00
movq -0x88(%rbp), %rcx
movslq 0x58(%rcx), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movsd 0x60(%rax), %xmm0
movsd %xmm0, -0xf8(%rbp)
jmp 0x694a
xorps %xmm0, %xmm0
movsd %xmm0, -0xf8(%rbp)
jmp 0x694a
movsd -0xf8(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xa0(%rbp)
movl $0x0, -0xa4(%rbp)
cmpl $0x4, -0xa4(%rbp)
jge 0x6adb
movq -0x88(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movsd 0x10(%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movslq -0xa4(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x69d7
movq -0x88(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movsd 0x10(%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movslq -0xa4(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x100(%rbp)
jmp 0x69e4
xorps %xmm0, %xmm0
movsd %xmm0, -0x100(%rbp)
jmp 0x69e4
movsd -0x100(%rbp), %xmm0
movslq -0xa4(%rbp), %rax
movsd %xmm0, -0x40(%rbp,%rax,8)
movq -0x18(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x88(%rbp), %rax
movslq -0xa4(%rbp), %rcx
subsd 0x30(%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6a54
movq -0x18(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x88(%rbp), %rax
movslq -0xa4(%rbp), %rcx
subsd 0x30(%rax,%rcx,8), %xmm0
movsd %xmm0, -0x108(%rbp)
jmp 0x6a61
xorps %xmm0, %xmm0
movsd %xmm0, -0x108(%rbp)
jmp 0x6a61
movsd -0x108(%rbp), %xmm0
movslq -0xa4(%rbp), %rax
movsd %xmm0, -0x60(%rbp,%rax,8)
movslq -0xa4(%rbp), %rax
movsd -0x40(%rbp,%rax,8), %xmm0
movslq -0xa4(%rbp), %rax
addsd -0x60(%rbp,%rax,8), %xmm0
movslq -0xa4(%rbp), %rax
movsd %xmm0, -0x80(%rbp,%rax,8)
movslq -0xa4(%rbp), %rax
movsd -0x40(%rbp,%rax,8), %xmm0
movslq -0xa4(%rbp), %rax
addsd -0x60(%rbp,%rax,8), %xmm0
addsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x696f
movsd -0xa0(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x6b00
jp 0x6b00
movsd 0x4532(%rip), %xmm0     # 0xb028
movsd %xmm0, -0x110(%rbp)
jmp 0x6b15
movsd -0xa0(%rbp), %xmm0
callq 0x70e0
movsd %xmm0, -0x110(%rbp)
movq -0xf0(%rbp), %rax
movsd -0x110(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movb $0x0, -0xb1(%rbp)
movb $0x0, -0xb2(%rbp)
cmpl $0x0, (%rax)
je 0x6b4b
movq -0xf0(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x6bbd
movq -0xf0(%rbp), %rdi
movl -0xc(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7130
movq -0xf0(%rbp), %rdi
movl %eax, -0xb8(%rbp)
movl -0xc(%rbp), %esi
callq 0x7390
movq -0xf0(%rbp), %rdi
movl %eax, -0xbc(%rbp)
movl -0xb8(%rbp), %eax
addl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
callq 0x1a50
movq %rax, %rcx
movl -0x114(%rbp), %eax
movslq -0x10(%rbp), %rdx
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
setg %al
andb $0x1, %al
movb %al, -0xb2(%rbp)
jmp 0x6bed
movq -0xf0(%rbp), %rdi
callq 0x1a50
movq %rax, %rdi
movq -0xf0(%rbp), %rax
movslq -0x10(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rdi
movl (%rax), %esi
callq 0x7420
andb $0x1, %al
movb %al, -0xb2(%rbp)
movsd -0xb0(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x6de5
movsd -0x98(%rbp), %xmm1
addsd -0xb0(%rbp), %xmm1
movq -0x88(%rbp), %rax
movsd 0x60(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x6de5
movq -0xf0(%rbp), %rax
cmpl $0x2, 0x17d8(%rax)
jl 0x6de5
testb $0x1, -0xb2(%rbp)
jne 0x6de5
movb $0x0, -0xbd(%rbp)
cmpl $0x0, -0x20(%rbp)
jle 0x6c92
movq -0x88(%rbp), %rax
cvtsi2sdl 0x70(%rax), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
callq 0x35a0
movaps %xmm0, %xmm1
movsd -0xc8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
seta %al
andb $0x1, %al
movb %al, -0xbd(%rbp)
movq -0xf0(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x6ce5
movq -0xf0(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x8c(%rbp), %r9d
movsd -0x98(%rbp), %xmm0
leaq -0x80(%rbp), %rax
movsd -0xb0(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm2
movq %rax, (%rsp)
callq 0x75e0
jmp 0x6de0
movq -0xf0(%rbp), %rax
cmpl $0x1, 0x18(%rax)
jne 0x6d3d
movq -0x88(%rbp), %rax
cmpl $0x0, 0x70(%rax)
jle 0x6d3d
movq -0xf0(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x8c(%rbp), %r9d
movsd -0x98(%rbp), %xmm0
leaq -0x80(%rbp), %rax
movsd -0xb0(%rbp), %xmm1
movq %rax, (%rsp)
callq 0x7af0
jmp 0x6dde
movq -0xf0(%rbp), %rax
cmpl $0x2, 0x18(%rax)
je 0x6d57
movq -0xf0(%rbp), %rax
cmpl $0x3, 0x18(%rax)
jne 0x6d9b
testb $0x1, -0xbd(%rbp)
je 0x6d9b
movq -0xf0(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x8c(%rbp), %r9d
movsd -0x98(%rbp), %xmm0
leaq -0x80(%rbp), %rax
movsd -0xb0(%rbp), %xmm1
movq %rax, (%rsp)
callq 0x7af0
jmp 0x6ddc
movq -0xf0(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x8c(%rbp), %r9d
movsd -0x98(%rbp), %xmm0
leaq -0x80(%rbp), %rax
movsd -0xb0(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm2
movq %rax, (%rsp)
callq 0x75e0
jmp 0x6dde
jmp 0x6de0
jmp 0x70ab
movl $0x0, -0xcc(%rbp)
cmpl $0x4, -0xcc(%rbp)
jge 0x6f1a
movq -0x88(%rbp), %rax
movslq -0xcc(%rbp), %rcx
movsd 0x10(%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movslq -0xcc(%rbp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x6e80
movq -0xf0(%rbp), %rax
movq -0x88(%rbp), %rcx
movslq -0xcc(%rbp), %rdx
movsd 0x10(%rcx,%rdx,8), %xmm0
movq -0x18(%rbp), %rcx
movslq -0xcc(%rbp), %rdx
subsd (%rcx,%rdx,8), %xmm0
mulsd 0x30(%rax), %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd -0xd8(%rbp), %xmm1
movq -0x88(%rbp), %rax
movslq -0xcc(%rbp), %rcx
movsd 0x10(%rax,%rcx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rax,%rcx,8)
movq -0x88(%rbp), %rax
movslq -0xcc(%rbp), %rcx
movsd 0x30(%rax,%rcx,8), %xmm1
movq -0x18(%rbp), %rax
movslq -0xcc(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x6f04
movq -0xf0(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0xcc(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq -0x88(%rbp), %rcx
movslq -0xcc(%rbp), %rdx
subsd 0x30(%rcx,%rdx,8), %xmm0
mulsd 0x30(%rax), %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0xe0(%rbp), %xmm0
movq -0x88(%rbp), %rax
movslq -0xcc(%rbp), %rcx
addsd 0x30(%rax,%rcx,8), %xmm0
movsd %xmm0, 0x30(%rax,%rcx,8)
jmp 0x6f06
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x6def
movsd -0xb0(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x6f5d
movsd -0x98(%rbp), %xmm1
addsd -0xb0(%rbp), %xmm1
movq -0x88(%rbp), %rax
movsd 0x60(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x6f5d
movq -0x88(%rbp), %rax
movl 0x70(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x70(%rax)
movq -0x88(%rbp), %rdi
callq 0x29e0
testb $0x1, %al
jne 0x700a
movq -0x18(%rbp), %rax
movq -0x88(%rbp), %rcx
movslq (%rcx), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x88(%rbp), %rax
movsd 0x8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jb 0x6fbd
movq -0xf0(%rbp), %rdi
movq -0x88(%rbp), %rax
movl 0x50(%rax), %esi
movl -0x10(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x20(%rbp), %r9d
callq 0x68b0
jmp 0x7005
movq -0x18(%rbp), %rax
movq -0x88(%rbp), %rcx
movslq (%rcx), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x88(%rbp), %rax
ucomisd 0x8(%rax), %xmm0
jbe 0x7003
movq -0xf0(%rbp), %rdi
movq -0x88(%rbp), %rax
movl 0x54(%rax), %esi
movl -0x10(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x20(%rbp), %r9d
callq 0x68b0
jmp 0x7005
jmp 0x70a9
movq -0xf0(%rbp), %rax
movb $0x0, -0xe1(%rbp)
cmpl $0x14, 0x1840(%rax)
jle 0x706f
xorps %xmm0, %xmm0
ucomisd -0xb0(%rbp), %xmm0
jbe 0x706f
movq -0xf0(%rbp), %rax
cmpl $0x2, 0x17d8(%rax)
jl 0x706f
movq -0xf0(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x8c(%rbp), %r9d
movsd -0x98(%rbp), %xmm0
callq 0x8220
andb $0x1, %al
movb %al, -0xe1(%rbp)
testb $0x1, -0xe1(%rbp)
jne 0x70a7
movq -0x88(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl 0x68(%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, 0x68(%rax,%rcx,4)
movq -0x88(%rbp), %rax
movsd 0x3f7b(%rip), %xmm0     # 0xb018
addsd 0x78(%rax), %xmm0
movsd %xmm0, 0x78(%rax)
jmp 0x70a9
jmp 0x70ab
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop