// File: /workspace/github_cpp_makefile_star2_O2/adamiaonr[P]rid-analytics/src/prob.cpp
// Function: Prob::calc_lm_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>>>>>*, bool)
int Prob::calc_lm_prob(
    uint8_t i, 
    Prob::fp_data * iface_fp_data,
    std::map<int, std::vector<uint8_t> > * tree_bitmasks,
    bool iface_complement) {

    // either 'i' or '~i (complement)' largest match probs
    std::vector<std::vector<std::vector<__float080> > > * lmp;
    if (!iface_complement) lmp = &(this->lm_cond_prob);
    else lmp = &(this->lm_complement_cond_prob);

    // 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);

    // if there are no entries for this iface, a match is impossible
    // as such, match size f = 0 has prob 1.0 of happening
    if (iface_fp_data->is_blocked) { 
        for (int t = (int) this->n; t >= 0; t--) (*lmp)[i][t][0] = 1.0;
        return 0; 
    }

    if (iface_fp_data->num_entries < 1.0) {
        for (int t = (int) this->n; t >= 0; t--) (*lmp)[i][t][0] = 1.0;
        return 0;
    }

    // calc the ratio of trees which can origin FP trees locally at the router
    // std::cout << "Prob::calc_lm_prob(iface) : [INFO] lt ratio iface  " << (iface_complement ? "~" : "") << (int) i << " :" << std::endl;
    // std::vector<__float080> lt_ratios(n, 0.0);
    // local_tree_ratios(iface_fp_data, tree_bitmasks, lt_ratios);
    std::vector<__float080> lt_ratios = std::vector<__float080>(n, 1.0);

    // std::cout << "Prob::calc_lm_prob(iface) : [INFO] iface " << (iface_complement ? "~" : "") << ((int) i) << " stats :"
    //     << "\n\t [n, m, k] : " << this->n << ", " << this->m << ", " << this->k << ")";
        // << "\n\t [# entries] : " << iface_fp_data->num_entries << " (" << (iface_fp_data->num_entries * lt_ratio) << ")";
    //     << "\t\n [lmp size] : " << (*lmp).size()
    //     << "\t\n [f_distr] : ";
    // for(uint8_t f = 0; f < this->n; f++) 
    //     std::cout << "[" << (int) f << "] = " << iface_fp_data->f_distr[f] << ", ";
    // std::cout << std::endl;

    // fill P(|FP| =/= f) = (1.0 - P(|FP| = f)), for all f
    calc_log_prob_fp_neq(iface_fp_data, log_prob_fp_neq, lt_ratios);
    // calc P(|FP| <= f), for all f
    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];

    for (int t = (int) this->n; t >= 0; t--) {
        for (int f = (int) this->n; f >= 0; f--) {
            _calc_lm_prob(
                f, t, 
                iface_fp_data, 
                &log_prob_fp_neq, &log_prob_fp_smeq,
                &((*lmp)[i][t]));
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
xorl %eax, %eax
testl %r8d, %r8d
pushq $0x18
popq %rcx
cmoveq %rax, %rcx
movq %rcx, 0x30(%rsp)
fldt 0x90(%rdi)
flds 0x6cad(%rip)            # 0x2a7a0
fld %st(0)
fstpt 0xc4(%rsp)
xorl %eax, %eax
fucomi %st(1), %st
setbe %al
fldz
fld %st(0)
fld %st(1)
fstpt 0xb8(%rsp)
fcmovbe %st(2), %st
fstp %st(2)
fxch %st(2)
fsubp %st, %st(1)
fnstcw 0xe(%rsp)
movzwl 0xe(%rsp), %ecx
orl $0xc00, %ecx            # imm = 0xC00
movw %cx, 0x2c(%rsp)
fldcw 0x2c(%rsp)
movq %rdx, %r14
movl %esi, %ebp
fistpll 0x90(%rsp)
fldcw 0xe(%rsp)
shlq $0x3f, %rax
xorq 0x90(%rsp), %rax
fstpt 0x70(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x70(%rsp), %rdx
leaq 0x50(%rsp), %rcx
movq %rax, %rsi
callq 0x18df8
movq %rbx, 0x10(%rsp)
fldt 0x90(%rbx)
fld1
faddp %st, %st(1)
xorl %esi, %esi
fldt 0xc4(%rsp)
fxch %st(1)
fucomi %st(1), %st
fldt 0xb8(%rsp)
fld %st(0)
fcmovnb %st(3), %st
fstp %st(3)
fxch %st(1)
fsubp %st, %st(2)
setae %sil
fnstcw 0xc(%rsp)
movzwl 0xc(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x2a(%rsp)
fldcw 0x2a(%rsp)
fxch %st(1)
fistpll 0x98(%rsp)
fldcw 0xc(%rsp)
shlq $0x3f, %rsi
xorq 0x98(%rsp), %rsi
fstpt 0x50(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
callq 0x18df8
cmpb $0x1, 0xb8(%r14)
jne 0x23c54
movq 0x10(%rsp), %rax
fldt 0x90(%rax)
fnstcw 0xa(%rsp)
movzwl 0xa(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x28(%rsp)
fldcw 0x28(%rsp)
fistpl 0x3c(%rsp)
fldcw 0xa(%rsp)
movl 0x3c(%rsp), %eax
movzbl %bpl, %ecx
imulq $0x18, %rcx, %rcx
fld1
testl %eax, %eax
js 0x23e83
movq 0x10(%rsp), %rdx
movq 0x30(%rsp), %rsi
movq (%rdx,%rsi), %rdx
movl %eax, %esi
movq (%rdx,%rcx), %rdx
imulq $0x18, %rsi, %rsi
movq (%rdx,%rsi), %rdx
fld %st(0)
fstpt (%rdx)
decl %eax
jmp 0x23c28
fldt 0x10(%r14)
fld1
fucompi %st(1), %st
fstp %st(0)
jbe 0x23cc4
movq 0x10(%rsp), %rax
fldt 0x90(%rax)
fnstcw 0x8(%rsp)
movzwl 0x8(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x26(%rsp)
fldcw 0x26(%rsp)
fistpl 0x40(%rsp)
fldcw 0x8(%rsp)
movl 0x40(%rsp), %eax
movzbl %bpl, %ecx
imulq $0x18, %rcx, %rcx
fld1
testl %eax, %eax
js 0x23e83
movq 0x10(%rsp), %rdx
movq 0x30(%rsp), %rsi
movq (%rdx,%rsi), %rdx
movl %eax, %esi
movq (%rdx,%rcx), %rdx
imulq $0x18, %rsi, %rsi
movq (%rdx,%rsi), %rdx
fld %st(0)
fstpt (%rdx)
decl %eax
jmp 0x23c98
movq 0x10(%rsp), %rax
fldt 0x90(%rax)
flds 0x6acb(%rip)            # 0x2a7a0
xorl %esi, %esi
fucomi %st(1), %st
setbe %sil
fldz
fcmovbe %st(1), %st
fstp %st(1)
fsubrp %st, %st(1)
fnstcw 0x6(%rsp)
movzwl 0x6(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x24(%rsp)
fldcw 0x24(%rsp)
fistpll 0x88(%rsp)
fldcw 0x6(%rsp)
shlq $0x3f, %rsi
xorq 0x88(%rsp), %rsi
fld1
fstpt 0xd0(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
leaq 0x2f(%rsp), %rcx
callq 0x18df8
leaq 0xe0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x19582
leaq 0xa0(%rsp), %r15
leaq 0xe0(%rsp), %r12
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x233e2
movq %r12, %rdi
callq 0x195d0
fldt 0x90(%rbx)
fld1
fchs
faddp %st, %st(1)
fnstcw 0x4(%rsp)
movzwl 0x4(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x22(%rsp)
fldcw 0x22(%rsp)
fistpl 0x4c(%rsp)
fldcw 0x4(%rsp)
movl 0x4c(%rsp), %eax
movq 0x70(%rsp), %rcx
leaq 0x10(%rcx), %rdx
movq (%r15), %rsi
testl %eax, %eax
js 0x23dc7
movl %eax, %edi
shlq $0x4, %rdi
fldt (%rdx,%rdi)
fldt (%rsi,%rdi)
faddp %st, %st(1)
fstpt (%rcx,%rdi)
decl %eax
jmp 0x23dae
movq %r14, %r12
movq 0x10(%rsp), %rax
fldt 0x90(%rax)
fnstcw 0x2(%rsp)
movzwl 0x2(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x20(%rsp)
fldcw 0x20(%rsp)
fistpl 0x48(%rsp)
fldcw 0x2(%rsp)
movl 0x48(%rsp), %r15d
movzbl %bpl, %eax
imulq $0x18, %rax, %rbx
leaq 0x70(%rsp), %r13
testl %r15d, %r15d
js 0x23e77
movq 0x10(%rsp), %rax
fldt 0x90(%rax)
fnstcw (%rsp)
movzwl (%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x1e(%rsp)
fldcw 0x1e(%rsp)
fistpl 0x44(%rsp)
fldcw (%rsp)
movl 0x44(%rsp), %ebp
movl %r15d, %eax
imulq $0x18, %rax, %r14
testl %ebp, %ebp
js 0x23e72
movq 0x10(%rsp), %rax
movq 0x30(%rsp), %rcx
movq (%rax,%rcx), %rax
movq (%rax,%rbx), %r9
addq %r14, %r9
movl %ebp, %edi
movl %r15d, %esi
movq %r12, %rdx
leaq 0xa0(%rsp), %rcx
movq %r13, %r8
callq 0x2372a
decl %ebp
jmp 0x23e3d
decl %r15d
jmp 0x23e06
leaq 0x50(%rsp), %rdi
callq 0x195d0
fldz
fstp %st(0)
leaq 0x70(%rsp), %rdi
callq 0x195d0
leaq 0xa0(%rsp), %rdi
callq 0x195d0
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x195d0
jmp 0x23ec2
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x195d0
jmp 0x23ed1
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x195d0
movq %rbx, %rdi
callq 0x4700