#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 */
scalar_t__ pow(double, int);
double sqrt(double);
double vec_norm(double *vec, int length) {
  double r = 0.0;
  int i;
  for (i = 0; i < length; i++)
    r += pow(vec[i], 2);
  return (sqrt(r));
}


x86 assembly (clang, -O0):
<vec_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,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    117e <vec_norm+0x5e>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    $0x2,%edi
call   1030 <pow@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113e <vec_norm+0x1e>
movsd  -0x18(%rbp),%xmm0
call   1040 <sqrt@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vec_norm>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xorpd  %xmm0,%xmm0
test   %esi,%esi
jle    1249 <vec_norm+0x129>
mov    %rdi,%r12
mov    %esi,%r15d
lea    -0x1(%r15),%rax
mov    %r15d,%r14d
and    $0x3,%r14d
cmp    $0x3,%rax
jae    1156 <vec_norm+0x36>
xorpd  %xmm0,%xmm0
xor    %ebx,%ebx
jmp    11f8 <vec_norm+0xd8>
and    $0xfffffffc,%r15d
xorpd  %xmm0,%xmm0
xor    %ebx,%ebx
movsd  %xmm0,(%rsp)
movsd  (%r12,%rbx,8),%xmm0
mov    $0x2,%edi
call   1030 <pow@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
movsd  0x8(%r12,%rbx,8),%xmm0
mov    $0x2,%edi
call   1030 <pow@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
movsd  0x10(%r12,%rbx,8),%xmm0
mov    $0x2,%edi
call   1030 <pow@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
movsd  0x18(%r12,%rbx,8),%xmm0
mov    $0x2,%edi
call   1030 <pow@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
addsd  (%rsp),%xmm0
add    $0x4,%rbx
cmp    %rbx,%r15
jne    1160 <vec_norm+0x40>
test   %r14,%r14
je     1249 <vec_norm+0x129>
lea    (%r12,%rbx,8),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm0,(%rsp)
movsd  (%r15,%rbx,8),%xmm0
mov    $0x2,%edi
call   1030 <pow@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1210 <vec_norm+0xf0>
ucomisd 0xdaf(%rip),%xmm0
jb     1263 <vec_norm+0x143>
sqrtsd %xmm0,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <sqrt@plt>

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

LLVM IR (-O2):
define double @vec_norm(double* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %58
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 %41, label %9
9: ; preds = %4
%10 = and i64 %5, 4294967292
br label %11
11: ; preds = %11, %9
%12 = phi i64 [ 0, %9 ], [ %38, %11 ]
%13 = phi double [ 0.000000e+00, %9 ], [ %37, %11 ]
%14 = phi i64 [ 0, %9 ], [ %39, %11 ]
%15 = getelementptr inbounds double, double* %0, i64 %12
%16 = load double, double* %15
%17 = tail call i64 @pow(double %16, i32 2)
%18 = sitofp i64 %17 to double
%19 = fadd double %13, %18
%20 = or i64 %12, 1
%21 = getelementptr inbounds double, double* %0, i64 %20
%22 = load double, double* %21
%23 = tail call i64 @pow(double %22, i32 2)
%24 = sitofp i64 %23 to double
%25 = fadd double %19, %24
%26 = or i64 %12, 2
%27 = getelementptr inbounds double, double* %0, i64 %26
%28 = load double, double* %27
%29 = tail call i64 @pow(double %28, i32 2)
%30 = sitofp i64 %29 to double
%31 = fadd double %25, %30
%32 = or i64 %12, 3
%33 = getelementptr inbounds double, double* %0, i64 %32
%34 = load double, double* %33
%35 = tail call i64 @pow(double %34, i32 2)
%36 = sitofp i64 %35 to double
%37 = fadd double %31, %36
%38 = add nuw nsw i64 %12, 4
%39 = add i64 %14, 4
%40 = icmp eq i64 %39, %10
br i1 %40, label %41, label %11
41: ; preds = %11, %4
%42 = phi double [ undef, %4 ], [ %37, %11 ]
%43 = phi i64 [ 0, %4 ], [ %38, %11 ]
%44 = phi double [ 0.000000e+00, %4 ], [ %37, %11 ]
%45 = icmp eq i64 %7, 0
br i1 %45, label %58, label %46
46: ; preds = %41, %46
%47 = phi i64 [ %55, %46 ], [ %43, %41 ]
%48 = phi double [ %54, %46 ], [ %44, %41 ]
%49 = phi i64 [ %56, %46 ], [ 0, %41 ]
%50 = getelementptr inbounds double, double* %0, i64 %47
%51 = load double, double* %50
%52 = tail call i64 @pow(double %51, i32 2)
%53 = sitofp i64 %52 to double
%54 = fadd double %48, %53
%55 = add nuw nsw i64 %47, 1
%56 = add i64 %49, 1
%57 = icmp eq i64 %56, %7
br i1 %57, label %58, label %46
58: ; preds = %41, %46, %2
%59 = phi double [ 0.000000e+00, %2 ], [ %42, %41 ], [ %54, %46 ]
%60 = tail call double @sqrt(double %59)
ret double %60
}