// File: /workspace/github_cpp_makefile_star2_O2/adamiaonr[P]rid-analytics/src/rid-analytics.cpp
// Function: RID_Analytics::get_origin_distance(RID_Router*)
int RID_Analytics::get_origin_distance(RID_Router * from_router) {

    // check if we're there yet
    if (from_router->get_id() == this->origin_server->get_id())
        return 0;

    uint8_t bitmask_value = (1 << (std::stoi(this->origin_server->get_id()) % 8));
    uint8_t bitmask_byte = (std::stoi(this->origin_server->get_id()) / 8);

    // std::cout << "RID_Analytics::get_origin_distance() : [INFO] "
    //     << "\n\torigin_server_id = " << this->origin_server->get_id() 
    //     << "\n\tbitmask_value = " << (int) bitmask_value 
    //     << "\n\tbitmask_byte = " << (int) bitmask_byte << std::endl;

    int origin_distance = 0;
    RID_Router * next_router = from_router;
    while (next_router->get_id() != this->origin_server->get_id()) {

        // std::cout << "RID_Analytics::get_origin_distance() : [INFO] "
        //     << "\n\tnext router id = " << next_router->get_id() << std::endl;

        // get next router in path
        uint8_t iface_num = next_router->get_iface_num();
        for (uint8_t k = 0; k < iface_num; k++) {

            std::vector<uint8_t> * tree_bitmask = next_router->get_tree_bitmask(k);
            // std::cout << "RID_Analytics::get_origin_distance() : [INFO] "
            //     << "\n\ttree_bitmask = " << (int) tree_bitmask[bitmask_byte] 
            //     << "\n\tbitmask_value = " << (int) bitmask_value << std::endl;

            if ((bitmask_value & (*tree_bitmask)[bitmask_byte]) == bitmask_value) {

                // save the next router in the path in next_router
                next_router = next_router->get_next_hop(k).router;
                // increment the origin distance
                origin_distance++;
                // jump off the cycle
                k = iface_num;
            }
        }
    }

    return origin_distance;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4510
movq 0x80(%rbx), %rsi
leaq 0x38(%rsp), %rdi
callq 0x4510
leaq 0x8(%rsp), %r15
leaq 0x38(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x21774
movl %eax, %ebp
movq %r12, %rdi
callq 0x41c0
movq %r15, %rdi
callq 0x41c0
xorl %r12d, %r12d
testb %bpl, %bpl
je 0x1f92a
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x80(%rbx), %rsi
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x4510
pushq $0xa
popq %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x16ca1
andb $0x7, %al
pushq $0x1
popq %rdx
movl %eax, %ecx
shll %cl, %edx
movl %edx, 0x4(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x41c0
movq 0x80(%rbx), %rsi
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x4510
xorl %r12d, %r12d
pushq $0xa
popq %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x16ca1
pushq $0x8
popq %rcx
cltd
idivl %ecx
movl %eax, %ebp
leaq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x41c0
movzbl %bpl, %eax
movq %rax, 0x30(%rsp)
leaq 0x38(%rsp), %rbp
movq %rbx, 0x28(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x4510
movq 0x80(%rbx), %rsi
movq %rbp, %rdi
callq 0x4510
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2179d
movq %rbp, %rdi
movl %eax, %ebp
movq %rdi, %r15
callq 0x41c0
movq %r13, %rdi
callq 0x41c0
testb %bpl, %bpl
je 0x1f918
movb 0x24(%r14), %r13b
xorl %ebx, %ebx
movq %r15, %rbp
cmpb %r13b, %bl
jae 0x1fa3b
movzbl %bl, %ebp
movq %r14, %rdi
movl %ebp, %esi
callq 0x217a8
movq (%rax), %rax
movq 0x30(%rsp), %rcx
movzbl (%rax,%rcx), %eax
testl %eax, 0x4(%rsp)
je 0x1fa37
movq 0x30(%r14), %rax
imulq $0xa0, %rbp, %rcx
movq 0x90(%rax,%rcx), %r14
incl %r12d
movl %r13d, %ebx
incb %bl
jmp 0x1f9f7
movq 0x28(%rsp), %rbx
leaq 0x8(%rsp), %r13
jmp 0x1f9ae
jmp 0x1fa50
jmp 0x1fa50
jmp 0x1fa50
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x41c0
movq %rbx, %rdi
callq 0x4700
nop