// File: /workspace/github_cpp_makefile_star2_O2/adamiaonr[P]rid-analytics/src/prob.cpp
// Function: Prob::calc_out_fptree_prob(unsigned char, 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>>*, std::vector<std::vector<long double, std::allocator<long double>>, std::allocator<std::vector<long double, std::allocator<long double>>>>&)
int Prob::calc_out_fptree_prob(
    uint8_t i,
    Prob::fp_data * iface_fp_data,
    std::map<int, std::vector<uint8_t> > * tree_bitmasks,
    std::vector<__float080> * in_fptree_prob,
    std::vector<std::vector<__float080> > & out_fptree_probs) {

    // aux. arrays used in fp rate calculation:
    //  - P(|FP| =/= f) = (1.0 - P(|FP| = f)) : prob of not having an fp match of size f
    std::vector<__float080> log_prob_fp_neq(n, 0.0);
    //  - P(|FP| <= f) : prob of fp match smaller than or equal to f
    std::vector<__float080> log_prob_fp_smeq(n + 1, 0.0);

    // init all probs to 0.0
    for (uint8_t t = 0; t <= this->n; t++)
        out_fptree_probs[i][t] = 0.0;

    // we add the influence of events 2.1 and 2.2
    //  - k = 0 : event 2.1
    //  - k = 1 : event 2.2
    std::vector<__float080> lt_ratios;

    for (int k = 0; k < 2; k++) {

        __float080 subevent_prob = 0.0;

        if (k == 0) {
        
            // 2.1) req. got into router w/ no assoc. w/ fp tree
            subevent_prob = (*in_fptree_prob)[0];
            // all entries count for fp prob calculation
            lt_ratios = std::vector<__float080>(n, 1.0);

        } else {

            // 2.2) req. assoc. fp tree, but not iface i
            subevent_prob = (1.0 - (*in_fptree_prob)[0]) * (this->fptree_prob[0][i][0]);
            // only account w/ % of entries coming from local trees
            lt_ratios = std::vector<__float080>(n, 1.0);
            // local_tree_ratios(iface_fp_data, tree_bitmasks, lt_ratios);
        }

        calc_log_prob_fp_neq(iface_fp_data, log_prob_fp_neq, lt_ratios);
        for (int f = (int) (this->n - 1); f >= 0; f--)
            log_prob_fp_smeq[f] = log_prob_fp_smeq[f + 1] + log_prob_fp_neq[f];

        // P(T_{out,i} = 0)
        if (iface_fp_data->tp_size > 0) {

            // the req. can advance to the next router w/ t = 0 w/ prob P(|FP| = 0)
            // this can happen if:
            //  - iface i is not assoc. to fp tree
            //      - cases 2.1 or 2.2 above
            //  - P(FP = 0)
            out_fptree_probs[i][0] += 
                subevent_prob                   // i not associated w/ fp tree
                * exp(log_prob_fp_smeq[0]);     // not have a FP > 0 (i.e. having only FP = 0)

            // std::cout << "Prob::calc_out_fptree_prob() : P[" 
            //     << (int) i << "](T_out = 0) = " << exp(log_prob_fp_smeq[0]) << std::endl;

        } else {

            // there's no way of having a req. advance to the next router
            // without at least 1 fp match
            out_fptree_probs[i][0] = 0.0;
        }

        // P(T_{out,i} > 0)
        for (uint8_t t = 1; t <= this->n; t++) {

            out_fptree_probs[i][t] +=
                subevent_prob
                * exp(log_prob_fp_smeq[t])              // not have a FP > t
                * (1.0 - exp(log_prob_fp_neq[t - 1]));  // have a FP of size t
        }
    }

    // contribution of event 1
    for (uint8_t t = 1; t <= this->n; t++)
        out_fptree_probs[i][t] += (1.0 - (*in_fptree_prob)[0]) * this->fptree_prob[0][i][t];

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %r9, %r15
movq %r8, 0x58(%rsp)
movq %rdi, %r14
fldt 0x90(%rdi)
flds 0x682c(%rip)            # 0x2a7a0
fld %st(0)
fstpt 0x4c(%rsp)
xorl %eax, %eax
fucomi %st(1), %st
setbe %al
fldz
fld %st(0)
fld %st(1)
fstpt 0x40(%rsp)
fcmovbe %st(2), %st
fstp %st(2)
fxch %st(2)
fsubp %st, %st(1)
fnstcw 0x26(%rsp)
movzwl 0x26(%rsp), %ecx
orl $0xc00, %ecx            # imm = 0xC00
movw %cx, 0x30(%rsp)
fldcw 0x30(%rsp)
movq %rdx, 0x68(%rsp)
movl %esi, %ebx
fistpll 0xd0(%rsp)
fldcw 0x26(%rsp)
shlq $0x3f, %rax
xorq 0xd0(%rsp), %rax
fstpt 0x70(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x70(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
movq %rax, %rsi
callq 0x18df8
movq %r14, 0x38(%rsp)
fldt 0x90(%r14)
fld1
faddp %st, %st(1)
xorl %esi, %esi
fldt 0x4c(%rsp)
fxch %st(1)
fucomi %st(1), %st
fldt 0x40(%rsp)
fld %st(0)
fcmovnb %st(3), %st
fstp %st(3)
fxch %st(1)
fsubp %st, %st(2)
setae %sil
fnstcw 0x24(%rsp)
movzwl 0x24(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x2e(%rsp)
fldcw 0x2e(%rsp)
fxch %st(1)
fistpll 0xd8(%rsp)
fldcw 0x24(%rsp)
shlq $0x3f, %rsi
xorq 0xd8(%rsp), %rsi
fstpt 0xa0(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
leaq 0xf8(%rsp), %rcx
callq 0x18df8
movzbl %bl, %eax
imulq $0x18, %rax, %r13
movq (%r15), %rax
addq %r13, %rax
xorl %ecx, %ecx
movzbl %cl, %ecx
movw %cx, 0x36(%rsp)
filds 0x36(%rsp)
movq 0x38(%rsp), %rdx
fldt 0x90(%rdx)
fucomi %st(1), %st
fstp %st(1)
jb 0x240a6
fstp %st(0)
movq (%rax), %rdx
movl %ecx, %esi
shll $0x4, %esi
fldz
fstpt (%rdx,%rsi)
incb %cl
jmp 0x24076
leaq 0xa0(%rsp), %rbx
andq $0x0, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
xorl %r12d, %r12d
leaq 0xf8(%rsp), %rbp
movq %r15, %r14
movq %r15, 0xb8(%rsp)
testl %r12d, %r12d
je 0x2417e
cmpl $0x2, %r12d
je 0x24364
movq 0x58(%rsp), %rax
movq (%rax), %rax
fldt (%rax)
fstpt 0x4c(%rsp)
movq 0x38(%rsp), %rax
movq 0x60(%rax), %rax
movq (%rax), %rax
movq (%rax,%r13), %rax
fldt (%rax)
fstpt 0x40(%rsp)
flds 0x6694(%rip)            # 0x2a7a0
xorl %esi, %esi
fxch %st(1)
fucomi %st(1), %st
setae %sil
fldz
fcmovnb %st(2), %st
fstp %st(2)
fsubp %st, %st(1)
fnstcw 0x20(%rsp)
movzwl 0x20(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x2a(%rsp)
fldcw 0x2a(%rsp)
fistpll 0xc0(%rsp)
fldcw 0x20(%rsp)
shlq $0x3f, %rsi
xorq 0xc0(%rsp), %rsi
fld1
fstpt 0x90(%rsp)
movq %rbp, %rdi
leaq 0x90(%rsp), %rdx
leaq 0x1d(%rsp), %rcx
callq 0x18df8
fld1
fldt 0x4c(%rsp)
fsubrp %st, %st(1)
fldt 0x40(%rsp)
fmulp %st, %st(1)
fstpt 0x40(%rsp)
jmp 0x241f0
movq 0x58(%rsp), %rax
movq (%rax), %rax
fldt (%rax)
fstpt 0x40(%rsp)
flds 0x660e(%rip)            # 0x2a7a0
xorl %esi, %esi
fxch %st(1)
fucomi %st(1), %st
setae %sil
fldz
fcmovnb %st(2), %st
fstp %st(2)
fsubp %st, %st(1)
fnstcw 0x22(%rsp)
movzwl 0x22(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x2c(%rsp)
fldcw 0x2c(%rsp)
fistpll 0xc8(%rsp)
fldcw 0x22(%rsp)
shlq $0x3f, %rsi
xorq 0xc8(%rsp), %rsi
fld1
fstpt 0x90(%rsp)
movq %rbp, %rdi
leaq 0x90(%rsp), %rdx
leaq 0x1d(%rsp), %rcx
callq 0x18df8
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1a1fc
movq %rbp, %rdi
callq 0x195d0
leaq 0x110(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x19582
movq 0x38(%rsp), %r15
movq %r15, %rdi
movq 0x68(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
movq %rbp, %rcx
callq 0x233e2
movq %rbp, %rdi
callq 0x195d0
fldt 0x90(%r15)
fld1
fchs
faddp %st, %st(1)
fnstcw 0x1e(%rsp)
movzwl 0x1e(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x28(%rsp)
fldcw 0x28(%rsp)
fistpl 0x64(%rsp)
fldcw 0x1e(%rsp)
movl 0x64(%rsp), %ecx
movq 0x70(%rsp), %rax
leaq 0x10(%rax), %rdx
movq 0xe0(%rsp), %rsi
movq 0xb8(%rsp), %rbp
testl %ecx, %ecx
js 0x2429d
movl %ecx, %edi
shlq $0x4, %rdi
fldt (%rdx,%rdi)
fldt (%rsi,%rdi)
faddp %st, %st(1)
fstpt (%rax,%rdi)
decl %ecx
jmp 0x24284
movq 0x68(%rsp), %rcx
cmpb $0x0, (%rcx)
je 0x242c5
fldt (%rax)
fstpt (%rsp)
callq 0x40f0
movq (%rbp), %rax
movq (%rax,%r13), %rax
fldt (%rax)
fldt 0x40(%rsp)
fmulp %st, %st(2)
faddp %st, %st(1)
jmp 0x242cf
movq (%rbp), %rax
movq (%rax,%r13), %rax
fldz
fstpt (%rax)
movb $0x1, %bpl
movzbl %bpl, %ebp
movw %bp, 0x32(%rsp)
filds 0x32(%rsp)
movq 0x38(%rsp), %rax
fldt 0x90(%rax)
fucomi %st(1), %st
fstp %st(1)
jb 0x24351
fstp %st(0)
movq 0x70(%rsp), %rax
movl %r12d, %r15d
movq %rbx, %r12
movl %ebp, %ebx
shll $0x4, %ebx
fldt (%rax,%rbx)
fstpt (%rsp)
callq 0x40f0
fldt 0x40(%rsp)
fmulp %st, %st(1)
fstpt 0x4c(%rsp)
movq 0xe0(%rsp), %rax
fldt -0x10(%rax,%rbx)
fstpt (%rsp)
callq 0x40f0
fld1
fsubp %st, %st(1)
movq (%r14), %rax
movq (%rax,%r13), %rax
fldt (%rax,%rbx)
fldt 0x4c(%rsp)
fmulp %st, %st(2)
faddp %st, %st(1)
fstpt (%rax,%rbx)
movq %r12, %rbx
movl %r15d, %r12d
incb %bpl
jmp 0x242d4
incl %r12d
movq %r14, %r15
leaq 0xf8(%rsp), %rbp
jmp 0x240cf
movq 0x38(%rsp), %rax
movq 0x60(%rax), %rax
movq (%r15), %rcx
addq %r13, %rcx
movb $0x1, %dl
movzbl %dl, %edx
movw %dx, 0x34(%rsp)
filds 0x34(%rsp)
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
jb 0x243c4
movq 0x58(%rsp), %rsi
movq (%rsi), %rsi
fldt (%rsi)
fld1
fsubp %st, %st(1)
movq (%rax), %rsi
movq (%rsi,%r13), %rsi
movl %edx, %edi
shll $0x4, %edi
fldt (%rsi,%rdi)
movq (%rcx), %rsi
fldt (%rsi,%rdi)
fxch %st(2)
fmulp %st, %st(1)
faddp %st, %st(1)
fstpt (%rsi,%rdi)
incb %dl
movq 0x38(%rsp), %rsi
fldt 0x90(%rsi)
jmp 0x24375
leaq 0xa0(%rsp), %rdi
callq 0x195d0
leaq 0x70(%rsp), %rdi
callq 0x195d0
leaq 0xe0(%rsp), %rdi
callq 0x195d0
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x2441f
jmp 0x24405
jmp 0x24405
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x195d0
leaq 0x70(%rsp), %rdi
callq 0x195d0
leaq 0xe0(%rsp), %rdi
callq 0x195d0
movq %rbx, %rdi
callq 0x4700