// File: /workspace/github_cpp_makefile_star2_O3/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, 0x20(%rsp)
movsd %xmm0, 0x28(%rsp)
movl %r8d, 0x3c(%rsp)
movq %rcx, %r14
movl %edx, 0x1c(%rsp)
movq %rdi, %r15
movq 0xb0(%rsp), %rax
leaq 0x44(%rdi), %rcx
movq %rcx, 0x48(%rsp)
movslq %esi, %rcx
shlq $0x7, %rcx
leaq (%rdi,%rcx), %rbx
addq $0x44, %rbx
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movsd 0x30(%rbx,%rcx,8), %xmm1
subsd 0x10(%rbx,%rcx,8), %xmm1
movsd %xmm1, (%rax,%rcx,8)
addsd %xmm1, %xmm0
incq %rcx
cmpq $0x4, %rcx
jne 0x4157
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4193
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
addsd (%rax,%rcx,8), %xmm0
incq %rcx
cmpq $0x4, %rcx
jne 0x4185
movsd (%rax), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax)
movl $0x1, %ecx
movsd (%rax,%rcx,8), %xmm2
divsd %xmm0, %xmm2
addsd %xmm2, %xmm1
movsd %xmm1, (%rax,%rcx,8)
incq %rcx
cmpq $0x4, %rcx
jne 0x41a4
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movl %esi, 0xc(%rsp)
movsd (%rax,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jae 0x41e2
incq %rcx
cmpq $0x4, %rcx
jne 0x41c9
movl $0x4, %ecx
cmpl $0x4, %ecx
movl $0x4, %eax
cmovll %ecx, %eax
movslq %eax, %rcx
movq %rcx, 0x30(%rsp)
leaq (%rbx,%rcx,8), %rax
addq $0x10, %rax
movq %rax, 0x68(%rsp)
movsd (%rax), %xmm0
leaq 0x30(%rbx,%rcx,8), %rax
movq %rax, 0x60(%rsp)
movsd (%rax), %xmm1
subsd %xmm0, %xmm1
xorpd %xmm2, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
movq %r15, %rdi
callq 0x26e4
movsd 0x28(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
movl %eax, 0x20(%rsp)
movslq %eax, %r12
shlq $0x7, %r12
movq 0x48(%rsp), %rbp
leaq (%r12,%rbp), %r13
movsd %xmm0, 0x60(%rbp,%r12)
movq %r15, %rdi
callq 0x26e4
movsd 0x17e8(%r15), %xmm0
movl %eax, 0x38(%rsp)
cltq
shlq $0x7, %rax
leaq (%rax,%rbp), %r8
leaq (%rax,%rbp), %rcx
addq $0x10, %rcx
movq %rcx, 0x58(%rsp)
movsd %xmm0, 0x50(%rcx)
movq %r15, %rdi
addl $-0x2, 0x17d8(%r15)
movq 0x30(%rsp), %rcx
movl %ecx, (%rbp,%r12)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rbp,%r12)
addq %rax, %rbp
addq $0x30, %rbp
xorl %eax, %eax
movsd 0x10(%rbx,%rax), %xmm0
minsd (%r14,%rax), %xmm0
movsd %xmm0, 0x10(%r13,%rax)
movsd 0x30(%rbx,%rax), %xmm0
maxsd (%r14,%rax), %xmm0
movsd %xmm0, 0x30(%r13,%rax)
movsd 0x10(%rbx,%rax), %xmm0
movsd %xmm0, -0x20(%rbp,%rax)
movsd 0x30(%rbx,%rax), %xmm0
movsd %xmm0, (%rbp,%rax)
addq $0x8, %rax
cmpq $0x20, %rax
jne 0x42b5
movq $0x0, 0x68(%r8)
movslq 0x58(%rbx), %rax
cmpq $-0x1, %rax
movl 0xc(%rsp), %esi
movq %r8, 0x28(%rsp)
jne 0x4344
movslq 0x17dc(%rdi), %rcx
imulq $-0x38, %rcx, %rcx
addq %rdi, %rcx
movslq 0x1c(%rsp), %rdx
imulq $0x38, %rdx, %rdx
cmpl %esi, 0x17bc(%rdx,%rcx)
jne 0x4344
leaq (%rdx,%rcx), %rax
addq $0x17bc, %rax           # imm = 0x17BC
jmp 0x4360
shlq $0x7, %rax
movq 0x48(%rsp), %rdx
leaq (%rdx,%rax), %rcx
addq $0x50, %rcx
cmpl %esi, (%rcx)
leaq 0x54(%rdx,%rax), %rax
cmoveq %rcx, %rax
movl 0x20(%rsp), %r12d
movl %r12d, (%rax)
xorl %eax, %eax
leaq 0x44(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x40(%rsp), %r8
movl %eax, (%r8)
movq %rdi, 0x50(%rsp)
movq 0x30(%rsp), %r15
movl %r15d, %edx
movsd 0x10(%rsp), %xmm0
callq 0x4a68
movl 0xc(%rsp), %esi
leaq 0x44(%rsp), %rax
movl (%rax), %eax
leaq 0x40(%rsp), %rcx
movl (%rcx), %edx
cmpl %edx, %eax
movq 0x68(%rsp), %rdi
cmovgq 0x60(%rsp), %rdi
cmovgq 0x58(%rsp), %rbp
movl %esi, %ecx
movl 0x38(%rsp), %r8d
cmovgl %r8d, %ecx
cmovgl %esi, %r8d
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rdi)
movsd %xmm0, (%rbp,%r15,8)
movq 0x50(%rsp), %rbp
movl %ecx, 0x54(%r13)
movl %r8d, 0x50(%r13)
movl 0x58(%rbx), %ecx
movl %ecx, 0x58(%r13)
movl %r12d, 0x58(%rbx)
movq 0x28(%rsp), %r15
movl %r12d, 0x58(%r15)
movl 0x14(%rbp), %ecx
cmpq $0x3, %rcx
ja 0x4488
leaq 0x1ccd(%rip), %r9       # 0x60d8
movslq (%r9,%rcx,4), %rcx
addq %r9, %rcx
jmpq *%rcx
xorl %ecx, %ecx
movl %ecx, 0x70(%rbx)
movl %ecx, 0x70(%r15)
jmp 0x4488
leal (%rdx,%rax), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm0, %xmm1
movsd 0x1bdf(%rip), %xmm0     # 0x6018
cvtsi2sdl 0x70(%rbx), %xmm2
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %ecx
jmp 0x4471
movl $0x0, 0x70(%r15)
decl 0x70(%rbx)
jmp 0x4488
xorps %xmm0, %xmm0
cvtsi2sdl 0x70(%rbx), %xmm0
movsd 0x1bcf(%rip), %xmm1     # 0x6038
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x70(%r15)
xorps %xmm0, %xmm0
cvtsi2sdl 0x70(%rbx), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x70(%rbx)
xorl %ebx, %ebx
xorl %ecx, %ecx
cmpl %edx, %eax
setg %bl
setle %cl
movl $0x0, 0x70(%r13)
leaq 0x70(%rsp), %r8
movq $0x0, (%r8)
movq %rbp, %rdi
movq 0x30(%rsp), %r12
movl %r12d, %edx
movsd 0x10(%rsp), %xmm0
callq 0x4c12
leaq 0x70(%rsp), %rax
movq (%rax), %xmm0
movq 0x68(%r15), %xmm1
paddd %xmm0, %xmm1
movq %xmm1, 0x68(%r15)
movq %rbp, %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
movsd 0x10(%rsp), %xmm0
movl %ebx, %ecx
movl 0x1c(%rsp), %r8d
callq 0x4eca
movsd (%r14,%r12,8), %xmm0
xorl %eax, %eax
ucomisd 0x10(%rsp), %xmm0
seta %al
movl 0x50(%r13,%rax,4), %esi
movq %rbp, %rdi
movq %r14, %rdx
movl 0x3c(%rsp), %ecx
callq 0x534a
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop