// File: 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
movq %rsp, %rbp
subq $0x510, %rsp            # imm = 0x510
movq %rdi, -0x4c8(%rbp)
movq %rdi, %rax
movq %rax, -0x4d0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movb $0x0, -0x29(%rbp)
callq 0x4150
movq -0x28(%rbp), %rax
movzwl (%rax), %edi
callq 0x3570
movq -0x4c8(%rbp), %rdi
movw %ax, (%rdi)
movq -0x28(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x3570
movq -0x4c8(%rbp), %rdi
movw %ax, 0x2(%rdi)
movq -0x28(%rbp), %rax
movzwl 0x4(%rax), %edi
callq 0x3570
movq -0x4c8(%rbp), %rdi
movw %ax, 0x4(%rdi)
movq -0x28(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x3570
movq -0x4c8(%rbp), %rdi
movw %ax, 0x6(%rdi)
movq -0x28(%rbp), %rax
movzwl 0x8(%rax), %edi
callq 0x3570
movq -0x4c8(%rbp), %rdi
movw %ax, 0x8(%rdi)
movq -0x28(%rbp), %rax
movzwl 0xa(%rax), %edi
callq 0x3570
movq -0x4c8(%rbp), %rdi
movw %ax, 0xa(%rdi)
movq -0x28(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x4c8(%rbp), %rcx
movl -0x3c(%rbp), %eax
movzwl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jae 0x1795c
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x14(%rbp), %edx
leaq -0x440(%rbp), %rcx
movl $0x400, %r8d            # imm = 0x400
callq 0x16fc0
movl %eax, -0x4d4(%rbp)
jmp 0x17783
movl -0x4d4(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x4, %rax
cmpq -0x20(%rbp), %rax
jbe 0x177ce
movb $0x1, -0x29(%rbp)
movl $0x1, -0x450(%rbp)
jmp 0x17a15
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x448(%rbp)
movl %eax, -0x44c(%rbp)
jmp 0x17a37
leaq -0x478(%rbp), %rdi
callq 0xb1d0
movq -0x38(%rbp), %rax
movzwl (%rax), %edi
callq 0x3570
movw %ax, -0x478(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x3570
movw %ax, -0x476(%rbp)
leaq -0x499(%rbp), %rdi
movq %rdi, -0x4e0(%rbp)
callq 0x3540
movq -0x4e0(%rbp), %rdx
leaq -0x498(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0xaab0
jmp 0x17830
leaq -0x470(%rbp), %rdi
movq %rdi, -0x4f0(%rbp)
leaq -0x498(%rbp), %rsi
movq %rsi, -0x4f8(%rbp)
callq 0x3430
movq -0x4f8(%rbp), %rdi
callq 0x3200
leaq -0x499(%rbp), %rdi
callq 0x33b0
movq -0x4f0(%rbp), %rdi
callq 0x32f0
movq -0x4f0(%rbp), %rdi
movq %rax, -0x4a8(%rbp)
callq 0x31d0
movq -0x4f0(%rbp), %rdi
movq %rax, -0x4b0(%rbp)
callq 0x32f0
movq %rax, -0x4b8(%rbp)
movq -0x4a8(%rbp), %rdi
movq -0x4b0(%rbp), %rsi
movq -0x4b8(%rbp), %rdx
movq 0xe71a(%rip), %rcx      # 0x25fd8
callq 0xadb0
movq %rax, -0x4e8(%rbp)
jmp 0x178cc
movq -0x4c8(%rbp), %rdi
movq -0x4e8(%rbp), %rax
movq %rax, -0x4c0(%rbp)
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
addq $0x10, %rdi
leaq -0x478(%rbp), %rsi
callq 0x40b0
jmp 0x178ff
leaq -0x478(%rbp), %rdi
callq 0x4750
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x17748
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x448(%rbp)
movl %eax, -0x44c(%rbp)
leaq -0x499(%rbp), %rdi
callq 0x33b0
jmp 0x1794b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x448(%rbp)
movl %eax, -0x44c(%rbp)
leaq -0x478(%rbp), %rdi
callq 0x4750
jmp 0x17a37
movq -0x4c8(%rbp), %r8
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x38(%rbp), %rdx
movzwl 0x6(%r8), %ecx
addq $0x28, %r8
xorl %r9d, %r9d
callq 0x17170
movq %rax, -0x500(%rbp)
jmp 0x17988
movq -0x4c8(%rbp), %r8
movq -0x500(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x38(%rbp), %rdx
movzwl 0x8(%r8), %ecx
addq $0x28, %r8
movl $0x1, %r9d
callq 0x17170
movq %rax, -0x508(%rbp)
jmp 0x179c2
movq -0x4c8(%rbp), %r8
movq -0x508(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x38(%rbp), %rdx
movzwl 0xa(%r8), %ecx
addq $0x28, %r8
movl $0x2, %r9d
callq 0x17170
movq %rax, -0x510(%rbp)
jmp 0x179fc
movq -0x510(%rbp), %rax
movq %rax, -0x38(%rbp)
movb $0x1, -0x29(%rbp)
movl $0x1, -0x450(%rbp)
testb $0x1, -0x29(%rbp)
jne 0x17a27
movq -0x4c8(%rbp), %rdi
callq 0x4120
movq -0x4d0(%rbp), %rax
addq $0x510, %rsp            # imm = 0x510
popq %rbp
retq
movq -0x4c8(%rbp), %rdi
callq 0x4120
movq -0x448(%rbp), %rdi
callq 0x3530
nop