// File: /workspace/github_cpp_makefile_star2_O1/davidgfnet[P]tadns/tadns.cc
// Function: DNSResolver::parse_udp(unsigned char const*, int)
void DNSResolver::parse_udp(const unsigned char *pkt, int len) {
    Query *q;
    struct header *header = (struct header *) pkt;

    /* Return if we did not send that query */
    if ((q = find_active_query(ntohs(header->tid))) == NULL)
        return;

    /* Actually parse the packet */
    auto dnsp = parse_udp_packet(pkt, len);

    std::vector< std::tuple<unsigned, void*, std::string, enum dns_record_type, dns_callback_t> > deferred;
    if (ntohs(header->nqueries) != 1)
        /* We sent 1 query. We want to see more that 1 answer. */
        call_user_rec(q, DNS_ERROR);
    else if ((header->nanswers | header->nauth | header->nother) == 0)
        /* Received 0 answers */
        call_user_rec(q, DNS_DOES_NOT_EXIST);
    else if (dnsp.questions.size() == 0 || dnsp.questions[0].name != q->name)
        /* Check whether the query matches at all! */
        call_user_rec(q, DNS_ERROR);
    else {
        struct cache_entry entry;
        entry.hits = 0;
        entry.expire = std::numeric_limits<time_t>::max();
        entry.replies = dnsp.answers;

        for (const auto &e: dnsp.answers)
            entry.expire = std::min(time(NULL) + (time_t)e.ttl, entry.expire);

        // Detected some 0 TTL answers, that's an issue cause we rely on the cache as an intermediate
        // structure. That works as long as ttls are significantly bigger than DNS response latency
        entry.expire = std::max(time(NULL) + MIN_TTL_TIME, entry.expire);

        // Insert finding in the cache
        auto cacheentry = std::make_pair(q->name, q->qtype);
        cached.erase(cacheentry);
        auto res = cached.emplace(cacheentry, entry);

        /* If query has a dependant, do not notify the user but re-schedule it */
        if (q->deps.size())
            for (auto de: q->deps)
                deferred.push_back(std::make_tuple(q->niter, de->ctx, de->name, (enum dns_record_type)de->qtype, de->callback));
        else
            call_user(this, q->name, q->qtype, &res.first->second, q->callback, q->ctx, DNS_OK);
    }

    // Delete from active queries and cleanup
    active.erase(dnsp.header.tid);
    ongoing.erase(std::make_pair(q->name, q->qtype));
    assert(active.size() == ongoing.size());

    // Remove the query from the deps list
    delete q;

    // Retry deferred queries
    for (auto df: deferred)
        resolveInt(std::get<0>(df), std::get<1>(df), std::get<2>(df), std::get<3>(df), std::get<4>(df));

    purgeCache();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movzwl (%rsi), %eax
rolw $0x8, %ax
movzwl %ax, %esi
callq 0x710a
testq %rax, %rax
je 0x4c7d
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x99ea
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
movl $0x3, %ebp
cmpw $0x100, 0x4(%r15)       # imm = 0x100
movq %rbx, 0x48(%rsp)
jne 0x4ac5
movzwl 0x8(%r15), %eax
orw 0x6(%r15), %ax
orw 0xa(%r15), %ax
je 0x4ac0
movq 0x108(%rsp), %rax
cmpq %rax, 0x110(%rsp)
je 0x4ac5
movq 0x10(%rax), %rdx
cmpq 0x18(%r14), %rdx
jne 0x4ac5
testq %rdx, %rdx
je 0x4a45
movq 0x10(%r14), %rsi
movq 0x8(%rax), %rdi
callq 0x32c0
testl %eax, %eax
jne 0x4ac5
xorps %xmm0, %xmm0
leaq 0xd0(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0x0, 0x20(%rdi)
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
movq %rax, 0x18(%rdi)
leaq 0x120(%rsp), %rsi
callq 0x3a6a
movq 0x120(%rsp), %rbx
movq 0x128(%rsp), %r15
xorl %edi, %edi
cmpq %r15, %rbx
je 0x4c8f
callq 0x3260
movl 0x48(%rbx), %ecx
addq %rax, %rcx
movq 0xe8(%rsp), %rax
cmpq %rcx, %rax
cmovlq %rax, %rcx
movq %rcx, 0xe8(%rsp)
addq $0x50, %rbx
jmp 0x4a8d
movl $0x1, %ebp
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x4f78
leaq 0x38(%rbx), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x7e60
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x3c70
leaq 0x70(%rbx), %r15
movzwl 0xc(%r14), %eax
movq %rsp, %rsi
movw %ax, 0x20(%rsi)
movq %r15, %rdi
callq 0x7cf0
movq %r15, %rdi
movq %rax, %rsi
callq 0x7dbc
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x4b39
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x50(%rbx), %rax
cmpq 0x98(%rbx), %rax
jne 0x4e5d
movq %r14, %rdi
callq 0x6ff2
movl $0x78, %esi
movq %r14, %rdi
callq 0x32b0
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %r14
cmpq %r14, %rbx
je 0x4c2d
leaq 0x20(%rsp), %r12
leaq 0x68(%rsp), %r15
movups (%rbx), %xmm0
movaps %xmm0, (%rsp)
movq %r12, %r13
movq %r12, 0x10(%rsp)
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0x3c70
movq 0x30(%rbx), %r12
movq %r12, 0x30(%rsp)
movl 0x38(%rbx), %ebp
movl %ebp, 0x38(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x3c70
movl 0x8(%rsp), %r8d
movq (%rsp), %r9
movq 0x48(%rsp), %rdi
movl %ebp, %esi
movq %r12, %rdx
movq %r15, %rcx
callq 0x50ea
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
movq %r13, %r12
je 0x4c09
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x4c20
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
addq $0x40, %rbx
cmpq %r14, %rbx
jne 0x4b7c
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x3260
leaq 0x8(%rsp), %rax
movl $0x0, (%rax)
movq %rbx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rbx, 0x20(%rax)
movq %rsp, %rdi
callq 0x793a
leaq 0x50(%rsp), %rdi
callq 0x7148
leaq 0x120(%rsp), %rdi
callq 0x3b82
leaq 0x108(%rsp), %rdi
callq 0x3bb2
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3260
addq $0x3c, %rax
movq 0xe8(%rsp), %rcx
cmpq %rcx, %rax
cmovleq %rcx, %rax
movq %rax, 0xe8(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x3c70
movzwl 0xc(%r14), %eax
leaq 0x88(%rsp), %rsi
movw %ax, 0x20(%rsi)
movq 0x48(%rsp), %rax
leaq 0xa0(%rax), %r15
movq %r15, %rdi
callq 0x7f88
movq %r15, %rdi
movq %rax, %rsi
callq 0x8054
leaq 0x88(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
movq %r15, %rdi
callq 0x80e8
cmpq $0x0, 0x68(%r14)
je 0x4db8
movq 0x58(%r14), %r15
movq %r14, %rbx
addq $0x48, %rbx
cmpq %rbx, %r15
je 0x4e21
leaq 0x20(%rsp), %r12
movq %rsp, %rbp
movq 0x20(%r15), %r13
movzwl 0xc(%r13), %eax
movq 0x38(%r13), %rcx
movq %rcx, (%rsp)
movl %eax, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0x3c70
movq 0x30(%r13), %rax
movq %rax, 0x30(%rsp)
movl 0x8(%r14), %eax
movl %eax, 0x38(%rsp)
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x8352
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x4da6
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
movq %r15, %rdi
callq 0x3300
movq %rax, %r15
cmpq %rbx, %rax
jne 0x4d42
jmp 0x4e21
movq %rax, %r15
leaq 0xc0(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
leaq 0xb0(%rsp), %rdi
callq 0x3c70
movzwl 0xc(%r14), %esi
addq $0x48, %r15
movq 0x30(%r14), %r8
movq 0x38(%r14), %rcx
leaq 0xb0(%rsp), %rdi
movq %r15, %rdx
xorl %r9d, %r9d
callq 0x5038
movq 0xb0(%rsp), %rdi
cmpq %r12, %rdi
je 0x4e21
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x4e46
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0xd0(%rsp), %rdi
callq 0x3b82
movq 0x48(%rsp), %rbx
jmp 0x4ad2
leaq 0x6254(%rip), %rdi      # 0xb0b8
leaq 0x626d(%rip), %rsi      # 0xb0d8
leaq 0x629e(%rip), %rcx      # 0xb110
movl $0x186, %edx            # imm = 0x186
callq 0x31b0
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r12, %rdi
je 0x4ec0
movq 0xc0(%rsp), %rsi
jmp 0x4eb8
jmp 0x4ea1
jmp 0x4e9a
movq %rax, %rbx
jmp 0x4ee5
jmp 0x4ea1
movq %rax, %rbx
jmp 0x4ec0
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x4ec0
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x4ee5
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0xd0(%rsp), %rdi
callq 0x3b82
jmp 0x4f4b
jmp 0x4f0b
jmp 0x4f0b
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x4f4b
movq 0x10(%rsp), %rsi
jmp 0x4f43
movq %rax, %rbx
jmp 0x4f4b
movq %rax, %rbx
jmp 0x4f34
movq %rax, %rbx
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x4f34
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x4f4b
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x50(%rsp), %rdi
callq 0x7148
leaq 0x120(%rsp), %rdi
callq 0x3b82
leaq 0x108(%rsp), %rdi
callq 0x3bb2
movq %rbx, %rdi
callq 0x3400
nop