// File: /workspace/github_cpp_makefile_star2_O3/davidgfnet[P]tadns/tadns.cc
// Function: DNSResolver::resolveInt(unsigned int, void*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, dns_record_type, void (*)(dns_cb_data*))
void DNSResolver::resolveInt(unsigned niter, void *ctx, std::string name,
                          enum dns_record_type qtype, dns_callback_t callback) {

    std::transform(name.begin(), name.end(), name.begin(), ::tolower);

    if (niter >= DNS_QUERY_MAXITER) {
        call_user(this, name, qtype, NULL, callback, ctx, DNS_TIMEOUT);
        return;
    }

    /* Check cache right away */
    struct cache_entry *dentry = find_cached_query(qtype, name);
    if (dentry) {
        call_user(this, name, qtype, dentry, callback, ctx, DNS_OK);
        return;
    }

    /* Do recursive solving */
    unsigned level = 0;
    union {
        struct sockaddr_storage addrst;
        struct sockaddr_in6 ipv6addr;
        struct sockaddr_in ipv4addr;
    } addrunion;
    struct sockaddr * dnss = NULL;

    while (1) {
        std::string part; bool lastq;
        std::tie(part, lastq) = tokenizedom(name, level);
        enum dns_record_type iqtype = lastq ? qtype : DNS_NS_RECORD;

        /* Search the cache first */
        struct cache_entry *centry = find_cached_query(iqtype, part);
        if (centry != NULL) {
            if (lastq) {
                assert(iqtype == qtype);
                // This is the original request, all set, shoot!
                call_user(this, name, qtype, centry, callback, ctx, DNS_OK);
                return;
            }

            // We get here cause the current "part" has some NS responses
            // Sometimes the NS responses come with the A/AAAA records (optimization!)
            // Also we don't want to always use the same NS server, so round robin it
            std::string ipaddr = getNSipaddr(level, niter, centry->replies, part, ctx, name, qtype, callback);

            if (!ipaddr.size())
                return;

            if (ipaddr.size() == 4) {
                addrunion.ipv4addr.sin_family = AF_INET;
                addrunion.ipv4addr.sin_port = htons(53);
                memcpy(&addrunion.ipv4addr.sin_addr, ipaddr.c_str(), 4); // FIXME: Check addr sizes

                dnss = (struct sockaddr*)&addrunion;
            }
        } else {
            // We don't have the NS for that record, ask for it (cache miss)
            Query * oq = createQuery(niter, ctx, name, qtype, callback);
            this->queue(niter, part, oq, iqtype, dnss);
            return; // Will come back here eventually :D
        }

        level++;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %r9, 0x18(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, %r12
movq %rdx, 0x10(%rsp)
movl %esi, 0x4(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x8(%rcx), %rbx
testq %rbx, %rbx
je 0x5120
movq (%r12), %r14
xorl %r15d, %r15d
movsbl (%r14,%r15), %edi
callq 0x3390
movb %al, (%r14,%r15)
incq %r15
cmpq %r15, %rbx
jne 0x510a
cmpl $0x10, 0x4(%rsp)
jb 0x518c
leaq 0x1b0(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x1a0(%rsp), %rbx
movq %rbx, %rdi
callq 0x3c72
movzwl 0x8(%rsp), %esi
movq %rbx, %rdi
xorl %edx, %edx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r8
movl $0x2, %r9d
callq 0x501c
movq 0x1a0(%rsp), %rdi
cmpq %r14, %rdi
je 0x5668
movq 0x1b0(%rsp), %rsi
jmp 0x5249
leaq 0x190(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x180(%rsp), %rbx
movq %rbx, %rdi
callq 0x3c72
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x46a4
movq %rax, %rbx
movq 0x180(%rsp), %rdi
cmpq %r14, %rdi
je 0x51e6
movq 0x190(%rsp), %rsi
incq %rsi
callq 0x32b0
testq %rbx, %rbx
je 0x5256
leaq 0x170(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x160(%rsp), %r14
movq %r14, %rdi
callq 0x3c72
movzwl 0x8(%rsp), %esi
movq %r14, %rdi
movq %rbx, %rdx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r8
xorl %r9d, %r9d
callq 0x501c
movq 0x160(%rsp), %rdi
cmpq %r15, %rdi
je 0x5668
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x5668
xorl %ebx, %ebx
leaq 0x118(%rsp), %r14
movzwl 0x8(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq $0x0, 0x50(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
leaq 0x128(%rsp), %rax
movq %rax, 0x118(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x3c72
leaq 0x138(%rsp), %r15
movq %r15, %rdi
movq %r14, %r13
movq %r14, %rsi
movl %ebx, %edx
callq 0x5a55
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x3340
movb 0x158(%rsp), %r15b
movq 0x138(%rsp), %rdi
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x5300
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x5325
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x8(%rsp), %rax
movl %eax, %r14d
testb %r15b, %r15b
jne 0x5338
movl $0x2, %r14d
leaq 0x108(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0xf8(%rsp), %rbp
movq %rbp, %rdi
callq 0x3c72
movq 0x20(%rsp), %rdi
movl %r14d, %esi
movq %rbp, %rdx
callq 0x46a4
movq %rax, %rbp
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x539d
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x32b0
testq %rbp, %rbp
je 0x541b
testb %r15b, %r15b
je 0x54e7
leaq 0xc8(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0xb8(%rsp), %rdi
callq 0x3c72
leaq 0xb8(%rsp), %rdi
movl 0x4c(%rsp), %esi
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r8
xorl %r9d, %r9d
callq 0x501c
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x54e0
movq 0xc8(%rsp), %rsi
jmp 0x54d8
leaq 0x88(%rsp), %rax
movq %rax, 0x78(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rdi
callq 0x3c72
movq 0x20(%rsp), %rdi
movl 0x4(%rsp), %esi
movq 0x10(%rsp), %rdx
leaq 0x78(%rsp), %rcx
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r9
callq 0x66fa
movq %rax, %rbp
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x5485
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x3c72
movq 0x20(%rsp), %rdi
movl 0x4(%rsp), %esi
leaq 0x58(%rsp), %rdx
movq %rbp, %rcx
movl %r14d, %r8d
movq 0x50(%rsp), %r9
callq 0x63e4
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x54e0
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x32b0
xorl %ebp, %ebp
jmp 0x5640
leaq 0x1c0(%rsp), %rdi
movq %rbp, %rsi
callq 0x7222
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x98(%rsp), %rdi
callq 0x3c72
leaq 0xe8(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0xd8(%rsp), %r14
movq %r14, %rdi
callq 0x3c72
leaq 0x138(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl %ebx, %edx
movl 0x4(%rsp), %ecx
leaq 0x1c0(%rsp), %r8
leaq 0x98(%rsp), %r9
pushq 0x18(%rsp)
pushq 0x10(%rsp)
pushq %r14
pushq 0x28(%rsp)
callq 0x5d7e
addq $0x20, %rsp
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x55ac
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x55d1
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x1c0(%rsp), %rdi
callq 0x3b80
movq 0x138(%rsp), %rdi
movq 0x140(%rsp), %r14
testq %r14, %r14
setne %bpl
cmpq $0x4, %r14
jne 0x561c
movl $0x35000002, 0x1d8(%rsp) # imm = 0x35000002
movl (%rdi), %eax
movl %eax, 0x1dc(%rsp)
leaq 0x1d8(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x5639
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x32b0
cmpq $0x1, %r14
sbbl $-0x1, %ebx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x565c
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32b0
testb %bpl, %bpl
movq %r13, %r14
jne 0x5272
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x160(%rsp), %rdi
cmpq %r15, %rdi
je 0x581b
movq 0x170(%rsp), %rsi
jmp 0x5813
movq %rax, %rbx
movq 0x180(%rsp), %rdi
cmpq %r14, %rdi
je 0x581b
movq 0x190(%rsp), %rsi
jmp 0x5813
movq %rax, %rbx
movq 0x1a0(%rsp), %rdi
cmpq %r14, %rdi
je 0x581b
movq 0x1b0(%rsp), %rsi
jmp 0x5813
jmp 0x57fc
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x57ff
movq 0xc8(%rsp), %rsi
jmp 0x57f2
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x5738
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x5738
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x5762
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x5762
movq %rax, %rbx
leaq 0x1c0(%rsp), %rdi
callq 0x3b80
jmp 0x57ff
movq %rax, %rbx
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x57ff
movq 0x88(%rsp), %rsi
jmp 0x57f2
movq %rax, %rbx
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x57ff
movq 0x68(%rsp), %rsi
jmp 0x57f2
jmp 0x57fc
jmp 0x57fc
movq %rax, %rbx
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x57ff
movq 0x128(%rsp), %rsi
jmp 0x57f2
movq %rax, %rbx
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x57ff
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x57ff
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x581b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32b0
movq %rbx, %rdi
callq 0x3400
nop