// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x10(%rax)
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1190
movl %eax, -0x74(%rbp)
cmpl $0x0, %eax
jge 0x1309
leaq 0x1d00(%rip), %rdi      # 0x3004
callq 0x21f0
movl $0x1, -0x78(%rbp)
movl -0x74(%rbp), %edi
movl $0x1, %esi
movl $0xf, %edx
leaq -0x78(%rbp), %rcx
movl $0x4, %r8d
callq 0x1040
cmpl $0x0, %eax
jge 0x133d
leaq 0x1cd3(%rip), %rdi      # 0x300b
callq 0x21f0
movl $0x3d09, -0x78(%rbp)    # imm = 0x3D09
movl -0x74(%rbp), %edi
movl $0x1, %esi
movl $0x8, %edx
leaq -0x78(%rbp), %rcx
movl $0x4, %r8d
callq 0x1040
cmpl $0x0, %eax
jge 0x1371
leaq 0x1cb7(%rip), %rdi      # 0x3023
callq 0x21f0
movl $0x3d09, -0x78(%rbp)    # imm = 0x3D09
movl -0x74(%rbp), %edi
movl $0x1, %esi
movl $0x7, %edx
leaq -0x78(%rbp), %rcx
movl $0x4, %r8d
callq 0x1040
cmpl $0x0, %eax
jge 0x13a5
leaq 0x1c83(%rip), %rdi      # 0x3023
callq 0x21f0
movl $0x4, -0x7c(%rbp)
movl -0x74(%rbp), %edi
movl $0x1, %esi
movl $0x8, %edx
leaq -0x78(%rbp), %rcx
leaq -0x7c(%rbp), %r8
callq 0x10c0
cmpl $0x0, %eax
jge 0x13d7
leaq 0x1c66(%rip), %rdi      # 0x3038
callq 0x21f0
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
movl -0x78(%rbp), %edx
leaq 0x1c65(%rip), %rdi      # 0x304d
movb $0x0, %al
callq 0x1070
movl $0x4, -0x7c(%rbp)
movl -0x74(%rbp), %edi
movl $0x1, %esi
movl $0x7, %edx
leaq -0x78(%rbp), %rcx
leaq -0x7c(%rbp), %r8
callq 0x10c0
cmpl $0x0, %eax
jge 0x1421
leaq 0x1c4b(%rip), %rdi      # 0x3067
callq 0x21f0
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
movl -0x78(%rbp), %edx
leaq 0x1c4a(%rip), %rdi      # 0x307c
movb $0x0, %al
callq 0x1070
movl -0x74(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rsi
movl $0x10, %edx
callq 0x1120
cmpl $0x0, %eax
jge 0x146d
leaq 0x1c2e(%rip), %rdi      # 0x3096
callq 0x21f0
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x1090
movb -0x70(%rbp), %al
andb $-0x39, %al
orb $0x20, %al
movb %al, -0x70(%rbp)
movb -0x70(%rbp), %al
andb $-0x8, %al
orb $0x4, %al
movb %al, -0x70(%rbp)
movb $0x1, -0x6f(%rbp)
movl $0x50505300, %edi       # imm = 0x50505300
callq 0x1080
movl %eax, -0x64(%rbp)
movb $-0x14, -0x6d(%rbp)
movl $0x10, -0xa0(%rbp)
movl -0x74(%rbp), %edi
leaq -0x9c(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %r8
leaq -0x40(%rbp), %rsi
movl $0x30, %edx
xorl %ecx, %ecx
leaq -0xa0(%rbp), %r9
callq 0x10e0
movl %eax, -0x8c(%rbp)
xorl %edi, %edi
leaq -0xb0(%rbp), %rsi
callq 0x1050
cmpl $0x0, -0x8c(%rbp)
jge 0x1508
leaq 0x1b98(%rip), %rdi      # 0x309b
callq 0x21f0
movb -0x40(%rbp), %al
shrb $0x3, %al
andb $0x7, %al
movzbl %al, %eax
cmpl $0x2, %eax
jl 0x1528
movb -0x40(%rbp), %al
shrb $0x3, %al
andb $0x7, %al
movzbl %al, %eax
cmpl $0x4, %eax
jle 0x153d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x18(%rax)
jmp 0x14a6
movb -0x40(%rbp), %al
andb $0x7, %al
movzbl %al, %eax
cmpl $0x7, %eax
jne 0x1572
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
leaq 0x1b4c(%rip), %rdi      # 0x30a4
movb $0x0, %al
callq 0x1070
movl -0x74(%rbp), %edi
callq 0x10b0
xorl %eax, %eax
addq $0xc0, %rsp
popq %rbp
retq
movb -0x40(%rbp), %al
andb $0x7, %al
movzbl %al, %eax
cmpl $0x3, %eax
je 0x1594
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20(%rax)
jmp 0x14a6
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movl -0x18(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0xb0(%rbp), %rax
movl %eax, %edi
callq 0x1970
movl %eax, -0x50(%rbp)
movl %eax, -0x60(%rbp)
movq -0xa8(%rbp), %rax
movl %eax, %edi
callq 0x19a0
movl %eax, -0x4c(%rbp)
movl %eax, -0x5c(%rbp)
xorl %edi, %edi
leaq -0xb0(%rbp), %rsi
callq 0x1050
movq -0xa8(%rbp), %rax
movl %eax, %edi
callq 0x19a0
movl %eax, -0x44(%rbp)
movq -0xb0(%rbp), %rax
movl %eax, %edi
callq 0x1970
movl %eax, -0x48(%rbp)
movl -0x74(%rbp), %edi
leaq -0x9c(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %r8
leaq -0x70(%rbp), %rsi
movl $0x30, %edx
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x10a0
cmpq $0x0, %rax
jge 0x164c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rax)
jmp 0x14a6
nopw %cs:(%rax,%rax)