// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq 0x8(%rcx), %rbx
testq %rbx, %rbx
je 0x513a
movq (%r12), %r14
xorl %r15d, %r15d
movsbl (%r14,%r15), %edi
callq 0x3390
movb %al, (%r14,%r15)
incq %r15
cmpq %r15, %rbx
jne 0x5124
cmpl $0x10, 0x4(%rsp)
jb 0x51a6
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 0x3c70
movzwl 0x8(%rsp), %esi
movq %rbx, %rdi
xorl %edx, %edx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r8
movl $0x2, %r9d
callq 0x5038
movq 0x1a0(%rsp), %rdi
cmpq %r14, %rdi
je 0x56a6
movq 0x1b0(%rsp), %rsi
jmp 0x5261
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 0x3c70
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x46c0
movq %rax, %rbx
movq 0x180(%rsp), %rdi
cmpq %r14, %rdi
je 0x51fe
movq 0x190(%rsp), %rsi
incq %rsi
callq 0x32b0
testq %rbx, %rbx
je 0x526e
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 0x3c70
movzwl 0x8(%rsp), %esi
movq %r14, %rdi
movq %rbx, %rdx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r8
xorl %r9d, %r9d
callq 0x5038
movq 0x160(%rsp), %rdi
cmpq %r15, %rdi
je 0x56a6
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x56a6
movq $0x0, 0x20(%rsp)
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 0x3c70
leaq 0x138(%rsp), %r15
movq %r15, %rdi
movq %r14, %rbx
movq %r14, %rsi
movq 0x20(%rsp), %rdx
callq 0x5ab5
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 0x5322
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x5347
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x8(%rsp), %rax
movl %eax, %r14d
testb %r15b, %r15b
jne 0x535a
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 0x3c70
movq %r13, %rdi
movl %r14d, %esi
movq %rbp, %rdx
callq 0x46c0
movq %rax, %rbp
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x53bd
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x32b0
testq %rbp, %rbp
je 0x544a
testb %r15b, %r15b
je 0x5512
cmpl 0x8(%rsp), %r14d
jne 0x56b8
leaq 0xc8(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0xb8(%rsp), %rdi
callq 0x3c70
leaq 0xb8(%rsp), %rdi
movl 0x4c(%rsp), %esi
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r8
xorl %r9d, %r9d
callq 0x5038
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x550b
movq 0xc8(%rsp), %rsi
jmp 0x5503
leaq 0x88(%rsp), %rax
movq %rax, 0x78(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rdi
callq 0x3c70
movq %r13, %rdi
movl 0x4(%rsp), %esi
movq 0x10(%rsp), %rdx
leaq 0x78(%rsp), %rcx
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r9
callq 0x6730
movq %rax, %rbp
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x54b2
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 0x3c70
movq %r13, %rdi
movl 0x4(%rsp), %esi
leaq 0x58(%rsp), %rdx
movq %rbp, %rcx
movl %r14d, %r8d
movq 0x50(%rsp), %r9
callq 0x642a
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x550b
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x32b0
xorl %ebp, %ebp
jmp 0x567e
leaq 0x1c0(%rsp), %rdi
movq %rbp, %rsi
callq 0x723e
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x98(%rsp), %rdi
callq 0x3c70
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 0x3c70
leaq 0x138(%rsp), %rdi
movq %r13, %rsi
movq 0x20(%rsp), %rdx
movl 0x4(%rsp), %ecx
leaq 0x1c0(%rsp), %r8
leaq 0x98(%rsp), %r9
pushq 0x18(%rsp)
pushq 0x10(%rsp)
pushq %r14
pushq 0x28(%rsp)
callq 0x5dc2
addq $0x20, %rsp
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x55d8
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x55fd
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x1c0(%rsp), %rdi
callq 0x3b82
movq 0x140(%rsp), %r14
testq %r14, %r14
setne %bpl
cmpq $0x4, %r14
jne 0x5648
movl $0x35000002, 0x1d8(%rsp) # imm = 0x35000002
movq 0x138(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x1dc(%rsp)
leaq 0x1d8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x138(%rsp), %rdi
leaq 0x148(%rsp), %rax
cmpq %rax, %rdi
je 0x566d
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x32b0
cmpq $0x1, %r14
movq 0x20(%rsp), %rax
sbbl $-0x1, %eax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x569a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32b0
testb %bpl, %bpl
movq %rbx, %r14
jne 0x5291
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5aad(%rip), %rdi      # 0xb16c
leaq 0x5a12(%rip), %rsi      # 0xb0d8
leaq 0x5aaf(%rip), %rcx      # 0xb17c
movl $0x257, %edx            # imm = 0x257
callq 0x31b0
movq %rax, %rbx
movq 0x160(%rsp), %rdi
cmpq %r15, %rdi
je 0x587b
movq 0x170(%rsp), %rsi
jmp 0x5873
movq %rax, %rbx
movq 0x180(%rsp), %rdi
cmpq %r14, %rdi
je 0x587b
movq 0x190(%rsp), %rsi
jmp 0x5873
movq %rax, %rbx
movq 0x1a0(%rsp), %rdi
cmpq %r14, %rdi
je 0x587b
movq 0x1b0(%rsp), %rsi
jmp 0x5873
movq %rax, %rbx
jmp 0x57c2
jmp 0x585c
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x585f
movq 0xc8(%rsp), %rsi
jmp 0x5852
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x579d
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x579d
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x57c2
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x1c0(%rsp), %rdi
callq 0x3b82
jmp 0x585f
movq %rax, %rbx
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x585f
movq 0x88(%rsp), %rsi
jmp 0x5852
movq %rax, %rbx
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x585f
movq 0x68(%rsp), %rsi
jmp 0x5852
jmp 0x585c
jmp 0x585c
movq %rax, %rbx
movq 0x118(%rsp), %rdi
leaq 0x128(%rsp), %rax
cmpq %rax, %rdi
je 0x585f
movq 0x128(%rsp), %rsi
jmp 0x5852
movq %rax, %rbx
movq 0xf8(%rsp), %rdi
leaq 0x108(%rsp), %rax
cmpq %rax, %rdi
je 0x585f
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x585f
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x587b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32b0
movq %rbx, %rdi
callq 0x3400
nop