#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);
int doubleweightedmeanrms(int ndat, double *dat, double *weight, double *mean,
                          double *sigma) {
  int i;
  double x;
  double sx = 0;
  double sx2 = 0;
  double w = 0;
  for (i = 0; i < ndat; i++) {
    x = dat[i] - sx;
    sx += x * (double)weight[i] / ((double)weight[i] + w);
    sx2 += x * x * (double)weight[i] * w / ((double)weight[i] + w);
    w += weight[i];
  }
  *mean = sx;
  if (w > 0)
    *sigma = sqrt(sx2 / w);
  return 0;
}


x86 assembly (clang, -O0):
<doubleweightedmeanrms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x50(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1200 <doubleweightedmeanrms+0xf0>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
addsd  -0x50(%rbp),%xmm1
divsd  %xmm1,%xmm0
addsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
mulsd  -0x38(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
mulsd  -0x50(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
addsd  -0x50(%rbp),%xmm1
divsd  %xmm1,%xmm0
addsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    114a <doubleweightedmeanrms+0x3a>
movsd  -0x40(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x50(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1236 <doubleweightedmeanrms+0x126>
movsd  -0x48(%rbp),%xmm0
divsd  -0x50(%rbp),%xmm0
call   1030 <sqrt@plt>
mov    -0x28(%rbp),%rax
movsd  %xmm0,(%rax)
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doubleweightedmeanrms>:
push   %rbx
test   %edi,%edi
jle    119d <doubleweightedmeanrms+0x8d>
mov    %r8,%rbx
mov    %edi,%eax
xorpd  %xmm0,%xmm0
xorpd  %xmm1,%xmm1
xor    %edi,%edi
nopl   0x0(%rax,%rax,1)
movapd %xmm0,%xmm2
movapd %xmm1,%xmm3
movsd  (%rsi,%rdi,8),%xmm0
movapd %xmm2,%xmm4
unpckhpd %xmm2,%xmm4
subsd  %xmm4,%xmm0
movsd  (%rdx,%rdi,8),%xmm4
addsd  %xmm4,%xmm1
unpcklpd %xmm0,%xmm3
mulsd  %xmm0,%xmm0
mulsd  %xmm4,%xmm0
unpcklpd %xmm4,%xmm0
mulpd  %xmm3,%xmm0
movapd %xmm1,%xmm3
unpcklpd %xmm1,%xmm3
divpd  %xmm3,%xmm0
addpd  %xmm2,%xmm0
add    $0x1,%rdi
cmp    %rdi,%rax
jne    1130 <doubleweightedmeanrms+0x20>
movhpd %xmm0,(%rcx)
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm1
jbe    11af <doubleweightedmeanrms+0x9f>
divsd  %xmm1,%xmm0
ucomisd %xmm2,%xmm0
jb     11a6 <doubleweightedmeanrms+0x96>
sqrtsd %xmm0,%xmm0
jmp    11ab <doubleweightedmeanrms+0x9b>
movq   $0x0,(%rcx)
jmp    11af <doubleweightedmeanrms+0x9f>
call   1030 <sqrt@plt>
movsd  %xmm0,(%rbx)
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
define i32 @doubleweightedmeanrms(i32 %0, double* nocapture %1, double* nocapture %2, double* nocapture writeonly %3, double* nocapture writeonly %4) {
%6 = icmp sgt i32 %0, 0
br i1 %6, label %8, label %7
7: ; preds = %5
store double 0.000000e+00, double* %3
br label %41
8: ; preds = %5
%9 = zext i32 %0 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %32, %10 ]
%12 = phi double [ 0.000000e+00, %8 ], [ %20, %10 ]
%13 = phi <2 x double> [ zeroinitializer, %8 ], [ %31, %10 ]
%14 = getelementptr inbounds double, double* %1, i64 %11
%15 = load double, double* %14
%16 = extractelement <2 x double> %13, i64 1
%17 = fsub double %15, %16
%18 = getelementptr inbounds double, double* %2, i64 %11
%19 = load double, double* %18
%20 = fadd double %12, %19
%21 = fmul double %17, %17
%22 = fmul double %19, %21
%23 = insertelement <2 x double> poison, double %12, i64 0
%24 = insertelement <2 x double> %23, double %17, i64 1
%25 = insertelement <2 x double> poison, double %22, i64 0
%26 = insertelement <2 x double> %25, double %19, i64 1
%27 = fmul <2 x double> %24, %26
%28 = insertelement <2 x double> poison, double %20, i64 0
%29 = shufflevector <2 x double> %28, <2 x double> poison, <2 x i32> zeroinitializer
%30 = fdiv <2 x double> %27, %29
%31 = fadd <2 x double> %13, %30
%32 = add nuw nsw i64 %11, 1
%33 = icmp eq i64 %32, %9
br i1 %33, label %34, label %10
34: ; preds = %10
%35 = extractelement <2 x double> %31, i64 1
store double %35, double* %3
%36 = fcmp ogt double %20, 0.000000e+00
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = extractelement <2 x double> %31, i64 0
%39 = fdiv double %38, %20
%40 = tail call double @sqrt(double %39)
store double %40, double* %4
br label %41
41: ; preds = %7, %37, %34
ret i32 0
}