// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdi, %r14
movq %rsi, 0x8(%rsp)
leaq 0x70(%rsp), %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movups %xmm0, (%rbx)
callq 0x6a50
movw %ax, 0x60(%rsp)
movq $0x10100, 0x62(%rsp)    # imm = 0x10100
movw $0x0, 0x6a(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
movq 0x8(%rsp), %rsi
movzwl 0xc(%rsi), %eax
movw %ax, -0x18(%r13)
movw $0x1, -0x16(%r13)
addq $0x10, %rsi
callq 0x3170
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x3b40
leaq 0x60(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x9eb1
movl %eax, %ebp
cmpl $0x800, %eax            # imm = 0x800
jae 0x6cdd
testq %r15, %r15
je 0x6ba1
incl 0x18(%r14)
jmp 0x6ba5
incl 0x14(%r14)
testq %r15, %r15
jne 0x6bb5
movq %r14, %rdi
callq 0x4660
movq %rax, %r15
movl %ebp, %r12d
xorl %eax, %eax
cmpw $0x2, (%r15)
setne %al
movl 0x8(%r14,%rax,4), %edi
movl $0x10, %eax
movl $0x1c, %r9d
cmovel %eax, %r9d
leaq 0xa0(%rsp), %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x3070
cmpq %r12, %rax
jne 0x6c6e
movq 0x8(%rsp), %r12
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x10(%r12), %rsi
movq 0x18(%r12), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0x3c70
movzwl 0xc(%r12), %eax
movw %ax, 0x30(%rsp)
leaq 0x38(%r14), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x90be
movq 0x8(%rsp), %r12
leaq 0x70(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0x7302
movq %r12, (%rax)
movq 0x50(%r14), %rax
cmpq 0x98(%r14), %rax
jne 0x6cfc
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x6c9f
movq 0x20(%rsp), %rsi
incq %rsi
jmp 0x6c9a
movq 0x8(%rsp), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0x4f78
movq 0x8(%rsp), %r14
testq %r14, %r14
je 0x6c9f
movq %r14, %rdi
callq 0x6ff2
movl $0x78, %esi
movq %r14, %rdi
callq 0x32b0
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0x6cb6
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x88(%rsp), %rdi
callq 0x3b82
movq %rbx, %rdi
callq 0x3bb2
addq $0x8a8, %rsp            # imm = 0x8A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4574(%rip), %rdi      # 0xb258
leaq 0x43ed(%rip), %rsi      # 0xb0d8
leaq 0x457a(%rip), %rcx      # 0xb26c
movl $0x2c9, %edx            # imm = 0x2C9
callq 0x31b0
leaq 0x43b5(%rip), %rdi      # 0xb0b8
leaq 0x43ce(%rip), %rsi      # 0xb0d8
leaq 0x455b(%rip), %rcx      # 0xb26c
movl $0x2dd, %edx            # imm = 0x2DD
callq 0x31b0
jmp 0x6d44
jmp 0x6d44
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x6d47
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0x6d47
jmp 0x6d44
movq %rax, %r14
jmp 0x6d5e
jmp 0x6d44
movq %rax, %r14
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0x6d5e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x88(%rsp), %rdi
callq 0x3b82
movq %rbx, %rdi
callq 0x3bb2
movq %r14, %rdi
callq 0x3400
nop