// File: /workspace/github_cpp_makefile_star2_O3/davidgfnet[P]tadns/tadns_common.cc
// Function: parse_udp_packet(unsigned char const*, int)
struct dns_packet parse_udp_packet(const uint8_t *pkt, int len) {
    const uint8_t *e = pkt + len;
    struct header *h = (struct header*)pkt;

    struct dns_packet ret;
    ret.header.tid      = ntohs(h->tid);
    ret.header.flags    = ntohs(h->flags);
    ret.header.nqueries = ntohs(h->nqueries);
    ret.header.nanswers = ntohs(h->nanswers);
    ret.header.nauth    = ntohs(h->nauth);
    ret.header.nother   = ntohs(h->nother);

    const uint8_t *p = &h->data[0];

    for (unsigned i = 0; i < ret.header.nqueries; i++) {
        char name[1025];
        p += fetch(pkt, p, len, name, sizeof(name) - 1);

        if (p + 4 > e) return ret;

        struct dns_question q;
        q.qtype  = ntohs(((uint16_t*)p)[0]);
        q.qclass = ntohs(((uint16_t*)p)[1]);
        q.name = std::string(name);
        std::transform(q.name.begin(), q.name.end(), q.name.begin(), ::tolower);
        p += 4;

        ret.questions.push_back(q);
    }

    p = parse_answer(pkt, len, p, ret.header.nanswers, ret.answers, RESPONSE_ANSWER);
    p = parse_answer(pkt, len, p, ret.header.nauth,    ret.answers, RESPONSE_AUTHORITATIVE);
    p = parse_answer(pkt, len, p, ret.header.nother,   ret.answers, RESPONSE_ADDITIONAL);

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x478, %rsp            # imm = 0x478
movl %edx, %ebp
movq %rsi, %r13
movq %rdi, %r15
leaq 0x10(%rdi), %rax
movq %rax, 0x38(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movq (%rsi), %rax
movq %rax, %xmm0
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
psllw $0x8, %xmm0
por %xmm1, %xmm0
movq %xmm0, (%rdi)
movzwl 0x8(%rsi), %ecx
rolw $0x8, %cx
movw %cx, 0x8(%rdi)
movzwl 0xa(%rsi), %ecx
rolw $0x8, %cx
movw %cx, 0xa(%rdi)
leaq 0xc(%rsi), %r14
shrq $0x20, %rax
testw %ax, %ax
movq %rdi, 0x30(%rsp)
je 0x9b28
movslq %ebp, %rax
addq %r13, %rax
movq %rax, 0x40(%rsp)
xorl %r12d, %r12d
leaq 0x70(%rsp), %rcx
movl %ebp, 0x4(%rsp)
movq %r15, %rbx
movq %r13, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl $0x400, %r8d            # imm = 0x400
movq %rcx, %rbp
callq 0x94dc
movl %eax, %eax
leaq (%r14,%rax), %rcx
addq $0x4, %rcx
cmpq 0x40(%rsp), %rcx
ja 0x9b7f
movq %rcx, 0x48(%rsp)
addq %rax, %r14
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movzwl (%r14), %eax
rolw $0x8, %ax
movw %ax, 0x8(%rsp)
movzwl 0x2(%r14), %eax
rolw $0x8, %ax
movw %ax, 0xa(%rsp)
leaq 0x60(%rsp), %r14
movq %r14, 0x50(%rsp)
movq %rbp, %rdi
callq 0x3140
leaq (%rsp,%rax), %rdx
addq $0x70, %rdx
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x8562
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x3340
movq 0x50(%rsp), %rdi
cmpq %r14, %rdi
je 0x9aa2
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x18(%rsp), %r14
testq %r14, %r14
je 0x9acc
movq 0x10(%rsp), %rbp
xorl %r15d, %r15d
movsbl (%rbp,%r15), %edi
callq 0x3390
movb %al, (%rbp,%r15)
incq %r15
cmpq %r15, %r14
jne 0x9ab4
movq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3b3e
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
movq 0x30(%rsp), %r15
movl 0x4(%rsp), %ebp
je 0x9b00
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
incl %r12d
movzwl 0x4(%r15), %eax
movq 0x48(%rsp), %rdx
movq %rdx, %r14
cmpl %eax, %r12d
leaq 0x70(%rsp), %rcx
jb 0x99eb
movzwl 0x6(%r15), %eax
movq %rdx, %r14
jmp 0x9b2d
pextrw $0x3, %xmm0, %eax
movzwl %ax, %ecx
leaq 0x28(%r15), %r12
movq %r13, %rdi
movl %ebp, %esi
movq %r14, %rdx
movq %r12, %r8
xorl %r9d, %r9d
callq 0x95e1
movzwl 0x8(%r15), %ecx
movq %r13, %rdi
movl %ebp, %esi
movq %rax, %rdx
movq %r12, %r8
movl $0x1, %r9d
callq 0x95e1
movzwl 0xa(%r15), %ecx
movq %r13, %rdi
movl %ebp, %esi
movq %rax, %rdx
movq %r12, %r8
movl $0x2, %r9d
callq 0x95e1
jmp 0x9b82
movq %rbx, %r15
movq %r15, %rax
addq $0x478, %rsp            # imm = 0x478
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
jmp 0x9bbd
jmp 0x9b9e
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x9bbd
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x30(%rsp), %rdi
addq $0x28, %rdi
callq 0x3b80
movq 0x38(%rsp), %rdi
callq 0x3bb0
movq %r14, %rdi
callq 0x3400