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

/* Variables and functions */
double sqrt(double);
double velavg(int npart, double vh[], double vaver, double h) {
  int i;
  double vaverh = vaver * h;
  double vel = 0.0;
  double sq;
  extern double count;
  count = 0.0;
  for (i = 0; i < npart * 3; i += 3) {
    sq = sqrt(vh[i] * vh[i] + vh[i + 1] * vh[i + 1] + vh[i + 2] * vh[i + 2]);
    if (sq > vaverh)
      count++;
    vel += sq;
  }
  vel /= h;
  return (vel);
}


x86 assembly (clang, -O0):
<velavg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    0x2e91(%rip),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
imul   $0x3,-0x4(%rbp),%ecx
cmp    %ecx,%eax
jge    122b <velavg+0x11b>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mulsd  (%rax,%rcx,8),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    120e <velavg+0xfe>
movsd  0xe08(%rip),%xmm0
mov    0x2dd9(%rip),%rax
addsd  (%rax),%xmm0
mov    0x2dce(%rip),%rax
movsd  %xmm0,(%rax)
movsd  -0x40(%rbp),%xmm0
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x24(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x24(%rbp)
jmp    1155 <velavg+0x45>
movsd  -0x20(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<velavg>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2eb6(%rip),%r14
movq   $0x0,(%r14)
test   %edi,%edi
movsd  %xmm1,(%rsp)
jle    11e8 <velavg+0xd8>
movapd %xmm0,%xmm2
mov    %rsi,%r12
mulsd  %xmm1,%xmm2
lea    (%rdi,%rdi,2),%eax
movslq %eax,%r15
xorpd  %xmm4,%xmm4
xor    %ebx,%ebx
xorpd  %xmm3,%xmm3
movsd  %xmm2,0x8(%rsp)
jmp    116d <velavg+0x5d>
nopl   0x0(%rax)
addsd  %xmm0,%xmm3
add    $0x3,%rbx
cmp    %r15,%rbx
jge    11ec <velavg+0xdc>
movsd  (%r12,%rbx,8),%xmm1
movsd  0x8(%r12,%rbx,8),%xmm0
mulsd  %xmm0,%xmm0
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0x10(%r12,%rbx,8),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
ucomisd %xmm4,%xmm0
jb     11b0 <velavg+0xa0>
sqrtsd %xmm0,%xmm0
ucomisd %xmm2,%xmm0
jbe    1160 <velavg+0x50>
jmp    11d1 <velavg+0xc1>
nopw   0x0(%rax,%rax,1)
movsd  %xmm3,0x10(%rsp)
call   1030 <sqrt@plt>
xorpd  %xmm4,%xmm4
movsd  0x10(%rsp),%xmm3
movsd  0x8(%rsp),%xmm2
ucomisd %xmm2,%xmm0
jbe    1160 <velavg+0x50>
movsd  (%r14),%xmm1
addsd  0xe22(%rip),%xmm1
movsd  %xmm1,(%r14)
jmp    1160 <velavg+0x50>
xorpd  %xmm3,%xmm3
divsd  (%rsp),%xmm3
movapd %xmm3,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@count = external global double
define double @velavg(i32 %0, double* %1, double %2, double %3) {
%5 = alloca i32
%6 = alloca double*
%7 = alloca double
%8 = alloca double
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca double
store i32 %0, i32* %5
store double* %1, double** %6
store double %2, double* %7
store double %3, double* %8
%13 = load double, double* %7
%14 = load double, double* %8
%15 = fmul double %13, %14
store double %15, double* %10
store double 0.000000e+00, double* %11
store double 0.000000e+00, double* @count
store i32 0, i32* %9
br label %16
16: ; preds = %70, %4
%17 = load i32, i32* %9
%18 = load i32, i32* %5
%19 = mul nsw i32 %18, 3
%20 = icmp slt i32 %17, %19
br i1 %20, label %21, label %73
21: ; preds = %16
%22 = load double*, double** %6
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds double, double* %22, i64 %24
%26 = load double, double* %25
%27 = load double*, double** %6
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds double, double* %27, i64 %29
%31 = load double, double* %30
%32 = load double*, double** %6
%33 = load i32, i32* %9
%34 = add nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double, double* %32, i64 %35
%37 = load double, double* %36
%38 = load double*, double** %6
%39 = load i32, i32* %9
%40 = add nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %38, i64 %41
%43 = load double, double* %42
%44 = fmul double %37, %43
%45 = call double @llvm.fmuladd.f64(double %26, double %31, double %44)
%46 = load double*, double** %6
%47 = load i32, i32* %9
%48 = add nsw i32 %47, 2
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds double, double* %46, i64 %49
%51 = load double, double* %50
%52 = load double*, double** %6
%53 = load i32, i32* %9
%54 = add nsw i32 %53, 2
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds double, double* %52, i64 %55
%57 = load double, double* %56
%58 = call double @llvm.fmuladd.f64(double %51, double %57, double %45)
%59 = call double @sqrt(double %58)
store double %59, double* %12
%60 = load double, double* %12
%61 = load double, double* %10
%62 = fcmp ogt double %60, %61
br i1 %62, label %63, label %66
63: ; preds = %21
%64 = load double, double* @count
%65 = fadd double %64, 1.000000e+00
store double %65, double* @count
br label %66
66: ; preds = %63, %21
%67 = load double, double* %12
%68 = load double, double* %11
%69 = fadd double %68, %67
store double %69, double* %11
br label %70
70: ; preds = %66
%71 = load i32, i32* %9
%72 = add nsw i32 %71, 3
store i32 %72, i32* %9
br label %16
73: ; preds = %16
%74 = load double, double* %8
%75 = load double, double* %11
%76 = fdiv double %75, %74
store double %76, double* %11
%77 = load double, double* %11
ret double %77
}

LLVM IR (-O2):
@count = external global double
define double @velavg(i32 %0, double* nocapture %1, double %2, double %3) {
%5 = fmul double %2, %3
store double 0.000000e+00, double* @count
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %33
7: ; preds = %4
%8 = mul nsw i32 %0, 3
%9 = sext i32 %8 to i64
br label %10
10: ; preds = %7, %29
%11 = phi i64 [ 0, %7 ], [ %31, %29 ]
%12 = phi double [ 0.000000e+00, %7 ], [ %30, %29 ]
%13 = getelementptr inbounds double, double* %1, i64 %11
%14 = load double, double* %13
%15 = add nuw nsw i64 %11, 1
%16 = getelementptr inbounds double, double* %1, i64 %15
%17 = load double, double* %16
%18 = fmul double %17, %17
%19 = tail call double @llvm.fmuladd.f64(double %14, double %14, double %18)
%20 = add nuw nsw i64 %11, 2
%21 = getelementptr inbounds double, double* %1, i64 %20
%22 = load double, double* %21
%23 = tail call double @llvm.fmuladd.f64(double %22, double %22, double %19)
%24 = tail call double @sqrt(double %23)
%25 = fcmp ogt double %24, %5
br i1 %25, label %26, label %29
26: ; preds = %10
%27 = load double, double* @count
%28 = fadd double %27, 1.000000e+00
store double %28, double* @count
br label %29
29: ; preds = %26, %10
%30 = fadd double %12, %24
%31 = add nuw nsw i64 %11, 3
%32 = icmp slt i64 %31, %9
br i1 %32, label %10, label %33
33: ; preds = %29, %4
%34 = phi double [ 0.000000e+00, %4 ], [ %30, %29 ]
%35 = fdiv double %34, %3
ret double %35
}