// File: /workspace/github_cpp_makefile_star2_O3/tenup-coin[P]seeder/dns.c
// Function: dnsserver
int dnsserver(dns_opt_t *opt) {
  struct sockaddr_in6 si_other;
  int senderSocket = -1;
  senderSocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  if (senderSocket == -1) 
    return -3;

  int replySocket;
  if (listenSocket == -1) {
    struct sockaddr_in6 si_me;
    if ((listenSocket=socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP))==-1) {
      listenSocket = -1;
      return -1;
    }
    replySocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    if (replySocket == -1)
    {
      close(listenSocket);
      return -1;
    }
    int sockopt = 1;
    setsockopt(listenSocket, IPPROTO_IPV6, DSTADDR_SOCKOPT, &sockopt, sizeof sockopt);
    memset((char *) &si_me, 0, sizeof(si_me));
    si_me.sin6_family = AF_INET6;
    si_me.sin6_port = htons(opt->port);
    si_me.sin6_addr = in6addr_any;
    if (bind(listenSocket, (struct sockaddr*)&si_me, sizeof(si_me))==-1)
      return -2;
  }
  
  unsigned char inbuf[BUFLEN], outbuf[BUFLEN];
  struct iovec iov[1] = {
    {
      .iov_base = inbuf,
      .iov_len = sizeof(inbuf),
    },
  };
  union control_data cmsg;
  struct msghdr msg = {
    .msg_name = &si_other,
    .msg_namelen = sizeof(si_other),
    .msg_iov = iov,
    .msg_iovlen = 1,
    .msg_control = &cmsg,
    .msg_controllen = sizeof(cmsg),
  };
  for (; 1; ++(opt->nRequests))
  {
    ssize_t insize = recvmsg(listenSocket, &msg, 0);
//    unsigned char *addr = (unsigned char*)&si_other.sin_addr.s_addr;
//    printf("DNS: Request %llu from %i.%i.%i.%i:%i of %i bytes\n", (unsigned long long)(opt->nRequests), addr[0], addr[1], addr[2], addr[3], ntohs(si_other.sin_port), (int)insize);
    if (insize <= 0)
      continue;

    ssize_t ret = dnshandle(opt, inbuf, insize, outbuf);
    if (ret <= 0)
      continue;

    bool handled = false;
    for (struct cmsghdr*hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr))
    {
      if (hdr->cmsg_level == IPPROTO_IP && hdr->cmsg_type == DSTADDR_SOCKOPT)
      {
        msg.msg_iov[0].iov_base = outbuf;
        msg.msg_iov[0].iov_len = ret;
        sendmsg(listenSocket, &msg, 0);
        msg.msg_iov[0].iov_base = inbuf;
        msg.msg_iov[0].iov_len = sizeof(inbuf);
        handled = true;
      }
    }
    if (!handled)
      sendto(listenSocket, outbuf, ret, 0, (struct sockaddr*)&si_other, sizeof(si_other));
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x868, %rsp            # imm = 0x868
movq %rdi, %rbx
movl $0xa, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x30b0
cmpl $-0x1, %eax
je 0x45ef
cmpl $-0x1, 0x184a4(%rip)    # 0x1c398
je 0x45f9
leaq 0x2e0(%rsp), %rcx
leaq 0xa0(%rsp), %rax
movq %rcx, (%rax)
movl $0x200, %edx            # imm = 0x200
movq %rdx, 0x8(%rax)
leaq 0x84(%rsp), %rsi
leaq 0x48(%rsp), %r13
movq %rsi, (%r13)
movl $0x1c, 0x8(%r13)
movq %rax, 0x10(%r13)
movq $0x1, 0x18(%r13)
leaq 0x2b8(%rsp), %rax
movq %rax, 0x20(%r13)
movq $0x28, 0x28(%r13)
movl $0x0, 0x30(%r13)
leaq 0xb0(%rsp), %rax
addq %rax, %rdx
movq %rdx, 0x20(%rsp)
negq %rcx
movq %rcx, 0x40(%rsp)
movl 0x18423(%rip), %edi     # 0x1c398
movq %r13, %rsi
xorl %edx, %edx
callq 0x30f0
testq %rax, %rax
jle 0x4137
cmpq $0xb, %rax
jbe 0x4137
movzwl 0x2e0(%rsp), %ecx
movw %cx, 0xb0(%rsp)
movb 0x2e2(%rsp), %dl
movb %dl, 0xb2(%rsp)
movb 0x2e3(%rsp), %cl
movl %ecx, %esi
andb $-0x10, %sil
movb %sil, 0xb3(%rsp)
testb %dl, %dl
js 0x4054
movb $0x4, %r12b
cmpb $0x7, %dl
ja 0x405c
andb $0x5, %dl
movb %dl, 0xb2(%rsp)
andb $0x70, %cl
movb %cl, 0xb3(%rsp)
movzwl 0x2e4(%rsp), %ecx
rolw $0x8, %cx
testw %cx, %cx
je 0x4059
movzwl %cx, %ecx
cmpl $0x1, %ecx
jne 0x405c
leaq 0x2ec(%rsp), %rcx
movq %rcx, 0x38(%rsp)
leaq (%rsp,%rax), %rbp
addq $0x2e0, %rbp            # imm = 0x2E0
movl $0x100, %r8d            # imm = 0x100
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
leaq 0x2e0(%rsp), %rdx
leaq 0x4e0(%rsp), %rcx
callq 0x46e5
cmpl $-0x1, %eax
je 0x4054
cmpl $-0x2, %eax
jne 0x4140
movb $0x5, %r12b
jmp 0x405c
movb $0x1, %r12b
jmp 0x405c
xorl %r12d, %r12d
orb %r12b, 0xb3(%rsp)
movq $0x0, 0xb4(%rsp)
movl $0xc, %ebp
movq 0x68(%rsp), %r14
cmpq $0x10, 0x70(%rsp)
setb %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x4111
xorl %r15d, %r15d
cmpl $0x0, 0x8(%r14)
jne 0x40a2
cmpl $0x32, 0xc(%r14)
je 0x40b7
movq %r13, %rdi
movq %r14, %rsi
callq 0x30a0
movq %rax, %r14
testq %rax, %rax
jne 0x4094
jmp 0x410b
movq 0x58(%rsp), %rax
leaq 0xb0(%rsp), %rcx
movq %rcx, (%rax)
movq %rbp, 0x8(%rax)
movl 0x182c7(%rip), %edi     # 0x1c398
movq %r13, %rsi
xorl %edx, %edx
callq 0x3340
movq 0x58(%rsp), %rax
leaq 0x2e0(%rsp), %rcx
movq %rcx, (%rax)
movq $0x200, 0x8(%rax)       # imm = 0x200
movq %r13, %rdi
movq %r14, %rsi
callq 0x30a0
movq %rax, %r14
movb $0x1, %r15b
testq %rax, %rax
jne 0x4094
jmp 0x4137
testb $0x1, %r15b
jne 0x4137
movl 0x18281(%rip), %edi     # 0x1c398
leaq 0xb0(%rsp), %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
leaq 0x84(%rsp), %r8
movl $0x1c, %r9d
callq 0x3060
incq 0x30(%rbx)
jmp 0x3f6f
leaq 0x4e0(%rsp), %r12
movq %r12, %rdi
callq 0x31c0
movq %rax, 0x10(%rsp)
movq 0x10(%rbx), %r14
movq %r14, %rdi
callq 0x31c0
movq %rax, %r15
movq %r12, %rdi
movq %r14, %rsi
callq 0x3430
testl %eax, %eax
je 0x41c2
leal 0x2(%r15), %eax
movb $0x5, %r12b
movq 0x10(%rsp), %rcx
cmpl %ecx, %eax
jg 0x405c
movl %r15d, %eax
notl %eax
addl %ecx, %eax
cltq
cmpb $0x2e, 0x4e0(%rsp,%rax)
jne 0x405c
movslq %ecx, %rax
leaq (%rsp,%rax), %rdi
addq $0x4e0, %rdi            # imm = 0x4E0
movslq %r15d, %rax
subq %rax, %rdi
movq %r14, %rsi
callq 0x3430
testl %eax, %eax
jne 0x405c
movq 0x38(%rsp), %r14
subq %r14, %rbp
movb $0x1, %r12b
cmpq $0x4, %rbp
jl 0x405c
leaq 0x4(%r14), %rdx
leaq 0x2ec(%rsp), %rsi
subq %rsi, %rdx
leaq 0xbc(%rsp), %r15
movq %r15, %rdi
callq 0x3350
movw $0x100, 0xb4(%rsp)      # imm = 0x100
movl $0x0, -0x6(%r15)
movw $0x0, -0x2(%r15)
orb $-0x80, 0xb2(%rsp)
movzwl (%r14), %ecx
movzwl 0x2(%r14), %eax
rolw $0x8, %cx
movw %cx, 0x1e(%rsp)
movzwl %cx, %r12d
rolw $0x8, %ax
movzwl %ax, %r15d
movq 0x40(%rsp), %rax
addq %r14, %rax
addq $0x4, %rax
leaq (%rsp,%rax), %r14
addq $0xb0, %r14
movq %r14, 0x8(%rsp)
cmpl $0xff, %r12d
je 0x4260
cmpl $0x2, %r12d
jne 0x4281
cmpl $0x1, %r15d
je 0x4321
movl $0x0, %eax
movq %rax, 0x10(%rsp)
cmpl $0xff, %r15d
je 0x432a
movq %r14, 0x5e0(%rsp)
movq 0x18(%rbx), %rcx
leaq 0x5e0(%rsp), %rdi
movq 0x20(%rsp), %rbp
movq %rbp, %rsi
xorl %edx, %edx
callq 0x47d6
movl %r15d, 0x18(%rsp)
movq 0x5e0(%rsp), %r15
subq %r14, %r15
movq %r14, 0x5e0(%rsp)
movl 0x8(%rbx), %eax
movl %eax, 0x10(%rsp)
movq 0x18(%rbx), %rax
movq %rax, 0x28(%rsp)
movq 0x20(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %edi, %edi
callq 0x33c0
leaq 0x5e0(%rsp), %rdi
movq %rbp, %rsi
movl 0x10(%rsp), %edx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movl %eax, %r9d
callq 0x4855
movslq %r15d, %rax
movq 0x5e0(%rsp), %rcx
subq %r14, %rcx
cmpq %rcx, %rax
cmovgeq %r15, %rcx
movl 0x18(%rsp), %r15d
movslq %ecx, %rax
movq %rax, 0x10(%rsp)
jmp 0x432a
movq $0x0, 0x10(%rsp)
movb $0x1, %bpl
cmpl $0xff, %r12d
je 0x433c
cmpl $0x2, %r12d
jne 0x4373
cmpl $0xff, %r15d
je 0x434b
cmpl $0x1, %r15d
jne 0x4373
movq 0x20(%rsp), %rsi
subq 0x10(%rsp), %rsi
movl 0x8(%rbx), %edx
movq 0x18(%rbx), %rcx
leaq 0x8(%rsp), %rdi
callq 0x47d6
testl %eax, %eax
jne 0x4373
incb 0xb7(%rsp)
xorl %ebp, %ebp
cmpl $0xff, %r12d
je 0x4382
cmpl $0x6, %r12d
jne 0x43ee
cmpl $0xff, %r15d
je 0x4391
cmpl $0x1, %r15d
jne 0x43ee
movq 0x20(%rbx), %r14
testq %r14, %r14
je 0x43ee
movl %r15d, 0x18(%rsp)
movq 0x20(%rsp), %r15
subq 0x10(%rsp), %r15
movl %ebp, 0x28(%rsp)
movl 0x8(%rbx), %ebp
movq 0x18(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %edi, %edi
callq 0x33c0
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movl 0x18(%rsp), %r15d
movl %ebp, %edx
movl 0x28(%rsp), %ebp
movq 0x30(%rsp), %rcx
movq %r14, %r8
movl %eax, %r9d
callq 0x4855
testl %eax, %eax
jne 0x43ee
incb 0xb7(%rsp)
cmpw $0x1, 0x1e(%rsp)
je 0x4409
cmpl $0xff, %r12d
je 0x4409
cmpl $0x1c, %r12d
jne 0x4566
cmpl $0xff, %r15d
je 0x441c
cmpl $0x1, %r15d
jne 0x4566
movl %ebp, 0x28(%rsp)
cmpl $0x1c, %r12d
sete %al
cmpl $0x1, %r12d
sete %cl
cmpl $0xff, %r12d
sete %dl
orb %dl, %cl
movzbl %cl, %r8d
orb %dl, %al
movzbl %al, %r9d
movq %rbx, %rdi
leaq 0x4e0(%rsp), %rsi
leaq 0x5e0(%rsp), %rdx
movl $0x20, %ecx
callq *0x28(%rbx)
testl %eax, %eax
jle 0x4562
movq 0x20(%rsp), %rbp
subq 0x10(%rsp), %rbp
movl %eax, %eax
movq %rax, 0x10(%rsp)
leaq 0x5e4(%rsp), %r15
xorl %r12d, %r12d
leaq (%r12,%r12,4), %rax
movl 0x5e0(%rsp,%rax,4), %eax
cmpl $0x6, %eax
je 0x44f0
cmpl $0x4, %eax
jne 0x4562
movl 0x4(%rbx), %ecx
movq 0x8(%rsp), %r14
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0x4a33
testl %eax, %eax
jne 0x4562
movq 0x8(%rsp), %rax
movq %rbp, %rcx
subq %rax, %rcx
cmpq $0x5, %rcx
jle 0x455d
movw $0x400, (%rax)          # imm = 0x400
addq $0x2, %rax
xorl %ecx, %ecx
movb (%r15,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpq $0x4, %rcx
jne 0x44de
jmp 0x453a
movl 0x4(%rbx), %ecx
movq 0x8(%rsp), %r14
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
movl $0x1c, %edx
callq 0x4a33
testl %eax, %eax
jne 0x4562
movq 0x8(%rsp), %rax
movq %rbp, %rcx
subq %rax, %rcx
cmpq $0x5, %rcx
jle 0x455d
movw $0x1000, (%rax)         # imm = 0x1000
addq $0x2, %rax
xorl %ecx, %ecx
movb (%r15,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpq $0x10, %rcx
jne 0x452a
addq %rcx, %rax
incb 0xb7(%rsp)
movq %rax, 0x8(%rsp)
incq %r12
addq $0x14, %r15
cmpq 0x10(%rsp), %r12
jne 0x4483
jmp 0x4562
movq %r14, 0x8(%rsp)
movl 0x28(%rsp), %ebp
movb 0xb7(%rsp), %al
testb %bpl, %bpl
je 0x458e
testb %al, %al
je 0x4592
movl 0x8(%rbx), %edx
movq 0x18(%rbx), %rcx
leaq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x47d6
jmp 0x45be
testb %al, %al
jne 0x45c9
movl 0x8(%rbx), %ebp
movq 0x18(%rbx), %r14
movq 0x20(%rbx), %r15
xorl %edi, %edi
callq 0x33c0
leaq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl %ebp, %edx
movq %r14, %rcx
movq %r15, %r8
movl %eax, %r9d
callq 0x4855
testl %eax, %eax
jne 0x45c9
incb 0xb9(%rsp)
orb $0x4, 0xb2(%rsp)
movq 0x8(%rsp), %rbp
leaq 0xb0(%rsp), %rax
subq %rax, %rbp
testq %rbp, %rbp
jg 0x4075
jmp 0x4137
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x46d3
movl $0xa, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x30b0
movl %eax, 0x17d85(%rip)     # 0x1c398
cmpl $-0x1, %eax
je 0x46b7
movl $0xa, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x30b0
cmpl $-0x1, %eax
je 0x46c3
leaq 0x2e0(%rsp), %rcx
movl $0x1, (%rcx)
movl 0x17d4b(%rip), %edi     # 0x1c398
movl $0x29, %esi
movl $0x32, %edx
movl $0x4, %r8d
callq 0x3660
xorps %xmm0, %xmm0
leaq 0x5e0(%rsp), %rsi
movups %xmm0, 0x4(%rsi)
movq $0x0, 0x14(%rsi)
movw $0xa, (%rsi)
movzwl (%rbx), %eax
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movq 0x17940(%rip), %rax     # 0x1bfd0
movups (%rax), %xmm0
movups %xmm0, 0x8(%rsi)
movl 0x17cfb(%rip), %edi     # 0x1c398
movl $0x1c, %edx
callq 0x3080
cmpl $-0x1, %eax
jne 0x3efa
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x46d3
movl $0xffffffff, 0x17cd7(%rip) # imm = 0xFFFFFFFF
jmp 0x46ce
movl 0x17ccf(%rip), %edi     # 0x1c398
callq 0x36e0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x868, %rsp            # imm = 0x868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq