// File: /workspace/github_cpp_makefile_star2_O2/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 $0x278, %rsp            # imm = 0x278
movq %r9, 0x20(%rsp)
movq %r8, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
movl %esi, 0x4(%rsp)
movq %rdi, 0x28(%rsp)
movq (%rcx), %rbx
movq %rcx, 0x10(%rsp)
movq 0x8(%rcx), %r14
xorl %ebp, %ebp
cmpq %rbp, %r14
je 0x4a41
movsbl (%rbx,%rbp), %edi
callq 0x3390
movb %al, (%rbx,%rbp)
incq %rbp
jmp 0x4a2b
cmpl $0x10, 0x4(%rsp)
jb 0x4a84
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x3360
movzwl 0x8(%rsp), %esi
pushq $0x2
popq %r9
movq %rbx, %rdi
xorl %edx, %edx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
callq 0x4970
leaq 0x110(%rsp), %rdi
jmp 0x4afa
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x3360
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x42e6
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x3140
testq %rbx, %rbx
je 0x4b11
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x3360
movzwl 0x8(%rsp), %esi
movq %r14, %rdi
movq %rbx, %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
xorl %r9d, %r9d
callq 0x4970
leaq 0xd0(%rsp), %rdi
callq 0x3140
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, 0x30(%rsp)
leaq 0x1d8(%rsp), %r14
leaq 0x130(%rsp), %r15
leaq 0x1b8(%rsp), %rbx
movzwl 0x8(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
andq $0x0, 0x50(%rsp)
movb $0x0, 0x58(%rsp)
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x3360
movq %rbx, %r12
movq %r15, %rdi
movq %r14, %rsi
movq 0x30(%rsp), %rdx
callq 0x5030
leaq 0x3(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x48(%rsp), %rbx
movq %rbx, 0x70(%rsp)
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x6252
movq %r15, %r13
movq %r15, %rdi
callq 0x3140
movq %r14, %r15
movq %r14, %rdi
callq 0x3140
cmpb $0x0, 0x3(%rsp)
pushq $0x2
popq %rbp
cmovnel 0x8(%rsp), %ebp
movq %r12, %rdi
movq %rbx, %rsi
movq %r12, %rbx
callq 0x3360
movq 0x28(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x42e6
movq %rax, %r14
movq %rbx, %rdi
callq 0x3140
testq %r14, %r14
je 0x4c37
cmpb $0x1, 0x3(%rsp)
jne 0x4cbc
cmpl 0x8(%rsp), %ebp
jne 0x4db2
leaq 0xb0(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3360
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movl 0x3c(%rsp), %esi
movq %r14, %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
xorl %r9d, %r9d
callq 0x4970
jmp 0x4cad
leaq 0x158(%rsp), %r12
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x3360
movq 0x28(%rsp), %rdi
movl 0x4(%rsp), %esi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %r9
callq 0x58b6
movq %rax, %r14
movq %r12, %rdi
callq 0x3140
leaq 0x90(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x3360
movq 0x28(%rsp), %rdi
movl 0x4(%rsp), %esi
leaq 0x90(%rsp), %r12
movq %r12, %rdx
movq %r14, %rcx
movl %ebp, %r8d
movq 0x40(%rsp), %r9
callq 0x569a
movq %r12, %rdi
callq 0x3140
xorl %ebp, %ebp
jmp 0x4d94
leaq 0x78(%rsp), %rdi
movq %r14, %rsi
callq 0x6278
leaq 0x198(%rsp), %r12
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x3360
leaq 0x178(%rsp), %rbp
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x3360
leaq 0x78(%rsp), %r14
movq %r13, %rdi
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl 0x4(%rsp), %ecx
movq %r14, %r8
movq %r12, %r9
pushq 0x20(%rsp)
pushq 0x10(%rsp)
pushq %rbp
pushq 0x30(%rsp)
callq 0x5218
addq $0x20, %rsp
movq %rbp, %rdi
callq 0x3140
movq %r12, %rdi
callq 0x3140
movq %r14, %rdi
callq 0x3a60
movq 0x138(%rsp), %r14
testq %r14, %r14
setne %bpl
cmpq $0x4, %r14
jne 0x4d7b
movl $0x35000002, 0x1f8(%rsp) # imm = 0x35000002
movq 0x130(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x1fc(%rsp)
leaq 0x1f8(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %r13, %rdi
callq 0x3140
cmpq $0x1, %r14
movq 0x30(%rsp), %rax
sbbl $-0x1, %eax
movq %rax, 0x30(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x3140
testb %bpl, %bpl
movq %r15, %r14
movq %r13, %r15
jne 0x4b44
jmp 0x4aff
leaq 0x5383(%rip), %rdi      # 0xa13c
leaq 0x52e8(%rip), %rsi      # 0xa0a8
leaq 0x5385(%rip), %rcx      # 0xa14c
movl $0x257, %edx            # imm = 0x257
callq 0x31c0
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
jmp 0x4e88
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
jmp 0x4e88
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
jmp 0x4e88
movq %rax, %rbx
jmp 0x4e37
jmp 0x4e80
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
jmp 0x4e79
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
callq 0x3140
jmp 0x4e2a
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
callq 0x3140
leaq 0x78(%rsp), %rdi
callq 0x3a60
jmp 0x4e83
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
jmp 0x4e79
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
jmp 0x4e79
jmp 0x4e80
jmp 0x4e80
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
jmp 0x4e79
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0x3140
jmp 0x4e83
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3140
movq %rbx, %rdi
callq 0x33f0
nop