// File: adamiaonr[P]rid-analytics/src/rid-analytics.cpp
// Function: RID_Analytics::run(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void RID_Analytics::run(
    std::string nw_filename,
    std::string start_router_id) {

    if (this->tp_sizes.empty())
        std::cout << "RID_Analytics::run() : [INFO] TP sizes is empty!";
    else {

        std::cout << "RID_Analytics::run() : [INFO] TP sizes keys : " << std::endl;
        RID_TPMap::iterator itr;
        for (itr = this->tp_sizes.begin(); itr != this->tp_sizes.end(); itr++)
            std::cout << "\tTP[" << itr->first << "]" << std::endl;
    }

    this->start_router = this->routers[start_router_id];

    // FIXME: not sure if it is ok to set the rid_router arg on Path_State() as NULL
    Path_State * init_state = new Path_State(NULL, request_size);
    init_state->set_length(0);

    // set the initial ingress probabilities: ptree_size = 0 gets 1.0 
    // probability, i.e. initially the request isn't bound to any prefix tree
    init_state->set_in_fptree_prob(0, 1.0);
    init_state->set_in_iface_prob(1.0);

    // all other prefix tree sizes get 0.0 probability
    for (uint8_t p = 1; p <= this->request_size; p++)
        init_state->set_in_fptree_prob(p, 0.0);

    // we first supply an empty tree bitmask for the path
    std::map<int, std::vector<uint8_t> > tree_bitmasks =
        (*this->start_router->get_tree_bitmasks(0));
    // fill the path tree bitmask w/ 0s
    std::map<int, std::vector<uint8_t> >::iterator itr;
    for (itr = tree_bitmasks.begin(); itr != tree_bitmasks.end(); itr++)
        std::fill(itr->second.begin(), itr->second.end(), 0x00);

    // set the path tree bitmask on path state
    init_state->set_tree_bitmasks(&tree_bitmasks);
    // set initial ttl to dist. from starting router to the origin server
    init_state->set_ttl(4);

    // we always start at the ingress iface, so that we don't have a local 
    // match at the initial router (not sure if this can work directly like this)
    run_rec(this->start_router, 0, init_state);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdx, -0xc8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
addq $0x20, %rdi
callq 0x52b80
testb $0x1, %al
jne 0x51568
jmp 0x51580
movq 0x30a49(%rip), %rdi     # 0x81fb8
leaq 0x12eb1(%rip), %rsi     # 0x64427
callq 0x5480
jmp 0x51654
movq 0x30a31(%rip), %rdi     # 0x81fb8
leaq 0x12eca(%rip), %rsi     # 0x64458
callq 0x5480
movq %rax, %rdi
movq 0x309f3(%rip), %rsi     # 0x81f90
callq 0x54f0
leaq -0x20(%rbp), %rdi
callq 0x52ba0
movq -0xc0(%rbp), %rdi
addq $0x20, %rdi
callq 0x52bc0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0xc0(%rbp), %rdi
addq $0x20, %rdi
callq 0x52c20
movq %rax, -0x30(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x52bf0
testb $0x1, %al
jne 0x515ee
jmp 0x51652
movq 0x309c3(%rip), %rdi     # 0x81fb8
leaq 0x12e8b(%rip), %rsi     # 0x64487
callq 0x5480
movq %rax, -0xd0(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x52c50
movq -0xd0(%rbp), %rdi
movq %rax, %rsi
callq 0x5430
movq %rax, %rdi
leaq 0xe6c7(%rip), %rsi      # 0x5fcf1
callq 0x5480
movq %rax, %rdi
movq 0x30957(%rip), %rsi     # 0x81f90
callq 0x54f0
leaq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x52c70
movq %rax, -0x38(%rbp)
jmp 0x515c7
jmp 0x51654
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
addq $0x50, %rdi
callq 0x51a60
movq %rax, %rcx
movq -0xc0(%rbp), %rax
movq (%rcx), %rcx
movq %rcx, 0x88(%rax)
movl $0x90, %edi
callq 0x54a0
movq %rax, %rdi
movq -0xc0(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0xd8(%rbp)
movzbl (%rax), %edx
xorl %eax, %eax
movl %eax, %esi
callq 0x3f4b0
jmp 0x516ab
movq -0xd8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x3f5a0
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movq %rsp, %rax
fld1
fstpt (%rax)
xorl %esi, %esi
callq 0x3f6e0
movq -0x40(%rbp), %rdi
movq %rsp, %rax
fld1
fstpt (%rax)
callq 0x3f720
movb $0x1, -0x4d(%rbp)
movq -0xc0(%rbp), %rcx
movzbl -0x4d(%rbp), %eax
movzbl (%rcx), %ecx
cmpl %ecx, %eax
jg 0x5173b
movq -0x40(%rbp), %rdi
movzbl -0x4d(%rbp), %esi
movq %rsp, %rax
fldz
fstpt (%rax)
callq 0x3f6e0
movb -0x4d(%rbp), %al
addb $0x1, %al
movb %al, -0x4d(%rbp)
jmp 0x516e9
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x90, %esi
callq 0x54b0
jmp 0x5189c
movq -0xc0(%rbp), %rax
movq 0x88(%rax), %rdi
xorl %esi, %esi
callq 0x528f0
movq %rax, %rsi
leaq -0x80(%rbp), %rdi
callq 0x52cb0
leaq -0x88(%rbp), %rdi
callq 0x351b0
leaq -0x80(%rbp), %rdi
callq 0x351d0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x35230
movq %rax, -0x98(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x35200
testb $0x1, %al
jne 0x517b2
jmp 0x51850
leaq -0x88(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x352a0
movq %rax, %rdi
addq $0x8, %rdi
callq 0x352c0
movq -0xe0(%rbp), %rdi
movq %rax, -0xa0(%rbp)
callq 0x352a0
movq %rax, %rdi
addq $0x8, %rdi
callq 0x352f0
movq %rax, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
leaq -0xac(%rbp), %rdx
callq 0x35260
jmp 0x5181d
jmp 0x5181f
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
callq 0x35320
movq %rax, -0xb8(%rbp)
jmp 0x51786
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x38fb0
jmp 0x5189c
movq -0x40(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x3f860
jmp 0x5185f
movq -0x40(%rbp), %rdi
movl $0x4, %esi
callq 0x3f8a0
jmp 0x5186f
movq -0xc0(%rbp), %rdi
movq 0x88(%rdi), %rsi
movq -0x40(%rbp), %rcx
xorl %edx, %edx
callq 0x50e90
jmp 0x5188a
leaq -0x80(%rbp), %rdi
callq 0x38fb0
addq $0xf0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x5920
nopw %cs:(%rax,%rax)
nop