// File: /workspace/github_cpp_makefile_star2_O2/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 $0x878, %rsp            # imm = 0x878
movq %rdi, 0x8(%rsp)
pushq $0xa
popq %rdi
pushq $0x2
popq %rsi
pushq $0x11
popq %rdx
callq 0x30b0
cmpl $-0x1, %eax
je 0x4129
cmpl $-0x1, 0x19998(%rip)    # 0x1d3c8
je 0x4130
leaq 0x3f0(%rsp), %rcx
leaq 0xb0(%rsp), %rax
movq %rcx, (%rax)
movl $0x200, %edx            # imm = 0x200
movq %rdx, 0x8(%rax)
leaq 0x94(%rsp), %rsi
leaq 0x58(%rsp), %r13
movq %rsi, (%r13)
movl $0x1c, 0x8(%r13)
movq %rax, 0x10(%r13)
movq $0x1, 0x18(%r13)
leaq 0x2c8(%rsp), %rax
movq %rax, 0x20(%r13)
movq $0x28, 0x28(%r13)
andl $0x0, 0x30(%r13)
leaq 0xc0(%rsp), %r15
addq %r15, %rdx
movq %rdx, 0x18(%rsp)
movq %rcx, %rbx
negq %rcx
movq %rcx, 0x50(%rsp)
movl 0x19917(%rip), %edi     # 0x1d3c8
movq %r13, %rsi
xorl %edx, %edx
callq 0x3100
testq %rax, %rax
jle 0x3c2b
cmpq $0xb, %rax
jbe 0x3c2b
movzwl 0x3f0(%rsp), %ecx
movw %cx, 0xc0(%rsp)
movb 0x3f2(%rsp), %dl
movb %dl, 0xc2(%rsp)
movb 0x3f3(%rsp), %cl
movl %ecx, %esi
andb $-0x10, %sil
movb %sil, 0xc3(%rsp)
testb %dl, %dl
js 0x3b83
movb $0x4, %bpl
cmpb $0x7, %dl
ja 0x3b8a
andb $0x5, %dl
movb %dl, 0xc2(%rsp)
andb $0x70, %cl
movb %cl, 0xc3(%rsp)
movzwl 0x3f4(%rsp), %ecx
rolw $0x8, %cx
testw %cx, %cx
je 0x3b88
movzwl %cx, %ecx
cmpl $0x1, %ecx
jne 0x3b8a
leaq 0x3fc(%rsp), %rcx
movq %rcx, 0x48(%rsp)
leaq (%rsp,%rax), %r12
addq $0x3f0, %r12            # imm = 0x3F0
movl $0x100, %r8d            # imm = 0x100
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
leaq 0x2f0(%rsp), %rcx
callq 0x41fe
cmpl $-0x1, %eax
je 0x3b83
cmpl $-0x2, %eax
jne 0x3c39
movb $0x5, %bpl
jmp 0x3b8a
movb $0x1, %bpl
jmp 0x3b8a
xorl %ebp, %ebp
orb %bpl, 0xc3(%rsp)
andq $0x0, 0xc4(%rsp)
pushq $0xc
popq %r12
xorl %r14d, %r14d
cmpq $0x10, 0x80(%rsp)
jb 0x3bb4
movq 0x78(%rsp), %rbp
jmp 0x3c01
xorl %ebp, %ebp
jmp 0x3c01
cmpl $0x0, 0x8(%rbp)
jne 0x3bf3
cmpl $0x32, 0xc(%rbp)
jne 0x3bf3
movq 0x68(%rsp), %rax
movq %r15, (%rax)
movq %r12, 0x8(%rax)
movl 0x197f2(%rip), %edi     # 0x1d3c8
movq %r13, %rsi
xorl %edx, %edx
callq 0x3350
movq 0x68(%rsp), %rax
movq %rbx, (%rax)
movq $0x200, 0x8(%rax)       # imm = 0x200
movb $0x1, %r14b
movq %r13, %rdi
movq %rbp, %rsi
callq 0x30a0
movq %rax, %rbp
testq %rbp, %rbp
jne 0x3bb8
testb $0x1, %r14b
jne 0x3c2b
movl 0x197b6(%rip), %edi     # 0x1d3c8
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
leaq 0x94(%rsp), %r8
pushq $0x1c
popq %r9
callq 0x3060
movq 0x8(%rsp), %rax
incq 0x30(%rax)
jmp 0x3aab
leaq 0x2f0(%rsp), %r14
movq %r14, %rdi
callq 0x31b0
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbp
movq %rbp, %rdi
callq 0x31b0
movq %rax, 0x20(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3450
testl %eax, %eax
je 0x3cc5
movq %rbp, %rsi
movq 0x20(%rsp), %rdx
leal 0x2(%rdx), %eax
movb $0x5, %bpl
movq 0x10(%rsp), %rcx
cmpl %ecx, %eax
jg 0x3b8a
movl %edx, %eax
notl %eax
addl %ecx, %eax
cltq
cmpb $0x2e, 0x2f0(%rsp,%rax)
jne 0x3b8a
movslq %ecx, %rax
leaq (%rsp,%rax), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq %edx, %rax
subq %rax, %rdi
callq 0x3450
testl %eax, %eax
jne 0x3b8a
movq 0x48(%rsp), %r14
subq %r14, %r12
movb $0x1, %bpl
cmpq $0x4, %r12
jl 0x3b8a
leaq 0x4(%r14), %rdx
leaq 0x3fc(%rsp), %rsi
subq %rsi, %rdx
leaq 0xcc(%rsp), %r12
movq %r12, %rdi
callq 0x3360
movw $0x100, 0xc4(%rsp)      # imm = 0x100
andl $0x0, -0x6(%r12)
andw $0x0, -0x2(%r12)
orb $-0x80, 0xc2(%rsp)
movzwl (%r14), %edx
movzwl 0x2(%r14), %eax
rolw $0x8, %dx
movzwl %dx, %r12d
rolw $0x8, %ax
movzwl %ax, %ecx
movq 0x50(%rsp), %rax
addq %r14, %rax
addq $0x4, %rax
leaq (%rsp,%rax), %rbp
addq $0xc0, %rbp
movq %rbp, (%rsp)
cmpw $0xff, %r12w
movw %dx, 0x10(%rsp)
je 0x3d5e
cmpl $0x2, %r12d
jne 0x3d79
cmpl $0x1, %ecx
je 0x3e2a
movl $0x0, %r14d
cmpl $0xff, %ecx
je 0x3e2d
movq %rbp, 0x5f0(%rsp)
movl %r12d, 0x30(%rsp)
movq 0x8(%rsp), %r12
movl %ecx, 0x20(%rsp)
movq 0x18(%r12), %rcx
leaq 0x5f0(%rsp), %rdi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x42db
movq 0x5f0(%rsp), %r14
subq %rbp, %r14
movq %rbp, 0x5f0(%rsp)
movl 0x8(%r12), %eax
movl %eax, 0x2c(%rsp)
movq 0x18(%r12), %rax
movq %rax, 0x40(%rsp)
movq 0x20(%r12), %rax
movq %rax, 0x38(%rsp)
movl 0x30(%rsp), %r12d
xorl %edi, %edi
callq 0x33d0
leaq 0x5f0(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl 0x2c(%rsp), %edx
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %r8
movl %eax, %r9d
callq 0x4356
movzwl 0x10(%rsp), %edx
movslq %r14d, %rax
movq 0x5f0(%rsp), %rcx
subq %rbp, %rcx
cmpq %rcx, %rax
cmovgeq %r14, %rcx
movslq %ecx, %r14
movl 0x20(%rsp), %ecx
jmp 0x3e2d
xorl %r14d, %r14d
movb $0x1, %sil
cmpw $0xff, %dx
je 0x3e3d
cmpl $0x2, %r12d
jne 0x3e87
cmpl $0xff, %ecx
je 0x3e4a
cmpl $0x1, %ecx
jne 0x3e87
movq 0x18(%rsp), %rsi
subq %r14, %rsi
movq 0x8(%rsp), %rax
movl 0x8(%rax), %edx
movq %r14, %rbp
movl %ecx, %r14d
movq 0x18(%rax), %rcx
movq %rsp, %rdi
callq 0x42db
movb $0x1, %sil
movzwl 0x10(%rsp), %edx
movl %r14d, %ecx
movq %rbp, %r14
testl %eax, %eax
jne 0x3e87
incb 0xc7(%rsp)
xorl %esi, %esi
cmpw $0xff, %dx
je 0x3e98
cmpl $0x6, %r12d
jne 0x3f21
cmpl $0xff, %ecx
je 0x3ea5
cmpl $0x1, %ecx
jne 0x3f21
movq 0x8(%rsp), %rax
movq 0x20(%rax), %r8
testq %r8, %r8
je 0x3f21
movq 0x18(%rsp), %rax
subq %r14, %rax
movq %rax, 0x38(%rsp)
movq 0x8(%rsp), %rax
movl 0x8(%rax), %edi
movl %edi, 0x2c(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x40(%rsp)
xorl %edi, %edi
movl %ecx, 0x20(%rsp)
movq %r14, 0x30(%rsp)
movl %esi, %ebp
movq %r8, %r14
callq 0x33d0
movq %rsp, %rdi
movq 0x38(%rsp), %rsi
movl 0x2c(%rsp), %edx
movq 0x40(%rsp), %rcx
movq %r14, %r8
movl %eax, %r9d
callq 0x4356
movl %ebp, %esi
movzwl 0x10(%rsp), %edx
movq 0x30(%rsp), %r14
movl 0x20(%rsp), %ecx
testl %eax, %eax
jne 0x3f21
incb 0xc7(%rsp)
cmpw $0x1, %dx
je 0x3f3a
cmpl $0xff, %r12d
je 0x3f3a
cmpl $0x1c, %r12d
jne 0x40a3
cmpl $0xff, %ecx
je 0x3f4b
cmpl $0x1, %ecx
jne 0x40a3
movl %esi, 0x30(%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 0x8(%rsp), %rdi
leaq 0x2f0(%rsp), %rsi
leaq 0x5f0(%rsp), %rdx
pushq $0x20
popq %rcx
callq *0x28(%rdi)
movl %eax, %ecx
movq 0x18(%rsp), %rax
subq %r14, %rax
movq %rax, 0x10(%rsp)
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
movq %rcx, 0x20(%rsp)
leaq 0x5f4(%rsp), %rbp
xorl %r12d, %r12d
cmpq 0x20(%rsp), %r12
je 0x409f
imulq $0x14, %r12, %rax
movl 0x5f0(%rsp,%rax), %eax
cmpl $0x6, %eax
je 0x4032
cmpl $0x4, %eax
jne 0x409f
movq 0x8(%rsp), %rax
movl 0x4(%rax), %ecx
movq (%rsp), %r14
movq %rsp, %rdi
movq 0x10(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x4528
movq 0x10(%rsp), %rcx
testl %eax, %eax
jne 0x409f
movq (%rsp), %rax
subq %rax, %rcx
cmpq $0x5, %rcx
jle 0x409b
movw $0x400, (%rax)          # imm = 0x400
addq $0x2, %rax
xorl %ecx, %ecx
cmpq $0x4, %rcx
je 0x4081
movb (%rbp,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
jmp 0x4020
movq 0x8(%rsp), %rax
movl 0x4(%rax), %ecx
movq (%rsp), %r14
movq %rsp, %rdi
movq 0x10(%rsp), %rsi
pushq $0x1c
popq %rdx
callq 0x4528
movq 0x10(%rsp), %rcx
testl %eax, %eax
jne 0x409f
movq (%rsp), %rax
subq %rax, %rcx
cmpq $0x5, %rcx
jle 0x409b
movw $0x1000, (%rax)         # imm = 0x1000
addq $0x2, %rax
xorl %ecx, %ecx
cmpq $0x10, %rcx
je 0x4081
movb (%rbp,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
jmp 0x406f
addq %rcx, %rax
movq %rax, (%rsp)
incq %r12
incb 0xc7(%rsp)
addq $0x14, %rbp
jmp 0x3fb7
movq %r14, (%rsp)
movl 0x30(%rsp), %esi
movb 0xc7(%rsp), %al
testb %sil, %sil
je 0x40ce
testb %al, %al
je 0x40d2
movq 0x8(%rsp), %rax
movl 0x8(%rax), %edx
movq 0x18(%rax), %rcx
movq %rsp, %rdi
movq 0x18(%rsp), %rsi
callq 0x42db
jmp 0x4101
testb %al, %al
jne 0x410c
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ebp
movq 0x18(%rax), %r12
movq 0x20(%rax), %r14
xorl %edi, %edi
callq 0x33d0
movq %rsp, %rdi
movq 0x18(%rsp), %rsi
movl %ebp, %edx
movq %r12, %rcx
movq %r14, %r8
movl %eax, %r9d
callq 0x4356
testl %eax, %eax
jne 0x410c
incb 0xc9(%rsp)
orb $0x4, 0xc2(%rsp)
movq (%rsp), %r12
subq %r15, %r12
testq %r12, %r12
jg 0x3b9f
jmp 0x3c2b
pushq $-0x3
jmp 0x41eb
pushq $0xa
popq %rdi
pushq $0x2
popq %rsi
pushq $0x11
popq %rdx
callq 0x30b0
movl %eax, 0x19284(%rip)     # 0x1d3c8
cmpl $-0x1, %eax
je 0x41d5
pushq $0xa
popq %rdi
pushq $0x2
popq %rsi
pushq $0x11
popq %rdx
callq 0x30b0
cmpl $-0x1, %eax
je 0x41de
leaq 0x3f0(%rsp), %rcx
movl $0x1, (%rcx)
movl 0x19254(%rip), %edi     # 0x1d3c8
pushq $0x29
popq %rsi
pushq $0x32
popq %rdx
pushq $0x4
popq %r8
callq 0x36a0
xorps %xmm0, %xmm0
leaq 0x5f0(%rsp), %rsi
movups %xmm0, 0x4(%rsi)
andq $0x0, 0x14(%rsi)
movw $0xa, (%rsi)
movq 0x8(%rsp), %rax
movzwl (%rax), %eax
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movq 0x18e1d(%rip), %rax     # 0x1cfd0
movups (%rax), %xmm0
movups %xmm0, 0x8(%rsi)
movl 0x19208(%rip), %edi     # 0x1d3c8
pushq $0x1c
popq %rdx
callq 0x3080
cmpl $-0x1, %eax
jne 0x3a36
pushq $-0x2
jmp 0x41eb
orl $-0x1, 0x191ec(%rip)    # 0x1d3c8
jmp 0x41e9
movl 0x191e4(%rip), %edi     # 0x1d3c8
callq 0x3740
pushq $-0x1
popq %rax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq