#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);
void calc_vec_norm(double *v, int npts, double *norm) {
  int i;
  *norm = 0;
  for (i = 0; i < npts; i++)
    *norm += v[i] * v[i];
  *norm = sqrt(*norm / (double)npts);
}


x86 assembly (clang, -O0):
<calc_vec_norm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    117d <calc_vec_norm+0x6d>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1135 <calc_vec_norm+0x25>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_vec_norm>:
push   %rbx
mov    %rdx,%rbx
movq   $0x0,(%rdx)
xorpd  %xmm1,%xmm1
xorpd  %xmm0,%xmm0
test   %esi,%esi
jle    11ca <calc_vec_norm+0xba>
mov    %esi,%ecx
lea    -0x1(%rcx),%rdx
mov    %ecx,%eax
and    $0x3,%eax
cmp    $0x3,%rdx
jae    1144 <calc_vec_norm+0x34>
xorpd  %xmm0,%xmm0
xor    %edx,%edx
jmp    11a0 <calc_vec_norm+0x90>
and    $0xfffffffc,%ecx
xorpd  %xmm0,%xmm0
xor    %edx,%edx
nopl   (%rax)
movsd  (%rdi,%rdx,8),%xmm2
mulsd  %xmm2,%xmm2
addsd  %xmm0,%xmm2
movsd  %xmm2,(%rbx)
movsd  0x8(%rdi,%rdx,8),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm2,%xmm0
movsd  %xmm0,(%rbx)
movsd  0x10(%rdi,%rdx,8),%xmm2
mulsd  %xmm2,%xmm2
addsd  %xmm0,%xmm2
movsd  %xmm2,(%rbx)
movsd  0x18(%rdi,%rdx,8),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm2,%xmm0
movsd  %xmm0,(%rbx)
add    $0x4,%rdx
cmp    %rdx,%rcx
jne    1150 <calc_vec_norm+0x40>
test   %rax,%rax
je     11ca <calc_vec_norm+0xba>
lea    (%rdi,%rdx,8),%rcx
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
movsd  (%rcx,%rdx,8),%xmm2
mulsd  %xmm2,%xmm2
addsd  %xmm2,%xmm0
movsd  %xmm0,(%rbx)
add    $0x1,%rdx
cmp    %rdx,%rax
jne    11b0 <calc_vec_norm+0xa0>
xorps  %xmm2,%xmm2
cvtsi2sd %esi,%xmm2
divsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jb     11e1 <calc_vec_norm+0xd1>
sqrtsd %xmm0,%xmm0
jmp    11e6 <calc_vec_norm+0xd6>
call   1030 <sqrt@plt>
movsd  %xmm0,(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
define void @calc_vec_norm(double* %0, i32 %1, double* %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca double*
%7 = alloca i32
store double* %0, double** %4
store i32 %1, i32* %5
store double* %2, double** %6
%8 = load double*, double** %6
store double 0.000000e+00, double* %8
store i32 0, i32* %7
br label %9
9: ; preds = %27, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %30
13: ; preds = %9
%14 = load double*, double** %4
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds double, double* %14, i64 %16
%18 = load double, double* %17
%19 = load double*, double** %4
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds double, double* %19, i64 %21
%23 = load double, double* %22
%24 = load double*, double** %6
%25 = load double, double* %24
%26 = call double @llvm.fmuladd.f64(double %18, double %23, double %25)
store double %26, double* %24
br label %27
27: ; preds = %13
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %9
30: ; preds = %9
%31 = load double*, double** %6
%32 = load double, double* %31
%33 = load i32, i32* %5
%34 = sitofp i32 %33 to double
%35 = fdiv double %32, %34
%36 = call double @sqrt(double %35)
%37 = load double*, double** %6
store double %36, double* %37
ret void
}

LLVM IR (-O2):
define void @calc_vec_norm(double* nocapture %0, i32 %1, double* nocapture %2) {
store double 0.000000e+00, double* %2
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %49
5: ; preds = %3
%6 = zext i32 %1 to i64
%7 = add nsw i64 %6, -1
%8 = and i64 %6, 3
%9 = icmp ult i64 %7, 3
br i1 %9, label %34, label %10
10: ; preds = %5
%11 = and i64 %6, 4294967292
br label %12
12: ; preds = %12, %10
%13 = phi double [ 0.000000e+00, %10 ], [ %30, %12 ]
%14 = phi i64 [ 0, %10 ], [ %31, %12 ]
%15 = phi i64 [ 0, %10 ], [ %32, %12 ]
%16 = getelementptr inbounds double, double* %0, i64 %14
%17 = load double, double* %16
%18 = tail call double @llvm.fmuladd.f64(double %17, double %17, double %13)
store double %18, double* %2
%19 = or i64 %14, 1
%20 = getelementptr inbounds double, double* %0, i64 %19
%21 = load double, double* %20
%22 = tail call double @llvm.fmuladd.f64(double %21, double %21, double %18)
store double %22, double* %2
%23 = or i64 %14, 2
%24 = getelementptr inbounds double, double* %0, i64 %23
%25 = load double, double* %24
%26 = tail call double @llvm.fmuladd.f64(double %25, double %25, double %22)
store double %26, double* %2
%27 = or i64 %14, 3
%28 = getelementptr inbounds double, double* %0, i64 %27
%29 = load double, double* %28
%30 = tail call double @llvm.fmuladd.f64(double %29, double %29, double %26)
store double %30, double* %2
%31 = add nuw nsw i64 %14, 4
%32 = add i64 %15, 4
%33 = icmp eq i64 %32, %11
br i1 %33, label %34, label %12
34: ; preds = %12, %5
%35 = phi double [ undef, %5 ], [ %30, %12 ]
%36 = phi double [ 0.000000e+00, %5 ], [ %30, %12 ]
%37 = phi i64 [ 0, %5 ], [ %31, %12 ]
%38 = icmp eq i64 %8, 0
br i1 %38, label %49, label %39
39: ; preds = %34, %39
%40 = phi double [ %45, %39 ], [ %36, %34 ]
%41 = phi i64 [ %46, %39 ], [ %37, %34 ]
%42 = phi i64 [ %47, %39 ], [ 0, %34 ]
%43 = getelementptr inbounds double, double* %0, i64 %41
%44 = load double, double* %43
%45 = tail call double @llvm.fmuladd.f64(double %44, double %44, double %40)
store double %45, double* %2
%46 = add nuw nsw i64 %41, 1
%47 = add i64 %42, 1
%48 = icmp eq i64 %47, %8
br i1 %48, label %49, label %39
49: ; preds = %34, %39, %3
%50 = phi double [ 0.000000e+00, %3 ], [ %35, %34 ], [ %45, %39 ]
%51 = sitofp i32 %1 to double
%52 = fdiv double %50, %51
%53 = tail call double @sqrt(double %52)
store double %53, double* %2
ret void
}