// File: /workspace/github_c_makefile_star2_O2/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)
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1170
movl %eax, %ebx
testl %eax, %eax
jns 0x12b9
leaq 0xd50(%rip), %rdi       # 0x2004
callq 0x1d14
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0xf
popq %rdx
pushq $0x4
popq %r8
movl %ebx, %edi
callq 0x1060
testl %eax, %eax
jns 0x12e5
leaq 0xd2b(%rip), %rdi       # 0x200b
callq 0x1d14
leaq 0x4(%rsp), %rcx
movl $0x3d09, (%rcx)         # imm = 0x3D09
pushq $0x1
popq %rsi
pushq $0x8
popq %rdx
pushq $0x4
popq %r8
movl %ebx, %edi
callq 0x1060
testl %eax, %eax
jns 0x1311
leaq 0xd17(%rip), %rdi       # 0x2023
callq 0x1d14
leaq 0x4(%rsp), %rcx
movl $0x3d09, (%rcx)         # imm = 0x3D09
pushq $0x1
popq %rsi
pushq $0x7
popq %rdx
pushq $0x4
popq %r8
movl %ebx, %edi
callq 0x1060
testl %eax, %eax
jns 0x133d
leaq 0xceb(%rip), %rdi       # 0x2023
callq 0x1d14
leaq 0xc(%rsp), %r8
movl $0x4, (%r8)
pushq $0x1
popq %rsi
pushq $0x8
popq %rdx
leaq 0x4(%rsp), %rcx
movl %ebx, %edi
callq 0x10c0
testl %eax, %eax
jns 0x136b
leaq 0xcd2(%rip), %rdi       # 0x2038
callq 0x1d14
movl 0x8(%r15), %esi
leaq 0x4(%rsp), %r14
movl (%r14), %edx
leaq 0xccf(%rip), %rdi       # 0x204d
xorl %eax, %eax
callq 0x1080
leaq 0xc(%rsp), %r8
movl $0x4, (%r8)
pushq $0x1
popq %rsi
pushq $0x7
popq %rdx
movl %ebx, %edi
movq %r14, %rcx
callq 0x10c0
testl %eax, %eax
jns 0x13b1
leaq 0xcbb(%rip), %rdi       # 0x2067
callq 0x1d14
movl 0x8(%r15), %esi
movl 0x4(%rsp), %edx
leaq 0xcbc(%rip), %rdi       # 0x207c
xorl %eax, %eax
callq 0x1080
movq 0x30(%r15), %rsi
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x1110
testl %eax, %eax
jns 0x13e5
leaq 0xcb6(%rip), %rdi       # 0x2096
callq 0x1d14
leaq 0x28(%r15), %rax
movq %rax, 0x30(%rsp)
leaq 0x20(%r15), %rax
movq %rax, 0x38(%rsp)
leaq 0x18(%r15), %r14
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)
pushq $0x30
popq %r13
leaq 0x78(%rsp), %rbp
leaq 0x10(%rsp), %r15
movl $0x10, 0x8(%rsp)
movl %ebx, %edi
leaq 0x88(%rsp), %rsi
movq %r13, %rdx
xorl %ecx, %ecx
movq %rbp, %r8
leaq 0x8(%rsp), %r9
callq 0x10e0
movq %rax, %r12
xorl %edi, %edi
movq %r15, %rsi
callq 0x1070
testl %r12d, %r12d
jns 0x147c
leaq 0xc24(%rip), %rdi       # 0x209b
callq 0x1d14
movzbl 0x88(%rsp), %eax
movl %eax, %edx
shrb $0x3, %dl
andb $0x7, %dl
addb $-0x5, %dl
movq %r14, %rcx
cmpb $-0x3, %dl
jb 0x1537
andl $0x7, %eax
cmpl $0x3, %eax
je 0x14b6
movq 0x38(%rsp), %rcx
cmpl $0x7, %eax
jne 0x1537
jmp 0x153f
movq 0x28(%rsp), %rax
incq (%rax)
movq 0xb0(%rsp), %rax
movq %rax, 0x58(%rsp)
movl 0x10(%rsp), %edi
callq 0x16cb
movl %eax, 0x60(%rsp)
movl %eax, 0x50(%rsp)
movl 0x18(%rsp), %edi
callq 0x16d4
movl %eax, 0x64(%rsp)
movl %eax, 0x54(%rsp)
xorl %edi, %edi
movq %r15, %rsi
callq 0x1070
movl 0x18(%rsp), %edi
callq 0x16d4
movl %eax, 0x6c(%rsp)
movl 0x10(%rsp), %edi
callq 0x16cb
movl %eax, 0x68(%rsp)
movl %ebx, %edi
leaq 0x40(%rsp), %rsi
movq %r13, %rdx
xorl %ecx, %ecx
movq %rbp, %r8
pushq $0x10
popq %r9
callq 0x10a0
movq 0x30(%rsp), %rcx
testq %rax, %rax
jns 0x143a
incq (%rcx)
jmp 0x143a
movq 0x20(%rsp), %rax
movl 0x8(%rax), %esi
leaq 0xb56(%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