// File: /workspace/github_cpp_makefile_star2_O2/adamiaonr[P]rid-analytics/src/prob.cpp
// Function: Prob::calc_fptree_probs(std::vector<std::vector<Prob::fp_data*, std::allocator<Prob::fp_data*>>, std::allocator<std::vector<Prob::fp_data*, std::allocator<Prob::fp_data*>>>>*, std::map<int, std::vector<unsigned char, std::allocator<unsigned char>>, std::less<int>, std::allocator<std::pair<int const, std::vector<unsigned char, std::allocator<unsigned char>>>>>*, std::vector<long double, std::allocator<long double>>*)
int Prob::calc_fptree_probs(
    std::vector<std::vector<Prob::fp_data *> > * iface_fp_data,
    std::map<int, std::vector<uint8_t> > * tree_bitmasks,
    std::vector<__float080> * in_fptree_prob) {

    // (1) calculate str[i][t] : shared tree ratios for iface i, size t
    std::vector<__float080> str_total(this->n + 1, 0.0);
    std::vector< std::vector<__float080> > str(this->iface_num, std::vector<__float080>(this->n + 1, 0.0));

    for (uint8_t i = 0; i < this->iface_num; i++) {
        // don't consider blocked ifaces
        if ((*iface_fp_data)[0][i]->is_blocked) continue;

        std::map<int, std::vector<uint8_t> >::iterator itr;
        for (itr = (*tree_bitmasks).begin(); itr != (*tree_bitmasks).end(); itr++) {

            int t = itr->first;
            if (t == 0) continue;

            // count shared trees w/ bitmask on iface i
            str[i][t] = (__float080) count_shared_trees(t, (*iface_fp_data)[0][i], tree_bitmasks);
            // keep track of total # of shared trees
            str_total[t] += str[i][t];
        }
    }

    // std::cout << "Prob::calc_fptree_probs() : shared tree ratios :" << std::endl;
    // for (uint8_t i = 0; i < this->iface_num; i++)
    //     for (uint8_t t = 1; t < (this->n + 1); t++)
    //         std::cout << "\tstr[" << (int) i << "][" << (int) t << "] = " << str[i][t] / ((str_total[t] > 0.0) ? str_total[t] : 1.0) << std::endl;

    // (2) calc P(T_i = t), for all {i,t} pairs
    for (uint8_t i = 0; i < this->iface_num; i++) {
        
        // if iface i is blocked, don't bother going on...
        if ((*iface_fp_data)[0][i]->is_blocked) continue;
        // uint8_t max_tree = 0;
        __float080 cumulative_fptree_prob[2] = {0.0, 0.0};
        for (uint8_t t = 1; t < (this->n + 1); t++) {

            // P(T_i > 0) = srt[i][t] * P(T_in = t)
            this->fptree_prob[0][i][t] = (str[i][t] / ((str_total[t] > 0.0) ? str_total[t] : 1.0)) * (*in_fptree_prob)[t];            
            // P(T_{~i} > 0) = P(T_in = t) - (srt[i][t] * P(T_in = t))
            this->fptree_prob[1][i][t] = ((*in_fptree_prob)[t] - this->fptree_prob[0][i][t]);

            // SUM {for all t > 0} [ srt[i][t] * P(T_in = t) ]
            //  - will be used in calc. of P(T_i = 0, B)
            cumulative_fptree_prob[0] += this->fptree_prob[0][i][t];
            cumulative_fptree_prob[1] += this->fptree_prob[1][i][t];
        }

        // P(T_i = 0) = 1.0 - SUM {for all t > 0} [ srt[i][t] * P(T_in = t) ]
        this->fptree_prob[0][i][0] = (__float080) ((float) 1.0 - (float) cumulative_fptree_prob[0]);
        this->fptree_prob[1][i][0] = (__float080) ((float) 1.0 - (float) cumulative_fptree_prob[1]);

        // std::cout << "Prob::calc_fptree_probs() : P(T[" << (int) i << "] = 0) = 1.0 - " 
        //     << cumulative_fptree_prob[0] << " = " << (this->fptree_prob[0][i][0]) << std::endl;
        // std::cout << "Prob::calc_fptree_probs() : P(T[~" << (int) i << "] = 0) = 1.0 - " 
        //     << cumulative_fptree_prob[1] << " = " << (this->fptree_prob[1][i][0]) << std::endl;
    }

    // std::cout << "Prob::calc_fptree_probs() : P(T_i = t) :" << std::endl;
    // for (uint8_t i = 0; i < this->iface_num; i++)
    //     for (uint8_t t = 0; t < (this->n + 1); t++)
    //         std::cout << "\tP(T[" << (int) i << "] = " << (int) t << "]) = " << this->fptree_prob[0][i][t] << std::endl;

    // std::cout << "Prob::calc_fptree_probs() : P(T_{~i} = t) :" << std::endl;
    // for (uint8_t i = 0; i < this->iface_num; i++)
    //     for (uint8_t t = 0; t < (this->n + 1); t++)
    //         std::cout << "\tP(T[~" << (int) i << "] = " << (int) t << "]) = " << this->fptree_prob[1][i][t] << std::endl;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdi, %rbx
fldt 0x90(%rdi)
fld1
fld %st(0)
fstpt 0xc4(%rsp)
faddp %st, %st(1)
flds 0x633f(%rip)            # 0x2a7a0
fld %st(0)
fstpt 0xb8(%rsp)
xorl %eax, %eax
fxch %st(1)
fucomi %st(1), %st
fldz
fld %st(0)
fld %st(1)
fstpt 0xac(%rsp)
fcmovnb %st(3), %st
fstp %st(3)
movq %rcx, %r12
fxch %st(1)
fsubp %st, %st(2)
setae %al
fnstcw 0xe(%rsp)
movzwl 0xe(%rsp), %ecx
orl $0xc00, %ecx            # imm = 0xC00
movw %cx, 0x12(%rsp)
fldcw 0x12(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
fxch %st(1)
fistpll 0x68(%rsp)
fldcw 0xe(%rsp)
shlq $0x3f, %rax
xorq 0x68(%rsp), %rax
fstpt 0x40(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x78(%rsp), %rcx
movq %rax, %rsi
callq 0x18df8
fldt 0x90(%rbx)
fldt 0xc4(%rsp)
faddp %st, %st(1)
xorl %esi, %esi
fldt 0xb8(%rsp)
fxch %st(1)
fucomi %st(1), %st
fldt 0xac(%rsp)
fld %st(0)
fcmovnb %st(3), %st
fstp %st(3)
movzbl 0xb0(%rbx), %r13d
fxch %st(1)
fsubp %st, %st(2)
setae %sil
fnstcw 0xc(%rsp)
movzwl 0xc(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x10(%rsp)
fldcw 0x10(%rsp)
fxch %st(1)
fistpll 0x70(%rsp)
fldcw 0xc(%rsp)
shlq $0x3f, %rsi
xorq 0x70(%rsp), %rsi
fstpt 0xd0(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
leaq 0x17(%rsp), %rcx
callq 0x18df8
leaq 0x40(%rsp), %rdi
leaq 0x78(%rsp), %rdx
leaq 0x16(%rsp), %rcx
movq %r13, %rsi
callq 0x1918c
leaq 0x78(%rsp), %rdi
callq 0x195d0
movq 0x18(%rsp), %rax
leaq 0x8(%rax), %r15
xorl %r14d, %r14d
movq %r12, 0x60(%rsp)
movzbl 0xb0(%rbx), %eax
cmpq %rax, %r14
jae 0x24630
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq (%rax,%r14,8), %rax
cmpb $0x0, 0xb8(%rax)
je 0x245c3
incq %r14
movq 0x60(%rsp), %r12
jmp 0x24591
movq 0x18(%rsp), %rax
movq 0x18(%rax), %r13
imulq $0x18, %r14, %r12
cmpq %r15, %r13
je 0x245b9
movslq 0x20(%r13), %rbp
testq %rbp, %rbp
je 0x24623
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq (%rax,%r14,8), %rsi
movl %ebp, %edi
movq 0x18(%rsp), %rdx
callq 0x23588
movl %eax, 0x2c(%rsp)
fildl 0x2c(%rsp)
movq 0x40(%rsp), %rax
movq (%rax,%r12), %rax
shlq $0x4, %rbp
fld %st(0)
fstpt (%rax,%rbp)
movq 0x90(%rsp), %rax
fldt (%rax,%rbp)
faddp %st, %st(1)
fstpt (%rax,%rbp)
movq %r13, %rdi
callq 0x42d0
movq %rax, %r13
jmp 0x245d0
movq 0x60(%rbx), %rcx
xorl %edx, %edx
movss 0x6f3a(%rip), %xmm0     # 0x2b578
cmpq %rax, %rdx
je 0x24745
movq 0x20(%rsp), %rsi
movq (%rsi), %rsi
movq (%rsi), %rsi
movq (%rsi,%rdx,8), %rsi
cmpb $0x0, 0xb8(%rsi)
jne 0x2473d
imulq $0x18, %rdx, %rsi
movq 0x40(%rsp), %rdi
addq %rsi, %rdi
movq 0x90(%rsp), %r8
movq 0x60(%rbx), %r9
movb $0x1, %r10b
fldz
fld %st(0)
movzbl %r10b, %r10d
movw %r10w, 0x14(%rsp)
filds 0x14(%rsp)
fldt 0x90(%rbx)
fld1
faddp %st, %st(1)
fucompi %st(1), %st
fstp %st(0)
jbe 0x246fa
movq (%rdi), %r11
movl %r10d, %r14d
shll $0x4, %r14d
fldt (%r11,%r14)
fldt (%r8,%r14)
fldz
fxch %st(1)
fucomi %st(1), %st
fstp %st(1)
fld1
fcmovnbe %st(1), %st
fstp %st(1)
fdivrp %st, %st(1)
movq (%r12), %r11
fldt (%r11,%r14)
fmulp %st, %st(1)
movq (%r9), %r15
movq (%r15,%rsi), %r15
fld %st(0)
fstpt (%r15,%r14)
fldt (%r11,%r14)
fsubp %st, %st(1)
movq 0x18(%r9), %r11
movq (%r11,%rsi), %r11
fld %st(0)
fstpt (%r11,%r14)
fldt (%r15,%r14)
faddp %st, %st(3)
faddp %st, %st(1)
incb %r10b
jmp 0x24682
fxch %st(1)
fstps 0x3c(%rsp)
movaps %xmm0, %xmm1
subss 0x3c(%rsp), %xmm1
movss %xmm1, 0x34(%rsp)
flds 0x34(%rsp)
movq (%rcx), %rdi
movq (%rdi,%rsi), %rdi
fstpt (%rdi)
fstps 0x38(%rsp)
movaps %xmm0, %xmm1
subss 0x38(%rsp), %xmm1
movss %xmm1, 0x30(%rsp)
flds 0x30(%rsp)
movq 0x18(%rcx), %rdi
movq (%rdi,%rsi), %rsi
fstpt (%rsi)
incq %rdx
jmp 0x2463e
leaq 0x40(%rsp), %rdi
callq 0x191e2
leaq 0x90(%rsp), %rdi
callq 0x195d0
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x195d0
jmp 0x24791
movq %rax, %rbx
jmp 0x24791
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x191e2
leaq 0x90(%rsp), %rdi
callq 0x195d0
movq %rbx, %rdi
callq 0x4700