// File: /workspace/github_cpp_makefile_star2_O2/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)
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rsp)
movups %xmm0, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
callq 0x5af6
movw %ax, 0x10(%rsp)
movq $0x10100, 0x12(%rsp)    # imm = 0x10100
andw $0x0, 0x1a(%rsp)
leaq 0x80(%rsp), %rbx
leaq 0x90(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movzwl 0xc(%r15), %ecx
movw %cx, -0x18(%rax)
movw $0x1, -0x16(%rax)
leaq 0x10(%r15), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3180
leaq 0x20(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x3a1a
leaq 0x10(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x908c
movl %eax, %ebp
cmpl $0x800, %eax            # imm = 0x800
jae 0x5d02
testq %r12, %r12
je 0x5c21
incl 0x18(%r14)
jmp 0x5c30
incl 0x14(%r14)
movq %r14, %rdi
callq 0x42b2
movq %rax, %r12
movl %ebp, %ebp
xorl %eax, %eax
cmpw $0x2, (%r12)
setne %al
movl 0x8(%r14,%rax,4), %edi
pushq $0x10
popq %rax
pushq $0x1c
popq %r9
cmovel %eax, %r9d
leaq 0xa0(%rsp), %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x3060
cmpq %rbp, %rax
jne 0x5cbd
addq $0xc, %r15
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x67e0
leaq 0x38(%r14), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x8420
movq 0x8(%rsp), %r15
leaq 0x70(%r14), %rdi
leaq 0x50(%rsp), %rsi
callq 0x6324
movq %r15, (%rax)
movq 0x50(%r14), %rax
cmpq 0x98(%r14), %rax
jne 0x5d21
leaq 0x50(%rsp), %rdi
callq 0x3140
jmp 0x5cde
pushq $0x4
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x48e0
movq %r15, %rdi
callq 0x5fb4
pushq $0x78
popq %rsi
movq %r15, %rdi
callq 0x32b0
movq %rbx, %rdi
callq 0x3140
leaq 0x10(%rsp), %rdi
callq 0x3a46
addq $0x8a8, %rsp            # imm = 0x8A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x451f(%rip), %rdi      # 0xa228
leaq 0x4398(%rip), %rsi      # 0xa0a8
leaq 0x4525(%rip), %rcx      # 0xa23c
movl $0x2c9, %edx            # imm = 0x2C9
callq 0x31c0
leaq 0x4360(%rip), %rdi      # 0xa088
leaq 0x4379(%rip), %rsi      # 0xa0a8
leaq 0x4506(%rip), %rcx      # 0xa23c
movl $0x2dd, %edx            # imm = 0x2DD
callq 0x31c0
jmp 0x5d5c
jmp 0x5d5c
movq %rax, %r14
leaq 0x50(%rsp), %rdi
callq 0x3140
jmp 0x5d5f
jmp 0x5d5c
movq %rax, %r14
jmp 0x5d67
jmp 0x5d5c
movq %rax, %r14
movq %rbx, %rdi
callq 0x3140
leaq 0x10(%rsp), %rdi
callq 0x3a46
movq %r14, %rdi
callq 0x33f0
nop