#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 */

/* Type definitions */
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
scalar_t__ calloc(int, int);
double current_time();
int /*<<< orphan*/ die(char *);
int fprintf(int /*<<< orphan*/, char *, double, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
double *ping(int s, int n) {
  char m[1];
  int i;
  int _ = fprintf(stderr, "%d bytes for record\n", sizeof(double) * n);
  double *rec = (double *)calloc(sizeof(double), n);
  struct pollfd fds[1];
  double roundtrip = 0.0;
  double start_t, end_t;
  fds->fd = s;
  fds->events = POLLIN;
  start_t = current_time();
  for (i = 0; i < n; i++) {
    double t0, t1;
    m[0] = 'a' + (i % 26);
    t0 = current_time();
    if (1 != send(s, m, 1, 0))
      die("send");
    if (1 != recv(s, m, 1, 0))
      die("recv");
    t1 = current_time();
    rec[i] = t1 - t0;
    roundtrip += (t1 - t0);
  }
  end_t = current_time();
  {
    double dt = end_t - start_t;
    fprintf(stderr,
            "wallclock time = %.1f us\n"
            "total roundtrip = %.1f us\n"
            "avg roundtrip = %.3f\n"
            "running = %.1f%%\n",
            dt, roundtrip, roundtrip / n, (dt - roundtrip) / dt * 100.0);
  }
  return rec;
}


x86 assembly (clang, -O0):
<ping>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
movslq -0x8(%rbp),%rax
shl    $0x3,%rax
movq   %rax,%xmm0
movaps 0xe75(%rip),%xmm1
punpckldq %xmm1,%xmm0
movapd 0xe79(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
lea    0xe7b(%rip),%rsi
mov    $0x1,%al
call   1070 <fprintf@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%esi
mov    $0x8,%edi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <current_time@plt>
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12c1 <ping+0x161>
mov    -0x10(%rbp),%eax
mov    $0x1a,%ecx
cltd
idiv   %ecx
add    $0x61,%edx
mov    %dl,%al
mov    %al,-0x9(%rbp)
mov    $0x0,%al
call   1060 <current_time@plt>
movsd  %xmm0,-0x48(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     124e <ping+0xee>
lea    0xdf4(%rip),%rdi
call   1080 <die@plt>
mov    -0x4(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     127c <ping+0x11c>
lea    0xdcb(%rip),%rdi
call   1080 <die@plt>
mov    $0x0,%al
call   1060 <current_time@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
subsd  -0x48(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x50(%rbp),%xmm0
subsd  -0x48(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11f5 <ping+0x95>
mov    $0x0,%al
call   1060 <current_time@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
movsd  -0x58(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm2
cvtsi2sdl -0x8(%rbp),%xmm3
divsd  %xmm3,%xmm2
movsd  -0x58(%rbp),%xmm3
subsd  -0x30(%rbp),%xmm3
divsd  -0x58(%rbp),%xmm3
movsd  0xd0c(%rip),%xmm4
mulsd  %xmm4,%xmm3
lea    0xd28(%rip),%rsi
mov    $0x4,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ping>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
movslq %esi,%rbx
lea    0x0(,%rbx,8),%rax
movq   %rax,%xmm1
punpckldq 0xe6c(%rip),%xmm1
subpd  0xe74(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
lea    0xe79(%rip),%rsi
mov    $0x1,%al
call   1070 <fprintf@plt>
mov    $0x8,%edi
mov    %ebx,%esi
call   1050 <calloc@plt>
mov    %rax,0x20(%rsp)
xor    %eax,%eax
call   1060 <current_time@plt>
movsd  %xmm0,0x28(%rsp)
test   %ebx,%ebx
mov    %r14d,0x1c(%rsp)
jle    12b6 <ping+0x156>
mov    %r14d,%r14d
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    $0x61,%r12b
lea    0xf(%rsp),%rbx
xor    %r13d,%r13d
xor    %r15d,%r15d
jmp    123d <ping+0xdd>
xchg   %ax,%ax
xor    %eax,%eax
call   1060 <current_time@plt>
subsd  0x30(%rsp),%xmm0
mov    0x20(%rsp),%rax
movsd  %xmm0,(%rax,%r15,8)
movsd  0x10(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
add    $0x1,%r15
add    $0x1,%r12b
add    $0x1,%r13d
cmp    %r15,%r14
je     12c0 <ping+0x160>
mov    %r13d,%eax
imul   $0x4ec4ec4f,%rax,%rax
shr    $0x23,%rax
lea    (%rax,%rax,4),%ecx
lea    (%rcx,%rcx,4),%ecx
add    %eax,%ecx
mov    %r12d,%eax
sub    %cl,%al
mov    %al,0xf(%rsp)
xor    %eax,%eax
call   1060 <current_time@plt>
movsd  %xmm0,0x30(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0x1,%eax
je     128b <ping+0x12b>
lea    0xdb7(%rip),%rdi
call   1080 <die@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x1,%eax
je     1200 <ping+0xa0>
lea    0xd96(%rip),%rdi
call   1080 <die@plt>
jmp    1200 <ping+0xa0>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1060 <current_time@plt>
subsd  0x28(%rsp),%xmm0
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
cvtsi2sdl 0x1c(%rsp),%xmm3
movsd  0x10(%rsp),%xmm1
movapd %xmm1,%xmm2
divsd  %xmm3,%xmm2
movapd %xmm0,%xmm3
subsd  %xmm1,%xmm3
divsd  %xmm0,%xmm3
mulsd  0xd22(%rip),%xmm3
lea    0xd42(%rip),%rsi
mov    $0x4,%al
call   1070 <fprintf@plt>
mov    0x20(%rsp),%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@stderr = global i32 0
@.str = [21 x i8] c"%d bytes for record\0A\00"
@POLLIN = global i32 0
@.str.1 = [5 x i8] c"send\00"
@.str.2 = [5 x i8] c"recv\00"
@.str.3 = [90 x i8] c"wallclock time = %.1f us\0Atotal roundtrip = %.1f us\0Aavg roundtrip = %.3f\0Arunning = %.1f%%\0A\00"
define double* @ping(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca double*
%9 = alloca [1 x %struct.pollfd]
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
store i32 %0, i32* %3
store i32 %1, i32* %4
%16 = load i32, i32* @stderr
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = mul i64 8, %18
%20 = uitofp i64 %19 to double
%21 = call i32 (i32, i8*, double, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), double %20)
store i32 %21, i32* %7
%22 = load i32, i32* %4
%23 = call i64 @calloc(i32 8, i32 %22)
%24 = inttoptr i64 %23 to double*
store double* %24, double** %8
store double 0.000000e+00, double* %10
%25 = load i32, i32* %3
%26 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %9, i64 0, i64 0
%27 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* @POLLIN
%29 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %9, i64 0, i64 0
%30 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %29, i32 0, i32 1
store i32 %28, i32* %30
%31 = call double (...) @current_time()
store double %31, double* %11
store i32 0, i32* %6
br label %32
32: ; preds = %70, %2
%33 = load i32, i32* %6
%34 = load i32, i32* %4
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %73
36: ; preds = %32
%37 = load i32, i32* %6
%38 = srem i32 %37, 26
%39 = add nsw i32 97, %38
%40 = trunc i32 %39 to i8
%41 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
store i8 %40, i8* %41
%42 = call double (...) @current_time()
store double %42, double* %13
%43 = load i32, i32* %3
%44 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
%45 = call i32 @send(i32 %43, i8* %44, i32 1, i32 0)
%46 = icmp ne i32 1, %45
br i1 %46, label %47, label %49
47: ; preds = %36
%48 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %47, %36
%50 = load i32, i32* %3
%51 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
%52 = call i32 @recv(i32 %50, i8* %51, i32 1, i32 0)
%53 = icmp ne i32 1, %52
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %54, %49
%57 = call double (...) @current_time()
store double %57, double* %14
%58 = load double, double* %14
%59 = load double, double* %13
%60 = fsub double %58, %59
%61 = load double*, double** %8
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds double, double* %61, i64 %63
store double %60, double* %64
%65 = load double, double* %14
%66 = load double, double* %13
%67 = fsub double %65, %66
%68 = load double, double* %10
%69 = fadd double %68, %67
store double %69, double* %10
br label %70
70: ; preds = %56
%71 = load i32, i32* %6
%72 = add nsw i32 %71, 1
store i32 %72, i32* %6
br label %32
73: ; preds = %32
%74 = call double (...) @current_time()
store double %74, double* %12
%75 = load double, double* %12
%76 = load double, double* %11
%77 = fsub double %75, %76
store double %77, double* %15
%78 = load i32, i32* @stderr
%79 = load double, double* %15
%80 = load double, double* %10
%81 = load double, double* %10
%82 = load i32, i32* %4
%83 = sitofp i32 %82 to double
%84 = fdiv double %81, %83
%85 = load double, double* %15
%86 = load double, double* %10
%87 = fsub double %85, %86
%88 = load double, double* %15
%89 = fdiv double %87, %88
%90 = fmul double %89, 1.000000e+02
%91 = call i32 (i32, i8*, double, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.3, i64 0, i64 0), double %79, double %80, double %84, double %90)
%92 = load double*, double** %8
ret double* %92
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"%d bytes for record\0A\00"
@POLLIN = global i32 0
@.str.1 = [5 x i8] c"send\00"
@.str.2 = [5 x i8] c"recv\00"
@.str.3 = [90 x i8] c"wallclock time = %.1f us\0Atotal roundtrip = %.1f us\0Aavg roundtrip = %.3f\0Arunning = %.1f%%\0A\00"
define double* @ping(i32 %0, i32 %1) {
%3 = alloca [1 x i8]
%4 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @stderr
%6 = sext i32 %1 to i64
%7 = shl nsw i64 %6, 3
%8 = uitofp i64 %7 to double
%9 = tail call i32 (i32, i8*, double, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), double %8)
%10 = tail call i64 @calloc(i32 8, i32 %1)
%11 = inttoptr i64 %10 to double*
%12 = tail call double (...) @current_time()
%13 = icmp sgt i32 %1, 0
br i1 %13, label %14, label %40
14: ; preds = %2
%15 = zext i32 %1 to i64
br label %16
16: ; preds = %14, %33
%17 = phi i64 [ 0, %14 ], [ %38, %33 ]
%18 = phi double [ 0.000000e+00, %14 ], [ %37, %33 ]
%19 = trunc i64 %17 to i32
%20 = urem i32 %19, 26
%21 = trunc i32 %20 to i8
%22 = add nuw nsw i8 %21, 97
store i8 %22, i8* %4
%23 = call double (...) @current_time()
%24 = call i32 @send(i32 %0, i8* %4, i32 1, i32 0)
%25 = icmp eq i32 %24, 1
br i1 %25, label %28, label %26
26: ; preds = %16
%27 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %16
%29 = call i32 @recv(i32 %0, i8* %4, i32 1, i32 0)
%30 = icmp eq i32 %29, 1
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = call double (...) @current_time()
%35 = fsub double %34, %23
%36 = getelementptr inbounds double, double* %11, i64 %17
store double %35, double* %36
%37 = fadd double %18, %35
%38 = add nuw nsw i64 %17, 1
%39 = icmp eq i64 %38, %15
br i1 %39, label %40, label %16
40: ; preds = %33, %2
%41 = phi double [ 0.000000e+00, %2 ], [ %37, %33 ]
%42 = call double (...) @current_time()
%43 = fsub double %42, %12
%44 = load i32, i32* @stderr
%45 = sitofp i32 %1 to double
%46 = fdiv double %41, %45
%47 = fsub double %43, %41
%48 = fdiv double %47, %43
%49 = fmul double %48, 1.000000e+02
%50 = call i32 (i32, i8*, double, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.3, i64 0, i64 0), double %43, double %41, double %46, double %49)
ret double* %11
}