// File: /workspace/github_cpp_makefile_star2_O3/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, 0xb0(%rsp)
movq %rdi, %rbx
fldt 0x90(%rdi)
flds 0x5ed5(%rip)            # 0x305e0
fld %st(0)
fstpt 0x80(%rsp)
xorl %eax, %eax
fucomi %st(1), %st
setbe %al
fldz
fld %st(0)
fld %st(1)
fstpt 0x34(%rsp)
fcmovbe %st(2), %st
fstp %st(2)
fxch %st(2)
fsubp %st, %st(1)
fnstcw 0x22(%rsp)
movzwl 0x22(%rsp), %ecx
orl $0xc00, %ecx            # imm = 0xC00
movw %cx, 0x2c(%rsp)
fldcw 0x2c(%rsp)
movq %rdx, %r12
movl %esi, %ebp
fistpll 0x100(%rsp)
fldcw 0x22(%rsp)
shlq $0x3f, %rax
xorq 0x100(%rsp), %rax
fstpt 0x90(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0x90(%rsp), %rdx
leaq 0x40(%rsp), %rcx
movq %rax, %rsi
callq 0x1d906
fldt 0x90(%rbx)
fld1
faddp %st, %st(1)
xorl %esi, %esi
fldt 0x80(%rsp)
fxch %st(1)
fucomi %st(1), %st
fldt 0x34(%rsp)
fld %st(0)
fcmovnb %st(3), %st
fstp %st(3)
fxch %st(1)
fsubp %st, %st(2)
setae %sil
fnstcw 0x20(%rsp)
movzwl 0x20(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x2a(%rsp)
fldcw 0x2a(%rsp)
fxch %st(1)
fistpll 0x108(%rsp)
fldcw 0x20(%rsp)
shlq $0x3f, %rsi
xorq 0x108(%rsp), %rsi
fstpt 0x40(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x60(%rsp), %rcx
callq 0x1d906
fldt 0x90(%rbx)
fldz
fxch %st(1)
fucomi %st(1), %st
fstp %st(1)
movzbl %bpl, %esi
jb 0x2a847
fstp %st(0)
movq (%r15), %rax
leaq (%rsi,%rsi,2), %rcx
movq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
fldz
fstp %st(0)
movzbl %cl, %ecx
movl %ecx, %edx
shll $0x4, %edx
fldz
fstpt (%rax,%rdx)
incb %cl
movzbl %cl, %edx
movw %dx, 0x32(%rsp)
filds 0x32(%rsp)
fldt 0x90(%rbx)
fucomi %st(1), %st
fstp %st(1)
jae 0x2a81e
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movb $0x1, %r13b
leaq 0x110(%rsp), %rbp
movq %rsi, 0xe0(%rsp)
leaq (%rsi,%rsi,2), %r14
movq %r12, 0xe8(%rsp)
movq 0xb0(%rsp), %rax
movq (%rax), %rax
fldt (%rax)
fstpt 0x34(%rsp)
testb $0x1, %r13b
je 0x2a8fb
flds 0x5d4a(%rip)            # 0x305e0
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 0x1e(%rsp)
movzwl 0x1e(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x28(%rsp)
fldcw 0x28(%rsp)
fistpll 0xf0(%rsp)
fldcw 0x1e(%rsp)
shlq $0x3f, %rsi
xorq 0xf0(%rsp), %rsi
fld1
fstpt 0xd0(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
leaq 0x19(%rsp), %rcx
callq 0x1d906
jmp 0x2a98a
movq 0x60(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r14,8), %rax
fldt (%rax)
fstpt 0x80(%rsp)
flds 0x5ccb(%rip)            # 0x305e0
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 0x1c(%rsp)
movzwl 0x1c(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x26(%rsp)
fldcw 0x26(%rsp)
fistpll 0xf8(%rsp)
fldcw 0x1c(%rsp)
shlq $0x3f, %rsi
xorq 0xf8(%rsp), %rsi
fld1
fstpt 0xd0(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
leaq 0x19(%rsp), %rcx
callq 0x1d906
fld1
fldt 0x34(%rsp)
fsubrp %st, %st(1)
fldt 0x80(%rsp)
fmulp %st, %st(1)
fstpt 0x34(%rsp)
movq 0x40(%rsp), %rdi
movq 0x50(%rsp), %rsi
movaps 0x60(%rsp), %xmm0
movaps %xmm0, 0x40(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x50(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
testq %rdi, %rdi
je 0x2a9dd
subq %rdi, %rsi
callq 0x43a0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x2a9dd
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x1e0c8
movq %rbx, %rdi
movq %r12, %rsi
leaq 0xb8(%rsp), %rdx
movq %rbp, %rcx
callq 0x2998e
movq 0x110(%rsp), %rdi
testq %rdi, %rdi
je 0x2aa1d
movq 0x120(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
fldt 0x90(%rbx)
fld1
fchs
faddp %st, %st(1)
fnstcw 0x1a(%rsp)
movzwl 0x1a(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x24(%rsp)
fldcw 0x24(%rsp)
fistpl 0x8c(%rsp)
fldcw 0x1a(%rsp)
movl 0x8c(%rsp), %edx
testl %edx, %edx
fldz
js 0x2aa88
fstp %st(0)
movq 0x90(%rsp), %rax
movq 0xb8(%rsp), %rcx
movl %edx, %edx
shlq $0x4, %rdx
fldt 0x10(%rax,%rdx)
fldt (%rcx,%rdx)
faddp %st, %st(1)
fld %st(0)
fstpt (%rax,%rdx)
addq $-0x10, %rdx
cmpq $-0x10, %rdx
jne 0x2aa74
fstp %st(0)
cmpb $0x0, (%r12)
je 0x2aab6
movq 0x90(%rsp), %rax
fldt (%rax)
fstpt (%rsp)
callq 0x40f0
movq (%r15), %rax
movq (%rax,%r14,8), %rcx
fldt (%rcx)
fldt 0x34(%rsp)
fmulp %st, %st(2)
faddp %st, %st(1)
jmp 0x2aabf
movq (%r15), %rax
movq (%rax,%r14,8), %rcx
fldz
fstpt (%rcx)
fldt 0x90(%rbx)
fld1
fxch %st(1)
fucomi %st(1), %st
fstp %st(1)
fldt 0x34(%rsp)
fstpt 0x34(%rsp)
jb 0x2ab61
fstp %st(0)
movb $0x2, %bpl
fldz
fstp %st(0)
leal -0x1(%rbp), %eax
movq 0x90(%rsp), %rcx
movzbl %al, %r12d
shll $0x4, %r12d
fldt (%rcx,%r12)
fstpt (%rsp)
callq 0x40f0
fldt 0x34(%rsp)
fmulp %st, %st(1)
fstpt 0x80(%rsp)
movq 0xb8(%rsp), %rax
fldt -0x10(%rax,%r12)
fstpt (%rsp)
callq 0x40f0
fld1
fsubp %st, %st(1)
movq (%r15), %rax
movq (%rax,%r14,8), %rcx
fldt (%rcx,%r12)
fldt 0x80(%rsp)
fmulp %st, %st(2)
faddp %st, %st(1)
fstpt (%rcx,%r12)
movzbl %bpl, %ebp
movw %bp, 0x30(%rsp)
filds 0x30(%rsp)
fldt 0x90(%rbx)
incb %bpl
fucomi %st(1), %st
fstp %st(1)
jae 0x2aae4
testb $0x1, %r13b
movl $0x0, %r13d
movq 0xe8(%rsp), %r12
leaq 0x110(%rsp), %rbp
jne 0x2a879
fld1
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
jb 0x2abf1
movq 0xb0(%rsp), %rcx
movq (%rcx), %rcx
movq 0x60(%rbx), %rdx
movq (%rdx), %rdx
movq 0xe0(%rsp), %rsi
shll $0x3, %esi
leaq (%rsi,%rsi,2), %rsi
movq (%rdx,%rsi), %rdx
movq (%rax,%rsi), %rax
movb $0x1, %sil
fldt (%rcx)
fld1
fsubp %st, %st(1)
movzbl %sil, %esi
movl %esi, %edi
shll $0x4, %edi
fldt (%rdx,%rdi)
fldt (%rax,%rdi)
fxch %st(2)
fmulp %st, %st(1)
faddp %st, %st(1)
fstpt (%rax,%rdi)
incb %sil
movzbl %sil, %edi
movw %di, 0x2e(%rsp)
filds 0x2e(%rsp)
fldt 0x90(%rbx)
fucompi %st(1), %st
fstp %st(0)
jae 0x2abb7
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2ac08
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x2ac25
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x2ac42
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
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 0x2ac96
jmp 0x2ac5f
jmp 0x2ac5f
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2ac79
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x2ac96
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x2acb3
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq %rbx, %rdi
callq 0x4700
nop