#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 vect_norm(double *a, int n) {
  int i;
  double norm = 0.0;
  for (i = 0; i < n; i++) {
    norm += a[i] * a[i];
  }
  norm = sqrt(norm);
  return norm;
}


x86 assembly (clang, -O0):
<vect_norm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1174 <vect_norm+0x64>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x18(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112e <vect_norm+0x1e>
movsd  -0x18(%rbp),%xmm0
call   1030 <sqrt@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vect_norm>:
xorpd  %xmm1,%xmm1
xorpd  %xmm0,%xmm0
test   %esi,%esi
jle    11b6 <vect_norm+0xa6>
mov    %esi,%ecx
lea    -0x1(%rcx),%rdx
mov    %ecx,%eax
and    $0x3,%eax
cmp    $0x3,%rdx
jae    1139 <vect_norm+0x29>
xorpd  %xmm0,%xmm0
xor    %edx,%edx
jmp    1190 <vect_norm+0x80>
and    $0xfffffffc,%ecx
xorpd  %xmm0,%xmm0
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  (%rdi,%rdx,8),%xmm2
movsd  0x8(%rdi,%rdx,8),%xmm3
mulsd  %xmm2,%xmm2
addsd  %xmm0,%xmm2
mulsd  %xmm3,%xmm3
addsd  %xmm2,%xmm3
movsd  0x10(%rdi,%rdx,8),%xmm2
mulsd  %xmm2,%xmm2
addsd  %xmm3,%xmm2
movsd  0x18(%rdi,%rdx,8),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm2,%xmm0
add    $0x4,%rdx
cmp    %rdx,%rcx
jne    1150 <vect_norm+0x40>
test   %rax,%rax
je     11b6 <vect_norm+0xa6>
lea    (%rdi,%rdx,8),%rcx
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
movsd  (%rcx,%rdx,8),%xmm2
mulsd  %xmm2,%xmm2
addsd  %xmm2,%xmm0
add    $0x1,%rdx
cmp    %rdx,%rax
jne    11a0 <vect_norm+0x90>
ucomisd %xmm1,%xmm0
jb     11c1 <vect_norm+0xb1>
sqrtsd %xmm0,%xmm0
ret
jmp    1030 <sqrt@plt>

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

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