// 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_block2(int, int, double const*, int)
void extend_block2(int const node_id, int const tree_id, feature_type const* features, int const label){
	//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);
	}

	if(E >= 0 && parent_tau + E < node.tau && node_available >= 2 && !pause_expension){//Introduce a new parent and a new sibling
		Utils::turn_array_into_probability(probabilities, feature_count, sum);
		//sample features with probability proportional to e_lower[i] + e_upper[i]
		int const dimension = Utils::pick_from_distribution<func>(probabilities, feature_count);

		//Select the bound to choose the split from
		double lower_value, upper_value;
		if(features[dimension] > node.bound_upper[dimension]){
			lower_value = node.bound_upper[dimension];
			upper_value = features[dimension];
		}
		else if(features[dimension] < node.bound_lower[dimension]){
			lower_value = features[dimension];
			upper_value = node.bound_lower[dimension];
		}

		//sample the split between [lower_value, upper_value]
		double const split_value = func::rand_uniform()*(upper_value - lower_value) + lower_value;
		int new_parent, new_sibling;
		//insert new node above the current one
		new_parent = available_node();
		nodes()[new_parent].split_dimension = dimension;
		nodes()[new_parent].split_value = split_value;
		nodes()[new_parent].tau = parent_tau + E;
		//insert new leaf, sibbling of the current one
		new_sibling = available_node();
		node_available -= 2;

		//Update the box of the new parent
		for(int i = 0; i < feature_count; ++i){
			nodes()[new_parent].bound_lower[i] = features[i] < node.bound_lower[i] ? features[i] : node.bound_lower[i];
			nodes()[new_parent].bound_upper[i] = features[i] > node.bound_upper[i] ? features[i] : node.bound_upper[i];
		}
		//NOTE Creates counters for the label of the new parent
		//for(int i = 0; i < label_count; ++i)
		//nodes[new_parent].counters[i] = node.counters[i];
		//nodes[new_parent].counters[label] += 1;

		//Creates counters for the label of the new sibling
		for(int i = 0; i < label_count; ++i)
			nodes()[new_sibling].counters[i] = 0;
		//No need to increase the counter for the current label because we will call sample_block soon on new_sibling

		//Make the connections between the new nodes
		nodes()[new_parent].parent = node.parent;
		if(!node.has_parent() && node_id == tree_bases()[tree_id].root)//We introduce a parent to the root
			tree_bases()[tree_id].root = new_parent;
		else{
			Node& parent = nodes()[node.parent];
			if(parent.child_left == node_id)
				parent.child_left = new_parent;
			else
				parent.child_right = new_parent;
		}


		node.parent = new_parent;

		nodes()[new_sibling].parent = new_parent;
		if(features[dimension] == upper_value){ //right
			nodes()[new_parent].child_right = new_sibling;
			nodes()[new_parent].child_left = node_id;
		}
		else{ //left
			nodes()[new_parent].child_left = new_sibling;
			nodes()[new_parent].child_right = node_id;
		}

		sample_block(new_sibling, features, label);
	}
	else if(E < 0 || parent_tau + E > node.tau){ //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])
				node.bound_lower[i] = features[i];
			if(node.bound_upper[i] < features[i])
				node.bound_upper[i] = features[i];
		}
		//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_block2(node.child_left, tree_id, features, label);
			else if(features[node.split_dimension] > node.split_value)
				extend_block2(node.child_right, tree_id, features, label);
			//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{
			//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 $0x78, %rsp
