#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 fabs(double const);
double pow(double, int);
double sqrt(double);
double c_dnrm2(const int N, const double *X, const int stride) {
  double scale;
  double ssq;
  double ax;
  int i;
  if (N <= 0 || stride <= 0) {
    return 0.0;
  }
  if (N == 1) {
    return fabs(X[0]);
  }
  scale = 0.0;
  ssq = 1.0;
  for (i = 0; i < N * stride; i += stride) {
    if (X[i] != 0.0) {
      ax = fabs(X[i]);
      if (scale < ax) {
        ssq = 1.0 + (ssq * pow(scale / ax, 2));
        scale = ax;
      } else {
        ssq += pow(ax / scale, 2);
      }
    }
  }
  return scale * sqrt(ssq);
}


x86 assembly (clang, -O0):
<c_dnrm2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1146 <c_dnrm2+0x26>
cmpl   $0x0,-0x1c(%rbp)
jg     1153 <c_dnrm2+0x33>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a3 <c_dnrm2+0x183>
cmpl   $0x1,-0xc(%rbp)
jne    117a <c_dnrm2+0x5a>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
movaps 0xea4(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a3 <c_dnrm2+0x183>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe76(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0xc(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
cmp    %ecx,%eax
jge    127e <c_dnrm2+0x15e>
mov    -0x18(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11cd <c_dnrm2+0xad>
jp     11cd <c_dnrm2+0xad>
jmp    126b <c_dnrm2+0x14b>
mov    -0x18(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movaps 0xe2f(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1248 <c_dnrm2+0x128>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x28(%rbp),%xmm0
divsd  -0x38(%rbp),%xmm0
mov    $0x2,%edi
call   1030 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xdd0(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1266 <c_dnrm2+0x146>
movsd  -0x38(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
mov    $0x2,%edi
call   1030 <pow@plt>
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    126b <c_dnrm2+0x14b>
jmp    1270 <c_dnrm2+0x150>
mov    -0x1c(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    1196 <c_dnrm2+0x76>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1040 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c_dnrm2>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
xorpd  %xmm1,%xmm1
test   %edi,%edi
jle    123b <c_dnrm2+0x11b>
test   %edx,%edx
jle    123b <c_dnrm2+0x11b>
mov    %rsi,%r15
cmp    $0x1,%edi
jne    1159 <c_dnrm2+0x39>
movsd  (%r15),%xmm0
andpd  0xebc(%rip),%xmm0
jmp    123f <c_dnrm2+0x11f>
imul   %edx,%edi
test   %edi,%edi
jle    120c <c_dnrm2+0xec>
mov    %edx,%r14d
mov    %edi,%r12d
movsd  0xe8e(%rip),%xmm0
xor    %ebx,%ebx
xorpd  %xmm1,%xmm1
jmp    11b2 <c_dnrm2+0x92>
nopw   0x0(%rax,%rax,1)
divsd  %xmm1,%xmm2
movapd %xmm2,%xmm0
mov    $0x2,%edi
movsd  %xmm1,(%rsp)
call   1030 <pow@plt>
movsd  (%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
addsd  %xmm0,%xmm2
movapd %xmm2,%xmm0
add    %r14,%rbx
cmp    %r12,%rbx
jge    1218 <c_dnrm2+0xf8>
movsd  (%r15,%rbx,8),%xmm2
ucomisd 0xe48(%rip),%xmm2
jne    11c4 <c_dnrm2+0xa4>
jnp    11aa <c_dnrm2+0x8a>
movsd  %xmm0,0x10(%rsp)
andpd  0xe3e(%rip),%xmm2
ucomisd %xmm1,%xmm2
jbe    1180 <c_dnrm2+0x60>
divsd  %xmm2,%xmm1
movapd %xmm1,%xmm0
mov    $0x2,%edi
movapd %xmm2,(%rsp)
call   1030 <pow@plt>
movsd  0x10(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
addsd  0xdfb(%rip),%xmm0
movapd (%rsp),%xmm1
jmp    11aa <c_dnrm2+0x8a>
movsd  0xdec(%rip),%xmm0
xorpd  %xmm1,%xmm1
ucomisd 0xde8(%rip),%xmm0
jb     1228 <c_dnrm2+0x108>
sqrtsd %xmm0,%xmm0
jmp    1237 <c_dnrm2+0x117>
movsd  %xmm1,(%rsp)
call   1040 <sqrt@plt>
movsd  (%rsp),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @c_dnrm2(i32 %0, double* %1, i32 %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca double*
%7 = alloca i32
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca i32
store i32 %0, i32* %5
store double* %1, double** %6
store i32 %2, i32* %7
%12 = load i32, i32* %5
%13 = icmp sle i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14, %3
store double 0.000000e+00, double* %4
br label %76
18: ; preds = %14
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load double*, double** %6
%23 = getelementptr inbounds double, double* %22, i64 0
%24 = load double, double* %23
%25 = call double @llvm.fabs.f64(double %24)
store double %25, double* %4
br label %76
26: ; preds = %18
store double 0.000000e+00, double* %8
store double 1.000000e+00, double* %9
store i32 0, i32* %11
br label %27
27: ; preds = %67, %26
%28 = load i32, i32* %11
%29 = load i32, i32* %5
%30 = load i32, i32* %7
%31 = mul nsw i32 %29, %30
%32 = icmp slt i32 %28, %31
br i1 %32, label %33, label %71
33: ; preds = %27
%34 = load double*, double** %6
%35 = load i32, i32* %11
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds double, double* %34, i64 %36
%38 = load double, double* %37
%39 = fcmp une double %38, 0.000000e+00
br i1 %39, label %40, label %66
40: ; preds = %33
%41 = load double*, double** %6
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds double, double* %41, i64 %43
%45 = load double, double* %44
%46 = call double @llvm.fabs.f64(double %45)
store double %46, double* %10
%47 = load double, double* %8
%48 = load double, double* %10
%49 = fcmp olt double %47, %48
br i1 %49, label %50, label %58
50: ; preds = %40
%51 = load double, double* %9
%52 = load double, double* %8
%53 = load double, double* %10
%54 = fdiv double %52, %53
%55 = call double @pow(double %54, i32 2)
%56 = call double @llvm.fmuladd.f64(double %51, double %55, double 1.000000e+00)
store double %56, double* %9
%57 = load double, double* %10
store double %57, double* %8
br label %65
58: ; preds = %40
%59 = load double, double* %10
%60 = load double, double* %8
%61 = fdiv double %59, %60
%62 = call double @pow(double %61, i32 2)
%63 = load double, double* %9
%64 = fadd double %63, %62
store double %64, double* %9
br label %65
65: ; preds = %58, %50
br label %66
66: ; preds = %65, %33
br label %67
67: ; preds = %66
%68 = load i32, i32* %7
%69 = load i32, i32* %11
%70 = add nsw i32 %69, %68
store i32 %70, i32* %11
br label %27
71: ; preds = %27
%72 = load double, double* %8
%73 = load double, double* %9
%74 = call double @sqrt(double %73)
%75 = fmul double %72, %74
store double %75, double* %4
br label %76
76: ; preds = %71, %21, %17
%77 = load double, double* %4
ret double %77
}

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