// File: /workspace/github_cpp_makefile_star2_O2/big-data-lab-team[P]OrpailleCC/./src/mondrian_coarse.hpp
// Function: int CoarseMondrianForest<double, funct, KappaMetrics<2, false>, 4, 2, 6000>::adjust_counters<100>(int, int, double, bool, int*)
int adjust_counters(int const start_id, int split_dimension, double split_value, bool discharge_left, int* to_remove){
	//Initialize an array to keep track of where we have to go at each tree level.
	//The max depth expected is MAX_DEPTH.
	int stack[max_stack_size];
	for(int i = 0; i < max_stack_size; ++i)
		stack[i] = -1;

	int node_id = start_id;
	int depth = 0;
	//a for loop instead of a while to avoid infinite loops. Since we don't expect to do more turn than node_count
	//*i* count the number of node deleted.
	int i = 0;
	while(i < node_count && node_id >= 0){
		Node& node = nodes()[node_id];
		if(!node.is_leaf()){  //Internal node
			if (stack[depth] == -1){ //Go right
				stack[depth] = 0;
				depth += 1;
				node_id = node.child_right;
			}
			else if (stack[depth] == 0){ //Go left
				stack[depth] = 1;
				depth += 1;
				node_id = node.child_left;
			}
			else if (stack[depth] == 1){ //Go up
				stack[depth] = -1; //Reset to -1
				depth -= 1;
				i += 1;
				if(node_id == start_id)
					break;
				node_id = node.parent;
			}
		}
		else{ //Leaf
			stack[depth] = -1; //Reset to -1
			depth -= 1;
			i += 1;


			//Compute size of box
			double const max_value = node.bound_upper[split_dimension] - node.bound_lower[split_dimension];
			//Get the percentage on the left (below the split value)
			double percentage = Utils::min(Utils::max((split_value - node.bound_lower[split_dimension]) / max_value, 0.0), 1.0);
			//If this sub-tree is not on the left, invert percentage
			if(!discharge_left)
				percentage = 1 - percentage;
			for(int i = 0; i < label_count; ++i){
				int counter_out = Utils::round(static_cast<double>(node.counters[i]) * percentage);
				int counter_in = Utils::round(static_cast<double>(node.counters[i]) * (1 - percentage));
				int const err = node.counters[i] - counter_in - counter_out;
				if(err != 0)
					cout << "Error counters" << endl;
				to_remove[i] += counter_out;
				node.counters[i] = counter_in;
			}
			if(node_id == start_id)
				break;
			node_id = node.parent;
		}
		if(depth >= max_stack_size)
			return -1;
	}
	if(depth != -1)
		return -1;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movq %r8, %rbx
movsd %xmm0, 0x38(%rsp)
xorl %eax, %eax
cmpq $0x64, %rax
je 0x4678
orl $-0x1, 0x50(%rsp,%rax,4)
incq %rax
jmp 0x4668
leaq 0x44(%rdi), %r9
movslq %edx, %r10
xorl %r11d, %r11d
movsd 0x198e(%rip), %xmm0     # 0x6018
xorpd %xmm4, %xmm4
movapd 0x19ca(%rip), %xmm5     # 0x6060
movapd 0x19d2(%rip), %xmm6     # 0x6070
pushq $0x50
popq %r14
pushq $0x54
popq %r13
xorl %r15d, %r15d
movl %esi, %ebp
cmpl 0x17d4(%rdi), %r11d
setl %al
testl %ebp, %ebp
setns %r8b
andb %al, %r8b
movl %ebp, %r12d
shlq $0x7, %r12
addq %r9, %r12
movslq %r15d, %rax
testb %r8b, %r8b
je 0x4889
cmpl $-0x1, (%r12)
je 0x4704
movl 0x50(%rsp,%rax,4), %edx
cmpl $0x1, %edx
je 0x484f
testl %edx, %edx
je 0x4867
cmpl $-0x1, %edx
jne 0x46cb
andl $0x0, 0x50(%rsp,%rax,4)
incl %eax
movq %r13, %rdx
jmp 0x4874
orl $-0x1, 0x50(%rsp,%rax,4)
movsd 0x10(%r12,%r10,8), %xmm1
movsd 0x30(%r12,%r10,8), %xmm2
subsd %xmm1, %xmm2
movapd %xmm0, %xmm3
movsd 0x38(%rsp), %xmm0
subsd %xmm1, %xmm0
divsd %xmm2, %xmm0
maxsd 0x18eb(%rip), %xmm0     # 0x6020
minsd %xmm3, %xmm0
testb %cl, %cl
movl %ecx, 0xc(%rsp)
movl %esi, 0x14(%rsp)
movq %rdi, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq %r10, 0x20(%rsp)
movq %r11, 0x18(%rsp)
movl %ebp, 0x10(%rsp)
jne 0x4769
movapd %xmm3, %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
decl %r15d
subsd %xmm0, %xmm3
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
xorl %ebp, %ebp
movapd %xmm3, 0x40(%rsp)
cmpq $0x2, %rbp
je 0x4815
movl 0x68(%r12,%rbp,4), %eax
cvtsi2sd %eax, %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm3, %xmm0
movapd %xmm0, %xmm1
cmpltpd %xmm4, %xmm1
movapd %xmm1, %xmm2
andnpd %xmm5, %xmm2
andpd %xmm6, %xmm1
orpd %xmm2, %xmm1
addpd %xmm0, %xmm1
cvttpd2dq %xmm1, %xmm0
movd %xmm0, %r14d
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %r13d
leal (%r14,%r13), %ecx
cmpl %ecx, %eax
je 0x4804
movq 0x4802(%rip), %rdi      # 0x8fd8
leaq 0x18e5(%rip), %rsi      # 0x60c2
callq 0x1060
movq %rax, %rdi
callq 0x1030
movapd 0x40(%rsp), %xmm3
movapd 0x1878(%rip), %xmm6     # 0x6070
movapd 0x1860(%rip), %xmm5     # 0x6060
xorpd %xmm4, %xmm4
addl %r13d, (%rbx,%rbp,4)
movl %r14d, 0x68(%r12,%rbp,4)
incq %rbp
jmp 0x477c
movl 0x14(%rsp), %esi
cmpl %esi, 0x10(%rsp)
je 0x4889
movq 0x18(%rsp), %r11
incl %r11d
pushq $0x58
popq %rdx
movl 0xc(%rsp), %ecx
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %r9
movq 0x20(%rsp), %r10
movsd 0x17d3(%rip), %xmm0     # 0x6018
pushq $0x50
popq %r14
pushq $0x54
popq %r13
jmp 0x4877
orl $-0x1, 0x50(%rsp,%rax,4)
movl %eax, %r15d
decl %r15d
cmpl %esi, %ebp
je 0x4889
incl %r11d
addq $0x58, %r12
jmp 0x4880
movl $0x1, 0x50(%rsp,%rax,4)
incl %eax
movq %r14, %rdx
movl %eax, %r15d
cmpl $0x63, %r15d
jg 0x48a6
addq %rdx, %r12
movl (%r12), %ebp
jmp 0x46ab
xorl %eax, %eax
cmpl $-0x1, %r15d
setne %al
negl %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rax
jmp 0x4894
nop