// File: /workspace/github_cpp_makefile_star2_O1/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 0x3fb0
cmpl $-0x1, 0x184a4(%rip)    # 0x1c398
jne 0x3fe0
movl $0xa, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x30b0
movl %eax, 0x18484(%rip)     # 0x1c398
cmpl $-0x1, %eax
je 0x3fba
movl $0xa, %edi
movl $0x2, %esi
movl $0x11, %edx
callq 0x30b0
cmpl $-0x1, %eax
je 0x3fc6
leaq 0x2e0(%rsp), %rcx
movl $0x1, (%rcx)
movl 0x1844a(%rip), %edi     # 0x1c398
movl $0x29, %esi
movl $0x32, %edx
movl $0x4, %r8d
callq 0x3660
xorps %xmm0, %xmm0
leaq 0x5e0(%rsp), %rsi
movaps %xmm0, (%rsi)
movups %xmm0, 0xc(%rsi)
movw $0xa, (%rsi)
movzwl (%rbx), %eax
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
movq 0x18044(%rip), %rax     # 0x1bfd0
movups (%rax), %xmm0
movups %xmm0, 0x8(%rsi)
movl 0x183ff(%rip), %edi     # 0x1c398
movl $0x1c, %edx
callq 0x3080
cmpl $-0x1, %eax
setne %cl
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x3fd8
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x46cc
movl $0xffffffff, 0x183d4(%rip) # imm = 0xFFFFFFFF
jmp 0x3fd1
movl 0x183cc(%rip), %edi     # 0x1c398
callq 0x36e0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
testb %cl, %cl
je 0x46cc
leaq 0x2e0(%rsp), %rcx
leaq 0xa0(%rsp), %rax
movq %rcx, (%rax)
movl $0x200, %ecx            # imm = 0x200
movq %rcx, 0x8(%rax)
leaq 0x84(%rsp), %rdx
leaq 0x48(%rsp), %r13
movq %rdx, (%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), %r15
addq %r15, %rcx
movq %rcx, 0x10(%rsp)
movl 0x18345(%rip), %edi     # 0x1c398
movq %r13, %rsi
xorl %edx, %edx
callq 0x30f0
testq %rax, %rax
jle 0x41ff
movq $-0x1, %rbp
cmpq $0xc, %rax
jb 0x415a
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 0x4139
movb $0x4, %r12b
cmpb $0x7, %dl
ja 0x4141
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 0x413e
movzwl %cx, %ecx
cmpl $0x1, %ecx
jne 0x4141
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 0x46de
cmpl $-0x1, %eax
je 0x4139
cmpl $-0x2, %eax
jne 0x4208
movb $0x5, %r12b
jmp 0x4141
movb $0x1, %r12b
jmp 0x4141
xorl %r12d, %r12d
orb %r12b, 0xb3(%rsp)
movq $0x0, 0xb4(%rsp)
movl $0xc, %ebp
testq %rbp, %rbp
jle 0x41ff
movq 0x68(%rsp), %r12
cmpq $0x10, 0x70(%rsp)
setb %al
testq %r12, %r12
sete %cl
orb %al, %cl
jne 0x41de
xorl %r14d, %r14d
cmpl $0x0, 0x8(%r12)
jne 0x41c5
cmpl $0x32, 0xc(%r12)
jne 0x41c5
movq 0x58(%rsp), %rax
movq %r15, (%rax)
movq %rbp, 0x8(%rax)
movl 0x181f8(%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
movb $0x1, %r14b
movq %r13, %rdi
movq %r12, %rsi
callq 0x30a0
movq %rax, %r12
testq %rax, %rax
jne 0x417e
testb $0x1, %r14b
jne 0x41ff
movl 0x181b4(%rip), %edi     # 0x1c398
movq %r15, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
leaq 0x84(%rsp), %r8
movl $0x1c, %r9d
callq 0x3060
incq 0x30(%rbx)
jmp 0x404d
leaq 0x4e0(%rsp), %r14
movq %r14, %rdi
callq 0x31c0
movq %rax, 0x28(%rsp)
movq 0x10(%rbx), %r12
movq %r12, %rdi
callq 0x31c0
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x3430
testl %eax, %eax
je 0x428f
movq %r12, %rsi
movq 0x8(%rsp), %rdx
leal 0x2(%rdx), %eax
movb $0x5, %r12b
movq 0x28(%rsp), %rcx
cmpl %ecx, %eax
jg 0x4141
movl %edx, %eax
notl %eax
addl %ecx, %eax
cltq
cmpb $0x2e, 0x4e0(%rsp,%rax)
jne 0x4141
movslq %ecx, %rax
leaq (%rsp,%rax), %rdi
addq $0x4e0, %rdi            # imm = 0x4E0
movslq %edx, %rax
subq %rax, %rdi
callq 0x3430
testl %eax, %eax
jne 0x4141
movq 0x38(%rsp), %r14
subq %r14, %rbp
movb $0x1, %r12b
cmpq $0x4, %rbp
jl 0x4141
leaq 0x4(%r14), %r12
movq %r12, %rdx
leaq 0x2ec(%rsp), %rsi
subq %rsi, %rdx
leaq 0xbc(%rsp), %rbp
movq %rbp, %rdi
callq 0x3350
movw $0x100, 0xb4(%rsp)      # imm = 0x100
movl $0x0, -0x6(%rbp)
movw $0x0, -0x2(%rbp)
orb $-0x80, 0xb2(%rsp)
movzwl (%r14), %ecx
rolw $0x8, %cx
movzwl 0x2(%r14), %eax
movw %cx, 0x28(%rsp)
movzwl %cx, %ebp
rolw $0x8, %ax
movzwl %ax, %edx
movq %r12, 0x38(%rsp)
leaq 0x2e0(%rsp), %rax
negq %rax
addq %r14, %rax
addq $0x4, %rax
leaq (%rsp,%rax), %r12
addq $0xb0, %r12
movq %r12, (%rsp)
cmpl $0xff, %ebp
je 0x4334
cmpl $0x2, %ebp
jne 0x4353
cmpl $0x1, %edx
je 0x43f0
movl $0x0, %eax
movq %rax, 0x8(%rsp)
cmpl $0xff, %edx
je 0x43f9
movq %r12, 0x5e0(%rsp)
movq 0x18(%rbx), %rcx
leaq 0x5e0(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %edx, 0x1c(%rsp)
xorl %edx, %edx
callq 0x4822
movq 0x5e0(%rsp), %r14
subq %r12, %r14
movq %r12, 0x5e0(%rsp)
movl 0x8(%rbx), %eax
movl %eax, 0x8(%rsp)
movq 0x18(%rbx), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %edi, %edi
callq 0x33c0
leaq 0x5e0(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl 0x8(%rsp), %edx
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %r8
movl %eax, %r9d
callq 0x48a1
movl 0x1c(%rsp), %edx
movslq %r14d, %rax
movq 0x5e0(%rsp), %rcx
subq %r12, %rcx
cmpq %rcx, %rax
cmovgeq %r14, %rcx
movslq %ecx, %rax
movq %rax, 0x8(%rsp)
jmp 0x43f9
movq $0x0, 0x8(%rsp)
movb $0x1, %cl
cmpl $0xff, %ebp
je 0x4408
cmpl $0x2, %ebp
jne 0x4444
cmpl $0xff, %edx
je 0x4415
cmpl $0x1, %edx
jne 0x4444
movq 0x10(%rsp), %rsi
subq 0x8(%rsp), %rsi
movl %edx, %r14d
movl 0x8(%rbx), %edx
movq 0x18(%rbx), %rcx
movq %rsp, %rdi
callq 0x4822
movl %r14d, %edx
testl %eax, %eax
setne %cl
jne 0x4444
incb 0xb7(%rsp)
xorl %ecx, %ecx
cmpl $0xff, %ebp
je 0x4451
cmpl $0x6, %ebp
jne 0x44bd
cmpl $0xff, %edx
je 0x445e
cmpl $0x1, %edx
jne 0x44bd
movq 0x20(%rbx), %r14
testq %r14, %r14
je 0x44bd
movq 0x10(%rsp), %r12
subq 0x8(%rsp), %r12
movl 0x8(%rbx), %eax
movl %eax, 0x30(%rsp)
movq 0x18(%rbx), %rax
movq %rax, 0x40(%rsp)
xorl %edi, %edi
movl %ecx, 0x20(%rsp)
movl %edx, 0x1c(%rsp)
callq 0x33c0
movq %rsp, %rdi
movq %r12, %rsi
movl 0x30(%rsp), %edx
movq 0x40(%rsp), %rcx
movq %r14, %r8
movl %eax, %r9d
callq 0x48a1
movl 0x1c(%rsp), %edx
movl 0x20(%rsp), %ecx
testl %eax, %eax
jne 0x44bd
incb 0xb7(%rsp)
cmpw $0x1, 0x28(%rsp)
je 0x44d6
cmpl $0xff, %ebp
je 0x44d6
cmpl $0x1c, %ebp
jne 0x4651
cmpl $0xff, %edx
je 0x44e7
cmpl $0x1, %edx
jne 0x4651
movl %ecx, 0x20(%rsp)
cmpl $0x1c, %ebp
sete %al
cmpl $0x1, %ebp
sete %cl
cmpl $0xff, %ebp
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)
movl %eax, 0x28(%rsp)
movq 0x10(%rsp), %r14
subq 0x8(%rsp), %r14
xorl %r12d, %r12d
cmpl 0x28(%rsp), %r12d
jge 0x464d
movslq %r12d, %rax
leaq (%rax,%rax,4), %rax
leaq (%rsp,%rax,4), %rbp
addq $0x5e0, %rbp            # imm = 0x5E0
movl (%rbp), %eax
cmpl $0x6, %eax
je 0x45c6
cmpl $0x4, %eax
jne 0x45ff
movl 0x4(%rbx), %ecx
movq (%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rsp, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x4a7f
testl %eax, %eax
jne 0x45ff
movq 0x8(%rsp), %rdx
movq (%rsp), %rcx
movq %r14, %rax
subq %rcx, %rax
cmpq $0x5, %rax
jle 0x45fb
leaq 0x1(%rcx), %rax
movq %rax, (%rsp)
movb $0x0, (%rcx)
leaq 0x2(%rcx), %rax
movq %rax, (%rsp)
movb $0x4, 0x1(%rcx)
xorl %ecx, %ecx
movb 0x4(%rbp,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpq $0x4, %rcx
jne 0x45b4
jmp 0x4642
movl 0x4(%rbx), %ecx
movq (%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rsp, %rdi
movq %r14, %rsi
movl $0x1c, %edx
callq 0x4a7f
testl %eax, %eax
jne 0x45ff
movq 0x8(%rsp), %rdx
movq (%rsp), %rcx
movq %r14, %rax
subq %rcx, %rax
cmpq $0x5, %rax
jg 0x4619
movq %rdx, (%rsp)
xorl %eax, %eax
testb %al, %al
je 0x460f
incl %r12d
incb 0xb7(%rsp)
testb %al, %al
jne 0x4538
jmp 0x464d
leaq 0x1(%rcx), %rax
movq %rax, (%rsp)
movb $0x0, (%rcx)
leaq 0x2(%rcx), %rax
movq %rax, (%rsp)
movb $0x10, 0x1(%rcx)
xorl %ecx, %ecx
movb 0x4(%rbp,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpq $0x10, %rcx
jne 0x4632
addq %rcx, %rax
movq %rax, (%rsp)
movb $0x1, %al
jmp 0x4601
movl 0x20(%rsp), %ecx
cmpb $0x0, 0xb7(%rsp)
setne %al
andb %al, %cl
cmpb $0x1, %cl
jne 0x4679
movl 0x8(%rbx), %edx
movq 0x18(%rbx), %rcx
movq %rsp, %rdi
movq 0x10(%rsp), %rsi
callq 0x4822
jmp 0x46ad
cmpb $0x0, 0xb7(%rsp)
jne 0x46b8
movl 0x8(%rbx), %ebp
movq 0x18(%rbx), %r12
movq 0x20(%rbx), %r14
xorl %edi, %edi
callq 0x33c0
movq %rsp, %rdi
movq 0x10(%rsp), %rsi
movl %ebp, %edx
movq %r12, %rcx
movq %r14, %r8
movl %eax, %r9d
callq 0x48a1
testl %eax, %eax
jne 0x46b8
incb 0xb9(%rsp)
orb $0x4, 0xb2(%rsp)
movq (%rsp), %rbp
subq %r15, %rbp
jmp 0x415a
addq $0x868, %rsp            # imm = 0x868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq