// File: /workspace/github_cpp_makefile_star2_O1/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 0x6611(%rip)            # 0x31800
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 0x1ddb8
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 0x1ddb8
leaq 0x30(%rsp), %rdi
leaq 0x48(%rsp), %rdx
leaq 0xe(%rsp), %rcx
movq %r13, %rsi
callq 0x1e116
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x2b327
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movb 0xb0(%r12), %dil
testb %dil, %dil
je 0x2b3e3
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 0x2b3c6
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r13
cmpq %r14, %r13
je 0x2b3c6
leaq (%rbx,%rbx,2), %r12
movslq 0x20(%r13), %rbp
testq %rbp, %rbp
je 0x2b3b6
movq (%r15), %rax
movq (%rax), %rax
movq (%rax,%rbx,8), %rsi
movl %ebp, %edi
movq 0x10(%rsp), %rdx
callq 0x2a045
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 0x2b370
incq %rbx
movq 0x80(%rsp), %r12
movzbl 0xb0(%r12), %edi
cmpq %rdi, %rbx
jb 0x2b34b
testb %dil, %dil
je 0x2b53b
movq (%r15), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rcx
movq 0x60(%rsp), %rdx
movq 0x60(%r12), %rsi
movzbl %dil, %edi
xorl %r8d, %r8d
movss 0x7228(%rip), %xmm0     # 0x32638
movq (%rax,%r8,8), %r9
cmpb $0x0, 0xb8(%r9)
jne 0x2b52f
fldt 0x90(%r12)
fld1
fadd %st, %st(1)
xorps %xmm2, %xmm2
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
xorps %xmm1, %xmm1
jbe 0x2b4ec
leaq (,%r8,8), %r9
leaq (%r9,%r9,2), %rbx
movq (%rcx,%rbx), %r9
movq 0x78(%rsp), %r10
movq (%r10), %r10
movq (%rsi), %r11
movq 0x18(%rsi), %r14
movq (%r11,%rbx), %r11
movq (%r14,%rbx), %rbx
fldz
movb $0x1, %bpl
fld %st(0)
movzbl %bpl, %ebp
movl %ebp, %r14d
shll $0x4, %r14d
fldt (%r9,%r14)
fldt (%rdx,%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 (%r10,%r14)
fmulp %st, %st(1)
fld %st(0)
fstpt (%r11,%r14)
fldt (%r10,%r14)
fsubp %st, %st(1)
fld %st(0)
fstpt (%rbx,%r14)
fldt (%r11,%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 0x2b46e
fstps 0x28(%rsp)
movss 0x28(%rsp), %xmm2
fstps 0x24(%rsp)
movss 0x24(%rsp), %xmm1
movaps %xmm0, %xmm3
subss %xmm2, %xmm3
movss %xmm3, 0x20(%rsp)
flds 0x20(%rsp)
movq (%rsi), %r9
leaq (,%r8,8), %r10
leaq (%r10,%r10,2), %r10
movq (%r9,%r10), %r9
fstpt (%r9)
movaps %xmm0, %xmm2
subss %xmm1, %xmm2
movss %xmm2, 0x1c(%rsp)
flds 0x1c(%rsp)
movq 0x18(%rsi), %r9
movq (%r9,%r10), %r9
fstpt (%r9)
incq %r8
cmpq %rdi, %r8
jne 0x2b410
leaq 0x30(%rsp), %rdi
callq 0x1e1ae
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x2b55c
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 0x2b59e
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
jmp 0x2b59e
movq %rax, %rbx
jmp 0x2b59e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1e1ae
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x2b5b5
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq %rbx, %rdi
callq 0x4700
nop