movq %rcx, %r14
movl %esi, %ebp
movq %rdi, %r15
leaq 0x44(%rdi), %r13
movslq %esi, %rax
shlq $0x7, %rax
movslq 0x9c(%rdi,%rax), %rcx
xorpd %xmm0, %xmm0
xorpd %xmm4, %xmm4
testq %rcx, %rcx
js 0x28c3
shlq $0x7, %rcx
movsd 0x60(%r13,%rcx), %xmm4
leaq (%r15,%rax), %rbx
addq $0x44, %rbx
xorl %eax, %eax
xorpd %xmm3, %xmm3
cmpq $0x4, %rax
je 0x2910
movsd (%r14,%rax,8), %xmm1
movsd 0x10(%rbx,%rax,8), %xmm2
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
movhpd 0x30(%rbx,%rax,8), %xmm1 # xmm1 = xmm1[0],mem[0]
subpd %xmm1, %xmm2
maxpd %xmm3, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
movsd %xmm1, 0x50(%rsp,%rax,8)
addsd %xmm1, %xmm0
incq %rax
jmp 0x28d1
movsd 0x3710(%rip), %xmm2     # 0x6028
divsd %xmm0, %xmm2
xorpd %xmm1, %xmm1
cmpeqsd %xmm0, %xmm1
movsd 0x3703(%rip), %xmm3     # 0x6030
andpd %xmm1, %xmm3
andnpd %xmm2, %xmm1
orpd %xmm1, %xmm3
movl (%r15), %eax
movslq %edx, %rsi
cmpl $0x2, %eax
jae 0x29c8
movq %r15, %rdi
movq %rsi, 0x20(%rsp)
movl %ebp, %esi
movl %edx, 0xc(%rsp)
xorl %edx, %edx
movl %r8d, %r12d
movsd %xmm0, 0x18(%rsp)
movsd %xmm4, 0x10(%rsp)
movapd %xmm3, 0x30(%rsp)
callq 0x38ac
movq %r13, 0x28(%rsp)
movl %eax, %r13d
movq %r15, %rdi
movl %ebp, %esi
callq 0x396e
movapd 0x30(%rsp), %xmm3
movsd 0x10(%rsp), %xmm4
movsd 0x18(%rsp), %xmm0
movq 0x20(%rsp), %rsi
movl %r12d, %r8d
addl %r13d, %eax
movq 0x28(%rsp), %r13
movslq 0x17dc(%r15), %rcx
imulq $-0x38, %rcx, %rcx
addq %r15, %rcx
imulq $0x38, %rsi, %rdx
addq %rdx, %rcx
addq $0x17b4, %rcx           # imm = 0x17B4
movl 0xc(%rsp), %edx
jmp 0x29f5
cmpl $0x4, %eax
je 0x29d6
cmpl $0x2, %eax
jne 0x2ade
movslq 0x17dc(%r15), %rax
imulq $-0x38, %rax, %rax
addq %r15, %rax
imulq $0x38, %rsi, %rcx
addq %rax, %rcx
addq $0x17b4, %rcx           # imm = 0x17B4
movl 0x4(%rcx), %eax
cmpl (%rcx), %eax
setge %al
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm3
jb 0x2a2d
movapd %xmm4, %xmm5
addsd %xmm3, %xmm5
movsd 0x60(%rbx), %xmm1
ucomisd %xmm5, %xmm1
jbe 0x2a2d
cmpl $0x2, 0x17d8(%r15)
setl %cl
orb %al, %cl
testb $0x1, %cl
je 0x2ae5
ucomisd %xmm3, %xmm2
ja 0x2a42
addsd %xmm3, %xmm4
ucomisd 0x60(%rbx), %xmm4
jbe 0x2ca1
xorl %eax, %eax
cmpq $0x4, %rax
je 0x2a7b
movsd 0x10(%rbx,%rax,8), %xmm1
movsd (%r14,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x2a68
movsd %xmm0, 0x10(%rbx,%rax,8)
movsd (%r14,%rax,8), %xmm0
ucomisd 0x30(%rbx,%rax,8), %xmm0
jbe 0x2a76
movsd %xmm0, 0x30(%rbx,%rax,8)
incq %rax
jmp 0x2a44
movslq (%rbx), %rax
cmpq $-0x1, %rax
je 0x2aa4
movsd (%r14,%rax,8), %xmm0
movsd 0x8(%rbx), %xmm1
ucomisd %xmm0, %xmm1
jae 0x2ac2
ucomisd %xmm1, %xmm0
jbe 0x2ca1
movl 0x54(%rbx), %esi
jmp 0x2ac5
movslq %r8d, %rax
incl 0x68(%rbx,%rax,4)
movsd 0x78(%rbx), %xmm0
addsd 0x3560(%rip), %xmm0     # 0x6018
movsd %xmm0, 0x78(%rbx)
jmp 0x2ca1
movl 0x50(%rbx), %esi
movq %r15, %rdi
movq %r14, %rcx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2882
xorl %eax, %eax
jmp 0x29fa
movsd %xmm5, 0x48(%rsp)
movq %rsi, 0x20(%rsp)
movq %r13, 0x28(%rsp)
movl %r8d, 0xc(%rsp)
leaq 0x50(%rsp), %r12
pushq $0x4
popq %r13
movq %r12, %rdi
movl %r13d, %esi
callq 0x3994
movq %r12, %rdi
movl %r13d, %esi
callq 0x39ee
movl %eax, %r13d
cltq
movsd (%r14,%rax,8), %xmm0
movsd 0x30(%rbx,%rax,8), %xmm1
ucomisd %xmm1, %xmm0
movq %rax, 0x18(%rsp)
jbe 0x2b3f
movapd %xmm0, %xmm2
movapd %xmm1, %xmm0
jmp 0x2b45
movsd 0x10(%rbx,%rax,8), %xmm2
movsd %xmm2, 0x10(%rsp)
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
xorpd %xmm2, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x40(%rsp)
movq %r15, %rdi
callq 0x2378
movl %eax, 0x30(%rsp)
movslq %eax, %r12
shlq $0x7, %r12
movq 0x28(%rsp), %rax
movl %r13d, (%rax,%r12)
movq %rax, %r13
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x8(%rax,%r12)
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x60(%rax,%r12)
movq %r15, %rdi
callq 0x2378
addl $-0x2, 0x17d8(%r15)
leaq (%r12,%r15), %rsi
addq $0x74, %rsi
xorl %edx, %edx
movl 0xc(%rsp), %ecx
cmpq $0x4, %rdx
je 0x2bea
movsd (%r14,%rdx,8), %xmm0
minsd 0x10(%rbx,%rdx,8), %xmm0
movsd %xmm0, -0x20(%rsi,%rdx,8)
movsd (%r14,%rdx,8), %xmm0
maxsd 0x30(%rbx,%rdx,8), %xmm0
movsd %xmm0, (%rsi,%rdx,8)
incq %rdx
jmp 0x2bbc
movslq %eax, %r8
shlq $0x7, %r8
leaq (%r8,%r15), %rdx
addq $0xac, %rdx
xorl %esi, %esi
movsd 0x10(%rsp), %xmm0
cmpq $0x2, %rsi
je 0x2c13
andl $0x0, (%rdx,%rsi,4)
incq %rsi
jmp 0x2c04
movslq 0x58(%rbx), %rdx
movl %edx, 0x58(%r13,%r12)
cmpq $-0x1, %rdx
jne 0x2c4c
movslq 0x17dc(%r15), %rsi
imulq $-0x38, %rsi, %rsi
addq %r15, %rsi
imulq $0x38, 0x20(%rsp), %rdi
cmpl %ebp, 0x17bc(%rdi,%rsi)
jne 0x2c4c
leaq (%rdi,%rsi), %rdx
addq $0x17bc, %rdx           # imm = 0x17BC
jmp 0x2c63
shlq $0x7, %rdx
leaq (%rdx,%r13), %rsi
addq $0x50, %rsi
cmpl %ebp, (%rsi)
leaq 0x54(%r13,%rdx), %rdx
cmoveq %rsi, %rdx
movl 0x30(%rsp), %esi
movl %esi, (%rdx)
movl %esi, 0x58(%rbx)
movl %esi, 0x58(%r13,%r8)
movq 0x18(%rsp), %rdx
ucomisd (%r14,%rdx,8), %xmm0
movl %eax, %edx
cmovnel %ebp, %edx
cmovpl %ebp, %edx
cmovnel %eax, %ebp
cmovpl %eax, %ebp
movl %edx, 0x54(%r13,%r12)
movl %ebp, 0x50(%r13,%r12)
movq %r15, %rdi
movl %eax, %esi
movq %r14, %rdx
callq 0x23cc
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq