// File: big-data-lab-team[P]OrpailleCC/./src/mondrian_coarse.hpp
// Function: CoarseMondrianForest<double, funct, KappaMetrics<2, false>, 4, 2, 6000>::compute_posterior_mean(MondrianNode<4, 2> const&, double*) const
void compute_posterior_mean(Node const& node, double *posterior_mean) const{
	//posterior_mean start with the parent posterior_mean and the function updates to the current node
	double const parent_tau = node.parent >= 0 ? nodes()[node.parent].tau : 0; //The tau value of the parent of the root is 0
	double const node_discount = func::exp(discount_factor * (node.tau - parent_tau));
	double sum_counters = 0;
	double sum_tab = 0;
	double tab[label_count];
	//Compute sum_counters and sum_tab, and set tab
	for(int i = 0; i < label_count; ++i){
		sum_counters += node.counters[i];
		tab[i] = Utils::min(node.counters[i], 1);
		sum_tab += tab[i];
	}
	//For each label with a counter higher than zero, compute the posterior mean. Otherwise it is simply the posterior mean of the parent
	for(int i = 0; i < label_count; ++i){
		if(node.counters[i] > 0){
			double const a = node.counters[i] - node_discount * tab[i];
			double const b = node_discount * sum_tab * posterior_mean[i];
			posterior_mean[i] = (a / sum_counters) + (b /sum_counters);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x58(%rax)
jl 0x2b4d
movq -0x70(%rbp), %rdi
callq 0x2a60
movq -0x10(%rbp), %rcx
movslq 0x58(%rcx), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movsd 0x60(%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
jmp 0x2b57
xorps %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
jmp 0x2b57
movq -0x70(%rbp), %rax
movsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x17f8(%rax), %xmm0
movq -0x10(%rbp), %rax
movsd 0x60(%rax), %xmm1
subsd -0x20(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x2ac0
movsd %xmm0, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movl $0x0, -0x54(%rbp)
cmpl $0x2, -0x54(%rbp)
jge 0x2c01
movq -0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
cvtsi2sdl 0x68(%rax,%rcx,4), %xmm0
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl 0x68(%rax,%rcx,4), %edi
movl $0x1, %esi
callq 0x2a00
cvtsi2sd %eax, %xmm0
movslq -0x54(%rbp), %rax
movsd %xmm0, -0x50(%rbp,%rax,8)
movslq -0x54(%rbp), %rax
movsd -0x50(%rbp,%rax,8), %xmm0
addsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2ba0
movl $0x0, -0x58(%rbp)
cmpl $0x2, -0x58(%rbp)
jge 0x2cb7
movq -0x10(%rbp), %rax
movslq -0x58(%rbp), %rcx
cmpl $0x0, 0x68(%rax,%rcx,4)
jle 0x2ca7
movq -0x10(%rbp), %rax
movslq -0x58(%rbp), %rcx
cvtsi2sdl 0x68(%rax,%rcx,4), %xmm1
movsd -0x28(%rbp), %xmm0
movslq -0x58(%rbp), %rax
movsd -0x50(%rbp,%rax,8), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x28(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
movq -0x18(%rbp), %rax
movslq -0x58(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x60(%rbp), %xmm0
divsd -0x30(%rbp), %xmm0
movsd -0x68(%rbp), %xmm1
divsd -0x30(%rbp), %xmm1
addsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movslq -0x58(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x2ca9
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x2c08
addq $0x80, %rsp
popq %rbp
retq