// File: /workspace/github_c_makefile_star2_O1/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 $0xa8, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1170
movl %eax, %ebp
testl %eax, %eax
jns 0x12be
leaq 0xd4b(%rip), %rdi       # 0x2004
callq 0x1d40
leaq 0xc(%rsp), %rcx
movl $0x1, (%rcx)
movl %ebp, %edi
movl $0x1, %esi
movl $0xf, %edx
movl $0x4, %r8d
callq 0x1060
testl %eax, %eax
jns 0x12f0
leaq 0xd20(%rip), %rdi       # 0x200b
callq 0x1d40
leaq 0xc(%rsp), %rcx
movl $0x3d09, (%rcx)         # imm = 0x3D09
movl %ebp, %edi
movl $0x1, %esi
movl $0x8, %edx
movl $0x4, %r8d
callq 0x1060
testl %eax, %eax
jns 0x1322
leaq 0xd06(%rip), %rdi       # 0x2023
callq 0x1d40
leaq 0xc(%rsp), %rcx
movl $0x3d09, (%rcx)         # imm = 0x3D09
movl %ebp, %edi
movl $0x1, %esi
movl $0x7, %edx
movl $0x4, %r8d
callq 0x1060
testl %eax, %eax
jns 0x1354
leaq 0xcd4(%rip), %rdi       # 0x2023
callq 0x1d40
leaq 0x14(%rsp), %r8
movl $0x4, (%r8)
leaq 0xc(%rsp), %rcx
movl %ebp, %edi
movl $0x1, %esi
movl $0x8, %edx
callq 0x10c0
testl %eax, %eax
jns 0x1386
leaq 0xcb7(%rip), %rdi       # 0x2038
callq 0x1d40
movl 0x8(%rbx), %esi
leaq 0xc(%rsp), %r14
movl (%r14), %edx
leaq 0xcb5(%rip), %rdi       # 0x204d
xorl %eax, %eax
callq 0x1080
leaq 0x14(%rsp), %r8
movl $0x4, (%r8)
movl %ebp, %edi
movl $0x1, %esi
movl $0x7, %edx
movq %r14, %rcx
callq 0x10c0
testl %eax, %eax
jns 0x13cf
leaq 0xc9d(%rip), %rdi       # 0x2067
callq 0x1d40
movl 0x8(%rbx), %esi
movl 0xc(%rsp), %edx
leaq 0xc9f(%rip), %rdi       # 0x207c
xorl %eax, %eax
callq 0x1080
movq 0x30(%rbx), %rsi
movl %ebp, %edi
movl $0x10, %edx
callq 0x1110
testl %eax, %eax
jns 0x1404
leaq 0xc97(%rip), %rdi       # 0x2096
callq 0x1d40
movq %rbx, %r15
leaq 0x10(%rbx), %rax
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
leaq 0x30(%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 0x68(%rsp), %r12
leaq 0x10(%rsp), %r13
leaq 0x18(%rsp), %r14
movl $0x10, 0x10(%rsp)
movl $0x30, %edx
movl %ebp, %edi
leaq 0x78(%rsp), %rsi
xorl %ecx, %ecx
movq %r12, %r8
movq %r13, %r9
callq 0x10e0
movq %rax, %rbx
xorl %edi, %edi
movq %r14, %rsi
callq 0x1070
testl %ebx, %ebx
jns 0x1480
leaq 0xc20(%rip), %rdi       # 0x209b
callq 0x1d40
movzbl 0x78(%rsp), %eax
movl %eax, %ecx
shrb $0x3, %cl
andb $0x7, %cl
addb $-0x5, %cl
cmpb $-0x4, %cl
ja 0x149e
incq 0x18(%r15)
jmp 0x155a
andl $0x7, %eax
cmpl $0x3, %eax
je 0x14d2
cmpl $0x7, %eax
jne 0x1556
movl 0x8(%r15), %esi
leaq 0xbea(%rip), %rdi       # 0x20a4
xorl %eax, %eax
callq 0x1080
movl %ebp, %edi
callq 0x10b0
movl $0x1, %ebx
jmp 0x155f
movq 0x28(%rsp), %rax
incq (%rax)
movq 0xa0(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0x18(%rsp), %edi
callq 0x16db
movl %eax, 0x50(%rsp)
movl %eax, 0x40(%rsp)
movl 0x20(%rsp), %edi
callq 0x16e4
movl %eax, 0x54(%rsp)
movl %eax, 0x44(%rsp)
xorl %ebx, %ebx
xorl %edi, %edi
movq %r14, %rsi
callq 0x1070
movl 0x20(%rsp), %edi
callq 0x16e4
movl %eax, 0x5c(%rsp)
movl 0x18(%rsp), %edi
callq 0x16db
movl %eax, 0x58(%rsp)
movl $0x30, %edx
movl %ebp, %edi
leaq 0x30(%rsp), %rsi
xorl %ecx, %ecx
movq %r12, %r8
movl $0x10, %r9d
callq 0x10a0
testq %rax, %rax
jns 0x155f
incq 0x28(%r15)
jmp 0x155f
incq 0x20(%r15)
movl $0x2, %ebx
testb $0x1, %bl
je 0x1442
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq