// File: /workspace/github_cpp_makefile_star2_O3/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 $0x128, %rsp            # imm = 0x128
movl %ecx, 0x2c(%rsp)
movl %edx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
leaq 0x120(%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), %rbx
movq 0x8(%r8), %r14
cmpq %r14, %rbx
je 0x606a
movq %r9, %r15
movq %r13, 0x18(%rsp)
movq %rsp, %r12
leaq 0xf0(%rsp), %r13
leaq 0x8(%rbx), %rbp
movq 0x10(%rbx), %rdx
cmpq 0x8(%r15), %rdx
jne 0x5e38
testq %rdx, %rdx
je 0x5e1b
movq (%r15), %rsi
movq (%rbp), %rdi
callq 0x32c0
testl %eax, %eax
sete %cl
jmp 0x5e1d
movb $0x1, %cl
movl (%rbx), %eax
cmpl $0x2, %eax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5e3a
leaq 0x28(%rbx), %rsi
movq %r12, %rdi
callq 0x7186
movl (%rbx), %eax
cmpl $0x1, %eax
jne 0x5e56
movq %r13, %rdi
movq %rbp, %rsi
callq 0x8a14
leaq 0x28(%rbx), %rsi
movq %rax, %rdi
callq 0x3170
addq $0x50, %rbx
cmpq %r14, %rbx
jne 0x5df5
movq (%rsp), %r12
movq 0x8(%rsp), %rbx
cmpq %r12, %rbx
movq 0x18(%rsp), %r13
je 0x606a
leaq 0x20(%r12), %rax
cmpq %rbx, %rax
je 0x5ece
movl $0x20, %r14d
callq 0x30e0
cltq
movq %r14, %rcx
sarq $0x5, %rcx
incq %rcx
cqto
idivq %rcx
shlq $0x5, %rdx
cmpq %rdx, %r14
je 0x5eb4
leaq (%r12,%r14), %rdi
addq %r12, %rdx
movq %rdx, %rsi
callq 0x3200
leaq (%r12,%r14), %rax
addq $0x20, %rax
addq $0x20, %r14
cmpq %rbx, %rax
jne 0x5e86
movq (%rsp), %r12
movq 0x8(%rsp), %rbx
cmpq %rbx, %r12
je 0x5f01
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x8e42
testq %rax, %rax
jne 0x608d
addq $0x20, %r12
cmpq %rbx, %r12
jne 0x5edb
movq (%rsp), %r12
movq 0x8(%rsp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
cmpq %r12, %rbx
je 0x6254
xorl %r14d, %r14d
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
leaq 0x30(%rsp), %r15
xorl %ebx, %ebx
movl %r13d, %ebp
shlq $0x5, %r14
leaq 0xe0(%rsp), %r13
movq %r13, 0xd0(%rsp)
movq (%r12,%r14), %rsi
movq 0x8(%r12,%r14), %rdx
addq %rsi, %rdx
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
callq 0x3c72
movq 0x20(%rsp), %rdi
movl $0x1, %esi
movq %r12, %rdx
callq 0x46a4
movq %rax, %r12
movq 0xd0(%rsp), %rdi
cmpq %r13, %rdi
je 0x5f90
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x32b0
movl %ebx, %r13d
testq %r12, %r12
je 0x5ff2
movq (%r12), %r13
movq 0x8(%r12), %r12
cmpq %r12, %r13
je 0x5fef
movq (%rsp), %rax
movq 0x10(%r13), %rdx
cmpq 0x8(%rax,%r14), %rdx
jne 0x5fe9
testq %rdx, %rdx
je 0x5fd0
addq %r14, %rax
movq (%rax), %rsi
movq 0x8(%r13), %rdi
callq 0x32c0
testl %eax, %eax
sete %al
jmp 0x5fd2
movb $0x1, %al
testb %al, %al
je 0x5fe9
cmpl $0x1, (%r13)
jne 0x5fe9
leaq 0x28(%r13), %rsi
movq %r15, %rdi
callq 0x7186
addq $0x50, %r13
jmp 0x5fa1
movl %ebp, %r13d
incl %ebx
movq (%rsp), %r12
movq 0x8(%rsp), %rbp
movq %rbp, %rax
subq %r12, %rax
sarq $0x5, %rax
movq %rbx, %r14
cmpq %rbx, %rax
ja 0x5f2b
movq 0x38(%rsp), %rax
cmpq 0x30(%rsp), %rax
je 0x60c6
callq 0x30e0
cltq
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rsi
subq %rcx, %rsi
sarq $0x5, %rsi
xorl %edx, %edx
divq %rsi
shlq $0x5, %rdx
movq 0x18(%rsp), %r13
leaq 0x10(%r13), %rax
movq %rax, (%r13)
movq (%rcx,%rdx), %rsi
movq 0x8(%rcx,%rdx), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x3c72
jmp 0x62db
leaq 0x10(%r13), %rax
movq %rax, (%r13)
leaq 0x50f1(%rip), %rsi      # 0xb16a
leaq 0x50eb(%rip), %rdx      # 0xb16b
movq %r13, %rdi
callq 0x8562
jmp 0x62e5
leaq 0xf0(%rsp), %rdi
movq %r12, %rsi
callq 0x8e7c
testq %rax, %rax
je 0x630f
leaq 0x10(%r13), %rcx
movq %rcx, (%r13)
movq 0x28(%rax), %rsi
movq 0x30(%rax), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x3c72
jmp 0x62e5
cmpl $0x3, 0x28(%rsp)
setae %al
cmpq %rbp, %r12
sete %cl
orb %al, %cl
movl 0x2c(%rsp), %r14d
jne 0x614a
leaq 0xc0(%rsp), %rbx
leaq 0xb0(%rsp), %r15
movq %rbx, 0xb0(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x3c72
movq 0x20(%rsp), %rdi
movl %r14d, %esi
movq %r15, %rdx
xorl %ecx, %ecx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0x63e4
movq 0xb0(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6141
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x32b0
addq $0x20, %r12
cmpq %rbp, %r12
jne 0x60ed
testl %r13d, %r13d
js 0x6254
leaq 0xa0(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x168(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x3c72
leaq 0x90(%rsp), %rcx
movq 0x20(%rsp), %rdi
movl %r14d, %esi
movq 0x160(%rsp), %rdx
movl 0x170(%rsp), %r8d
movq 0x178(%rsp), %r9
callq 0x66fa
movq %rax, %r15
movq 0x90(%rsp), %rdi
cmpq %rbx, %rdi
je 0x61cb
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x32b0
movl %r13d, %eax
movq (%rsp), %rcx
shlq $0x5, %rax
leaq 0x80(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rcx,%rax), %rsi
movq 0x8(%rcx,%rax), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x3c72
leaq 0x70(%rsp), %rdx
movq 0x20(%rsp), %rdi
movl %r14d, %esi
movq %r15, %rcx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0x63e4
movq 0x18(%rsp), %r13
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6235
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x10(%r13), %rax
movq %rax, (%r13)
leaq 0x4df4(%rip), %rdx      # 0xb038
movq %r13, %rdi
movq %rdx, %rsi
callq 0x8562
jmp 0x62db
leaq 0x60(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x168(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x50(%rsp), %rdi
callq 0x3c72
movq 0x18(%rsp), %r13
movl 0x170(%rsp), %eax
movzwl %ax, %esi
leaq 0x50(%rsp), %rdi
xorl %edx, %edx
movq 0x178(%rsp), %rcx
movq 0x160(%rsp), %r8
movl $0x1, %r9d
callq 0x501c
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x62c1
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x10(%r13), %rax
movq %rax, (%r13)
leaq 0x4d68(%rip), %rdx      # 0xb038
movq %r13, %rdi
movq %rdx, %rsi
callq 0x8562
leaq 0x30(%rsp), %rdi
callq 0x71c4
leaq 0xf0(%rsp), %rdi
callq 0x79fa
movq %rsp, %rdi
callq 0x71c4
movq %r13, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4f7a(%rip), %rdi      # 0xb290
callq 0x3380
jmp 0x63b5
movq %rax, %r14
movq 0x70(%rsp), %rdi
cmpq %rbx, %rdi
je 0x63b8
movq 0x80(%rsp), %rsi
jmp 0x63ab
movq %rax, %r14
movq 0x90(%rsp), %rdi
cmpq %rbx, %rdi
je 0x63b8
movq 0xa0(%rsp), %rsi
jmp 0x63ab
jmp 0x63b5
jmp 0x63b5
movq %rax, %r14
movq 0x50(%rsp), %rdi
cmpq %rbx, %rdi
je 0x63b8
movq 0x60(%rsp), %rsi
jmp 0x63ab
jmp 0x63b5
jmp 0x63c4
jmp 0x63c4
jmp 0x63b5
movq %rax, %r14
movq 0xb0(%rsp), %rdi
cmpq %rbx, %rdi
je 0x63b8
movq 0xc0(%rsp), %rsi
jmp 0x63ab
jmp 0x63c4
jmp 0x63b5
movq %rax, %r14
movq 0xd0(%rsp), %rdi
cmpq %r13, %rdi
je 0x63b8
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x63b8
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x71c4
jmp 0x63c7
movq %rax, %r14
leaq 0xf0(%rsp), %rdi
callq 0x79fa
movq %rsp, %rdi
callq 0x71c4
movq %r14, %rdi
callq 0x3400