// 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>::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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
xorl %eax, %eax
cmpq $0x64, %rax
je 0x4cdb
orl $-0x1, -0x60(%rsp,%rax,4)
incq %rax
jmp 0x4ccb
xorl %eax, %eax
cmpq $0x4, %rax
je 0x4cf3
andq $0x0, -0x80(%rsp,%rax,8)
andq $0x0, (%rdx,%rax,8)
incq %rax
jmp 0x4cdd
movl 0x17d4(%rdi), %ecx
addq $0x44, %rdi
xorl %r15d, %r15d
movabsq $0x58, %r8
movsd 0x1326(%rip), %xmm0     # 0x6038
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movabsq $0x50, %r9
movabsq $0x54, %r10
movl %esi, %ebp
xorl %r11d, %r11d
cmpl %ecx, %r11d
setl %bl
testl %ebp, %ebp
setns %r12b
andb %bl, %r12b
movl %ebp, %ebx
shlq $0x7, %rbx
addq %rdi, %rbx
movslq %r15d, %r14
testb %r12b, %r12b
je 0x4e28
cmpl $-0x1, (%rbx)
je 0x4d88
movl -0x60(%rsp,%r14,4), %r13d
cmpl $0x1, %r13d
je 0x4ded
testl %r13d, %r13d
je 0x4e03
cmpl $-0x1, %r13d
jne 0x4d4b
andl $0x0, -0x60(%rsp,%r14,4)
incl %r14d
movq %r10, %r15
jmp 0x4e12
orl $-0x1, -0x60(%rsp,%r14,4)
decl %r14d
cvtsi2sdl 0x68(%rbx), %xmm2
cvtsi2sdl 0x6c(%rbx), %xmm1
addsd %xmm2, %xmm1
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0x4de1
movsd 0x30(%rbx,%r15,8), %xmm2
addsd 0x10(%rbx,%r15,8), %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
addsd (%rdx,%r15,8), %xmm2
movsd %xmm2, (%rdx,%r15,8)
movsd -0x80(%rsp,%r15,8), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, -0x80(%rsp,%r15,8)
incq %r15
jmp 0x4da2
cmpl %esi, %ebp
je 0x4e25
incl %r11d
movq %r8, %r15
jmp 0x4e12
orl $-0x1, -0x60(%rsp,%r14,4)
decl %r14d
cmpl %esi, %ebp
je 0x4e25
incl %r11d
addq $0x58, %rbx
jmp 0x4e1b
movl $0x1, -0x60(%rsp,%r14,4)
incl %r14d
movq %r9, %r15
cmpl $0x63, %r14d
jg 0x4e2e
addq %r15, %rbx
movl (%rbx), %ebp
movl %r14d, %r15d
jmp 0x4d30
movl %r14d, %r15d
cmpl $-0x1, %r15d
je 0x4e40
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x4, %rcx
je 0x4e2e
movsd (%rdx,%rcx,8), %xmm0
divsd -0x80(%rsp,%rcx,8), %xmm0
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
jmp 0x4e44
nop