// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movsd %xmm0, -0x28(%rbp)
movsd %xmm1, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
callq 0x1b00
movslq -0xc(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
cmpl $0x4, -0x4c(%rbp)
jge 0x7b9e
movq -0x48(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd 0x30(%rax,%rcx,8), %xmm0
movq -0x48(%rbp), %rax
movslq -0x4c(%rbp), %rcx
subsd 0x10(%rax,%rcx,8), %xmm0
movq 0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x7b4d
movq 0x10(%rbp), %rdi
movsd -0x40(%rbp), %xmm0
movl $0x4, %esi
callq 0x7480
movq 0x10(%rbp), %rdi
movl $0x4, %esi
callq 0x7540
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movslq -0x50(%rbp), %rcx
movsd 0x10(%rax,%rcx,8), %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x48(%rbp), %rax
movslq -0x50(%rbp), %rcx
movsd 0x30(%rax,%rcx,8), %xmm0
movsd %xmm0, -0x60(%rbp)
callq 0x35a0
movq -0xc8(%rbp), %rdi
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
movsd -0x60(%rbp), %xmm2
subsd -0x58(%rbp), %xmm2
movsd -0x58(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
callq 0x3860
movq -0xc8(%rbp), %rdi
movl %eax, -0x34(%rbp)
movsd -0x28(%rbp), %xmm0
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
callq 0x1b00
movsd -0xd8(%rbp), %xmm0
movq -0xc8(%rbp), %rdi
movslq -0x34(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movsd %xmm0, 0x60(%rax)
callq 0x3860
movq -0xc8(%rbp), %rdi
movl %eax, -0x38(%rbp)
movsd 0x17e8(%rdi), %xmm0
movsd %xmm0, -0xd0(%rbp)
callq 0x1b00
movsd -0xd0(%rbp), %xmm0
movq -0xc8(%rbp), %rdi
movslq -0x38(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movsd %xmm0, 0x60(%rax)
movl 0x17d8(%rdi), %eax
subl $0x2, %eax
movl %eax, 0x17d8(%rdi)
callq 0x1b00
movq -0xc8(%rbp), %rdi
movslq -0x34(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
callq 0x1b00
movslq -0x38(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movl -0x50(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, (%rax)
movsd -0x70(%rbp), %xmm0
movq -0x78(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movl $0x0, -0x84(%rbp)
cmpl $0x4, -0x84(%rbp)
jge 0x7dd4
movq -0x48(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd 0x10(%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
callq 0x8bc0
movq -0x78(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd %xmm0, 0x10(%rax,%rcx,8)
movq -0x48(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd 0x30(%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
callq 0x2a80
movq -0x78(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd %xmm0, 0x30(%rax,%rcx,8)
movq -0x48(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd 0x10(%rax,%rcx,8), %xmm0
movq -0x80(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd %xmm0, 0x10(%rax,%rcx,8)
movq -0x48(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd 0x30(%rax,%rcx,8), %xmm0
movq -0x80(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd %xmm0, 0x30(%rax,%rcx,8)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x7d01
movl $0x0, -0x88(%rbp)
cmpl $0x2, -0x88(%rbp)
jge 0x7e0b
movq -0x80(%rbp), %rax
movslq -0x88(%rbp), %rcx
movl $0x0, 0x68(%rax,%rcx,4)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x7dde
movq -0x48(%rbp), %rdi
callq 0x75b0
testb $0x1, %al
jne 0x7e71
movq -0xc8(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, -0xdc(%rbp)
callq 0x1a50
movq %rax, %rcx
movl -0xdc(%rbp), %eax
movslq -0x10(%rbp), %rdx
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jne 0x7e71
movq -0xc8(%rbp), %rdi
movl -0x34(%rbp), %eax
movl %eax, -0xe0(%rbp)
callq 0x1a50
movl -0xe0(%rbp), %ecx
movslq -0x10(%rbp), %rdx
imulq $0x38, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
jmp 0x7ec0
movq -0xc8(%rbp), %rdi
callq 0x1b00
movq -0x48(%rbp), %rcx
movslq 0x58(%rcx), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movl 0x50(%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0x7eb1
movl -0x34(%rbp), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x50(%rax)
jmp 0x7ebe
movl -0x34(%rbp), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x54(%rax)
jmp 0x7ec0
movq -0xc8(%rbp), %rdi
movl $0x0, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
movl -0xc(%rbp), %esi
movl -0x50(%rbp), %edx
movsd -0x70(%rbp), %xmm0
leaq -0x94(%rbp), %rcx
leaq -0x98(%rbp), %r8
callq 0x8c00
movl -0x94(%rbp), %eax
cmpl -0x98(%rbp), %eax
setg %al
andb $0x1, %al
movb %al, -0x99(%rbp)
testb $0x1, -0x99(%rbp)
je 0x7f55
movl -0x38(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x54(%rax)
movl -0xc(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x50(%rax)
movsd -0x70(%rbp), %xmm0
movq -0x48(%rbp), %rax
movslq -0x50(%rbp), %rcx
movsd %xmm0, 0x30(%rax,%rcx,8)
movsd -0x70(%rbp), %xmm0
movq -0x80(%rbp), %rax
movslq -0x50(%rbp), %rcx
movsd %xmm0, 0x10(%rax,%rcx,8)
jmp 0x7f8f
movl -0xc(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x54(%rax)
movl -0x38(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x50(%rax)
movsd -0x70(%rbp), %xmm0
movq -0x48(%rbp), %rax
movslq -0x50(%rbp), %rcx
movsd %xmm0, 0x10(%rax,%rcx,8)
movsd -0x70(%rbp), %xmm0
movq -0x80(%rbp), %rax
movslq -0x50(%rbp), %rcx
movsd %xmm0, 0x30(%rax,%rcx,8)
movq -0xc8(%rbp), %rax
movq -0x48(%rbp), %rcx
movl 0x58(%rcx), %edx
movq -0x78(%rbp), %rcx
movl %edx, 0x58(%rcx)
movl -0x34(%rbp), %edx
movq -0x48(%rbp), %rcx
movl %edx, 0x58(%rcx)
movl -0x34(%rbp), %edx
movq -0x80(%rbp), %rcx
movl %edx, 0x58(%rcx)
cmpl $0x0, 0x14(%rax)
jne 0x7fd9
movq -0x48(%rbp), %rax
movl $0x0, 0x70(%rax)
movq -0x80(%rbp), %rax
movl $0x0, 0x70(%rax)
jmp 0x810c
movq -0xc8(%rbp), %rax
cmpl $0x1, 0x14(%rax)
jne 0x8027
movq -0x48(%rbp), %rax
cvtsi2sdl 0x70(%rax), %xmm1
movsd 0x3039(%rip), %xmm0     # 0xb030
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movq -0x80(%rbp), %rax
movl %ecx, 0x70(%rax)
movq -0x48(%rbp), %rax
cvtsi2sdl 0x70(%rax), %xmm0
movsd 0x3019(%rip), %xmm1     # 0xb030
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x70(%rax)
jmp 0x810a
movq -0xc8(%rbp), %rax
cmpl $0x2, 0x14(%rax)
jne 0x80e1
movl -0x94(%rbp), %eax
addl -0x98(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xa8(%rbp)
testb $0x1, -0x99(%rbp)
je 0x8073
cvtsi2sdl -0x94(%rbp), %xmm0
divsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
jmp 0x808b
cvtsi2sdl -0x94(%rbp), %xmm0
divsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd -0xe8(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd 0x2f75(%rip), %xmm0     # 0xb018
subsd -0xb0(%rbp), %xmm0
movq -0x48(%rbp), %rax
cvtsi2sdl 0x70(%rax), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movq -0x80(%rbp), %rax
movl %ecx, 0x70(%rax)
movsd -0xb0(%rbp), %xmm1
movq -0x48(%rbp), %rax
cvtsi2sdl 0x70(%rax), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x70(%rax)
jmp 0x8108
movq -0xc8(%rbp), %rax
cmpl $0x3, 0x14(%rax)
jne 0x8106
movq -0x80(%rbp), %rax
movl $0x0, 0x70(%rax)
movq -0x48(%rbp), %rax
movl 0x70(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x70(%rax)
jmp 0x8108
jmp 0x810a
jmp 0x810c
movq -0x78(%rbp), %rax
movl $0x0, 0x70(%rax)
leaq -0xb8(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1030
movq -0xc8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x50(%rbp), %edx
movsd -0x70(%rbp), %xmm0
movb -0x99(%rbp), %al
xorb $-0x1, %al
leaq -0xb8(%rbp), %r8
movzbl %al, %ecx
andl $0x1, %ecx
callq 0x9050
movl $0x0, -0xbc(%rbp)
cmpl $0x2, -0xbc(%rbp)
jge 0x819b
movslq -0xbc(%rbp), %rax
movl -0xb8(%rbp,%rax,4), %edx
movq -0x80(%rbp), %rax
movslq -0xbc(%rbp), %rcx
addl 0x68(%rax,%rcx,4), %edx
movl %edx, 0x68(%rax,%rcx,4)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x8160
movq -0xc8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x50(%rbp), %edx
movsd -0x70(%rbp), %xmm0
movb -0x99(%rbp), %al
movl -0x10(%rbp), %r8d
andb $0x1, %al
movzbl %al, %ecx
callq 0x9450
movq -0x18(%rbp), %rax
movslq -0x50(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x70(%rbp), %xmm0
jbe 0x81f1
movq -0xc8(%rbp), %rdi
movq -0x78(%rbp), %rax
movl 0x54(%rax), %esi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
callq 0x9be0
jmp 0x820b
movq -0xc8(%rbp), %rdi
movq -0x78(%rbp), %rax
movl 0x50(%rax), %esi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
callq 0x9be0
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop