#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct timeval {
  int member_0;
  int tv_sec;
  double tv_usec;
  int /*<<< orphan*/ member_1;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  void *sin_port;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr_list;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ remaddr;
typedef int /*<<< orphan*/ myaddr;
typedef int /*<<< orphan*/ host_to_contact;

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
void *htons(int);
scalar_t__ inet_aton(char *, TYPE_1__ *);
int /*<<< orphan*/ inet_ntoa(struct in_addr);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  struct sockaddr_in myaddr;
  struct sockaddr_in remaddr;
  socklen_t slen = sizeof(remaddr);
  int fd;
  int port;
  char buf[100];
  int times;
  char host_to_contact[50];
  struct hostent *hp, *gethostbyname();
  char ip[100];
  struct hostent *he;
  struct in_addr **addr_list;
  struct timeval starttime, endtime;
  struct timeval timeout = {2, 0};
  int i;
  if (argc == 4) {
    strncpy(host_to_contact, argv[1], sizeof(host_to_contact));
    port = atoi(argv[2]);
    times = atoi(argv[3]);
  } else {
    printf("Usage: %s <host name> <port number> <number of messages>\n",
           argv[0]);
    exit(1);
  }
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("cannot create socket\n");
    exit(1);
  }
  he = gethostbyname(host_to_contact);
  addr_list = (struct in_addr **)he->h_addr_list;
  for (i = 0; addr_list[i] != NULL; i++) {
    strcpy(ip, inet_ntoa(*addr_list[i]));
  }
  memset((char *)&myaddr, 0, sizeof(myaddr));
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  myaddr.sin_port = htons(0);
  if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
    perror("bind failed");
    return 0;
  }
  memset((char *)&remaddr, 0, sizeof(remaddr));
  remaddr.sin_family = AF_INET;
  remaddr.sin_port = htons(port);
  if (inet_aton(ip, &remaddr.sin_addr) == 0) {
    fprintf(stderr, "inet_aton() failed\n");
    exit(1);
  }
  setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
             sizeof(struct timeval));
  for (i = 0; i < times; i++) {
    char send_buf[] = "ping";
    if (sendto(fd, send_buf, strlen(buf), 0, (struct sockaddr *)&remaddr,
               slen) == -1) {
      perror("sendto");
      printf("\n---------------\n");
      continue;
    }
    gettimeofday(&starttime, 0);
    printf("Ping packet send to %s port %d\n", host_to_contact, port);
    int recvlen = recvfrom(fd, buf, 100, 0, (struct sockaddr *)&remaddr, &slen);
    if (recvlen >= 0) {
      buf[recvlen] = 0;
      printf("\nPong Message Received\n");
      gettimeofday(&endtime, 0);
      double timeuse = 1000000 * (endtime.tv_sec - starttime.tv_sec) +
                       endtime.tv_usec - starttime.tv_usec;
      timeuse /= 1000;
      printf("RTT: %.3f Seconds\n", timeuse);
    } else {
      printf("\nMessage Receive Timeout or Error\n");
      printf("\n---------------\n");
      continue;
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x18,-0x44(%rbp)
mov    0xd91(%rip),%rax
mov    %rax,-0x1b8(%rbp)
mov    0xd8b(%rip),%rax
mov    %rax,-0x1b0(%rbp)
mov    0xd85(%rip),%rax
mov    %rax,-0x1a8(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    12e5 <main+0x95>
lea    -0xf0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x32,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1150 <atoi@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1150 <atoi@plt>
mov    %eax,-0xb4(%rbp)
jmp    1304 <main+0xb4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    $0x1,%edi
call   1160 <exit@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%rdi
mov    0x2cbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1170 <socket@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
jge    1340 <main+0xf0>
lea    0xd29(%rip),%rdi
call   1140 <perror@plt>
mov    $0x1,%edi
call   1160 <exit@plt>
lea    -0xf0(%rbp),%rdi
mov    $0x0,%al
call   10f0 <gethostbyname@plt>
mov    %rax,-0x168(%rbp)
mov    -0x168(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x170(%rbp)
movl   $0x0,-0x1bc(%rbp)
mov    -0x170(%rbp),%rax
movslq -0x1bc(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     13d2 <main+0x182>
lea    -0x160(%rbp),%rax
mov    %rax,-0x1d8(%rbp)
mov    -0x170(%rbp),%rax
movslq -0x1bc(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1060 <inet_ntoa@plt>
mov    -0x1d8(%rbp),%rdi
mov    %eax,%esi
call   1040 <strcpy@plt>
mov    -0x1bc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1bc(%rbp)
jmp    1370 <main+0x120>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   10c0 <memset@plt>
mov    0x2bf7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2be1(%rip),%rax
mov    (%rax),%edi
call   10b0 <htonl@plt>
mov    %eax,-0x28(%rbp)
xor    %edi,%edi
call   1080 <htons@plt>
mov    %rax,-0x20(%rbp)
mov    -0x48(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1130 <bind@plt>
cmp    $0x0,%rax
jge    143f <main+0x1ef>
lea    0xc42(%rip),%rdi
call   1140 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1696 <main+0x446>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   10c0 <memset@plt>
mov    0x2b8a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x4c(%rbp),%edi
call   1080 <htons@plt>
mov    %rax,-0x38(%rbp)
lea    -0x160(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1110 <inet_aton@plt>
cmp    $0x0,%rax
jne    14a2 <main+0x252>
mov    0x2b2e(%rip),%rax
mov    (%rax),%edi
lea    0xbe9(%rip),%rsi
call   1100 <fprintf@plt>
mov    $0x1,%edi
call   1160 <exit@plt>
mov    -0x48(%rbp),%edi
mov    0x2b3c(%rip),%rax
mov    (%rax),%esi
mov    0x2b0b(%rip),%rax
mov    (%rax),%edx
lea    -0x1b8(%rbp),%rcx
mov    $0x18,%r8d
call   1050 <setsockopt@plt>
movl   $0x0,-0x1bc(%rbp)
mov    -0x1bc(%rbp),%eax
cmp    -0xb4(%rbp),%eax
jge    1687 <main+0x437>
mov    0xba5(%rip),%eax
mov    %eax,-0x1c1(%rbp)
mov    0xb9d(%rip),%al
mov    %al,-0x1bd(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x1e4(%rbp)
lea    -0x1c1(%rbp),%rax
mov    %rax,-0x1e0(%rbp)
lea    -0xb0(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x1e4(%rbp),%edi
mov    -0x1e0(%rbp),%rsi
mov    %eax,%edx
lea    -0x40(%rbp),%r8
mov    -0x44(%rbp),%r9d
xor    %ecx,%ecx
call   10d0 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1566 <main+0x316>
lea    0xb47(%rip),%rdi
call   1140 <perror@plt>
lea    0xb42(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    1673 <main+0x423>
lea    -0x188(%rbp),%rdi
xor    %esi,%esi
call   10a0 <gettimeofday@plt>
lea    -0xf0(%rbp),%rsi
mov    -0x4c(%rbp),%edx
lea    0xb29(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x48(%rbp),%edi
lea    -0xb0(%rbp),%rsi
lea    -0x40(%rbp),%r8
mov    $0x64,%edx
xor    %ecx,%ecx
lea    -0x44(%rbp),%r9
call   1120 <recvfrom@plt>
mov    %eax,-0x1c8(%rbp)
cmpl   $0x0,-0x1c8(%rbp)
jl     164d <main+0x3fd>
movslq -0x1c8(%rbp),%rax
movb   $0x0,-0xb0(%rbp,%rax,1)
lea    0xafb(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    -0x1a0(%rbp),%rdi
xor    %esi,%esi
call   10a0 <gettimeofday@plt>
mov    -0x19c(%rbp),%eax
sub    -0x184(%rbp),%eax
imul   $0xf4240,%eax,%eax
cvtsi2sd %eax,%xmm0
addsd  -0x198(%rbp),%xmm0
ff
subsd  -0x180(%rbp),%xmm0
ff
movsd  %xmm0,-0x1d0(%rbp)
ff
movsd  -0x1d0(%rbp),%xmm0
ff
movsd  0x9da(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x1d0(%rbp)
ff
movsd  -0x1d0(%rbp),%xmm0
ff
lea    0xaa5(%rip),%rdi
mov    $0x1,%al
call   1090 <printf@plt>
jmp    166e <main+0x41e>
lea    0xaa5(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    0xa3a(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    1673 <main+0x423>
jmp    1673 <main+0x423>
mov    -0x1bc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1bc(%rbp)
jmp    14d3 <main+0x283>
mov    -0x48(%rbp),%edi
call   10e0 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1a8,%rsp
mov    %rsi,%rbx
movl   $0x18,0x4(%rsp)
mov    0xd95(%rip),%rax
mov    %rax,0x50(%rsp)
movupd 0xd78(%rip),%xmm0
movapd %xmm0,0x40(%rsp)
cmp    $0x4,%edi
jne    1556 <main+0x2f6>
mov    0x8(%rbx),%rsi
lea    0x90(%rsp),%rdi
mov    $0x32,%edx
call   1030 <strncpy@plt>
mov    0x10(%rbx),%rdi
call   1160 <atoi@plt>
mov    %eax,(%rsp)
mov    0x18(%rbx),%rdi
call   1160 <atoi@plt>
mov    %eax,%ebp
mov    0x2d0d(%rip),%r14
mov    (%r14),%rdi
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1180 <socket@plt>
test   %eax,%eax
js     1571 <main+0x311>
mov    %eax,%r12d
lea    0x90(%rsp),%rdi
xor    %eax,%eax
call   1100 <gethostbyname@plt>
mov    (%rax),%rbx
mov    (%rbx),%rax
test   %rax,%rax
je     133d <main+0xdd>
add    $0x8,%rbx
lea    0xd0(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    (%rax),%edi
call   1070 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1040 <strcpy@plt>
mov    (%rbx),%rax
add    $0x8,%rbx
test   %rax,%rax
jne    1320 <main+0xc0>
lea    0x28(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   10d0 <memset@plt>
mov    (%r14),%rax
mov    %rax,0x38(%rsp)
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
call   10c0 <htonl@plt>
mov    %eax,0x28(%rsp)
xor    %edi,%edi
call   1090 <htons@plt>
mov    %rax,0x30(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x18,%edx
call   1140 <bind@plt>
test   %rax,%rax
js     1548 <main+0x2e8>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   10d0 <memset@plt>
mov    (%r14),%rax
mov    %rax,0x20(%rsp)
mov    (%rsp),%edi
call   1090 <htons@plt>
mov    %rax,0x18(%rsp)
lea    0xd0(%rsp),%rdi
mov    %rbx,%rsi
call   1120 <inet_aton@plt>
test   %rax,%rax
je     1587 <main+0x327>
mov    0x2c0f(%rip),%rax
mov    (%rax),%esi
mov    0x2bde(%rip),%rax
mov    (%rax),%edx
lea    0x40(%rsp),%rcx
mov    %r12d,%edi
mov    $0x18,%r8d
call   1060 <setsockopt@plt>
test   %ebp,%ebp
jle    152c <main+0x2cc>
lea    0x140(%rsp),%r15
lea    0x8(%rsp),%rbx
lea    0x10(%rsp),%r13
lea    0x4(%rsp),%r14
jmp    1485 <main+0x225>
nopl   0x0(%rax,%rax,1)
mov    %eax,%eax
movb   $0x0,0x140(%rsp,%rax,1)
lea    0xcc0(%rip),%rdi
call   1050 <puts@plt>
lea    0x60(%rsp),%rdi
xor    %esi,%esi
call   10b0 <gettimeofday@plt>
mov    0x64(%rsp),%eax
sub    0x7c(%rsp),%eax
imul   $0xf4240,%eax,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
addsd  0x68(%rsp),%xmm0
subsd  0x80(%rsp),%xmm0
divsd  0xb92(%rip),%xmm0
lea    0xc47(%rip),%rdi
mov    $0x1,%al
call   10a0 <printf@plt>
add    $0xffffffff,%ebp
je     152c <main+0x2cc>
movb   $0x0,0xc(%rsp)
movl   $0x676e6970,0x8(%rsp)
67
mov    %r15,%rdi
call   1080 <strlen@plt>
mov    0x4(%rsp),%r9d
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   10e0 <sendto@plt>
cmp    $0xffffffff,%eax
je     151e <main+0x2be>
lea    0x78(%rsp),%rdi
xor    %esi,%esi
call   10b0 <gettimeofday@plt>
lea    0xbd3(%rip),%rdi
lea    0x90(%rsp),%rsi
mov    (%rsp),%edx
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    %r14,%r9
call   1130 <recvfrom@plt>
test   %eax,%eax
jns    1420 <main+0x1c0>
lea    0xbcd(%rip),%rdi
call   1050 <puts@plt>
lea    0xbfa(%rip),%rdi
call   1050 <puts@plt>
add    $0xffffffff,%ebp
jne    1485 <main+0x225>
jmp    152c <main+0x2cc>
lea    0xb70(%rip),%rdi
call   1150 <perror@plt>
jmp    1507 <main+0x2a7>
mov    %r12d,%edi
call   10f0 <close@plt>
xor    %eax,%eax
add    $0x1a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb21(%rip),%rdi
call   1150 <perror@plt>
jmp    1534 <main+0x2d4>
mov    (%rbx),%rsi
lea    0xac0(%rip),%rdi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    $0x1,%edi
call   1170 <exit@plt>
lea    0xae2(%rip),%rdi
call   1150 <perror@plt>
mov    $0x1,%edi
call   1170 <exit@plt>
mov    0x2a2a(%rip),%rax
mov    (%rax),%edi
lea    0xae5(%rip),%rsi
call   1110 <fprintf@plt>
mov    $0x1,%edi
call   1170 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32, double, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.timeout = %struct.timeval { i32 2, i32 0, double 0.000000e+00, i32 0 }
@.str = [58 x i8] c"Usage: %s <host name> <port number> <number of messages>\0A\00"
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@.str.1 = [22 x i8] c"cannot create socket\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [12 x i8] c"bind failed\00"
@stderr = global i32 0
@.str.3 = [20 x i8] c"inet_aton() failed\0A\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@__const.main.send_buf = [5 x i8] c"ping\00"
@.str.4 = [7 x i8] c"sendto\00"
@.str.5 = [18 x i8] c"\0A---------------\0A\00"
@.str.6 = [32 x i8] c"Ping packet send to %s port %d\0A\00"
@.str.7 = [24 x i8] c"\0APong Message Received\0A\00"
@.str.8 = [19 x i8] c"RTT: %.3f Seconds\0A\00"
@.str.9 = [35 x i8] c"\0AMessage Receive Timeout or Error\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [100 x i8]
%12 = alloca i32
%13 = alloca [50 x i8]
%14 = alloca %struct.hostent*
%15 = alloca [100 x i8]
%16 = alloca %struct.hostent*
%17 = alloca %struct.in_addr**
%18 = alloca %struct.timeval
%19 = alloca %struct.timeval
%20 = alloca %struct.timeval
%21 = alloca i32
%22 = alloca [5 x i8]
%23 = alloca i32
%24 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 24, i32* %8
%25 = bitcast %struct.timeval* %20 to i8*
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 4
br i1 %27, label %28, label %42
28: ; preds = %2
%29 = getelementptr inbounds [50 x i8], [50 x i8]* %13, i64 0, i64 0
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @strncpy(i8* %29, i8* %32, i32 50)
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %10
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 3
%40 = load i8*, i8** %39
%41 = call i32 @atoi(i8* %40)
store i32 %41, i32* %12
br label %48
42: ; preds = %2
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 0
%45 = load i8*, i8** %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %28
%49 = load i8*, i8** @AF_INET
%50 = load i32, i32* @SOCK_DGRAM
%51 = call i32 @socket(i8* %49, i32 %50, i32 0)
store i32 %51, i32* %9
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %48
%57 = getelementptr inbounds [50 x i8], [50 x i8]* %13, i64 0, i64 0
%58 = call %struct.hostent* (i8*, ...) bitcast (%struct.hostent* (...)* @gethostbyname to %struct.hostent* (i8*, ...)*)(i8* %57)
store %struct.hostent* %58, %struct.hostent** %16
%59 = load %struct.hostent*, %struct.hostent** %16
%60 = getelementptr inbounds %struct.hostent, %struct.hostent* %59, i32 0, i32 0
%61 = load i64, i64* %60
%62 = inttoptr i64 %61 to %struct.in_addr**
store %struct.in_addr** %62, %struct.in_addr*** %17
store i32 0, i32* %21
br label %63
63: ; preds = %81, %56
%64 = load %struct.in_addr**, %struct.in_addr*** %17
%65 = load i32, i32* %21
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %64, i64 %66
%68 = load %struct.in_addr*, %struct.in_addr** %67
%69 = icmp ne %struct.in_addr* %68, null
br i1 %69, label %70, label %84
70: ; preds = %63
%71 = getelementptr inbounds [100 x i8], [100 x i8]* %15, i64 0, i64 0
%72 = load %struct.in_addr**, %struct.in_addr*** %17
%73 = load i32, i32* %21
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %72, i64 %74
%76 = load %struct.in_addr*, %struct.in_addr** %75
%77 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %76, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call i32 @inet_ntoa(i32 %78)
%80 = call i32 @strcpy(i8* %71, i32 %79)
br label %81
81: ; preds = %70
%82 = load i32, i32* %21
%83 = add nsw i32 %82, 1
store i32 %83, i32* %21
br label %63
84: ; preds = %63
%85 = bitcast %struct.sockaddr_in* %6 to i8*
%86 = call i32 @memset(i8* %85, i32 0, i32 24)
%87 = load i8*, i8** @AF_INET
%88 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i8* %87, i8** %88
%89 = load i32, i32* @INADDR_ANY
%90 = call i32 @htonl(i32 %89)
%91 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%92 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %91, i32 0, i32 0
store i32 %90, i32* %92
%93 = call i8* @htons(i32 0)
%94 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i8* %93, i8** %94
%95 = load i32, i32* %9
%96 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%97 = call i64 @bind(i32 %95, %struct.sockaddr* %96, i32 24)
%98 = icmp slt i64 %97, 0
br i1 %98, label %99, label %101
99: ; preds = %84
%100 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %184
101: ; preds = %84
%102 = bitcast %struct.sockaddr_in* %7 to i8*
%103 = call i32 @memset(i8* %102, i32 0, i32 24)
%104 = load i8*, i8** @AF_INET
%105 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i8* %104, i8** %105
%106 = load i32, i32* %10
%107 = call i8* @htons(i32 %106)
%108 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i8* %107, i8** %108
%109 = getelementptr inbounds [100 x i8], [100 x i8]* %15, i64 0, i64 0
%110 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%111 = call i64 @inet_aton(i8* %109, %struct.TYPE_2__* %110)
%112 = icmp eq i64 %111, 0
br i1 %112, label %113, label %117
113: ; preds = %101
%114 = load i32, i32* @stderr
%115 = call i32 @fprintf(i32 %114, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%116 = call i32 @exit(i32 1)
unreachable
117: ; preds = %101
%118 = load i32, i32* %9
%119 = load i32, i32* @SOL_SOCKET
%120 = load i32, i32* @SO_RCVTIMEO
%121 = bitcast %struct.timeval* %20 to i8*
%122 = call i32 @setsockopt(i32 %118, i32 %119, i32 %120, i8* %121, i32 24)
store i32 0, i32* %21
br label %123
123: ; preds = %178, %117
%124 = load i32, i32* %21
%125 = load i32, i32* %12
%126 = icmp slt i32 %124, %125
br i1 %126, label %127, label %181
127: ; preds = %123
%128 = bitcast [5 x i8]* %22 to i8*
%129 = load i32, i32* %9
%130 = getelementptr inbounds [5 x i8], [5 x i8]* %22, i64 0, i64 0
%131 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%132 = call i32 @strlen(i8* %131)
%133 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%134 = load i32, i32* %8
%135 = call i32 @sendto(i32 %129, i8* %130, i32 %132, i32 0, %struct.sockaddr* %133, i32 %134)
%136 = icmp eq i32 %135, -1
br i1 %136, label %137, label %140
137: ; preds = %127
%138 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%139 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %178
140: ; preds = %127
%141 = call i32 @gettimeofday(%struct.timeval* %18, i32 0)
%142 = getelementptr inbounds [50 x i8], [50 x i8]* %13, i64 0, i64 0
%143 = load i32, i32* %10
%144 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %142, i32 %143)
%145 = load i32, i32* %9
%146 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%147 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%148 = call i32 @recvfrom(i32 %145, i8* %146, i32 100, i32 0, %struct.sockaddr* %147, i32* %8)
store i32 %148, i32* %23
%149 = load i32, i32* %23
%150 = icmp sge i32 %149, 0
br i1 %150, label %151, label %174
151: ; preds = %140
%152 = load i32, i32* %23
%153 = sext i32 %152 to i64
%154 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 %153
store i8 0, i8* %154
%155 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%156 = call i32 @gettimeofday(%struct.timeval* %19, i32 0)
%157 = getelementptr inbounds %struct.timeval, %struct.timeval* %19, i32 0, i32 1
%158 = load i32, i32* %157
%159 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 1
%160 = load i32, i32* %159
%161 = sub nsw i32 %158, %160
%162 = mul nsw i32 1000000, %161
%163 = sitofp i32 %162 to double
%164 = getelementptr inbounds %struct.timeval, %struct.timeval* %19, i32 0, i32 2
%165 = load double, double* %164
%166 = fadd double %163, %165
%167 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 2
%168 = load double, double* %167
%169 = fsub double %166, %168
store double %169, double* %24
%170 = load double, double* %24
%171 = fdiv double %170, 1.000000e+03
store double %171, double* %24
%172 = load double, double* %24
%173 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), double %172)
br label %177
174: ; preds = %140
%175 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
%176 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %178
177: ; preds = %151
br label %178
178: ; preds = %177, %174, %137
%179 = load i32, i32* %21
%180 = add nsw i32 %179, 1
store i32 %180, i32* %21
br label %123
181: ; preds = %123
%182 = load i32, i32* %9
%183 = call i32 @close(i32 %182)
store i32 0, i32* %3
br label %184
184: ; preds = %181, %99
%185 = load i32, i32* %3
ret i32 %185
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32, double, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.timeout = %struct.timeval { i32 2, i32 0, double 0.000000e+00, i32 0 }
@.str = [58 x i8] c"Usage: %s <host name> <port number> <number of messages>\0A\00"
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@.str.1 = [22 x i8] c"cannot create socket\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [12 x i8] c"bind failed\00"
@stderr = global i32 0
@.str.3 = [20 x i8] c"inet_aton() failed\0A\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@__const.main.send_buf = [5 x i8] c"ping\00"
@.str.4 = [7 x i8] c"sendto\00"
@.str.6 = [32 x i8] c"Ping packet send to %s port %d\0A\00"
@.str.8 = [19 x i8] c"RTT: %.3f Seconds\0A\00"
@str = [34 x i8] c"\0AMessage Receive Timeout or Error\00"
@str.11 = [23 x i8] c"\0APong Message Received\00"
@str.12 = [17 x i8] c"\0A---------------\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca [50 x i8]
%8 = alloca [100 x i8]
%9 = alloca %struct.timeval
%10 = alloca %struct.timeval
%11 = alloca %struct.timeval
%12 = alloca [5 x i8]
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = bitcast %struct.sockaddr_in* %4 to i8*
%15 = bitcast i32* %5 to i8*
store i32 24, i32* %5
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%17 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%19 = bitcast %struct.timeval* %9 to i8*
%20 = bitcast %struct.timeval* %10 to i8*
%21 = bitcast %struct.timeval* %11 to i8*
%22 = icmp eq i32 %0, 4
br i1 %22, label %23, label %37
23: ; preds = %2
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @strncpy(i8* %17, i8* %25, i32 50)
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
%30 = getelementptr inbounds i8*, i8** %1, i64 3
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
%33 = load i8*, i8** @AF_INET
%34 = load i32, i32* @SOCK_DGRAM
%35 = call i32 @socket(i8* %33, i32 %34, i32 0)
%36 = icmp slt i32 %35, 0
br i1 %36, label %41, label %44
37: ; preds = %2
%38 = load i8*, i8** %1
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i8* %38)
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %23
%42 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %23
%45 = call %struct.hostent* (i8*, ...) bitcast (%struct.hostent* (...)* @gethostbyname to %struct.hostent* (i8*, ...)*)(i8* %17)
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i64 0, i32 0
%47 = load i64, i64* %46
%48 = inttoptr i64 %47 to %struct.in_addr**
%49 = load %struct.in_addr*, %struct.in_addr** %48
%50 = icmp eq %struct.in_addr* %49, null
br i1 %50, label %62, label %51
51: ; preds = %44, %51
%52 = phi i64 [ %58, %51 ], [ 0, %44 ]
%53 = phi %struct.in_addr* [ %60, %51 ], [ %49, %44 ]
%54 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %53, i64 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @inet_ntoa(i32 %55)
%57 = call i32 @strcpy(i8* %18, i32 %56)
%58 = add nuw i64 %52, 1
%59 = getelementptr inbounds %struct.in_addr*, %struct.in_addr** %48, i64 %58
%60 = load %struct.in_addr*, %struct.in_addr** %59
%61 = icmp eq %struct.in_addr* %60, null
br i1 %61, label %62, label %51
62: ; preds = %51, %44
%63 = call i32 @memset(i8* %13, i32 0, i32 24)
%64 = load i8*, i8** @AF_INET
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i8* %64, i8** %65
%66 = load i32, i32* @INADDR_ANY
%67 = call i32 @htonl(i32 %66)
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %67, i32* %68
%69 = call i8* @htons(i32 0)
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i8* %69, i8** %70
%71 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%72 = call i64 @bind(i32 %35, %struct.sockaddr* %71, i32 24)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %62
%75 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %139
76: ; preds = %62
%77 = call i32 @memset(i8* %14, i32 0, i32 24)
%78 = load i8*, i8** @AF_INET
%79 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i8* %78, i8** %79
%80 = call i8* @htons(i32 %29)
%81 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i8* %80, i8** %81
%82 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%83 = call i64 @inet_aton(i8* %18, %struct.TYPE_2__* %82)
%84 = icmp eq i64 %83, 0
br i1 %84, label %85, label %89
85: ; preds = %76
%86 = load i32, i32* @stderr
%87 = call i32 @fprintf(i32 %86, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%88 = call i32 @exit(i32 1)
unreachable
89: ; preds = %76
%90 = load i32, i32* @SOL_SOCKET
%91 = load i32, i32* @SO_RCVTIMEO
%92 = call i32 @setsockopt(i32 %35, i32 %90, i32 %91, i8* %21, i32 24)
%93 = icmp sgt i32 %32, 0
br i1 %93, label %94, label %137
94: ; preds = %89
%95 = getelementptr inbounds [5 x i8], [5 x i8]* %12, i64 0, i64 0
%96 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%97 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i64 0, i32 1
%98 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i64 0, i32 1
%99 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i64 0, i32 2
%100 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i64 0, i32 2
br label %101
101: ; preds = %94, %134
%102 = phi i32 [ 0, %94 ], [ %135, %134 ]
%103 = call i32 @strlen(i8* %16)
%104 = load i32, i32* %5
%105 = call i32 @sendto(i32 %35, i8* %95, i32 %103, i32 0, %struct.sockaddr* %96, i32 %104)
%106 = icmp eq i32 %105, -1
br i1 %106, label %107, label %110
107: ; preds = %101
%108 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%109 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.12, i64 0, i64 0))
br label %134
110: ; preds = %101
%111 = call i32 @gettimeofday(%struct.timeval* %9, i32 0)
%112 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %17, i32 %29)
%113 = call i32 @recvfrom(i32 %35, i8* %16, i32 100, i32 0, %struct.sockaddr* %96, i32* %5)
%114 = icmp sgt i32 %113, -1
br i1 %114, label %115, label %131
115: ; preds = %110
%116 = zext i32 %113 to i64
%117 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 %116
store i8 0, i8* %117
%118 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0))
%119 = call i32 @gettimeofday(%struct.timeval* %10, i32 0)
%120 = load i32, i32* %97
%121 = load i32, i32* %98
%122 = sub nsw i32 %120, %121
%123 = mul nsw i32 %122, 1000000
%124 = sitofp i32 %123 to double
%125 = load double, double* %99
%126 = fadd double %125, %124
%127 = load double, double* %100
%128 = fsub double %126, %127
%129 = fdiv double %128, 1.000000e+03
%130 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), double %129)
br label %134
131: ; preds = %110
%132 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%133 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.12, i64 0, i64 0))
br label %134
134: ; preds = %131, %115, %107
%135 = add nuw nsw i32 %102, 1
%136 = icmp eq i32 %135, %32
br i1 %136, label %137, label %101
137: ; preds = %134, %89
%138 = call i32 @close(i32 %35)
br label %139
139: ; preds = %137, %74
ret i32 0
}