// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
movq %rdi, %r12
fldt 0x90(%rdi)
fld1
fld %st(0)
fstpt 0xb4(%rsp)
faddp %st, %st(1)
flds 0x58f7(%rip)            # 0x305e0
fld %st(0)
fstpt 0xa8(%rsp)
xorl %eax, %eax
fxch %st(1)
fucomi %st(1), %st
fldz
fld %st(0)
fld %st(1)
fstpt 0x9c(%rsp)
fcmovnb %st(3), %st
fstp %st(3)
movq %rcx, 0x78(%rsp)
fxch %st(1)
fsubp %st, %st(2)
setae %al
fnstcw 0x6(%rsp)
movzwl 0x6(%rsp), %ecx
orl $0xc00, %ecx            # imm = 0xC00
movw %cx, 0xa(%rsp)
fldcw 0xa(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %r15
fxch %st(1)
fistpll 0x88(%rsp)
fldcw 0x6(%rsp)
shlq $0x3f, %rax
xorq 0x88(%rsp), %rax
fstpt 0x30(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x30(%rsp), %rdx
leaq 0x48(%rsp), %rcx
movq %rax, %rsi
callq 0x1d906
fldt 0x90(%r12)
fldt 0xb4(%rsp)
faddp %st, %st(1)
xorl %esi, %esi
fldt 0xa8(%rsp)
fxch %st(1)
fucomi %st(1), %st
fldt 0x9c(%rsp)
fld %st(0)
fcmovnb %st(3), %st
fstp %st(3)
movzbl 0xb0(%r12), %r13d
fxch %st(1)
fsubp %st, %st(2)
setae %sil
fnstcw 0x4(%rsp)
movzwl 0x4(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x8(%rsp)
fldcw 0x8(%rsp)
fxch %st(1)
fistpll 0x90(%rsp)
fldcw 0x4(%rsp)
shlq $0x3f, %rsi
xorq 0x90(%rsp), %rsi
fstpt 0xc0(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
leaq 0xf(%rsp), %rcx
callq 0x1d906
leaq 0x30(%rsp), %rdi
leaq 0x48(%rsp), %rdx
leaq 0xe(%rsp), %rcx
movq %r13, %rsi
callq 0x1dca0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x2ae21
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movb 0xb0(%r12), %cl
testb %cl, %cl
je 0x2b028
movq 0x10(%rsp), %rax
leaq 0x8(%rax), %r14
xorl %ebx, %ebx
movq %r12, 0x80(%rsp)
movq (%r15), %rax
movq (%rax), %rax
movq (%rax,%rbx,8), %rax
cmpb $0x0, 0xb8(%rax)
jne 0x2aecf
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r13
cmpq %r14, %r13
je 0x2aecf
leaq (%rbx,%rbx,2), %r12
movslq 0x20(%r13), %rbp
testq %rbp, %rbp
je 0x2aeaf
movq (%r15), %rax
movq (%rax), %rax
movq (%rax,%rbx,8), %rsi
movl %ebp, %edi
movq 0x10(%rsp), %rdx
callq 0x29b66
movl %eax, 0x2c(%rsp)
fildl 0x2c(%rsp)
movq 0x30(%rsp), %rax
movq (%rax,%r12,8), %rax
shlq $0x4, %rbp
fld %st(0)
fstpt (%rax,%rbp)
movq 0x60(%rsp), %rax
fldt (%rax,%rbp)
faddp %st, %st(1)
fstpt (%rax,%rbp)
movq %r13, %rdi
callq 0x42b0
movq %rax, %r13
cmpq %r14, %rax
jne 0x2ae69
movq 0x80(%rsp), %r12
movb 0xb0(%r12), %cl
incq %rbx
movzbl %cl, %eax
cmpq %rax, %rbx
jb 0x2ae44
testb %cl, %cl
je 0x2b028
movq (%r15), %rcx
movq (%rcx), %rcx
movq 0x30(%rsp), %rdx
movq 0x60(%rsp), %rsi
movq 0x60(%r12), %rdi
xorl %r8d, %r8d
movss 0x6512(%rip), %xmm0     # 0x31418
movq (%rcx,%r8,8), %r9
cmpb $0x0, 0xb8(%r9)
jne 0x2b01c
fldt 0x90(%r12)
fld1
fadd %st, %st(1)
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
movq (%rdi), %r9
leaq (,%r8,8), %r10
jbe 0x2afde
leaq (%r10,%r10,2), %r14
movq (%rdx,%r14), %r11
movq 0x78(%rsp), %r10
movq (%r10), %rbx
movq (%r9,%r14), %r10
movq 0x18(%rdi), %r9
movq (%r9,%r14), %r9
fldz
movb $0x1, %bpl
fld %st(0)
movzbl %bpl, %ebp
movl %ebp, %r14d
shll $0x4, %r14d
fldt (%r11,%r14)
fldt (%rsi,%r14)
fldz
fxch %st(1)
fucomi %st(1), %st
fstp %st(1)
fld1
fld %st(0)
fcmovnbe %st(2), %st
fstp %st(2)
fxch %st(2)
fdivp %st, %st(1)
fldt (%rbx,%r14)
fmulp %st, %st(1)
fld %st(0)
fstpt (%r10,%r14)
fldt (%rbx,%r14)
fsubp %st, %st(1)
fld %st(0)
fstpt (%r9,%r14)
fldt (%r10,%r14)
faddp %st, %st(3)
faddp %st, %st(3)
incb %bpl
movzbl %bpl, %r14d
movw %r14w, 0xc(%rsp)
filds 0xc(%rsp)
fldt 0x90(%r12)
faddp %st, %st(2)
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
ja 0x2af5e
fstps 0x28(%rsp)
movss 0x28(%rsp), %xmm1
fstps 0x24(%rsp)
movss 0x24(%rsp), %xmm2
jmp 0x2aff4
leaq (%r10,%r10,2), %r11
movq (%r9,%r11), %r10
movq 0x18(%rdi), %r9
movq (%r9,%r11), %r9
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
movaps %xmm0, %xmm3
subss %xmm1, %xmm3
movss %xmm3, 0x20(%rsp)
flds 0x20(%rsp)
fstpt (%r10)
movaps %xmm0, %xmm1
subss %xmm2, %xmm1
movss %xmm1, 0x1c(%rsp)
flds 0x1c(%rsp)
fstpt (%r9)
incq %r8
cmpq %rax, %r8
jne 0x2af06
leaq 0x30(%rsp), %rdi
callq 0x1dd38
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x2b049
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x2b08b
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
jmp 0x2b08b
movq %rax, %rbx
jmp 0x2b08b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1dd38
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x2b0a2
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq %rbx, %rdi
callq 0x4700