// File: davidgfnet[P]tadns/tadns_common.cc
// Function: parse_answer(unsigned char const*, int, unsigned char const*, unsigned int, std::vector<dns_record, std::allocator<dns_record>>&, dns_response_type)
const uint8_t* parse_answer(const uint8_t *pkt, int len, const uint8_t *p, unsigned count,
                            std::vector<struct dns_record> &r, enum dns_response_type respt) {

    const uint8_t *e = pkt + len;
    char name[1025];

    /* Loop through the answers, we want A type answer */
    for (unsigned a = 0; a < count && &p[12] < e; a++) {
        /* Get answer NAME, make sure it matches! */
        p += fetch(pkt, p, len, name, sizeof(name) - 1);

        uint16_t type = ntohs(((uint16_t *)p)[0]);
        uint32_t ttl = ntohl(((uint32_t *)p)[1]);
        uint16_t dlen = ntohs(((uint16_t *)p)[4]);

        p += 10;

        struct dns_record rec;
        rec.rtype = (enum dns_record_type)type,
        rec.ttl = ttl;
        rec.name = std::string(name);
        rec.resptype = respt;

        std::transform(rec.name.begin(), rec.name.end(), rec.name.begin(), ::tolower);

        if (type == DNS_MX_RECORD ||
            type == DNS_NS_RECORD ||
            type == DNS_CNAME_RECORD) {

            const uint8_t *poff = type == DNS_MX_RECORD ? &p[2] : p;

            fetch(pkt, poff, len, name, sizeof(name) - 1);
            rec.addr = std::string(name);

            std::transform(rec.addr.begin(), rec.addr.end(), rec.addr.begin(), ::tolower);
        } else {
            if (dlen > e-p && e > p)
                dlen = e-p;
            rec.addr = std::string((char*)p, dlen);
        }
        r.push_back(rec);

        p += dlen;
    }

    return p;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5e0, %rsp            # imm = 0x5E0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x444(%rbp)
movl -0x444(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1c(%rbp), %ecx
movb %al, -0x56a(%rbp)
jae 0x171d2
movq -0x18(%rbp), %rax
addq $0xc, %rax
cmpq -0x38(%rbp), %rax
setb %al
movb %al, -0x56a(%rbp)
movb -0x56a(%rbp), %al
testb $0x1, %al
jne 0x171e1
jmp 0x17643
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0xc(%rbp), %edx
leaq -0x440(%rbp), %rcx
movq %rcx, -0x580(%rbp)
movl $0x400, %r8d            # imm = 0x400
callq 0x16fc0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movzwl (%rax), %edi
callq 0x3570
movw %ax, -0x446(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x3450
movl %eax, -0x44c(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x8(%rax), %edi
callq 0x3570
movw %ax, -0x44e(%rbp)
movq -0x18(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x18(%rbp)
leaq -0x4a0(%rbp), %rdi
callq 0x18150
movzwl -0x446(%rbp), %eax
movl %eax, -0x4a0(%rbp)
movl -0x44c(%rbp), %eax
movl %eax, -0x458(%rbp)
leaq -0x4c1(%rbp), %rdi
movq %rdi, -0x578(%rbp)
callq 0x3540
movq -0x580(%rbp), %rsi
movq -0x578(%rbp), %rdx
leaq -0x4c0(%rbp), %rdi
callq 0xaab0
jmp 0x172ad
leaq -0x498(%rbp), %rdi
movq %rdi, -0x590(%rbp)
leaq -0x4c0(%rbp), %rsi
movq %rsi, -0x598(%rbp)
callq 0x3430
movq -0x598(%rbp), %rdi
callq 0x3200
leaq -0x4c1(%rbp), %rdi
callq 0x33b0
movq -0x590(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, -0x49c(%rbp)
callq 0x32f0
movq -0x590(%rbp), %rdi
movq %rax, -0x4e0(%rbp)
callq 0x31d0
movq -0x590(%rbp), %rdi
movq %rax, -0x4e8(%rbp)
callq 0x32f0
movq %rax, -0x4f0(%rbp)
movq -0x4e0(%rbp), %rdi
movq -0x4e8(%rbp), %rsi
movq -0x4f0(%rbp), %rdx
movq 0xec94(%rip), %rcx      # 0x25fd8
callq 0xadb0
movq %rax, -0x588(%rbp)
jmp 0x17352
movq -0x588(%rbp), %rax
movq %rax, -0x4f8(%rbp)
movzwl -0x446(%rbp), %eax
cmpl $0xf, %eax
je 0x17388
movzwl -0x446(%rbp), %eax
cmpl $0x2, %eax
je 0x17388
movzwl -0x446(%rbp), %eax
cmpl $0x5, %eax
jne 0x1751b
movzwl -0x446(%rbp), %eax
cmpl $0xf, %eax
jne 0x173a5
movq -0x18(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x5a0(%rbp)
jmp 0x173b0
movq -0x18(%rbp), %rax
movq %rax, -0x5a0(%rbp)
movq -0x5a0(%rbp), %rax
movq %rax, -0x500(%rbp)
movq -0x8(%rbp), %rdi
movq -0x500(%rbp), %rsi
movl -0xc(%rbp), %edx
leaq -0x440(%rbp), %rcx
movl $0x400, %r8d            # imm = 0x400
callq 0x16fc0
jmp 0x173e0
leaq -0x521(%rbp), %rdi
movq %rdi, -0x5a8(%rbp)
callq 0x3540
movq -0x5a8(%rbp), %rdx
leaq -0x520(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0xaab0
jmp 0x1740f
leaq -0x478(%rbp), %rdi
movq %rdi, -0x5b8(%rbp)
leaq -0x520(%rbp), %rsi
movq %rsi, -0x5c0(%rbp)
callq 0x3430
movq -0x5c0(%rbp), %rdi
callq 0x3200
leaq -0x521(%rbp), %rdi
callq 0x33b0
movq -0x5b8(%rbp), %rdi
callq 0x32f0
movq -0x5b8(%rbp), %rdi
movq %rax, -0x530(%rbp)
callq 0x31d0
movq -0x5b8(%rbp), %rdi
movq %rax, -0x538(%rbp)
callq 0x32f0
movq %rax, -0x540(%rbp)
movq -0x530(%rbp), %rdi
movq -0x538(%rbp), %rsi
movq -0x540(%rbp), %rdx
movq 0xeb3b(%rip), %rcx      # 0x25fd8
callq 0xadb0
movq %rax, -0x5b0(%rbp)
jmp 0x174ab
movq -0x5b0(%rbp), %rax
movq %rax, -0x548(%rbp)
jmp 0x175ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4d0(%rbp)
movl %eax, -0x4d4(%rbp)
leaq -0x4c1(%rbp), %rdi
callq 0x33b0
jmp 0x17635
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4d0(%rbp)
movl %eax, -0x4d4(%rbp)
jmp 0x17635
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4d0(%rbp)
movl %eax, -0x4d4(%rbp)
leaq -0x521(%rbp), %rdi
callq 0x33b0
jmp 0x17635
movzwl -0x44e(%rbp), %eax
movq -0x38(%rbp), %rcx
movq -0x18(%rbp), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jle 0x1754e
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x1754e
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
subq %rcx, %rax
movw %ax, -0x44e(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x5d8(%rbp)
movzwl -0x44e(%rbp), %eax
movq %rax, -0x5d0(%rbp)
leaq -0x569(%rbp), %rdi
movq %rdi, -0x5c8(%rbp)
callq 0x3540
movq -0x5d8(%rbp), %rsi
movq -0x5d0(%rbp), %rdx
movq -0x5c8(%rbp), %rcx
leaq -0x568(%rbp), %rdi
callq 0x33e0
jmp 0x1759d
leaq -0x4a0(%rbp), %rdi
addq $0x28, %rdi
leaq -0x568(%rbp), %rsi
callq 0x3430
leaq -0x568(%rbp), %rdi
callq 0x3200
leaq -0x569(%rbp), %rdi
callq 0x33b0
jmp 0x175ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4d0(%rbp)
movl %eax, -0x4d4(%rbp)
leaq -0x569(%rbp), %rdi
callq 0x33b0
jmp 0x17635
movq -0x28(%rbp), %rdi
leaq -0x4a0(%rbp), %rsi
callq 0x18180
jmp 0x17600
movzwl -0x44e(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
leaq -0x4a0(%rbp), %rdi
callq 0x4510
movl -0x444(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x444(%rbp)
jmp 0x171aa
leaq -0x4a0(%rbp), %rdi
callq 0x4510
jmp 0x17650
movq -0x18(%rbp), %rax
addq $0x5e0, %rsp            # imm = 0x5E0
popq %rbp
retq
movq -0x4d0(%rbp), %rdi
callq 0x3530
nopl (%rax)