// File: /workspace/github_c_makefile_star2_O3/ddrown[P]pretend-ntp/pretend-ntp.c
// Function: ntp_server
void *ntp_server(void *arg) {
  struct thread_info *tinfo = arg;
  struct ntp_msg in, out;
  int sock;
  int optval;
  socklen_t optlen;

  tinfo->thread_queries = tinfo->unknown_version = tinfo->unknown_mode = tinfo->failed_send = 0;

  if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror_exit("socket");
  }

  optval = 1;
  if(setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)) < 0) {
    perror_exit("setsockopt SO_REUSEADDR");
  }

  optval = 31250/2; // ~250us at 1G
  if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &optval, sizeof(optval)) < 0) {
    perror_exit("setsockopt SO_RCVBUF");
  }

  optval = 31250/2; // ~250us at 1G
  if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &optval, sizeof(optval)) < 0) {
    perror_exit("setsockopt SO_RCVBUF");
  }

  optlen = sizeof(optval);
  if(getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &optval, &optlen) < 0) {
    perror_exit("getsockopt SO_RCVBUF");
  }
  printf("THD%d socket rcvbuf = %d\n", tinfo->thread_num, optval);

  optlen = sizeof(optval);
  if(getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &optval, &optlen) < 0) {
    perror_exit("getsockopt SO_SNDBUF");
  }
  printf("THD%d socket sndbuf = %d\n", tinfo->thread_num, optval);

  if(bind(sock, tinfo->addr, sizeof(*tinfo->addr)) < 0) {
    perror_exit("bind");
  }

  memset(&out, 0, sizeof(out));
  out.version = NTP_VERS_4;
  out.mode = NTP_MODE_SERVER;
  out.stratum = 1;
  out.ident = htonl(0x50505300); // PPS
  out.precision = -20;

  while(1) {
    int len;
    struct sockaddr_in source_addr;
    socklen_t addrlen = sizeof(source_addr); 
    struct timespec t;

    len = recvfrom(sock, &in, sizeof(in), 0, &source_addr, &addrlen);
    clock_gettime(CLOCK_REALTIME, &t);

    if(len < 0) 
      perror_exit("recvfrom");

    if(in.version < 2 || in.version > 4) {
      tinfo->unknown_version++;
      continue;
    }
    if(in.mode == NTP_MODE_PRIV) { // TODO: better exit
      printf("thread %d exiting\n", tinfo->thread_num);
      close(sock);
      return NULL;
    }
    if(in.mode != NTP_MODE_CLIENT) {
      tinfo->unknown_mode++;
      continue;
    }

    tinfo->thread_queries++;

    out.org_time = in.trans_time;
    out.org_time_fb = in.trans_time_fb;

    out.ref_time = out.recv_time = unixtime_to_ntp_s(t.tv_sec);
    out.ref_time_fb = out.recv_time_fb = ns_to_ntp_frac(t.tv_nsec);

    clock_gettime(CLOCK_REALTIME, &t);
    out.trans_time_fb = ns_to_ntp_frac(t.tv_nsec);
    out.trans_time = unixtime_to_ntp_s(t.tv_sec);

    if(sendto(sock, &out, sizeof(out), 0, &source_addr, sizeof(source_addr)) < 0) {
      tinfo->failed_send++;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1170
movl %eax, %ebx
testl %eax, %eax
jns 0x12be
leaq 0xd4b(%rip), %rdi       # 0x2004
callq 0x1d64
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
movl %ebx, %edi
movl $0x1, %esi
movl $0xf, %edx
movl $0x4, %r8d
callq 0x1060
testl %eax, %eax
jns 0x12f0
leaq 0xd20(%rip), %rdi       # 0x200b
callq 0x1d64
leaq 0x4(%rsp), %rcx
movl $0x3d09, (%rcx)         # imm = 0x3D09
movl %ebx, %edi
movl $0x1, %esi
movl $0x8, %edx
movl $0x4, %r8d
callq 0x1060
testl %eax, %eax
jns 0x1322
leaq 0xd06(%rip), %rdi       # 0x2023
callq 0x1d64
leaq 0x4(%rsp), %rcx
movl $0x3d09, (%rcx)         # imm = 0x3D09
movl %ebx, %edi
movl $0x1, %esi
movl $0x7, %edx
movl $0x4, %r8d
callq 0x1060
testl %eax, %eax
jns 0x1354
leaq 0xcd4(%rip), %rdi       # 0x2023
callq 0x1d64
leaq 0xc(%rsp), %r8
movl $0x4, (%r8)
leaq 0x4(%rsp), %rcx
movl %ebx, %edi
movl $0x1, %esi
movl $0x8, %edx
callq 0x10c0
testl %eax, %eax
jns 0x1386
leaq 0xcb7(%rip), %rdi       # 0x2038
callq 0x1d64
movl 0x8(%r15), %esi
leaq 0x4(%rsp), %r14
movl (%r14), %edx
leaq 0xcb4(%rip), %rdi       # 0x204d
xorl %eax, %eax
callq 0x1080
leaq 0xc(%rsp), %r8
movl $0x4, (%r8)
movl %ebx, %edi
movl $0x1, %esi
movl $0x7, %edx
movq %r14, %rcx
callq 0x10c0
testl %eax, %eax
jns 0x13d0
leaq 0xc9c(%rip), %rdi       # 0x2067
callq 0x1d64
movl 0x8(%r15), %esi
movl 0x4(%rsp), %edx
leaq 0xc9d(%rip), %rdi       # 0x207c
xorl %eax, %eax
callq 0x1080
movq 0x30(%r15), %rsi
movl %ebx, %edi
movl $0x10, %edx
callq 0x1110
testl %eax, %eax
jns 0x1406
leaq 0xc95(%rip), %rdi       # 0x2096
callq 0x1d64
leaq 0x28(%r15), %rax
movq %rax, 0x30(%rsp)
leaq 0x20(%r15), %rax
movq %rax, 0x38(%rsp)
leaq 0x18(%r15), %r12
movq %r15, 0x20(%rsp)
leaq 0x10(%r15), %rax
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movw $0x124, (%rax)          # imm = 0x124
movl $0x535050, 0xc(%rax)    # imm = 0x535050
movb $-0x14, 0x3(%rax)
leaq 0x78(%rsp), %r13
leaq 0x8(%rsp), %rbp
leaq 0x10(%rsp), %r14
movl $0x10, 0x8(%rsp)
movl $0x30, %edx
movl %ebx, %edi
leaq 0x88(%rsp), %rsi
xorl %ecx, %ecx
movq %r13, %r8
movq %rbp, %r9
callq 0x10e0
movq %rax, %r15
xorl %edi, %edi
movq %r14, %rsi
callq 0x1070
testl %r15d, %r15d
jns 0x149e
leaq 0xc02(%rip), %rdi       # 0x209b
callq 0x1d64
movzbl 0x88(%rsp), %eax
movl %eax, %edx
shrb $0x3, %dl
andb $0x7, %dl
addb $-0x5, %dl
movq %r12, %rcx
cmpb $-0x3, %dl
jb 0x155d
andl $0x7, %eax
cmpl $0x3, %eax
je 0x14d8
movq 0x38(%rsp), %rcx
cmpl $0x7, %eax
jne 0x155d
jmp 0x1565
movq 0x28(%rsp), %rax
incq (%rax)
movq 0xb0(%rsp), %rax
movq %rax, 0x58(%rsp)
movl 0x10(%rsp), %edi
callq 0x16f3
movl %eax, 0x60(%rsp)
movl %eax, 0x50(%rsp)
movl 0x18(%rsp), %edi
callq 0x16fc
movl %eax, 0x64(%rsp)
movl %eax, 0x54(%rsp)
xorl %edi, %edi
movq %r14, %rsi
callq 0x1070
movl 0x18(%rsp), %edi
callq 0x16fc
movl %eax, 0x6c(%rsp)
movl 0x10(%rsp), %edi
callq 0x16f3
movl %eax, 0x68(%rsp)
movl $0x30, %edx
movl %ebx, %edi
leaq 0x40(%rsp), %rsi
xorl %ecx, %ecx
movq %r13, %r8
movl $0x10, %r9d
callq 0x10a0
movq 0x30(%rsp), %rcx
testq %rax, %rax
jns 0x145c
incq (%rcx)
jmp 0x145c
movq 0x20(%rsp), %rax
movl 0x8(%rax), %esi
leaq 0xb30(%rip), %rdi       # 0x20a4
xorl %eax, %eax
callq 0x1080
movl %ebx, %edi
callq 0x10b0
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq