// File: /workspace/github_cpp_makefile_star2_O1/davidgfnet[P]tadns/tadns.cc
// Function: DNSResolver::getNSipaddr(unsigned int, unsigned int, std::vector<dns_record, std::allocator<dns_record>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, void*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, dns_record_type, void (*)(dns_cb_data*))
std::string
DNSResolver::getNSipaddr(unsigned level, unsigned niter, std::vector<struct dns_record> recs,
                         std::string dom, void *ctx, std::string name,
                         enum dns_record_type qtype, dns_callback_t callback) {

    // Filter all the NS records
    std::vector<std::string> nsrecs;
    std::unordered_map<std::string, std::string> arecs;
    for (const auto & e: recs) {
        if (e.name == dom && e.rtype == DNS_NS_RECORD)
            nsrecs.push_back(e.addr);
        if (e.rtype == DNS_A_RECORD)
            arecs[e.name] = e.addr;
    }

    // If we have no NS servers, it gets a bit more interesting.
    // According to rfc7816 (Qname minimization) we should need to go one level up
    // trying the same server
    if (!nsrecs.size())
        return "-";   // Hack, shame on me

    // Get a random permutation to avoid banging the same server
    std::random_shuffle(nsrecs.begin(), nsrecs.end());

    // Now go and check whether we have the A adress for them (in response)
    for (const auto & e: nsrecs)
        if (arecs.count(e))
            return arecs.at(e);

    // Seems like we don't have it in the packet, check the query cache
    std::vector<std::string> ipaddrs;
    int totry = -1;
    //for (const auto & e: nsrecs) {
    for (unsigned i = 0; i < nsrecs.size(); i++) {
        struct cache_entry *nsentry = find_cached_query(DNS_A_RECORD, nsrecs[i]);
        if (nsentry != NULL) {
            // Get any valid IP address (random)
            for (const auto & aent: nsentry->replies)
                if (aent.name == nsrecs[i] && aent.rtype == DNS_A_RECORD)
                    ipaddrs.push_back(aent.addr);
        }
        else
            totry = i;
    }
    // Pick a random one if any, a bit biased here
    if (ipaddrs.size())
        return ipaddrs[std::rand() % ipaddrs.size()];

    // Now, packet was useless, cache too, issue a request for ips
    // Prefetch all of them for better balancing and stuff
    #ifdef DO_DNS_PREFETCH
    if (level <= DO_DNS_PREFETCH) {
        for (const auto & e: nsrecs)
            this->queue(niter, e, NULL, DNS_A_RECORD, NULL);
    }
    #endif

    if (totry >= 0) {
        Query * oq = createQuery(niter, ctx, name, qtype, callback);
        this->queue(niter, nsrecs[totry], oq, DNS_A_RECORD, NULL);
        return "";
    }
    else {
        // None of the NS servers are really up for this!
        call_user(this, name, qtype, NULL, callback, ctx, DNS_DOES_NOT_EXIST);
    }
    return "";
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %ecx, 0x4(%rsp)
movl %edx, 0x3c(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x130(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq (%r8), %r14
movq 0x8(%r8), %rbp
cmpq %rbp, %r14
je 0x5e9e
movq %r9, %r12
leaq 0x10(%rsp), %r13
leaq 0x100(%rsp), %r15
leaq 0x8(%r14), %rbx
movq 0x10(%r14), %rdx
cmpq 0x8(%r12), %rdx
jne 0x5e5c
testq %rdx, %rdx
je 0x5e60
movq (%r12), %rsi
movq (%rbx), %rdi
callq 0x32c0
testl %eax, %eax
sete %al
jmp 0x5e62
xorl %eax, %eax
jmp 0x5e62
movb $0x1, %al
testb %al, %al
je 0x5e78
cmpl $0x2, (%r14)
jne 0x5e78
leaq 0x28(%r14), %rsi
movq %r13, %rdi
callq 0x71a4
cmpl $0x1, (%r14)
jne 0x5e95
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8a94
leaq 0x28(%r14), %rsi
movq %rax, %rdi
callq 0x3170
addq $0x50, %r14
cmpq %rbp, %r14
jne 0x5e35
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %rbx, %r14
je 0x6056
leaq 0x20(%rbx), %rax
cmpq %r14, %rax
je 0x5eff
movl $0x20, %r15d
callq 0x30e0
cltq
movq %r15, %rcx
sarq $0x5, %rcx
incq %rcx
cqto
idivq %rcx
shlq $0x5, %rdx
cmpq %rdx, %r15
je 0x5eee
leaq (%rbx,%r15), %rdi
addq %rbx, %rdx
movq %rdx, %rsi
callq 0x3200
leaq (%rbx,%r15), %rax
addq $0x20, %rax
addq $0x20, %r15
cmpq %r14, %rax
jne 0x5ec0
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0x5f33
leaq 0x100(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8ecc
testq %rax, %rax
jne 0x607d
addq $0x20, %rbx
cmpq %r14, %rbx
jne 0x5f16
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rbp
cmpq %rbx, %rbp
je 0x60ba
xorl %r14d, %r14d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
leaq 0x40(%rsp), %r13
xorl %r12d, %r12d
shlq $0x5, %r14
leaq 0xf0(%rsp), %rbp
movq %rbp, 0xe0(%rsp)
movq (%rbx,%r14), %rsi
movq 0x8(%rbx,%r14), %rdx
addq %rsi, %rdx
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdi
callq 0x3c70
movq 0x30(%rsp), %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x46c0
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5fca
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x32b0
movl %r12d, %ecx
testq %rbx, %rbx
je 0x602e
movq (%rbx), %rbp
movq 0x8(%rbx), %rbx
cmpq %rbx, %rbp
je 0x602b
movq 0x10(%rsp), %rax
movq 0x10(%rbp), %rdx
cmpq 0x8(%rax,%r14), %rdx
jne 0x6009
testq %rdx, %rdx
je 0x600d
addq %r14, %rax
movq (%rax), %rsi
movq 0x8(%rbp), %rdi
callq 0x32c0
testl %eax, %eax
sete %al
jmp 0x600f
xorl %eax, %eax
jmp 0x600f
movb $0x1, %al
testb %al, %al
je 0x6025
cmpl $0x1, (%rbp)
jne 0x6025
leaq 0x28(%rbp), %rsi
movq %r13, %rdi
callq 0x71a4
addq $0x50, %rbp
jmp 0x5fd9
movl %r15d, %ecx
incl %r12d
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rbp
movq %rbp, %rax
subq %rbx, %rax
sarq $0x5, %rax
movq %r12, %r14
movl %ecx, %r15d
cmpq %r12, %rax
ja 0x5f68
jmp 0x60bf
movq 0x8(%rsp), %r15
leaq 0x10(%r15), %rax
movq %rax, (%r15)
leaq 0x5101(%rip), %rsi      # 0xb16a
leaq 0x50fb(%rip), %rdx      # 0xb16b
movq %r15, %rdi
callq 0x85b8
jmp 0x6321
leaq 0x100(%rsp), %rdi
movq %rbx, %rsi
callq 0x8f06
movq 0x8(%rsp), %r15
testq %rax, %rax
je 0x634d
leaq 0x10(%r15), %rcx
movq %rcx, (%r15)
movq 0x28(%rax), %rsi
movq 0x30(%rax), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x3c70
jmp 0x6321
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x48(%rsp), %rax
cmpq 0x40(%rsp), %rax
je 0x6111
callq 0x30e0
cltq
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rsi
subq %rcx, %rsi
sarq $0x5, %rsi
xorl %edx, %edx
divq %rsi
shlq $0x5, %rdx
movq 0x8(%rsp), %r15
leaq 0x10(%r15), %rax
movq %rax, (%r15)
movq (%rcx,%rdx), %rsi
movq 0x8(%rcx,%rdx), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x3c70
jmp 0x6317
movl %ecx, %r13d
cmpl $0x2, 0x3c(%rsp)
movl 0x4(%rsp), %r12d
ja 0x6190
cmpq %rbp, %rbx
je 0x6190
leaq 0xd0(%rsp), %r15
leaq 0xc0(%rsp), %r14
movq %r15, 0xc0(%rsp)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x3c70
movq 0x30(%rsp), %rdi
movl %r12d, %esi
movq %r14, %rdx
xorl %ecx, %ecx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0x642a
movq 0xc0(%rsp), %rdi
cmpq %r15, %rdi
je 0x6187
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x32b0
addq $0x20, %rbx
cmpq %rbp, %rbx
jne 0x6135
movq 0x188(%rsp), %rbp
movl 0x180(%rsp), %ebx
movq 0x178(%rsp), %rax
movq 0x170(%rsp), %r12
testl %r13d, %r13d
js 0x62aa
leaq 0xb0(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0xa0(%rsp), %rdi
callq 0x3c70
movq 0x8(%rsp), %r15
leaq 0xa0(%rsp), %rcx
movq 0x30(%rsp), %rdi
movl 0x4(%rsp), %esi
movq %r12, %rdx
movl %ebx, %r8d
movq %rbp, %r9
callq 0x6730
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
cmpq %r14, %rdi
je 0x621f
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x32b0
movl %r13d, %eax
movq 0x10(%rsp), %rcx
shlq $0x5, %rax
leaq 0x90(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rcx,%rax), %rsi
movq 0x8(%rcx,%rax), %rdx
addq %rsi, %rdx
leaq 0x80(%rsp), %rdi
callq 0x3c70
leaq 0x80(%rsp), %rdx
movq 0x30(%rsp), %rdi
movl 0x4(%rsp), %esi
movq %rbx, %rcx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0x642a
movq 0x80(%rsp), %rdi
cmpq %r14, %rdi
je 0x628f
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x10(%r15), %rax
movq %rax, (%r15)
leaq 0x4d9b(%rip), %rdx      # 0xb038
movq %r15, %rdi
movq %rdx, %rsi
callq 0x85b8
jmp 0x6317
leaq 0x70(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %rdi
callq 0x3c70
movq 0x8(%rsp), %r15
movzwl %bx, %esi
leaq 0x60(%rsp), %rdi
xorl %edx, %edx
movq %rbp, %rcx
movq %r12, %r8
movl $0x1, %r9d
callq 0x5038
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x62fe
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x10(%r15), %rax
movq %rax, (%r15)
leaq 0x4d2c(%rip), %rdx      # 0xb038
movq %r15, %rdi
movq %rdx, %rsi
callq 0x85b8
leaq 0x40(%rsp), %rdi
callq 0x71e2
leaq 0x100(%rsp), %rdi
callq 0x7a40
leaq 0x10(%rsp), %rdi
callq 0x71e2
movq %r15, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4fb2(%rip), %rdi      # 0xb306
callq 0x3380
jmp 0x63fe
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0x6401
movq 0x70(%rsp), %rsi
jmp 0x63ef
jmp 0x63fe
movq %rax, %rbx
movq 0x80(%rsp), %rdi
cmpq %r14, %rdi
je 0x6401
movq 0x90(%rsp), %rsi
jmp 0x63ef
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
cmpq %r14, %rdi
je 0x6401
movq 0xb0(%rsp), %rsi
jmp 0x63ef
jmp 0x63fe
jmp 0x63fe
jmp 0x63f9
jmp 0x63f9
jmp 0x63fe
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
cmpq %r15, %rdi
je 0x6401
movq 0xd0(%rsp), %rsi
jmp 0x63ef
jmp 0x63f9
jmp 0x63fe
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x6401
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x6401
movq %rax, %rbx
jmp 0x640b
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x71e2
leaq 0x100(%rsp), %rdi
callq 0x7a40
leaq 0x10(%rsp), %rdi
callq 0x71e2
movq %rbx, %rdi
callq 0x3400