// 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>::split_node(int, int, double const*, int, int, double, double*, double)
void split_node(int const node_id, int const tree_id, feature_type const* features, int const label, int const parent_id, double const parent_tau, double* probabilities, double const E){
	int new_parent_id, new_sibling_id;
	double sum = 0;
	Node& node = nodes()[node_id];
	for(int i = 0; i < feature_count; ++i){
		probabilities[i] = node.bound_upper[i] - node.bound_lower[i];
		sum += probabilities[i];
	}
	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 = node.bound_lower[dimension], upper_value = node.bound_upper[dimension];

	//sample the split between [lower_value, upper_value]
	double const random_value = func::rand_uniform();
	double const split_value = random_value*(upper_value - lower_value) + lower_value;

	new_parent_id = available_node();
	nodes()[new_parent_id].tau = parent_tau + E;
	new_sibling_id = available_node();
	nodes()[new_sibling_id].tau = lifetime;
	node_available -= 2;

	Node &new_parent = nodes()[new_parent_id];
	Node &new_sibling = nodes()[new_sibling_id];

	new_parent.split_dimension = dimension;
	new_parent.split_value = split_value;
	//Set the box for the new children and the new_parent and the node (depending on the values in features
	//The current datapoint is passed through after readjusting counters and shape
	for(int i = 0; i < feature_count; ++i){
		new_parent.bound_lower[i] = Utils::min(node.bound_lower[i], features[i]);
		new_parent.bound_upper[i] = Utils::max(node.bound_upper[i], features[i]);
		new_sibling.bound_lower[i] = node.bound_lower[i];
		new_sibling.bound_upper[i] = node.bound_upper[i];
	}

	//Creates counters for the label of the new sibling
	for(int i = 0; i < label_count; ++i)
		new_sibling.counters[i] = 0;
	//No need to increase the counter for the current label because we will do it later, when passing down.

	//Make the connections between the new nodes
	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_id;
	else{ //Otherwise, replace the existing pointer to parent
		Node& parent = nodes()[node.parent];
		if(parent.child_left == node_id)
			parent.child_left = new_parent_id;
		else
			parent.child_right = new_parent_id;
	}

	//Count sub-trees !
	int count_left = 0, count_right = 0;
	count_sides(node_id, dimension, split_value, count_left, count_right);
	bool const subtree_on_left = (count_left > count_right);
	//If more count are higher than split ---> new sibling goes left
	//set the connections
	if(subtree_on_left){
		new_parent.child_right = new_sibling_id;
		new_parent.child_left = node_id;
		node.bound_upper[dimension] = split_value;
		new_sibling.bound_lower[dimension] = split_value;
	}
	else{
		new_parent.child_right = node_id;
		new_parent.child_left = new_sibling_id;
		node.bound_lower[dimension] = split_value;
		new_sibling.bound_upper[dimension] = split_value;
	}
	new_parent.parent = node.parent;
	node.parent = new_parent_id;
	new_sibling.parent = new_parent_id;

	//Distribute Forced_extend
	if(fe_distribution == FE_DISTRIBUTION_ZERO){
		node.forced_extend = 0;
		new_sibling.forced_extend = 0;
	}
	else if(fe_distribution == FE_DISTRIBUTION_SPLIT){
		new_sibling.forced_extend = 0.5 * node.forced_extend;
		node.forced_extend *= 0.5;
	}
	else if(fe_distribution == FE_DISTRIBUTION_PROPORTIONAL){
		double const sum_count = static_cast<double>(count_left + count_right);
		double const ratio_fe = subtree_on_left ? static_cast<double>(count_left)/sum_count : static_cast<double>(count_left)/sum_count;
		new_sibling.forced_extend = (1-ratio_fe) * node.forced_extend;
		node.forced_extend *= ratio_fe;
	}
	else if(fe_distribution == FE_DISTRIBUTION_DECREMENT){
		new_sibling.forced_extend = 0;
		node.forced_extend --;
	}
	new_parent.forced_extend = 0;

	int to_remove[label_count] = {0};
	//If subtree_on_left, then discharge on right
	adjust_counters(node_id, dimension, split_value, !subtree_on_left, to_remove);
	//Place the new counters in new_sibling
	for(int i = 0; i < label_count; ++i)
		new_sibling.counters[i] += to_remove[i];
	//Adjust the boxes' bound and reshape the tree is needed
	adjust_boxes(node_id, dimension, split_value, subtree_on_left, tree_id);

	//Propage the datapoint on the right side of the new split
	if(features[dimension] > split_value){
		pass_data_point_down(new_parent.child_right, features, label);
	}
	else{
		pass_data_point_down(new_parent.child_left, features, label);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movsd %xmm1, 0x38(%rsp)
movsd %xmm0, 0x40(%rsp)
movl %r8d, 0x54(%rsp)
movq %rcx, %r14
movl %edx, 0x24(%rsp)
movq 0xb0(%rsp), %rbx
leaq 0x44(%rdi), %rax
movq %rax, 0x28(%rsp)
movslq %esi, %rax
shlq $0x7, %rax
movq %rdi, 0x10(%rsp)
leaq (%rdi,%rax), %r15
addq $0x44, %r15
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq $0x4, %rax
je 0x3ca1
movsd 0x30(%r15,%rax,8), %xmm1
subsd 0x10(%r15,%rax,8), %xmm1
movsd %xmm1, (%rbx,%rax,8)
addsd %xmm1, %xmm0
incq %rax
jmp 0x3c7f
pushq $0x4
popq %rbp
movq %rbx, %rdi
movl %esi, 0xc(%rsp)
movl %ebp, %esi
callq 0x3994
movq %rbx, %rdi
movl %ebp, %esi
callq 0x39ee
movl %eax, 0x50(%rsp)
movslq %eax, %rcx
movq %rcx, 0x30(%rsp)
leaq (%r15,%rcx,8), %rax
addq $0x10, %rax
movq %rax, 0x48(%rsp)
movsd (%rax), %xmm0
leaq 0x30(%r15,%rcx,8), %rax
movq %rax, 0x68(%rsp)
movsd (%rax), %xmm1
subsd %xmm0, %xmm1
xorpd %xmm2, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x18(%rsp)
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0x2378
movsd 0x40(%rsp), %xmm0
addsd 0x38(%rsp), %xmm0
movl %eax, 0x40(%rsp)
movslq %eax, %r13
shlq $0x7, %r13
movq 0x28(%rsp), %r12
leaq (%r12,%r13), %rbx
movsd %xmm0, 0x60(%r12,%r13)
movq %rbp, %rdi
callq 0x2378
movl 0xc(%rsp), %esi
movsd 0x17e8(%rbp), %xmm0
movq %rbp, %rdx
movl %eax, 0x38(%rsp)
cltq
shlq $0x7, %rax
leaq (%r12,%rax), %rbp
leaq (%r12,%rax), %rcx
addq $0x10, %rcx
movq %rcx, 0x60(%rsp)
movsd %xmm0, 0x50(%rcx)
addl $-0x2, 0x17d8(%rdx)
movq 0x30(%rsp), %rcx
movl %ecx, (%r12,%r13)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x8(%r12,%r13)
leaq (%r12,%rax), %r13
addq $0x30, %r13
xorl %eax, %eax
cmpq $0x20, %rax
je 0x3ddd
movsd 0x10(%r15,%rax), %xmm0
minsd (%r14,%rax), %xmm0
movsd %xmm0, 0x10(%rbx,%rax)
movsd 0x30(%r15,%rax), %xmm0
maxsd (%r14,%rax), %xmm0
movsd %xmm0, 0x30(%rbx,%rax)
movsd 0x10(%r15,%rax), %xmm0
movsd %xmm0, -0x20(%r13,%rax)
movsd 0x30(%r15,%rax), %xmm0
movsd %xmm0, (%r13,%rax)
addq $0x8, %rax
jmp 0x3d8f
andq $0x0, 0x68(%rbp)
movslq 0x58(%r15), %rax
cmpq $-0x1, %rax
jne 0x3e1d
movq 0x10(%rsp), %rdx
movslq 0x17dc(%rdx), %rcx
imulq $-0x38, %rcx, %rcx
addq %rdx, %rcx
movslq 0x24(%rsp), %rdx
imulq $0x38, %rdx, %rdx
cmpl %esi, 0x17bc(%rdx,%rcx)
jne 0x3e1d
leaq (%rdx,%rcx), %rax
addq $0x17bc, %rax           # imm = 0x17BC
jmp 0x3e39
shlq $0x7, %rax
movq 0x28(%rsp), %rdx
leaq (%rdx,%rax), %rcx
addq $0x50, %rcx
cmpl %esi, (%rcx)
leaq 0x54(%rdx,%rax), %rax
cmoveq %rcx, %rax
movl 0x40(%rsp), %r12d
movl %r12d, (%rax)
leaq 0x5c(%rsp), %rcx
andl $0x0, (%rcx)
leaq 0x58(%rsp), %r8
andl $0x0, (%r8)
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movq 0x30(%rsp), %rdx
movsd 0x18(%rsp), %xmm0
callq 0x44c2
movl 0xc(%rsp), %r9d
leaq 0x5c(%rsp), %rax
movl (%rax), %esi
leaq 0x58(%rsp), %rax
movl (%rax), %edi
cmpl %edi, %esi
movq 0x48(%rsp), %rcx
cmovgq 0x68(%rsp), %rcx
cmovgq 0x60(%rsp), %r13
movl %r9d, %eax
movl 0x38(%rsp), %r8d
cmovgl %r8d, %eax
cmovgl %r9d, %r8d
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rcx)
movq 0x30(%rsp), %rcx
movsd %xmm0, (%r13,%rcx,8)
movl %eax, 0x54(%rbx)
movl %r8d, 0x50(%rbx)
movl 0x58(%r15), %eax
movl %eax, 0x58(%rbx)
movl %r12d, 0x58(%r15)
movl %r12d, 0x58(%rbp)
movq 0x10(%rsp), %rax
movl 0x14(%rax), %eax
cmpq $0x3, %rax
movl 0x50(%rsp), %edx
ja 0x3f5a
leaq 0x21b2(%rip), %rcx      # 0x6098
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
andl $0x0, 0x70(%r15)
andl $0x0, 0x70(%rbp)
jmp 0x3f5a
leal (%rdi,%rsi), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sd %esi, %xmm1
divsd %xmm0, %xmm1
movsd 0x2107(%rip), %xmm0     # 0x6018
cvtsi2sdl 0x70(%r15), %xmm2
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %eax
jmp 0x3f45
andl $0x0, 0x70(%rbp)
decl 0x70(%r15)
jmp 0x3f5a
cvtsi2sdl 0x70(%r15), %xmm0
movsd 0x20fb(%rip), %xmm1     # 0x6038
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x70(%rbp)
cvtsi2sdl 0x70(%r15), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x70(%r15)
leaq 0x54(%rbx), %rax
movq %rax, 0x48(%rsp)
leaq 0x50(%rbx), %r13
xorl %ecx, %ecx
movq %rsi, 0x28(%rsp)
movq %rdi, %r15
cmpl %edi, %esi
setle %cl
andl $0x0, 0x70(%rbx)
leaq 0x70(%rsp), %r8
andq $0x0, (%r8)
movq 0x10(%rsp), %rdi
movl %r9d, %esi
movl %edx, %r12d
movsd 0x18(%rsp), %xmm0
callq 0x464c
xorl %eax, %eax
cmpq $0x2, %rax
je 0x3fae
movl 0x70(%rsp,%rax,4), %ecx
addl %ecx, 0x68(%rbp,%rax,4)
incq %rax
jmp 0x3f9b
xorl %ecx, %ecx
cmpl %r15d, 0x28(%rsp)
setg %cl
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
movsd 0x18(%rsp), %xmm0
movl 0x24(%rsp), %r8d
callq 0x48ac
movq 0x30(%rsp), %rax
movsd (%r14,%rax,8), %xmm0
ucomisd 0x18(%rsp), %xmm0
cmovaq 0x48(%rsp), %r13
movl (%r13), %esi
movq %rbx, %rdi
movq %r14, %rdx
movl 0x54(%rsp), %ecx
callq 0x4c36
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq