// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
movl %r9d, 0x10(%rsp)
movq %r8, 0x88(%rsp)
movl %ecx, 0x14(%rsp)
movq %rdx, %r13
movl %esi, 0x8(%rsp)
movslq %esi, %rax
movq %rdi, 0x28(%rsp)
addq %rdi, %rax
movq %rax, 0x20(%rsp)
leaq 0x90(%rsp), %rbp
xorl %ecx, %ecx
cmpl 0x14(%rsp), %ecx
jae 0x8cc2
leaq 0xc(%r13), %rax
cmpq 0x20(%rsp), %rax
jae 0x8cc2
movl %ecx, 0x1c(%rsp)
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movl 0x8(%rsp), %edx
leaq 0xb0(%rsp), %r14
movq %r14, %rcx
movl $0x400, %r8d            # imm = 0x400
callq 0x896c
movl %eax, %r15d
movzwl (%r13,%r15), %ebx
movw %bx, 0x6(%rsp)
rolw $0x8, %bx
movl 0x4(%r13,%r15), %eax
bswapl %eax
movzwl 0x8(%r13,%r15), %r12d
leaq 0x50(%rsp), %rcx
movq %rcx, 0x40(%rsp)
andq $0x0, 0x48(%rsp)
xorl %edx, %edx
movb %dl, 0x50(%rsp)
leaq 0x70(%rsp), %rcx
movq %rcx, 0x60(%rsp)
andq $0x0, 0x68(%rsp)
movb %dl, 0x70(%rsp)
movzwl %bx, %ecx
movl %ecx, 0x18(%rsp)
movl %ecx, 0x38(%rsp)
movl %eax, 0x80(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x5(%rsp), %rdx
callq 0x61b8
movq %rbp, %r14
leaq (%r15,%r13), %rbp
rolw $0x8, %r12w
movl %r12d, 0xc(%rsp)
addq %r15, %r13
addq $0xa, %r13
movq %r13, 0x30(%rsp)
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x3320
movq %r14, %rdi
callq 0x3140
movl 0x10(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r14
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x8b84
movsbl (%r15,%r13), %edi
callq 0x3390
movb %al, (%r15,%r13)
incq %r13
jmp 0x8b6c
cmpw $0xf, %bx
ja 0x8c34
movl $0x8024, %eax           # imm = 0x8024
movl 0x18(%rsp), %ecx
btl %ecx, %eax
jae 0x8c34
addq $0xc, %rbp
cmpw $0xf00, 0x6(%rsp)       # imm = 0xF00
movq 0x30(%rsp), %r13
cmovneq %r13, %rbp
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
movl 0x8(%rsp), %edx
leaq 0xb0(%rsp), %rbx
movq %rbx, %rcx
movl $0x400, %r8d            # imm = 0x400
callq 0x896c
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x5(%rsp), %rdx
callq 0x61b8
leaq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x3320
movq %rbp, %rdi
callq 0x3140
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %r14
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x8c28
movsbl (%rbx,%r15), %edi
callq 0x3390
movb %al, (%rbx,%r15)
incq %r15
jmp 0x8c10
leaq 0x38(%rsp), %rbx
movl 0xc(%rsp), %r14d
jmp 0x8c98
movzwl 0xc(%rsp), %eax
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %r13
subq %r13, %rcx
movl %ecx, %r14d
cmovbel %eax, %r14d
cmpq %rax, %rcx
cmovgel %eax, %r14d
movzwl %r14w, %edx
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
addq %r13, %rdx
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7a40
leaq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x3320
movq %rbp, %rdi
callq 0x3140
leaq 0x38(%rsp), %rbx
movq 0x88(%rsp), %rdi
movq %rbx, %rsi
callq 0x91dc
movzwl %r14w, %eax
addq %rax, %r13
movq %rbx, %rdi
callq 0x3afa
movl 0x1c(%rsp), %ecx
incl %ecx
jmp 0x8a7b
movq %r13, %rax
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8cdd
jmp 0x8cdd
jmp 0x8cdd
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3afa
movq %rbx, %rdi
callq 0x33f0