// File: /workspace/github_cpp_makefile_star2_O2/adamiaonr[P]rid-analytics/src/rid-router.cpp
// Function: RID_Router::add_fwd_table_entry(int, long double, std::map<int, long double, std::less<int>, std::allocator<std::pair<int const, long double>>>*, 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>>>>>*, RID_Router*, int)
int RID_Router::add_fwd_table_entry(
    int i, 
    __float080 iface_proportion, 
    std::map<int, __float080> * f_distr,
    std::map<int, std::vector<uint8_t> > * tree_bitmasks,
    RID_Router * next_hop_router,
    int next_hop_iface) {

    if ((i < 0) || (i > (this->iface_num - 1))) {
        std::cerr << "RID_Router::add_fwd_table_entry() : [ERROR] invalid iface index: "
            << i << " vs. " << (int) this->iface_num << std::endl;
        return -1;
    }

    this->fwd_table[i].num_entries = (__float080) this->fwd_table_size * iface_proportion;
    std::cout << "RID_Router::add_fwd_table_entry() : [INFO] iface[" << i 
        << "].num_entries = " << this->fwd_table[i].num_entries << std::endl;
    
    this->fwd_table[i].iface = i;
    // iface_proportion : % of table entries associated w/ this iface
    this->fwd_table[i].iface_proportion = iface_proportion;
    // distribution of sizes among the entries associated w/ this iface
    this->fwd_table[i].f_distr = std::vector<__float080>(this->req_size, 0.0);

    for (int f = 0; f < this->req_size; f++) {
        this->fwd_table[i].f_distr[f] = (*f_distr)[f + 1];
        // set bit in the bitmask of fwd entry sizes if f_distr[f] > 0.0
        if (this->fwd_table[i].f_distr[f] > 0.0)
            this->fwd_table[i].f_bitmask |= (1 << f);
    }

    // copy the map of tree bitmasks into the router's iface
    this->fwd_table[i].tree_bitmasks = (*tree_bitmasks);

    // next hop information: remote router pointer and iface
    this->fwd_table[i].next_hop.router = next_hop_router;
    this->fwd_table[i].next_hop.iface = next_hop_iface;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %esi, %r13d
movq %rdi, %rbx
fldt 0xb0(%rsp)
testl %esi, %esi
js 0x17efa
movzbl 0x24(%rbx), %eax
cmpl %r13d, %eax
jbe 0x17efa
movq %rdx, %r12
movq %rcx, 0x18(%rsp)
movq %r8, 0x20(%rsp)
movl %r9d, 0x14(%rsp)
movq 0x28(%rbx), %rcx
movq 0x30(%rbx), %rax
movq %rcx, 0x28(%rsp)
xorl %edx, %edx
testq %rcx, %rcx
sets %dl
fildll 0x28(%rsp)
leaq 0x129ea(%rip), %rcx     # 0x2a798
fadds (%rcx,%rdx,4)
fmul %st(1), %st
fxch %st(1)
fstpt 0x44(%rsp)
flds 0x129e1(%rip)           # 0x2a7a0
xorl %ecx, %ecx
fxch %st(1)
fucomi %st(1), %st
fldz
fld %st(0)
fstpt 0x38(%rsp)
fcmovnb %st(2), %st
fstp %st(2)
fsubp %st, %st(1)
setae %cl
fnstcw 0xe(%rsp)
movzwl 0xe(%rsp), %edx
orl $0xc00, %edx            # imm = 0xC00
movw %dx, 0x10(%rsp)
fldcw 0x10(%rsp)
fistpll 0x30(%rsp)
fldcw 0xe(%rsp)
shlq $0x3f, %rcx
xorq 0x30(%rsp), %rcx
movl %r13d, %edx
imulq $0xa0, %rdx, %r14
movq %rcx, 0x58(%rax,%r14)
movq 0x2319b(%rip), %rdi     # 0x3afb0
leaq 0x12a56(%rip), %rsi     # 0x2a872
callq 0x43a0
movq %rax, %rdi
movl %r13d, %esi
callq 0x46d0
leaq 0x12a71(%rip), %rsi     # 0x2a8a4
movq %rax, %rdi
callq 0x43a0
movq 0x30(%rbx), %rcx
movq 0x58(%rcx,%r14), %rsi
movq %rax, %rdi
callq 0x42c0
movq %rax, %rdi
callq 0x41e0
movq 0x30(%rbx), %rax
movb %r13b, (%rax,%r14)
fldt 0x44(%rsp)
fstpt 0x10(%rax,%r14)
movzbl 0x21(%rbx), %esi
fldt 0x38(%rsp)
fstpt 0x50(%rsp)
leaq 0x60(%rsp), %r13
leaq 0x50(%rsp), %rdx
leaq 0x13(%rsp), %rcx
movq %r13, %rdi
callq 0x18df8
movq 0x30(%rbx), %rax
leaq (%rax,%r14), %rdi
addq $0x20, %rdi
movq %r13, %rsi
callq 0x1a1fc
movq %r13, %rdi
callq 0x195d0
xorl %r15d, %r15d
xorl %ebp, %ebp
movzbl 0x21(%rbx), %eax
cmpq %rax, %rbp
jae 0x17f42
leal 0x1(%rbp), %eax
movl %eax, 0x60(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x18e4e
fldt (%rax)
movq 0x30(%rbx), %rax
movq 0x20(%rax,%r14), %rcx
fld %st(0)
fstpt (%rcx,%r15)
fldz
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
jbe 0x17ef1
pushq $0x1
popq %rdx
movl %ebp, %ecx
shll %cl, %edx
addq %r14, %rax
orw %dx, 0x50(%rax)
addq $0x10, %r15
incq %rbp
jmp 0x17ea9
fstp %st(0)
movq 0x230ed(%rip), %rdi     # 0x3aff0
leaq 0x12920(%rip), %rsi     # 0x2a82a
callq 0x43a0
movq %rax, %rdi
movl %r13d, %esi
callq 0x46d0
leaq 0x1294b(%rip), %rsi     # 0x2a86c
movq %rax, %rdi
callq 0x43a0
movzbl 0x24(%rbx), %esi
movq %rax, %rdi
callq 0x46d0
movq %rax, %rdi
callq 0x41e0
pushq $-0x1
popq %rax
jmp 0x17f77
movq 0x30(%rbx), %rax
leaq (%rax,%r14), %rdi
addq $0x60, %rdi
movq 0x18(%rsp), %rsi
callq 0x196f2
movq 0x30(%rbx), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x90(%rax,%r14)
movl 0x14(%rsp), %ecx
movb %cl, 0x98(%rax,%r14)
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq