// 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>::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, 0x28(%rsp)
movsd %xmm0, 0x30(%rsp)
movl %r8d, 0x44(%rsp)
movq %rcx, %r14
movl %edx, 0x24(%rsp)
movq 0xb0(%rsp), %rax
leaq 0x44(%rdi), %rcx
movq %rcx, 0x50(%rsp)
movslq %esi, %rcx
shlq $0x7, %rcx
movq %rdi, 0x10(%rsp)
leaq (%rdi,%rcx), %r15
addq $0x44, %r15
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movsd 0x30(%r15,%rcx,8), %xmm1
subsd 0x10(%r15,%rcx,8), %xmm1
movsd %xmm1, (%rax,%rcx,8)
addsd %xmm1, %xmm0
incq %rcx
cmpq $0x4, %rcx
jne 0x40e7
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4125
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
addsd (%rax,%rcx,8), %xmm0
incq %rcx
cmpq $0x4, %rcx
jne 0x4117
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 0x4136
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movl %esi, 0xc(%rsp)
movsd (%rax,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jae 0x4174
incq %rcx
cmpq $0x4, %rcx
jne 0x415b
movl $0x4, %ecx
cmpl $0x4, %ecx
movl $0x4, %eax
cmovll %ecx, %eax
movl %eax, 0x40(%rsp)
movslq %eax, %rcx
movq %rcx, 0x38(%rsp)
leaq (%r15,%rcx,8), %rax
addq $0x10, %rax
movq %rax, 0x68(%rsp)
leaq 0x30(%r15), %r13
leaq 0x30(%r15,%rcx,8), %rax
movsd -0x20(%rax), %xmm0
movq %rax, 0x60(%rsp)
movsd (%rax), %xmm1
subsd %xmm0, %xmm1
xorpd %xmm2, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x18(%rsp)
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x2600
movsd 0x30(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movl %eax, 0x30(%rsp)
cltq
shlq $0x7, %rax
movq %rax, 0x58(%rsp)
movq 0x50(%rsp), %rbx
leaq (%rbx,%rax), %rbp
movsd %xmm0, 0x60(%rbx,%rax)
movq %r12, %rdi
callq 0x2600
movsd 0x17e8(%r12), %xmm0
movl %eax, 0x28(%rsp)
cltq
shlq $0x7, %rax
movsd %xmm0, 0x60(%rbx,%rax)
addl $-0x2, 0x17d8(%r12)
leaq (%rbx,%rax), %r12
movq 0x38(%rsp), %rax
movq 0x58(%rsp), %rcx
movl %eax, (%rbx,%rcx)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x8(%rbx,%rcx)
xorl %eax, %eax
movsd -0x20(%r13,%rax), %xmm0
minsd (%r14,%rax), %xmm0
movsd %xmm0, 0x10(%rbp,%rax)
movsd (%r13,%rax), %xmm0
maxsd (%r14,%rax), %xmm0
movsd %xmm0, 0x30(%rbp,%rax)
movsd -0x20(%r13,%rax), %xmm0
movsd %xmm0, 0x10(%r12,%rax)
movsd (%r13,%rax), %xmm0
movsd %xmm0, 0x30(%r12,%rax)
addq $0x8, %rax
cmpq $0x20, %rax
jne 0x4246
movq $0x0, 0x68(%r12)
movslq 0x58(%r15), %rax
cmpq $-0x1, %rax
movl 0xc(%rsp), %esi
jne 0x42da
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 0x42da
leaq (%rdx,%rcx), %rax
addq $0x17bc, %rax           # imm = 0x17BC
jmp 0x42f6
shlq $0x7, %rax
movq 0x50(%rsp), %rdx
leaq (%rdx,%rax), %rcx
addq $0x50, %rcx
cmpl %esi, (%rcx)
leaq 0x54(%rdx,%rax), %rax
cmoveq %rcx, %rax
movl 0x30(%rsp), %r13d
movl %r13d, (%rax)
xorl %eax, %eax
leaq 0x4c(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x48(%rsp), %r8
movl %eax, (%r8)
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movq 0x38(%rsp), %rbx
movl %ebx, %edx
movsd 0x18(%rsp), %xmm0
callq 0x49c4
movl 0xc(%rsp), %edx
leaq 0x4c(%rsp), %rax
movl (%rax), %edi
leaq 0x48(%rsp), %rax
movl (%rax), %r8d
xorl %eax, %eax
cmpl %r8d, %edi
setle %al
movl %edx, %ecx
movl 0x28(%rsp), %r9d
cmovgl %r9d, %ecx
cmovgl %edx, %r9d
movq 0x68(%rsp), %rsi
cmovgq 0x60(%rsp), %rsi
shll $0x5, %eax
movl %ecx, 0x54(%rbp)
movl %r9d, 0x50(%rbp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsi)
addq %r12, %rax
movsd %xmm0, 0x10(%rax,%rbx,8)
movl 0x58(%r15), %eax
movl %eax, 0x58(%rbp)
movl %r13d, 0x58(%r15)
movl %r13d, 0x58(%r12)
movq 0x10(%rsp), %rax
movl 0x14(%rax), %eax
cmpq $0x3, %rax
ja 0x442c
leaq 0x1cf3(%rip), %rcx      # 0x6098
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %eax, %eax
movl %eax, 0x70(%r15)
movl %eax, 0x70(%r12)
jmp 0x442c
leal (%r8,%rdi), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
cvtsi2sd %edi, %xmm1
divsd %xmm0, %xmm1
movsd 0x1c42(%rip), %xmm0     # 0x6018
cvtsi2sdl 0x70(%r15), %xmm2
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %eax
jmp 0x4412
movl $0x0, 0x70(%r12)
decl 0x70(%r15)
jmp 0x442c
xorps %xmm0, %xmm0
cvtsi2sdl 0x70(%r15), %xmm0
movsd 0x1c26(%rip), %xmm1     # 0x6030
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x70(%r12)
xorps %xmm0, %xmm0
cvtsi2sdl 0x70(%r15), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x70(%r15)
xorl %ecx, %ecx
movq %rdi, %rbx
movq %r8, %r13
cmpl %r8d, %edi
setle %cl
movl $0x0, 0x70(%rbp)
leaq 0x70(%rsp), %r8
movq $0x0, (%r8)
movq 0x10(%rsp), %rdi
movl %edx, %esi
movl 0x40(%rsp), %r15d
movl %r15d, %edx
movsd 0x18(%rsp), %xmm0
callq 0x4b90
xorl %eax, %eax
movl 0x70(%rsp,%rax,4), %ecx
addl %ecx, 0x68(%r12,%rax,4)
incq %rax
cmpq $0x1, %rax
je 0x4469
xorl %ecx, %ecx
cmpl %r13d, %ebx
setg %cl
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl %r15d, %edx
movsd 0x18(%rsp), %xmm0
movl 0x24(%rsp), %r8d
callq 0x4e60
movq 0x38(%rsp), %rax
movsd (%r14,%rax,8), %xmm0
xorl %eax, %eax
ucomisd 0x18(%rsp), %xmm0
seta %al
movl 0x50(%rbp,%rax,4), %esi
movq %rbx, %rdi
movq %r14, %rdx
movl 0x44(%rsp), %ecx
callq 0x52f6
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq