// File: /workspace/github_c_makefile_star2_O2/ddrown[P]pretend-ntp/ntp-loop.c
// Function: main
int main(int argc, char *argv[]) {
  int sock, opt;
  struct ntp_msg bufs;
  struct sockaddr_in addr;
  struct ntptimes t;
  struct msghdr msgs;
  int status;
  uint8_t cmsgbuf[256];
  uint8_t lines = 0;
  uint32_t stop;

  if(argc < 4) {
    printf("%s [ip] [port] [stop]\n", argv[0]);
    exit(1);
  }
  if(inet_aton(argv[1], &addr.sin_addr) == INADDR_NONE) {
    printf("invalid ip %s\n", argv[1]);
    exit(1);
  }
  stop = strtoul(argv[3], NULL, 10);
  addr.sin_family = AF_INET;

  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock == -1) {
    perror_exit("socket");
  }
  opt = 1;
  if(setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt)) < 0) {
    perror_exit("setsockopt(IP_PKTINFO)");
  }
  addr.sin_port = htons(strtoul(argv[2], NULL, 10));

  memset(&msgs, 0, sizeof(msgs));
  msgs.msg_control    = cmsgbuf;
  msgs.msg_controllen = sizeof(cmsgbuf);

  for(uint32_t i = 0; i < stop; i++) {
    if(!lines) {
      printf("%20s %8s %8s %12s %12s %12s %12s %12s\n","ts","r.delay", "disper", "send", "recv", "proc", "sendto", "recvmsg");
      lines = 80;
    }
    lines--;
    ntp_request(&bufs);

    status = ntp_xchange(sock,&bufs,(struct sockaddr *)&addr,&t,&msgs,DEFAULT_TIMEOUT_MS);

    if(status > 0) {
      print_t(&t.before_sendto);
      printf(" ");

      print_short_ntp(&bufs, &t);
    }

    printf(" ");
    print_diff_ts(&t.after_sendto, &t.before_sendto);
    printf(" ");
    print_diff_ts(&t.after_recvmsg, &t.before_sendto);
    printf("\n");
    fflush(stdout);
    sleep(1);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rsi, %r14
cmpl $0x3, %edi
jle 0x141b
movq 0x8(%r14), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1090
cmpl $-0x1, %eax
je 0x1427
movq 0x18(%r14), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x10e0
movq %rax, %rbx
movw $0x2, 0xc(%rsp)
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1110
movl %eax, %ebp
cmpl $-0x1, %eax
jne 0x1287
leaq 0xda8(%rip), %rdi       # 0x202a
callq 0x1a9c
leaq 0x1c(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x8
popq %rdx
pushq $0x4
popq %r8
movl %ebp, %edi
xorl %esi, %esi
callq 0x1050
testl %eax, %eax
jns 0x12b2
leaq 0xd84(%rip), %rdi       # 0x2031
callq 0x1a9c
movq 0x10(%r14), %rdi
xorl %r12d, %r12d
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x10e0
rolw $0x8, %ax
leaq 0xc(%rsp), %rcx
movw %ax, 0x2(%rcx)
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rcx
movaps %xmm0, 0x20(%rcx)
movaps %xmm0, (%rcx)
andq $0x0, 0x30(%rcx)
movaps %xmm0, 0x10(%rcx)
leaq 0xc0(%rsp), %rax
movq %rax, 0x20(%rcx)
movq $0x100, 0x28(%rcx)      # imm = 0x100
leaq 0x90(%rsp), %r15
leaq 0x20(%rsp), %r13
pushq $0x20
popq %r14
subl $0x1, %ebx
jb 0x1407
testb %r12b, %r12b
jne 0x1372
subq $0x8, %rsp
leaq 0xd22(%rip), %rdi       # 0x2048
leaq 0xd42(%rip), %rsi       # 0x206f
leaq 0xd3e(%rip), %rdx       # 0x2072
leaq 0xd3f(%rip), %rcx       # 0x207a
leaq 0xd3f(%rip), %r8        # 0x2081
leaq 0xd3d(%rip), %r9        # 0x2086
xorl %eax, %eax
leaq 0xd45(%rip), %r10       # 0x2097
pushq %r10
leaq 0xd35(%rip), %r10       # 0x2090
pushq %r10
leaq 0xd27(%rip), %r10       # 0x208b
pushq %r10
callq 0x1070
addq $0x20, %rsp
movb $0x50, %r12b
movq %r15, %rdi
callq 0x181a
movl %ebp, %edi
movq %r15, %rsi
leaq 0xc(%rsp), %rdx
movq %r13, %rcx
leaq 0x50(%rsp), %r8
movl $0x1f4, %r9d            # imm = 0x1F4
callq 0x1870
testl %eax, %eax
jle 0x13b6
movq %r13, %rdi
callq 0x19b4
movl %r14d, %edi
callq 0x1030
movq %r15, %rdi
movq %r13, %rsi
callq 0x1700
decb %r12b
movl %r14d, %edi
callq 0x1030
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x1a3b
movl %r14d, %edi
callq 0x1030
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x1a3b
pushq $0xa
popq %rdi
callq 0x1030
movq 0x2bee(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x10a0
pushq $0x1
popq %rdi
callq 0x1100
jmp 0x130d
xorl %eax, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rsi
leaq 0xbdf(%rip), %rdi       # 0x2004
jmp 0x1432
movq 0x8(%r14), %rsi
leaq 0xbe9(%rip), %rdi       # 0x201b
xorl %eax, %eax
callq 0x1070
pushq $0x1
popq %rdi
callq 0x10f0
nopl (%rax)