// 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>::find_barycenter<100>(int, double*)
int find_barycenter(int const start_id, double* avg_features){
	//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;
	double count[feature_count];
	for(int i = 0; i < feature_count; ++i){
		count[i] = 0;
		avg_features[i] = 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;

			//Count the number of data points
			double data_point_count = 0;
			for(int i = 0; i < label_count; ++i)
				data_point_count += static_cast<double>(node.counters[i]);
			for(int i = 0; i < feature_count; ++i){
				double const middle = (node.bound_upper[i] + node.bound_lower[i]) * 0.5;
				avg_features[i] += middle * data_point_count;
				count[i] += data_point_count;
			}

			if(node_id == start_id)
				break;
			node_id = node.parent;
		}
		if(depth >= max_stack_size)
			return -1;
	}
	if(depth != -1)
		return -1;
	for(int i = 0; i < feature_count; ++i){
		avg_features[i] /= count[i];
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
leaq 0x20(%rsp), %rdi
movl $0x190, %edx            # imm = 0x190
movl $0xff, %esi
callq 0x1040
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd %xmm0, (%rsp)
movupd %xmm0, 0x10(%rbx)
movupd %xmm0, (%rbx)
movl 0x17d4(%r14), %ecx
testl %ecx, %ecx
setle %dl
testl %ebp, %ebp
sets %sil
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
orb %dl, %sil
jne 0x5563
addq $0x44, %r14
xorl %edx, %edx
movsd 0xc01(%rip), %xmm0      # 0x6038
xorl %esi, %esi
movl %ebp, %r8d
movl %r8d, %r9d
shlq $0x7, %r9
leaq (%r14,%r9), %rdi
cmpl $-0x1, (%r14,%r9)
movslq %esi, %r9
je 0x548a
movl 0x20(%rsp,%r9,4), %r10d
cmpl $0x1, %r10d
je 0x5526
testl %r10d, %r10d
je 0x54f9
cmpl $-0x1, %r10d
jne 0x5517
movl $0x0, 0x20(%rsp,%r9,4)
incl %r9d
movl $0x54, %r8d
jmp 0x550b
movl $0xffffffff, 0x20(%rsp,%r9,4) # imm = 0xFFFFFFFF
movl %r9d, %esi
decl %esi
xorps %xmm2, %xmm2
cvtsi2sdl 0x68(%rdi), %xmm2
xorps %xmm1, %xmm1
cvtsi2sdl 0x6c(%rdi), %xmm1
addsd %xmm2, %xmm1
xorl %r9d, %r9d
movsd 0x30(%rdi,%r9,8), %xmm2
addsd 0x10(%rdi,%r9,8), %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
addsd (%rbx,%r9,8), %xmm2
movsd %xmm2, (%rbx,%r9,8)
movsd (%rsp,%r9,8), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, (%rsp,%r9,8)
incq %r9
cmpq $0x4, %r9
jne 0x54af
cmpl %ebp, %r8d
je 0x5541
incl %edx
movl $0x58, %r8d
jmp 0x550e
movl $0x1, 0x20(%rsp,%r9,4)
incl %r9d
movl $0x50, %r8d
movl %r9d, %esi
cmpl $0x63, %esi
jg 0x5563
movl (%rdi,%r8), %r8d
cmpl %ecx, %edx
jge 0x5541
testl %r8d, %r8d
jns 0x543c
jmp 0x5541
movl $0xffffffff, 0x20(%rsp,%r9,4) # imm = 0xFFFFFFFF
movl %r9d, %esi
decl %esi
cmpl %ebp, %r8d
je 0x5541
incl %edx
movl 0x58(%rdi), %r8d
jmp 0x5517
cmpl $-0x1, %esi
jne 0x5563
xorl %eax, %eax
xorl %ecx, %ecx
movupd (%rbx,%rcx,8), %xmm0
divpd (%rsp,%rcx,8), %xmm0
movupd %xmm0, (%rbx,%rcx,8)
addq $0x2, %rcx
cmpq $0x4, %rcx
jne 0x554a
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbx
popq %r14
popq %rbp
retq
nop