// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
movq %r8, 0xa8(%rsp)
movq %rdx, %r14
movl %esi, 0x10(%rsp)
movslq %esi, %rdx
movq %rdi, 0x50(%rsp)
addq %rdi, %rdx
movl %ecx, 0x14(%rsp)
testl %ecx, %ecx
setne %al
leaq 0xc(%r14), %rcx
movq %rdx, 0x48(%rsp)
cmpq %rdx, %rcx
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x9924
leaq 0x70(%rsp), %r15
leaq 0x90(%rsp), %r12
xorl %ecx, %ecx
movl %ecx, 0x20(%rsp)
movq 0x50(%rsp), %rdi
movq %r14, %rsi
movl 0x10(%rsp), %edx
leaq 0xb0(%rsp), %rbp
movq %rbp, %rcx
movl $0x400, %r8d            # imm = 0x400
callq 0x94dc
movl %eax, %ebx
movzwl (%r14,%rbx), %r13d
movw %r13w, 0xe(%rsp)
rolw $0x8, %r13w
movl 0x4(%r14,%rbx), %eax
bswapl %eax
movzwl 0x8(%r14,%rbx), %ecx
movl %ecx, 0x24(%rsp)
movq %r15, 0x60(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x68(%rsp)
movb %cl, 0x70(%rsp)
movq %r12, 0x80(%rsp)
movq %rcx, 0x88(%rsp)
movb %cl, 0x90(%rsp)
movzwl %r13w, %ecx
movl %ecx, 0x1c(%rsp)
movl %ecx, 0x58(%rsp)
movl %eax, 0xa0(%rsp)
leaq 0x38(%rsp), %r15
movq %r15, 0x28(%rsp)
movq %rbp, %rdi
callq 0x3140
leaq (%rsp,%rax), %rdx
addq $0xb0, %rdx
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x8562
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x3340
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x9717
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32b0
movl 0x18(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0x68(%rsp), %rbp
testq %rbp, %rbp
je 0x9747
movq 0x60(%rsp), %r15
xorl %r12d, %r12d
movsbl (%r15,%r12), %edi
callq 0x3390
movb %al, (%r15,%r12)
incq %r12
cmpq %r12, %rbp
jne 0x9731
movl 0x24(%rsp), %ecx
rolw $0x8, %cx
leaq (%r14,%rbx), %rbp
addq $0xa, %rbp
cmpw $0xf, %r13w
ja 0x98ae
movl $0x8024, %eax           # imm = 0x8024
movl 0x1c(%rsp), %edx
btl %edx, %eax
jae 0x98ae
movl %ecx, %r13d
addq %rbx, %r14
movzwl 0xe(%rsp), %eax
addq $0xc, %r14
cmpl $0xf00, %eax            # imm = 0xF00
cmovneq %rbp, %r14
movq 0x50(%rsp), %rdi
movq %r14, %rsi
movl 0x10(%rsp), %edx
leaq 0xb0(%rsp), %r14
movq %r14, %rcx
movl $0x400, %r8d            # imm = 0x400
callq 0x94dc
leaq 0x38(%rsp), %rbx
movq %rbx, 0x28(%rsp)
movq %r14, %rdi
callq 0x3140
leaq (%rsp,%rax), %rdx
addq $0xb0, %rdx
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x8562
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
callq 0x3340
movq 0x28(%rsp), %rdi
cmpq %rbx, %rdi
leaq 0x90(%rsp), %r12
je 0x980a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x88(%rsp), %rbx
testq %rbx, %rbx
je 0x9838
movq 0x80(%rsp), %r14
xorl %r15d, %r15d
movsbl (%r14,%r15), %edi
callq 0x3390
movb %al, (%r14,%r15)
incq %r15
cmpq %r15, %rbx
jne 0x9822
leaq 0x70(%rsp), %r15
movq 0xa8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x9f40
movq 0x80(%rsp), %rdi
cmpq %r12, %rdi
je 0x986c
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32b0
movq 0x60(%rsp), %rdi
cmpq %r15, %rdi
je 0x9883
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x32b0
movzwl %r13w, %eax
addq %rax, %rbp
movl 0x20(%rsp), %ecx
incl %ecx
cmpl 0x14(%rsp), %ecx
jae 0x9927
leaq 0xc(%rbp), %rax
movq %rbp, %r14
cmpq 0x48(%rsp), %rax
jb 0x9643
jmp 0x9927
movzwl %cx, %eax
movq 0x48(%rsp), %rcx
subq %rbp, %rcx
movl %ecx, %edx
cmovbel %eax, %edx
cmpq %rax, %rcx
cmovgel %eax, %edx
movl %edx, %r13d
movzwl %dx, %edx
leaq 0x38(%rsp), %rbx
movq %rbx, 0x28(%rsp)
addq %rbp, %rdx
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x8562
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x3340
movq 0x28(%rsp), %rdi
cmpq %rbx, %rdi
leaq 0x70(%rsp), %r15
leaq 0x90(%rsp), %r12
je 0x983d
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x983d
movq %r14, %rbp
movq %rbp, %rax
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9942
jmp 0x9942
jmp 0x9942
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x9f6a
movq %rbx, %rdi
callq 0x3400