// File: /workspace/github_cpp_makefile_star2_O3/davidgfnet[P]tadns/tadns.cc
// Function: DNSResolver::send_dns_packet(Query*, sockaddr*)
void DNSResolver::send_dns_packet(Query *query, struct sockaddr * dnss) {
    struct dns_packet outp;

    /* Prepare DNS packet header */
    outp.header.tid      = getNewTid();
    outp.header.flags    = 0x100;        /* Haha. guess what it is */
    outp.header.nqueries = 1;            /* Just one query */
    outp.header.nanswers = 0;
    outp.header.nauth    = 0;
    outp.header.nother   = 0;

    struct dns_question q;
    q.qtype = query->qtype;
    q.qclass = 0x0001;
    q.name = query->name;
    outp.questions.push_back(q);

    uint8_t pkt[DNS_PACKET_LEN];
    unsigned psize = serialize_udp_packet(&outp, pkt);
    assert(psize < sizeof(pkt));

    /* FIXME Add some queueing mechanism for packets? */
    if (dnss)
        pkts_auth_servers++;
    else
        pkts_dns_servers++;

    /* Pick a server to send query to */
    struct sockaddr * serv = dnss ? dnss : getNextServer();
    int ssock = serv->sa_family == AF_INET ? sock4 : sock6;
    int addrl = serv->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);

    if (sendto(ssock, pkt, psize, 0, serv, addrl) != psize) {
        call_user_rec(query, DNS_NET_ERROR);
        delete query;
    } else {
        auto lookup = std::make_pair(query->name, query->qtype);
        active.emplace(outp.header.tid, query);
        ongoing[lookup] = query;
        assert(active.size() == ongoing.size());
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8a8, %rsp            # imm = 0x8A8
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r14
movq %rsi, 0x8(%rsp)
leaq 0x70(%rsp), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r13)
movups %xmm0, 0x10(%r13)
movups %xmm0, (%r13)
callq 0x6a1a
movw %ax, 0x60(%rsp)
movq $0x10100, 0x62(%rsp)    # imm = 0x10100
movw $0x0, 0x6a(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq $0x0, -0x8(%rbx)
movb $0x0, (%rbx)
movzwl 0xc(%r15), %eax
movw %ax, -0x18(%rbx)
movw $0x1, -0x16(%rbx)
leaq 0x10(%r15), %rsi
callq 0x3170
leaq 0x38(%rsp), %rsi
movq %r13, %rdi
callq 0x3b3e
leaq 0x60(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x9dbd
movl %eax, %ebp
cmpl $0x800, %eax            # imm = 0x800
jae 0x6cb8
testq %r12, %r12
je 0x6b80
incl 0x18(%r14)
jmp 0x6b8f
incl 0x14(%r14)
movq %r14, %rdi
callq 0x4644
movq %rax, %r12
movl %ebp, %r13d
xorl %eax, %eax
cmpw $0x2, (%r12)
setne %al
movl 0x8(%r14,%rax,4), %edi
movl $0x10, %eax
movl $0x1c, %r9d
cmovel %eax, %r9d
leaq 0xa0(%rsp), %rsi
movq %r13, %rdx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x3070
cmpq %r13, %rax
jne 0x6c50
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0x3c72
movzwl 0xc(%r15), %eax
movw %ax, 0x30(%rsp)
leaq 0x38(%r14), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x70(%rsp), %r13
callq 0x9032
movq 0x8(%rsp), %r15
leaq 0x70(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0x72e6
movq %r15, (%rax)
movq 0x50(%r14), %rax
cmpq 0x98(%r14), %rax
jne 0x6cd7
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x6c7a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x6c7a
movq %r14, %rdi
movq %r15, %rsi
movl $0x4, %edx
callq 0x4f5c
movq %r15, %rdi
callq 0x6fcc
movl $0x78, %esi
movq %r15, %rdi
callq 0x32b0
leaq 0x70(%rsp), %r13
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6c91
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x88(%rsp), %rdi
callq 0x3b80
movq %r13, %rdi
callq 0x3bb0
addq $0x8a8, %rsp            # imm = 0x8A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4523(%rip), %rdi      # 0xb1e2
leaq 0x4412(%rip), %rsi      # 0xb0d8
leaq 0x4529(%rip), %rcx      # 0xb1f6
movl $0x2c9, %edx            # imm = 0x2C9
callq 0x31b0
leaq 0x43da(%rip), %rdi      # 0xb0b8
leaq 0x43f3(%rip), %rsi      # 0xb0d8
leaq 0x450a(%rip), %rcx      # 0xb1f6
movl $0x2dd, %edx            # imm = 0x2DD
callq 0x31b0
jmp 0x6d1f
jmp 0x6d1f
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x6d22
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x6d22
jmp 0x6d1f
movq %rax, %r14
jmp 0x6d39
jmp 0x6d1f
movq %rax, %r14
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6d39
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x88(%rsp), %rdi
callq 0x3b80
leaq 0x70(%rsp), %rdi
callq 0x3bb0
movq %r14, %rdi
callq 0x3400