#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 __ocml_rsqrt_f64(double);
double rnorm(int dim, const double *a) {
  double r = 0;
  while (dim--) {
    r += a[0] * a[0];
    ++a;
  }
  return __ocml_rsqrt_f64(r);
}


x86 assembly (clang, -O0):
<rnorm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4(%rbp)
cmp    $0x0,%eax
je     116e <rnorm+0x5e>
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)
jmp    1127 <rnorm+0x17>
movsd  -0x18(%rbp),%xmm0
call   1030 <__ocml_rsqrt_f64@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rnorm>:
test   %edi,%edi
je     1151 <rnorm+0x41>
lea    -0x1(%rdi),%eax
test   $0x3,%dil
je     1159 <rnorm+0x49>
mov    %edi,%ecx
and    $0x3,%ecx
xorpd  %xmm0,%xmm0
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
movsd  (%rsi),%xmm1
mulsd  %xmm1,%xmm1
addsd  %xmm1,%xmm0
add    $0x8,%rsi
add    $0x1,%rdx
cmp    %edx,%ecx
jne    1130 <rnorm+0x20>
sub    %edx,%edi
cmp    $0x3,%eax
jae    1162 <rnorm+0x52>
jmp    11af <rnorm+0x9f>
xorps  %xmm0,%xmm0
jmp    1030 <__ocml_rsqrt_f64@plt>
xorpd  %xmm0,%xmm0
cmp    $0x3,%eax
jb     11af <rnorm+0x9f>
mov    %edi,%eax
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
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    1170 <rnorm+0x60>
jmp    1030 <__ocml_rsqrt_f64@plt>

LLVM IR (-O0):
define double @rnorm(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 = %10, %2
%7 = load i32, i32* %3
%8 = add nsw i32 %7, -1
store i32 %8, i32* %3
%9 = icmp ne i32 %7, 0
br i1 %9, label %10, label %21
10: ; preds = %6
%11 = load double*, double** %4
%12 = getelementptr inbounds double, double* %11, i64 0
%13 = load double, double* %12
%14 = load double*, double** %4
%15 = getelementptr inbounds double, double* %14, i64 0
%16 = load double, double* %15
%17 = load double, double* %5
%18 = call double @llvm.fmuladd.f64(double %13, double %16, double %17)
store double %18, double* %5
%19 = load double*, double** %4
%20 = getelementptr inbounds double, double* %19, i32 1
store double* %20, double** %4
br label %6
21: ; preds = %6
%22 = load double, double* %5
%23 = call double @__ocml_rsqrt_f64(double %22)
ret double %23
}

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