// File: 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
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x218(%rbp)
movl $0x0, -0x1b4(%rbp)
cmpl $0x64, -0x1b4(%rbp)
jge 0x9de7
movslq -0x1b4(%rbp), %rax
movl $0xffffffff, -0x1b0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0x1b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b4(%rbp)
jmp 0x9dbb
movl -0x14(%rbp), %eax
movl %eax, -0x1b8(%rbp)
movl $0x0, -0x1bc(%rbp)
movl $0x0, -0x1e4(%rbp)
cmpl $0x4, -0x1e4(%rbp)
jge 0x9e44
movslq -0x1e4(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, -0x1e0(%rbp,%rax,8)
movq -0x20(%rbp), %rax
movslq -0x1e4(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x1e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e4(%rbp)
jmp 0x9e04
movl $0x0, -0x1e8(%rbp)
movq -0x218(%rbp), %rdx
movl -0x1e8(%rbp), %ecx
xorl %eax, %eax
cmpl 0x17d4(%rdx), %ecx
movb %al, -0x219(%rbp)
jge 0x9e7b
cmpl $0x0, -0x1b8(%rbp)
setge %al
movb %al, -0x219(%rbp)
movb -0x219(%rbp), %al
testb $0x1, %al
jne 0x9e8a
jmp 0xa120
movq -0x218(%rbp), %rdi
callq 0x1b00
movslq -0x1b8(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x1f0(%rbp)
movq -0x1f0(%rbp), %rdi
callq 0x29e0
testb $0x1, %al
jne 0x9fb4
movslq -0x1bc(%rbp), %rax
cmpl $-0x1, -0x1b0(%rbp,%rax,4)
jne 0x9f06
movslq -0x1bc(%rbp), %rax
movl $0x0, -0x1b0(%rbp,%rax,4)
movl -0x1bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1bc(%rbp)
movq -0x1f0(%rbp), %rax
movl 0x54(%rax), %eax
movl %eax, -0x1b8(%rbp)
jmp 0x9faf
movslq -0x1bc(%rbp), %rax
cmpl $0x0, -0x1b0(%rbp,%rax,4)
jne 0x9f4a
movslq -0x1bc(%rbp), %rax
movl $0x1, -0x1b0(%rbp,%rax,4)
movl -0x1bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1bc(%rbp)
movq -0x1f0(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, -0x1b8(%rbp)
jmp 0x9fad
movslq -0x1bc(%rbp), %rax
cmpl $0x1, -0x1b0(%rbp,%rax,4)
jne 0x9fab
movslq -0x1bc(%rbp), %rax
movl $0xffffffff, -0x1b0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0x1bc(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1bc(%rbp)
movl -0x1e8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e8(%rbp)
movl -0x1b8(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x9f9b
jmp 0xa120
movq -0x1f0(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x1b8(%rbp)
jmp 0x9fad
jmp 0x9faf
jmp 0xa109
movslq -0x1bc(%rbp), %rax
movl $0xffffffff, -0x1b0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0x1bc(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1bc(%rbp)
movl -0x1e8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x1f8(%rbp)
movl $0x0, -0x1fc(%rbp)
cmpl $0x2, -0x1fc(%rbp)
jge 0xa037
movq -0x1f0(%rbp), %rax
movslq -0x1fc(%rbp), %rcx
cvtsi2sdl 0x68(%rax,%rcx,4), %xmm0
addsd -0x1f8(%rbp), %xmm0
movsd %xmm0, -0x1f8(%rbp)
movl -0x1fc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1fc(%rbp)
jmp 0x9ff9
movl $0x0, -0x200(%rbp)
cmpl $0x4, -0x200(%rbp)
jge 0xa0ec
movq -0x1f0(%rbp), %rax
movslq -0x200(%rbp), %rcx
movsd 0x30(%rax,%rcx,8), %xmm0
movq -0x1f0(%rbp), %rax
movslq -0x200(%rbp), %rcx
addsd 0x10(%rax,%rcx,8), %xmm0
movsd 0xfb2(%rip), %xmm1      # 0xb030
mulsd %xmm1, %xmm0
movsd %xmm0, -0x208(%rbp)
movsd -0x208(%rbp), %xmm0
movsd -0x1f8(%rbp), %xmm2
movq -0x20(%rbp), %rax
movslq -0x200(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd -0x1f8(%rbp), %xmm0
movslq -0x200(%rbp), %rax
addsd -0x1e0(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x1e0(%rbp,%rax,8)
movl -0x200(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x200(%rbp)
jmp 0xa041
movl -0x1b8(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0xa0f9
jmp 0xa120
movq -0x1f0(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x1b8(%rbp)
cmpl $0x64, -0x1bc(%rbp)
jl 0xa11b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa186
jmp 0x9e4e
cmpl $-0x1, -0x1bc(%rbp)
je 0xa132
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa186
movl $0x0, -0x20c(%rbp)
cmpl $0x4, -0x20c(%rbp)
jge 0xa17f
movslq -0x20c(%rbp), %rax
movsd -0x1e0(%rbp,%rax,8), %xmm1
movq -0x20(%rbp), %rax
movslq -0x20c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x20c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20c(%rbp)
jmp 0xa13c
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)