#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 sqrt(double);
double dnrm21(int n, const double dx[]) {
  int i;
  double dssq = 1.0, dscale = 0.0;
  for (i = 0; i < n; i++) {
    if (dx[i] != 0.0) {
      double dabsxi = fabs(dx[i]);
      if (dscale < dabsxi) {
        double ratio = dscale / dabsxi;
        dssq = 1.0 + dssq * ratio * ratio;
        dscale = dabsxi;
      } else {
        double ratio = dabsxi / dscale;
        dssq += ratio * ratio;
      }
    }
  }
  return dscale * sqrt(dssq);
}


x86 assembly (clang, -O0):
<dnrm21>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  0xed9(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1222 <dnrm21+0x112>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    116c <dnrm21+0x5c>
jp     116c <dnrm21+0x5c>
jmp    120f <dnrm21+0xff>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movaps 0xe90(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11df <dnrm21+0xcf>
movsd  -0x28(%rbp),%xmm0
divsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x38(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe39(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    120a <dnrm21+0xfa>
movsd  -0x30(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    120f <dnrm21+0xff>
jmp    1214 <dnrm21+0x104>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    113b <dnrm21+0x2b>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
mulsd  %xmm1,%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dnrm21>:
push   %rax
xorpd  %xmm1,%xmm1
test   %edi,%edi
jle    1182 <dnrm21+0x72>
mov    %edi,%eax
xorpd  %xmm2,%xmm2
movsd  0xed9(%rip),%xmm3
xor    %ecx,%ecx
movapd 0xedf(%rip),%xmm4
movapd %xmm3,%xmm0
xorpd  %xmm6,%xmm6
jmp    1155 <dnrm21+0x45>
nopl   0x0(%rax,%rax,1)
divsd  %xmm6,%xmm5
mulsd  %xmm5,%xmm5
addsd  %xmm5,%xmm0
add    $0x1,%rcx
cmp    %rcx,%rax
je     118e <dnrm21+0x7e>
movsd  (%rsi,%rcx,8),%xmm5
ucomisd %xmm2,%xmm5
jne    1162 <dnrm21+0x52>
jnp    114c <dnrm21+0x3c>
andpd  %xmm4,%xmm5
ucomisd %xmm6,%xmm5
jbe    1140 <dnrm21+0x30>
divsd  %xmm5,%xmm6
mulsd  %xmm6,%xmm0
mulsd  %xmm6,%xmm0
addsd  %xmm3,%xmm0
movapd %xmm5,%xmm6
jmp    114c <dnrm21+0x3c>
movsd  0xe76(%rip),%xmm0
xorpd  %xmm6,%xmm6
ucomisd %xmm1,%xmm0
jb     119a <dnrm21+0x8a>
sqrtsd %xmm0,%xmm0
jmp    11a9 <dnrm21+0x99>
movsd  %xmm6,(%rsp)
call   1030 <sqrt@plt>
movsd  (%rsp),%xmm6
mulsd  %xmm0,%xmm6
movapd %xmm6,%xmm0
pop    %rax
ret

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

LLVM IR (-O2):
define double @dnrm21(i32 %0, double* nocapture %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %28
4: ; preds = %2
%5 = zext i32 %0 to i64
br label %6
6: ; preds = %4, %23
%7 = phi i64 [ 0, %4 ], [ %26, %23 ]
%8 = phi double [ 1.000000e+00, %4 ], [ %25, %23 ]
%9 = phi double [ 0.000000e+00, %4 ], [ %24, %23 ]
%10 = getelementptr inbounds double, double* %1, i64 %7
%11 = load double, double* %10
%12 = fcmp une double %11, 0.000000e+00
br i1 %12, label %13, label %23
13: ; preds = %6
%14 = tail call double @llvm.fabs.f64(double %11)
%15 = fcmp olt double %9, %14
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = fdiv double %9, %14
%18 = fmul double %8, %17
%19 = tail call double @llvm.fmuladd.f64(double %18, double %17, double 1.000000e+00)
br label %23
20: ; preds = %13
%21 = fdiv double %14, %9
%22 = tail call double @llvm.fmuladd.f64(double %21, double %21, double %8)
br label %23
23: ; preds = %16, %20, %6
%24 = phi double [ %9, %6 ], [ %14, %16 ], [ %9, %20 ]
%25 = phi double [ %8, %6 ], [ %19, %16 ], [ %22, %20 ]
%26 = add nuw nsw i64 %7, 1
%27 = icmp eq i64 %26, %5
br i1 %27, label %28, label %6
28: ; preds = %23, %2
%29 = phi double [ 0.000000e+00, %2 ], [ %24, %23 ]
%30 = phi double [ 1.000000e+00, %2 ], [ %25, %23 ]
%31 = tail call double @sqrt(double %30)
%32 = fmul double %29, %31
ret double %32
}