// File: /workspace/github_cpp_makefile_star2_O1/big-data-lab-team[P]OrpailleCC/./src/mondrian_coarse.hpp
// Function: CoarseMondrianForest<double, funct, KappaMetrics<2, false>, 4, 2, 6000>::extend_block0(int, int, double const*, int)
void extend_block0(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);
		//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
		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] = Utils::min(1, node.counters[i]);

		//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;
		}
		//cout << "Split dimension " << dimension << " on " << split_value << "(total count: " << total_count << ")" << endl;

		sample_block(new_sibling, features, label);
	}
	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])
				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_block0(node.child_left, tree_id, features, label);
			else if(features[node.split_dimension] > node.split_value)
				extend_block0(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 on
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %r14
movq %rdi, %r15
leaq 0x44(%rdi), %rbx
movslq %esi, %rax
shlq $0x7, %rax
movslq 0x9c(%rdi,%rax), %rcx
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
testq %rcx, %rcx
js 0x2704
shlq $0x7, %rcx
movsd 0x60(%rbx,%rcx), %xmm3
leaq (%r15,%rax), %r13
addq $0x44, %r13
xorl %eax, %eax
xorpd %xmm4, %xmm4
movsd 0x10(%r13,%rax,8), %xmm1
movsd (%r14,%rax,8), %xmm2
subsd %xmm2, %xmm1
maxsd %xmm0, %xmm1
subsd 0x30(%r13,%rax,8), %xmm2
maxsd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x50(%rsp,%rax,8)
addsd %xmm2, %xmm4
incq %rax
cmpq $0x4, %rax
jne 0x2712
movsd 0x38cf(%rip), %xmm0     # 0x6020
divsd %xmm4, %xmm0
xorpd %xmm1, %xmm1
cmpeqsd %xmm4, %xmm1
movsd 0x38c2(%rip), %xmm2     # 0x6028
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm1, %xmm2
movl (%r15), %eax
cmpl $0x2, %eax
jae 0x2804
movapd %xmm2, 0x30(%rsp)
movsd %xmm4, 0x10(%rsp)
movsd %xmm3, 0x18(%rsp)
movl %edx, %r12d
movl %r8d, 0xc(%rsp)
movq %r15, %rdi
xorl %edx, %edx
movl %esi, %ebp
callq 0x3d4a
movl %ebp, %esi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl %ebp, 0x17d4(%r15)
jle 0x27c8
movl %esi, %edx
movslq %edx, %rdx
shlq $0x7, %rdx
movl 0x58(%rbx,%rdx), %edx
incl %ecx
cmpl $-0x1, %edx
jne 0x27b6
addl %eax, %ecx
movslq 0x17dc(%r15), %rax
imulq $-0x38, %rax, %rax
addq %r15, %rax
movl %r12d, %edx
movslq %r12d, %rdi
imulq $0x38, %rdi, %rdi
addq %rdi, %rax
addq $0x17b4, %rax           # imm = 0x17B4
movl 0xc(%rsp), %r8d
movsd 0x18(%rsp), %xmm3
movsd 0x10(%rsp), %xmm4
movapd 0x30(%rsp), %xmm2
jmp 0x2833
cmpl $0x4, %eax
je 0x2812
cmpl $0x2, %eax
jne 0x290d
movslq 0x17dc(%r15), %rax
imulq $-0x38, %rax, %rax
addq %r15, %rax
movslq %edx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
addq $0x17b4, %rax           # imm = 0x17B4
movl 0x4(%rax), %ecx
cmpl (%rax), %ecx
setge %al
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jb 0x2868
addsd %xmm2, %xmm3
movsd 0x60(%r13), %xmm0
ucomisd %xmm3, %xmm0
jbe 0x2868
cmpl $0x2, 0x17d8(%r15)
setl %cl
orb %al, %cl
testb $0x1, %cl
je 0x2914
xorl %eax, %eax
movsd 0x10(%r13,%rax,8), %xmm1
movsd (%r14,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x2884
movsd %xmm0, 0x10(%r13,%rax,8)
movsd (%r14,%rax,8), %xmm0
ucomisd 0x30(%r13,%rax,8), %xmm0
jbe 0x289a
movsd %xmm0, 0x30(%r13,%rax,8)
incq %rax
cmpq $0x4, %rax
jne 0x286a
movslq (%r13), %rax
cmpq $-0x1, %rax
je 0x28cf
movsd (%r14,%rax,8), %xmm0
movsd 0x8(%r13), %xmm1
ucomisd %xmm0, %xmm1
jae 0x28f0
ucomisd %xmm1, %xmm0
jbe 0x2b84
movl 0x54(%r13), %esi
jmp 0x28f4
movslq %r8d, %rax
incl 0x68(%r13,%rax,4)
movsd 0x78(%r13), %xmm0
addsd 0x3733(%rip), %xmm0     # 0x6018
movsd %xmm0, 0x78(%r13)
jmp 0x2b84
movl 0x50(%r13), %esi
movq %r15, %rdi
movq %r14, %rcx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x26c6
xorl %eax, %eax
jmp 0x2838
xorpd %xmm0, %xmm0
ucomisd %xmm4, %xmm0
jbe 0x2933
xorl %eax, %eax
xorpd %xmm4, %xmm4
addsd 0x50(%rsp,%rax,8), %xmm4
incq %rax
cmpq $0x4, %rax
jne 0x2924
movsd 0x50(%rsp), %xmm0
divsd %xmm4, %xmm0
movsd %xmm0, 0x50(%rsp)
movl $0x1, %eax
movsd 0x50(%rsp,%rax,8), %xmm1
divsd %xmm4, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp,%rax,8)
incq %rax
cmpq $0x4, %rax
jne 0x2948
xorl %eax, %eax
xorpd %xmm0, %xmm0
movl %edx, 0x2c(%rsp)
movsd 0x50(%rsp,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jae 0x2989
incq %rax
cmpq $0x4, %rax
jne 0x296f
movl $0x4, %eax
cmpl $0x4, %eax
movl $0x4, %r12d
cmovll %eax, %r12d
movslq %r12d, %rax
movsd (%r14,%rax,8), %xmm0
movsd 0x30(%r13,%rax,8), %xmm1
ucomisd %xmm1, %xmm0
movl %r8d, 0xc(%rsp)
movl %esi, 0x28(%rsp)
movsd %xmm3, 0x18(%rsp)
movq %rax, 0x10(%rsp)
jbe 0x29ca
movapd %xmm0, %xmm2
movapd %xmm1, %xmm0
jmp 0x29d1
movsd 0x10(%r13,%rax,8), %xmm2
movsd %xmm2, 0x30(%rsp)
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
xorpd %xmm2, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x48(%rsp)
movq %r15, %rdi
callq 0x2600
movl %eax, 0x24(%rsp)
movslq %eax, %rbp
shlq $0x7, %rbp
movl %r12d, (%rbx,%rbp)
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x8(%rbx,%rbp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x60(%rbx,%rbp)
movq %r15, %rdi
callq 0x2600
addl $-0x2, 0x17d8(%r15)
leaq (%r15,%rbp), %rcx
addq $0x74, %rcx
xorl %edx, %edx
movsd (%r14,%rdx,8), %xmm0
minsd 0x10(%r13,%rdx,8), %xmm0
movsd %xmm0, -0x20(%rcx,%rdx,8)
movsd (%r14,%rdx,8), %xmm0
maxsd 0x30(%r13,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %rdx
cmpq $0x4, %rdx
jne 0x2a3a
leaq (%r15,%rbp), %rcx
addq $0xac, %rcx
xorl %edx, %edx
movl $0x1, %esi
movl 0x2c(%rsp), %r8d
movl 0x28(%rsp), %r9d
movl 0x68(%r13,%rdx,4), %edi
testl %edi, %edi
cmovgl %esi, %edi
movl %edi, (%rcx,%rdx,4)
incq %rdx
cmpq $0x1, %rdx
je 0x2a84
movslq %eax, %rcx
shlq $0x7, %rcx
movq $0x0, 0xac(%r15,%rcx)
movl 0x58(%r13), %edx
movl %edx, 0x58(%rbx,%rbp)
movslq 0x58(%r13), %rdx
cmpq $-0x1, %rdx
movsd 0x30(%rsp), %xmm1
jne 0x2af1
movslq 0x17dc(%r15), %rsi
imulq $-0x38, %rsi, %rsi
addq %r15, %rsi
movslq %r8d, %rdi
imulq $0x38, %rdi, %rdi
cmpl %r9d, 0x17bc(%rdi,%rsi)
jne 0x2af1
leaq (%rdi,%rsi), %rdx
addq $0x17bc, %rdx           # imm = 0x17BC
jmp 0x2b09
shlq $0x7, %rdx
leaq (%rbx,%rdx), %rsi
addq $0x50, %rsi
cmpl %r9d, (%rsi)
leaq 0x54(%rbx,%rdx), %rdx
cmoveq %rsi, %rdx
movl 0x24(%rsp), %esi
movl %esi, (%rdx)
movl %esi, 0x58(%r13)
movl %esi, 0x58(%rbx,%rcx)
movq 0x10(%rsp), %rdx
movsd (%r14,%rdx,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x2b37
jp 0x2b37
leaq (%rbx,%rbp), %rdx
addq $0x50, %rdx
movl %eax, 0x4(%rdx)
jmp 0x2b42
leaq (%rbx,%rbp), %rdx
addq $0x54, %rdx
movl %eax, -0x4(%rdx)
movl %r9d, (%rdx)
addq %rcx, %rbx
xorl %eax, %eax
movl 0xc(%rsp), %ecx
movsd (%r14,%rax,8), %xmm0
movsd %xmm0, 0x10(%rbx,%rax,8)
movsd (%r14,%rax,8), %xmm0
movsd %xmm0, 0x30(%rbx,%rax,8)
incq %rax
cmpq $0x4, %rax
jne 0x2b4e
movslq %ecx, %rax
incl 0x68(%rbx,%rax,4)
movsd 0x17e8(%r15), %xmm0
movsd %xmm0, 0x60(%rbx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop