double getweightedmean_double(int n, double *data, double *sig) {
  int i;
  double tmp;
  double num = (double)n;
  long double mean_val1 = 0, mean_val2 = 0;
  for (i = 0; i < n; i++) {
    tmp = 1. / sig[i] / sig[i];
    mean_val1 += (long double)data[i] * tmp;
    mean_val2 += (long double)tmp;
  }
  return (double)(mean_val1 / mean_val2);
}


x86 assembly (clang, -O0):
<getweightedmean_double>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cvtsi2sdl -0x4(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
fldz
fld    %st(0)
fstpt  -0x40(%rbp)
fstpt  -0x50(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    118b <getweightedmean_double+0x8b>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  0xeb5(%rip),%xmm0
divsd  %xmm1,%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
fldl   (%rax,%rcx,8)
fldl   -0x28(%rbp)
fldt   -0x40(%rbp)
fxch   %st(2)
fmulp  %st,%st(1)
faddp  %st,%st(1)
fstpt  -0x40(%rbp)
fldl   -0x28(%rbp)
fldt   -0x50(%rbp)
faddp  %st,%st(1)
fstpt  -0x50(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112a <getweightedmean_double+0x2a>
fldt   -0x40(%rbp)
fldt   -0x50(%rbp)
fdivrp %st,%st(1)
fstpl  -0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<getweightedmean_double>:
test   %edi,%edi
jle    115a <getweightedmean_double+0x5a>
mov    %edi,%eax
fldz
xor    %ecx,%ecx
movsd  0xef6(%rip),%xmm0
fld    %st(0)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  (%rdx,%rcx,8),%xmm1
movapd %xmm0,%xmm2
divsd  %xmm1,%xmm2
divsd  %xmm1,%xmm2
movsd  %xmm2,-0x8(%rsp)
fldl   -0x8(%rsp)
fld    %st(0)
fmull  (%rsi,%rcx,8)
faddp  %st,%st(2)
faddp  %st,%st(2)
add    $0x1,%rcx
cmp    %rcx,%rax
jne    1120 <getweightedmean_double+0x20>
fdivp  %st,%st(1)
fstpl  -0x10(%rsp)
movsd  -0x10(%rsp),%xmm0
ret
movsd  0xe9e(%rip),%xmm0
ret

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

LLVM IR (-O2):
define double @getweightedmean_double(i32 %0, double* nocapture %1, double* nocapture %2) {
%4 = icmp sgt i32 %0, 0
br i1 %4, label %5, label %26
5: ; preds = %3
%6 = zext i32 %0 to i64
br label %7
7: ; preds = %5, %7
%8 = phi i64 [ 0, %5 ], [ %21, %7 ]
%9 = phi x86_fp80 [ 0xK00000000000000000000, %5 ], [ %20, %7 ]
%10 = phi x86_fp80 [ 0xK00000000000000000000, %5 ], [ %19, %7 ]
%11 = getelementptr inbounds double, double* %2, i64 %8
%12 = load double, double* %11
%13 = fdiv double 1.000000e+00, %12
%14 = fdiv double %13, %12
%15 = getelementptr inbounds double, double* %1, i64 %8
%16 = load double, double* %15
%17 = fpext double %16 to x86_fp80
%18 = fpext double %14 to x86_fp80
%19 = tail call x86_fp80 @llvm.fmuladd.f80(x86_fp80 %17, x86_fp80 %18, x86_fp80 %10)
%20 = fadd x86_fp80 %9, %18
%21 = add nuw nsw i64 %8, 1
%22 = icmp eq i64 %21, %6
br i1 %22, label %23, label %7
23: ; preds = %7
%24 = fdiv x86_fp80 %19, %20
%25 = fptrunc x86_fp80 %24 to double
br label %26
26: ; preds = %23, %3
%27 = phi double [ %25, %23 ], [ 0x7FF8000000000000, %3 ]
ret double %27
}