// File: /workspace/github_cpp_makefile_star2_O3/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 0x18(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rsi), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0x1bdb4
movq %rbx, 0x48(%rsp)
movq 0x80(%rbx), %rax
leaq 0x38(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0x1bdb4
movq 0x10(%rsp), %rdx
cmpq 0x30(%rsp), %rdx
jne 0x25409
movq 0x28(%rsp), %r15
testq %rdx, %rdx
je 0x25412
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x43b0
testl %eax, %eax
sete %bl
jmp 0x25414
movq 0x28(%rsp), %r15
xorl %ebx, %ebx
jmp 0x25414
movb $0x1, %bl
leaq 0x38(%rsp), %rax
cmpq %rax, %r15
je 0x2542e
movq 0x38(%rsp), %rsi
incq %rsi
movq %r15, %rdi
callq 0x43a0
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x25445
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
xorl %ebp, %ebp
testb %bl, %bl
je 0x2545c
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rsp), %rax
movq 0x80(%rax), %rax
leaq 0x8(%rsp), %r15
movq %r12, (%r15)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x1bdb4
movq (%r15), %r12
callq 0x4060
movq %rax, %r15
movl (%rax), %ebp
movl $0x0, (%rax)
leaq 0x28(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
movl $0xa, %edx
callq 0x4520
cmpq %r12, (%r13)
je 0x256cb
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x256d7
movl (%r15), %ecx
cmpl $0x22, %ecx
je 0x256d7
testl %ecx, %ecx
leaq 0x18(%rsp), %r12
jne 0x254d8
movl %ebp, (%r15)
andb $0x7, %al
movl $0x1, %edx
movl %eax, %ecx
shll %cl, %edx
movl %edx, 0x54(%rsp)
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x254fe
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
movq 0x48(%rsp), %rax
movq 0x80(%rax), %rax
leaq 0x8(%rsp), %r13
movq %r12, (%r13)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x1bdb4
movq (%r13), %rbp
movl (%r15), %ebx
movl $0x0, (%r15)
leaq 0x28(%rsp), %r13
movq %rbp, %rdi
movq %r13, %rsi
movl $0xa, %edx
callq 0x4520
cmpq %rbp, (%r13)
je 0x256e3
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
leaq -0x80000000(%rax), %rdx
cmpq %rcx, %rdx
jb 0x256ef
movl (%r15), %ecx
cmpl $0x22, %ecx
je 0x256ef
testl %ecx, %ecx
jne 0x2557f
movl %ebx, (%r15)
leal 0x7(%rax), %ebx
testl %eax, %eax
cmovnsl %eax, %ebx
sarl $0x3, %ebx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x255a1
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
movzbl %bl, %ebx
xorl %ebp, %ebp
leaq 0x18(%rsp), %r12
movq %r12, 0x8(%rsp)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0x1bdb4
movq 0x48(%rsp), %rax
movq 0x80(%rax), %rax
leaq 0x38(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0x1bdb4
movq 0x10(%rsp), %rdx
cmpq 0x30(%rsp), %rdx
jne 0x25619
movq 0x28(%rsp), %r13
testq %rdx, %rdx
je 0x25623
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x43b0
testl %eax, %eax
setne %r15b
jmp 0x25626
movb $0x1, %r15b
movq 0x28(%rsp), %r13
jmp 0x25626
xorl %r15d, %r15d
leaq 0x38(%rsp), %rax
cmpq %rax, %r13
je 0x25640
movq 0x38(%rsp), %rsi
incq %rsi
movq %r13, %rdi
callq 0x43a0
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x25657
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
testb %r15b, %r15b
je 0x2544b
movb 0x24(%r14), %r13b
testb %r13b, %r13b
je 0x255a6
xorl %r15d, %r15d
movzbl %r15b, %eax
movq 0x30(%r14), %rcx
leaq (%rax,%rax,4), %r12
movl %r12d, %eax
shll $0x5, %eax
leaq (%rcx,%rax), %rdi
addq $0x60, %rdi
movl $0x0, 0x8(%rsp)
leaq 0x8(%rsp), %rsi
callq 0x28182
movq (%rax), %rax
movzbl (%rax,%rbx), %eax
testl %eax, 0x54(%rsp)
je 0x256be
movq 0x30(%r14), %rax
shll $0x5, %r12d
movq 0x90(%rax,%r12), %r14
incl %ebp
movl %r13d, %r15d
incb %r15b
cmpb %r13b, %r15b
jb 0x25670
jmp 0x255a6
leaq 0xad48(%rip), %rdi      # 0x3041a
callq 0x4250
leaq 0xad3c(%rip), %rdi      # 0x3041a
callq 0x4580
leaq 0xad30(%rip), %rdi      # 0x3041a
callq 0x4250
leaq 0xad24(%rip), %rdi      # 0x3041a
callq 0x4580
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x25715
movl %ebx, (%r15)
jmp 0x25715
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x25715
movl %ebp, (%r15)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
jne 0x25735
jmp 0x25742
jmp 0x25728
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x25742
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
movq %r14, %rdi
callq 0x4700