// File: /workspace/github_cpp_makefile_star2_O1/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 0x1c27c
movq %rbx, 0x28(%rsp)
movq 0x80(%rbx), %rax
leaq 0x48(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x1c27c
movq 0x10(%rsp), %rdx
cmpq 0x40(%rsp), %rdx
jne 0x25920
testq %rdx, %rdx
je 0x25924
movq 0x38(%rsp), %rsi
movq 0x8(%rsp), %rdi
callq 0x43b0
testl %eax, %eax
sete %bl
jmp 0x25926
xorl %ebx, %ebx
jmp 0x25926
movb $0x1, %bl
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x2593d
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x43a0
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x25954
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
xorl %ebp, %ebp
testb %bl, %bl
je 0x2596b
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%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 0x1c27c
movq (%r15), %r12
callq 0x4060
movq %rax, %r15
movl (%rax), %r13d
movl $0x0, (%rax)
leaq 0x38(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
movl $0xa, %edx
callq 0x4520
cmpq %r12, (%rbp)
je 0x25bcb
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x25bd7
movl (%r15), %ecx
cmpl $0x22, %ecx
je 0x25bd7
testl %ecx, %ecx
jne 0x259e3
movl %r13d, (%r15)
andb $0x7, %al
movl $0x1, %edx
movl %eax, %ecx
shll %cl, %edx
movl %edx, 0x34(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %r13
cmpq %r13, %rdi
je 0x25a0e
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
movq 0x28(%rsp), %rax
movq 0x80(%rax), %rax
leaq 0x8(%rsp), %r12
movq %r13, (%r12)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x1c27c
movq (%r12), %r12
movl (%r15), %ebx
movl $0x0, (%r15)
leaq 0x38(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
movl $0xa, %edx
callq 0x4520
cmpq %r12, (%rbp)
je 0x25be3
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
leaq -0x80000000(%rax), %rdx
cmpq %rcx, %rdx
jb 0x25bef
movl (%r15), %ecx
cmpl $0x22, %ecx
je 0x25bef
testl %ecx, %ecx
jne 0x25a8f
movl %ebx, (%r15)
leal 0x7(%rax), %ebx
testl %eax, %eax
cmovnsl %eax, %ebx
sarl $0x3, %ebx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x25ab1
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
movzbl %bl, %r12d
xorl %ebp, %ebp
leaq 0x18(%rsp), %r15
movq %r15, 0x8(%rsp)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %rdi
callq 0x1c27c
movq 0x28(%rsp), %rax
movq 0x80(%rax), %rax
leaq 0x48(%rsp), %r13
movq %r13, 0x38(%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x1c27c
movq 0x10(%rsp), %rdx
movb $0x1, %bl
cmpq 0x40(%rsp), %rdx
jne 0x25b2a
testq %rdx, %rdx
je 0x25b28
movq 0x38(%rsp), %rsi
movq 0x8(%rsp), %rdi
callq 0x43b0
testl %eax, %eax
setne %bl
jmp 0x25b2a
xorl %ebx, %ebx
movq 0x38(%rsp), %rdi
cmpq %r13, %rdi
je 0x25b41
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x43a0
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x25b58
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
testb %bl, %bl
je 0x2595a
movb 0x24(%r14), %bl
testb %bl, %bl
je 0x25ab7
xorl %r15d, %r15d
movzbl %r15b, %eax
movq 0x30(%r14), %rcx
leaq (%rax,%rax,4), %r13
movl %r13d, %eax
shll $0x5, %eax
leaq (%rcx,%rax), %rdi
addq $0x60, %rdi
movl $0x0, 0x8(%rsp)
leaq 0x8(%rsp), %rsi
callq 0x28660
movq (%rax), %rax
movzbl (%rax,%r12), %eax
testl %eax, 0x34(%rsp)
je 0x25bbe
movq 0x30(%r14), %rax
shll $0x5, %r13d
movq 0x90(%rax,%r13), %r14
incl %ebp
movl %ebx, %r15d
incb %r15b
cmpb %bl, %r15b
jb 0x25b6f
jmp 0x25ab7
leaq 0xba68(%rip), %rdi      # 0x3163a
callq 0x4250
leaq 0xba5c(%rip), %rdi      # 0x3163a
callq 0x4580
leaq 0xba50(%rip), %rdi      # 0x3163a
callq 0x4250
leaq 0xba44(%rip), %rdi      # 0x3163a
callq 0x4580
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x25c15
movl %ebx, (%r15)
jmp 0x25c15
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x25c15
movl %r13d, (%r15)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
jne 0x25c42
jmp 0x25c4f
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
jne 0x25c42
jmp 0x25c4f
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x25c4f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x43a0
movq %r14, %rdi
callq 0x4700
nop