// File: /workspace/github_cpp_makefile_star2_O3/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, 0x38(%rsp)
movl %ecx, 0x4(%rsp)
movq %xmm0, 0x30(%rsp)
movl %edx, %r15d
movl %esi, %ebx
movq %rdi, %r14
leaq 0x50(%rsp), %rdi
movl $0x190, %edx            # imm = 0x190
movl $0xff, %esi
callq 0x1040
movl 0x17d4(%r14), %esi
testl %esi, %esi
setle %cl
testl %ebx, %ebx
sets %dl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
orb %cl, %dl
jne 0x4eb8
leaq 0x44(%r14), %rdi
movslq %r15d, %rax
movq %rax, 0x28(%rsp)
xorl %r8d, %r8d
xorpd %xmm3, %xmm3
movapd 0x1419(%rip), %xmm4     # 0x60a0
movapd 0x1421(%rip), %xmm5     # 0x60b0
movq 0x3342(%rip), %r13      # 0x7fd8
movl %ebx, %eax
xorl %r9d, %r9d
movq %r14, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movl %ebx, (%rsp)
movl %eax, %ecx
shlq $0x7, %rcx
leaq (%rdi,%rcx), %r12
cmpl $-0x1, (%rdi,%rcx)
movslq %r8d, %rcx
je 0x4ced
movl 0x50(%rsp,%rcx,4), %edx
cmpl $0x1, %edx
je 0x4e85
testl %edx, %edx
je 0x4e53
cmpl $-0x1, %edx
jne 0x4e76
movl $0x0, 0x50(%rsp,%rcx,4)
incl %ecx
movl $0x54, %eax
jmp 0x4e62
movl %eax, 0xc(%rsp)
movl $0xffffffff, 0x50(%rsp,%rcx,4) # imm = 0xFFFFFFFF
movq 0x28(%rsp), %rax
movsd 0x10(%r12,%rax,8), %xmm1
movsd 0x30(%r12,%rax,8), %xmm2
subsd %xmm1, %xmm2
movsd 0x30(%rsp), %xmm0
subsd %xmm1, %xmm0
divsd %xmm2, %xmm0
movsd 0x12f2(%rip), %xmm2     # 0x6018
maxsd 0x12f2(%rip), %xmm0     # 0x6020
minsd %xmm2, %xmm0
cmpb $0x0, 0x4(%rsp)
movl %r9d, 0x8(%rsp)
jne 0x4d4a
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
decl %r8d
movq %r8, 0x10(%rsp)
movapd %xmm2, %xmm6
subsd %xmm0, %xmm6
unpcklpd %xmm0, %xmm6            # xmm6 = xmm6[0],xmm0[0]
movb $0x1, %r15b
xorl %ebp, %ebp
movapd %xmm6, 0x40(%rsp)
movl 0x68(%r12,%rbp,4), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm6, %xmm0
movapd %xmm0, %xmm1
cmpltpd %xmm3, %xmm1
movapd %xmm1, %xmm2
andnpd %xmm4, %xmm2
andpd %xmm5, %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, %ebx
leal (%r14,%rbx), %ecx
cmpl %ecx, %eax
je 0x4e0a
movl $0xe, %edx
movq %r13, %rdi
leaq 0x133f(%rip), %rsi      # 0x6102
callq 0x1090
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x1080
movsbl %al, %esi
movq %r13, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1060
movapd 0x40(%rsp), %xmm6
movapd 0x12b2(%rip), %xmm5     # 0x60b0
movapd 0x129a(%rip), %xmm4     # 0x60a0
xorpd %xmm3, %xmm3
movq 0x38(%rsp), %rax
addl %ebx, (%rax,%rbp,4)
movl %r14d, 0x68(%r12,%rbp,4)
movl $0x1, %ebp
testb $0x1, %r15b
movl $0x0, %r15d
jne 0x4d69
movl (%rsp), %ebx
cmpl %ebx, 0xc(%rsp)
je 0x4ea8
movl 0x8(%rsp), %r9d
incl %r9d
movl $0x58, %eax
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
jmp 0x4e65
movl $0x1, 0x50(%rsp,%rcx,4)
incl %ecx
movl $0x50, %eax
movl %ecx, %r8d
cmpl $0x63, %r8d
jg 0x4ea1
movl (%r12,%rax), %eax
movl 0x17d4(%r14), %esi
cmpl %esi, %r9d
jge 0x4ead
testl %eax, %eax
jns 0x4ca8
jmp 0x4ead
movl $0xffffffff, 0x50(%rsp,%rcx,4) # imm = 0xFFFFFFFF
movl %ecx, %r8d
decl %r8d
cmpl %ebx, %eax
je 0x4ead
incl %r9d
movl 0x58(%r12), %eax
jmp 0x4e76
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x4eb8
movq 0x10(%rsp), %r8
xorl %eax, %eax
cmpl $-0x1, %r8d
setne %al
negl %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq