// File: /workspace/github_c_makefile_star2_O0/ddrown[P]pretend-ntp/ntp-query.c
// Function: main
int main(int argc, char *argv[]) {
  struct ntp_msg bufs;
  struct timespec t[3];
  int status[6];
  int sock, opt;
  struct sockaddr_in addr;
  struct msghdr msg;
  struct iovec iovec;
  uint8_t cmsgbuf[256];
  struct pollfd readfd;

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

  memset(&bufs, 0, sizeof(bufs));
  bufs.version = NTP_VERS_4;
  bufs.mode = NTP_MODE_CLIENT;
  clock_gettime(CLOCK_REALTIME, &t[0]);
  bufs.trans_time = unixtime_to_ntp_s(t[0].tv_sec);
  bufs.trans_time_fb = ns_to_ntp_frac(t[0].tv_nsec);
  printf("=== request ===\n");
  print_ntp(&bufs, NULL, NULL);

  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)");
  }

  memset(&msg, 0, sizeof(msg));
  iovec.iov_base          = &bufs;
  iovec.iov_len           = sizeof(bufs);
  msg.msg_iov     = &iovec;
  msg.msg_iovlen  = 1;
  msg.msg_name    = &addr;
  msg.msg_namelen = sizeof(addr);
  msg.msg_control = cmsgbuf;
  msg.msg_controllen = sizeof(cmsgbuf);

  readfd.fd = sock;
  readfd.events = POLLIN;

  status[0] = clock_gettime(CLOCK_REALTIME, &t[0]);
  status[1] = sendto(sock, &bufs, sizeof(bufs), 0, &addr, sizeof(addr));
  status[2] = clock_gettime(CLOCK_REALTIME, &t[1]);
  status[3] = poll(&readfd, 1, DEFAULT_TIMEOUT_MS);
  status[4] = clock_gettime(CLOCK_REALTIME, &t[2]);
  status[5] = recvmsg(sock, &msg, MSG_DONTWAIT);
  if((status[5] < 0) && (errno != EAGAIN)) {
    perror_exit("recvmsg");
  }
  if(status[4] != 0) {
    perror_exit("clock_gettime #3");
  }
  if(status[3] < 0) {
    perror_exit("poll");
  }
  if(status[2] != 0) {
    perror_exit("clock_gettime #2");
  }
  if(status[1] < 0) {
    perror_exit("sendto");
  }
  if(status[0] != 0) {
    perror_exit("clock_gettime #1");
  }

  printf("=== %d packet(s) received ===\n=== response ===\n", status[3]);
  if(status[5] > 0) {
    printf("rtt = ");
    print_diff_ts(&t[2], &t[1]);
    printf("\n");

    printf("source = ");
    print_addr(&addr);
    for(struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
      if(cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO) {
        print_pktinfo((struct in_pktinfo *)CMSG_DATA(cmsg));
      }
    }
    printf("len = %u\n", status[5]);
    print_ntp(&bufs, &t[1], &t[2]);
  }

  printf("before sendto = ");
  print_t(&t[0]);
  printf("\n");
  printf("after sendto = ");
  print_t(&t[1]);
  printf(" ");
  print_diff_ts(&t[1], &t[0]);
  printf("\n");
  printf("after recvmmsg = ");
  print_t(&t[2]);
  printf(" ");
  print_diff_ts(&t[2], &t[1]);
  printf("\n");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
jge 0x134e
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x1ce9(%rip), %rdi      # 0x3026
movb $0x0, %al
callq 0x1080
movl $0x1, %edi
callq 0x1120
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0xa8(%rbp), %rsi
addq $0x4, %rsi
callq 0x10c0
cmpl $-0x1, %eax
jne 0x138b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x1cbc(%rip), %rdi      # 0x3036
movb $0x0, %al
callq 0x1080
movl $0x1, %edi
callq 0x1120
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x1110
movzwl %ax, %edi
callq 0x1070
movw %ax, -0xa6(%rbp)
movw $0x2, -0xa8(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x10a0
movb -0x40(%rbp), %al
andb $-0x39, %al
orb $0x20, %al
movb %al, -0x40(%rbp)
movb -0x40(%rbp), %al
andb $-0x8, %al
orb $0x3, %al
movb %al, -0x40(%rbp)
leaq -0x70(%rbp), %rsi
xorl %edi, %edi
callq 0x1050
movq -0x70(%rbp), %rax
movl %eax, %edi
callq 0x1860
movl %eax, -0x18(%rbp)
movq -0x68(%rbp), %rax
movl %eax, %edi
callq 0x1890
movl %eax, -0x14(%rbp)
leaq 0x1c3a(%rip), %rdi      # 0x3045
movb $0x0, %al
callq 0x1080
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x1950
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1150
movl %eax, -0x94(%rbp)
cmpl $-0x1, -0x94(%rbp)
jne 0x144b
leaq 0x1c10(%rip), %rdi      # 0x3056
callq 0x20e0
movl $0x1, -0x98(%rbp)
movl -0x94(%rbp), %edi
xorl %esi, %esi
movl $0x8, %edx
leaq -0x98(%rbp), %rcx
movl $0x4, %r8d
callq 0x1040
cmpl $0x0, %eax
jge 0x1485
leaq 0x1bdd(%rip), %rdi      # 0x305d
callq 0x20e0
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x10a0
leaq -0x40(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq $0x30, -0xe8(%rbp)
leaq -0xf0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq $0x1, -0xc8(%rbp)
leaq -0xa8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl $0x10, -0xd8(%rbp)
leaq -0x1f0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq $0x100, -0xb8(%rbp) # imm = 0x100
movl -0x94(%rbp), %eax
movl %eax, -0x1f8(%rbp)
movw $0x1, -0x1f4(%rbp)
leaq -0x70(%rbp), %rsi
xorl %edi, %edi
callq 0x1050
movl %eax, -0x90(%rbp)
movl -0x94(%rbp), %edi
leaq -0xa8(%rbp), %rax
movq %rax, -0x200(%rbp)
movq -0x200(%rbp), %r8
leaq -0x40(%rbp), %rsi
movl $0x30, %edx
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x10b0
movl %eax, -0x8c(%rbp)
leaq -0x70(%rbp), %rsi
addq $0x10, %rsi
xorl %edi, %edi
callq 0x1050
movl %eax, -0x88(%rbp)
leaq -0x1f8(%rbp), %rdi
movl $0x1, %esi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x10f0
movl %eax, -0x84(%rbp)
leaq -0x70(%rbp), %rsi
addq $0x20, %rsi
xorl %edi, %edi
callq 0x1050
movl %eax, -0x80(%rbp)
movl -0x94(%rbp), %edi
leaq -0xe0(%rbp), %rsi
movl $0x40, %edx
callq 0x10d0
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jge 0x15ce
callq 0x1030
cmpl $0xb, (%rax)
je 0x15ce
leaq 0x1aab(%rip), %rdi      # 0x3074
callq 0x20e0
cmpl $0x0, -0x80(%rbp)
je 0x15e0
leaq 0x1aa1(%rip), %rdi      # 0x307c
callq 0x20e0
cmpl $0x0, -0x84(%rbp)
jge 0x15f5
leaq 0x1a9d(%rip), %rdi      # 0x308d
callq 0x20e0
cmpl $0x0, -0x88(%rbp)
je 0x160a
leaq 0x1a8d(%rip), %rdi      # 0x3092
callq 0x20e0
cmpl $0x0, -0x8c(%rbp)
jge 0x161f
leaq 0x1a89(%rip), %rdi      # 0x30a3
callq 0x20e0
cmpl $0x0, -0x90(%rbp)
je 0x1634
leaq 0x1a7b(%rip), %rdi      # 0x30aa
callq 0x20e0
movl -0x84(%rbp), %esi
leaq 0x1a7a(%rip), %rdi      # 0x30bb
movb $0x0, %al
callq 0x1080
cmpl $0x0, -0x7c(%rbp)
jle 0x174c
leaq 0x1a92(%rip), %rdi      # 0x30eb
movb $0x0, %al
callq 0x1080
leaq -0x70(%rbp), %rdi
addq $0x20, %rdi
leaq -0x70(%rbp), %rsi
addq $0x10, %rsi
callq 0x2050
leaq 0x1a6d(%rip), %rdi      # 0x30e9
movb $0x0, %al
callq 0x1080
leaq 0x1a68(%rip), %rdi      # 0x30f2
movb $0x0, %al
callq 0x1080
leaq -0xa8(%rbp), %rdi
callq 0x1260
cmpq $0x10, -0xb8(%rbp)
jb 0x16b7
movq -0xc0(%rbp), %rax
movq %rax, -0x210(%rbp)
jmp 0x16c2
xorl %eax, %eax
movq %rax, -0x210(%rbp)
jmp 0x16c2
movq -0x210(%rbp), %rax
movq %rax, -0x208(%rbp)
cmpq $0x0, -0x208(%rbp)
je 0x1722
movq -0x208(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x1704
movq -0x208(%rbp), %rax
cmpl $0x8, 0xc(%rax)
jne 0x1704
movq -0x208(%rbp), %rdi
addq $0x10, %rdi
callq 0x12b0
jmp 0x1706
movq -0x208(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x1140
movq %rax, -0x208(%rbp)
jmp 0x16d0
movl -0x7c(%rbp), %esi
leaq 0x19d0(%rip), %rdi      # 0x30fc
movb $0x0, %al
callq 0x1080
leaq -0x70(%rbp), %rsi
addq $0x10, %rsi
leaq -0x70(%rbp), %rdx
addq $0x20, %rdx
leaq -0x40(%rbp), %rdi
callq 0x1950
leaq 0x19b3(%rip), %rdi      # 0x3106
movb $0x0, %al
callq 0x1080
leaq -0x70(%rbp), %rdi
callq 0x1f60
leaq 0x197f(%rip), %rdi      # 0x30e9
movb $0x0, %al
callq 0x1080
leaq 0x199f(%rip), %rdi      # 0x3117
movb $0x0, %al
callq 0x1080
leaq -0x70(%rbp), %rdi
addq $0x10, %rdi
callq 0x1f60
leaq 0x1967(%rip), %rdi      # 0x30fa
movb $0x0, %al
callq 0x1080
leaq -0x70(%rbp), %rdi
addq $0x10, %rdi
leaq -0x70(%rbp), %rsi
callq 0x2050
leaq 0x1937(%rip), %rdi      # 0x30e9
movb $0x0, %al
callq 0x1080
leaq 0x1967(%rip), %rdi      # 0x3127
movb $0x0, %al
callq 0x1080
leaq -0x70(%rbp), %rdi
addq $0x20, %rdi
callq 0x1f60
leaq 0x191f(%rip), %rdi      # 0x30fa
movb $0x0, %al
callq 0x1080
leaq -0x70(%rbp), %rdi
addq $0x20, %rdi
leaq -0x70(%rbp), %rsi
addq $0x10, %rsi
callq 0x2050
leaq 0x18eb(%rip), %rdi      # 0x30e9
movb $0x0, %al
callq 0x1080
movl -0x4(%rbp), %eax
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)