#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 norm_of_a_vector(int dim, double *ptr) {
  double result;
  result = 0.0;
  do {
    result += (*ptr) * (*ptr);
    ptr++;
  } while (--dim);
  return sqrt(result);
}


x86 assembly (clang, -O0):
<norm_of_a_vector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm2
movsd  -0x18(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jne    1127 <norm_of_a_vector+0x17>
movsd  -0x18(%rbp),%xmm0
call   1030 <sqrt@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<norm_of_a_vector>:
lea    -0x1(%rdi),%eax
test   $0x3,%dil
je     1151 <norm_of_a_vector+0x41>
mov    %edi,%ecx
and    $0x3,%ecx
xorpd  %xmm0,%xmm0
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  (%rsi),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm1,%xmm0
add    $0x8,%rsi
add    $0x1,%rdx
cmp    %edx,%ecx
jne    1130 <norm_of_a_vector+0x20>
sub    %edx,%edi
cmp    $0x3,%eax
jae    115a <norm_of_a_vector+0x4a>
jmp    119f <norm_of_a_vector+0x8f>
xorpd  %xmm0,%xmm0
cmp    $0x3,%eax
jb     119f <norm_of_a_vector+0x8f>
mov    %edi,%eax
xor    %ecx,%ecx
xchg   %ax,%ax
movsd  (%rsi,%rcx,8),%xmm1
movsd  0x8(%rsi,%rcx,8),%xmm2
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
mulsd  %xmm2,%xmm2
addsd  %xmm1,%xmm2
movsd  0x10(%rsi,%rcx,8),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm2,%xmm1
movsd  0x18(%rsi,%rcx,8),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
add    $0x4,%rcx
cmp    %ecx,%eax
jne    1160 <norm_of_a_vector+0x50>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     11ae <norm_of_a_vector+0x9e>
sqrtsd %xmm0,%xmm0
ret
jmp    1030 <sqrt@plt>

LLVM IR (-O0):
define double @norm_of_a_vector(i32 %0, double* %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca double
store i32 %0, i32* %3
store double* %1, double** %4
store double 0.000000e+00, double* %5
br label %6
6: ; preds = %15, %2
%7 = load double*, double** %4
%8 = load double, double* %7
%9 = load double*, double** %4
%10 = load double, double* %9
%11 = load double, double* %5
%12 = call double @llvm.fmuladd.f64(double %8, double %10, double %11)
store double %12, double* %5
%13 = load double*, double** %4
%14 = getelementptr inbounds double, double* %13, i32 1
store double* %14, double** %4
br label %15
15: ; preds = %6
%16 = load i32, i32* %3
%17 = add nsw i32 %16, -1
store i32 %17, i32* %3
%18 = icmp ne i32 %17, 0
br i1 %18, label %6, label %19
19: ; preds = %15
%20 = load double, double* %5
%21 = call double @sqrt(double %20)
ret double %21
}

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