// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %r8, 0xb0(%rsp)
movq %rdi, %rbx
fldt 0x90(%rdi)
flds 0x6bf5(%rip)            # 0x31800
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 0x1ddb8
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 0x1ddb8
fldt 0x90(%rbx)
fldz
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
movzbl %bpl, %esi
jb 0x2ad41
movq (%r14), %rax
leaq (%rsi,%rsi,2), %rcx
movq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
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)
fucompi %st(1), %st
fstp %st(0)
jae 0x2ad1a
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
xorl %r13d, %r13d
leaq 0x110(%rsp), %rbp
movq %rsi, 0xe0(%rsp)
leaq (%rsi,%rsi,2), %r15
movq %r12, 0xe8(%rsp)
movq 0xb0(%rsp), %rax
movq (%rax), %rax
fldt (%rax)
fstpt 0x34(%rsp)
testl %r13d, %r13d
je 0x2ae22
movq 0x60(%rbx), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rax
fldt (%rax)
fstpt 0x80(%rsp)
fldt 0x90(%rbx)
flds 0x6a53(%rip)            # 0x31800
xorl %esi, %esi
fucomi %st(1), %st
setbe %sil
fldz
fcmovbe %st(1), %st
fstp %st(1)
fsubrp %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 0x1ddb8
fld1
fldt 0x34(%rsp)
fsubrp %st, %st(1)
fldt 0x80(%rsp)
fmulp %st, %st(1)
fstpt 0x34(%rsp)
jmp 0x2ae8c
fldt 0x90(%rbx)
flds 0x69d2(%rip)            # 0x31800
xorl %esi, %esi
fucomi %st(1), %st
setbe %sil
fldz
fcmovbe %st(1), %st
fstp %st(1)
fsubrp %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 0x1ddb8
movq 0x40(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x60(%rsp), %rax
movq %rax, 0x40(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, 0x48(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
testq %rdi, %rdi
je 0x2aec8
subq %rdi, %rsi
callq 0x43a0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x2aedf
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x1e548
movq %rbx, %rdi
movq %r12, %rsi
leaq 0xb8(%rsp), %rdx
movq %rbp, %rcx
callq 0x29e64
movq 0x110(%rsp), %rdi
testq %rdi, %rdi
je 0x2af1f
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 0x2af8f
fstp %st(0)
movq 0x90(%rsp), %rax
movq 0xb8(%rsp), %rcx
movl %edx, %edx
movq %rdx, %rsi
shlq $0x4, %rsi
fldt 0x10(%rax,%rsi)
incq %rdx
fldt (%rcx,%rsi)
faddp %st, %st(1)
fld %st(0)
fstpt (%rax,%rsi)
addq $-0x10, %rsi
decq %rdx
jg 0x2af7c
fstp %st(0)
cmpb $0x0, (%r12)
je 0x2afbd
movq 0x90(%rsp), %rax
fldt (%rax)
fstpt (%rsp)
callq 0x40f0
movq (%r14), %rax
movq (%rax,%r15,8), %rax
fldt (%rax)
fldt 0x34(%rsp)
fmulp %st, %st(2)
faddp %st, %st(1)
jmp 0x2afc6
movq (%r14), %rax
movq (%rax,%r15,8), %rax
fldz
fstpt (%rax)
fldt 0x90(%rbx)
fld1
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
fldt 0x34(%rsp)
fstpt 0x34(%rsp)
jb 0x2b05e
movb $0x2, %bpl
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 (%r14), %rax
movq (%rax,%r15,8), %rax
fldt (%rax,%r12)
fldt 0x80(%rsp)
fmulp %st, %st(2)
faddp %st, %st(1)
fstpt (%rax,%r12)
movzbl %bpl, %ebp
movw %bp, 0x30(%rsp)
filds 0x30(%rsp)
fldt 0x90(%rbx)
incb %bpl
fucompi %st(1), %st
fstp %st(0)
jae 0x2afe3
leal 0x1(%r13), %eax
testl %r13d, %r13d
movl %eax, %r13d
movq 0xe8(%rsp), %r12
leaq 0x110(%rsp), %rbp
je 0x2ad73
fldt 0x90(%rbx)
fld1
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
jb 0x2b0f7
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movq 0x60(%rbx), %rcx
movq (%rcx), %rcx
movq 0xe0(%rsp), %rdx
shll $0x3, %edx
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx), %rcx
movq (%r14), %rsi
movq (%rsi,%rdx), %rdx
movb $0x1, %sil
fldt (%rax)
fld1
fsubp %st, %st(1)
movzbl %sil, %esi
movl %esi, %edi
shll $0x4, %edi
fldt (%rcx,%rdi)
fldt (%rdx,%rdi)
fxch %st(2)
fmulp %st, %st(1)
faddp %st, %st(1)
fstpt (%rdx,%rdi)
incb %sil
movzbl %sil, %edi
movw %di, 0x2e(%rsp)
filds 0x2e(%rsp)
fldt 0x90(%rbx)
fucompi %st(1), %st
fstp %st(0)
jae 0x2b0bd
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2b10e
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x2b12b
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x2b148
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 0x2b19c
jmp 0x2b165
jmp 0x2b165
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2b17f
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x2b19c
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x2b1b9
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x43a0
movq %rbx, %rdi
callq 0x4700
nop