void decorr_image_sysrem(double *star_coeff, double **mag, double **sig,
                         double *mag_ave, int numlc, int numJD, int **clip,
                         double sigma_clip2, double upperdiff,
                         double *image_coeff, double saturation) {
  int j, k;
  long double av_val1, av_val2, val1, val2;
  int n;
  for (k = 0; k < numJD; k++) {
    n = 0;
    val1 = 0.0;
    val2 = 0.0;
    av_val1 = 0.0;
    av_val2 = 0.0;
    n = 0;
    for (j = 0; j < numlc; j++) {
      if (clip[j][k]) {
        val1 += (long double)(mag[j][k] - mag_ave[j]) * star_coeff[j] /
                (sig[j][k] * sig[j][k]);
        val2 += (long double)(star_coeff[j] * star_coeff[j]) /
                (sig[j][k] * sig[j][k]);
        n++;
      }
    }
    if (n > 0 && val2 > 0.0)
      image_coeff[k] = (double)val1 / val2;
    else
      image_coeff[k] = 0.0;
  }
}


x86 assembly (clang, -O0):
<decorr_image_sysrem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
movsd  %xmm0,-0x30(%rbp)
movsd  %xmm1,-0x38(%rbp)
movsd  %xmm2,-0x40(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1308 <decorr_image_sysrem+0x208>
movl   $0x0,-0x94(%rbp)
fldz
fld    %st(0)
fstpt  -0x80(%rbp)
fld    %st(0)
fstpt  -0x90(%rbp)
fld    %st(0)
fstpt  -0x60(%rbp)
fstpt  -0x70(%rbp)
movl   $0x0,-0x94(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1278 <decorr_image_sysrem+0x178>
mov    0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x48(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     1265 <decorr_image_sysrem+0x165>
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rdx
mov    (%rax,%rdx,8),%rax
movslq -0x48(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
movsd  (%rax,%rdx,8),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0xc0(%rbp)
ff
fldl   -0xc0(%rbp)
mov    -0x8(%rbp),%rax
fldl   (%rax,%rdx,8)
fmulp  %st,%st(1)
mov    -0x18(%rbp),%rax
mov    (%rax,%rdx,8),%rax
movsd  (%rax,%rcx,8),%xmm0
mulsd  %xmm0,%xmm0
movsd  %xmm0,-0xb8(%rbp)
ff
fldl   -0xb8(%rbp)
fdivrp %st,%st(1)
fldt   -0x80(%rbp)
faddp  %st,%st(1)
fstpt  -0x80(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mulsd  %xmm0,%xmm0
movsd  %xmm0,-0xc8(%rbp)
ff
fldl   -0xc8(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax,%rcx,8),%rax
movslq -0x48(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mulsd  %xmm0,%xmm0
movsd  %xmm0,-0xd0(%rbp)
ff
fldl   -0xd0(%rbp)
fdivrp %st,%st(1)
fldt   -0x90(%rbp)
faddp  %st,%st(1)
fstpt  -0x90(%rbp)
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x94(%rbp)
jmp    126a <decorr_image_sysrem+0x16a>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    117c <decorr_image_sysrem+0x7c>
cmpl   $0x0,-0x94(%rbp)
jle    12e5 <decorr_image_sysrem+0x1e5>
fldt   -0x90(%rbp)
fldz
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jbe    12e5 <decorr_image_sysrem+0x1e5>
jmp    129e <decorr_image_sysrem+0x19e>
fldt   -0x80(%rbp)
fstpl  -0xa0(%rbp)
movsd  -0xa0(%rbp),%xmm0
ff
movsd  %xmm0,-0xa8(%rbp)
ff
fldl   -0xa8(%rbp)
fldt   -0x90(%rbp)
fdivrp %st,%st(1)
fstpl  -0xb0(%rbp)
movsd  -0xb0(%rbp),%xmm0
ff
mov    0x18(%rbp),%rax
movslq -0x48(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    12f5 <decorr_image_sysrem+0x1f5>
mov    0x18(%rbp),%rax
movslq -0x48(%rbp),%rcx
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
jmp    12fa <decorr_image_sysrem+0x1fa>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    113e <decorr_image_sysrem+0x3e>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decorr_image_sysrem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
test   %r9d,%r9d
fldz
jle    1218 <decorr_image_sysrem+0x118>
fstp   %st(0)
mov    0x30(%rsp),%r10
mov    0x28(%rsp),%r11
mov    %r9d,%r9d
mov    %r8d,%r14d
xor    %ebx,%ebx
fldz
jmp    114d <decorr_image_sysrem+0x4d>
fstp   %st(1)
fstp   %st(0)
nopl   (%rax)
fldz
fldz
fxch   %st(1)
fstp   %st(0)
fstp   %st(0)
movsd  %xmm0,(%r10,%rbx,8)
add    $0x1,%rbx
cmp    %r9,%rbx
je     1218 <decorr_image_sysrem+0x118>
xorps  %xmm0,%xmm0
test   %r8d,%r8d
jle    1130 <decorr_image_sysrem+0x30>
xor    %eax,%eax
xor    %r15d,%r15d
fld    %st(0)
fld    %st(1)
jmp    1169 <decorr_image_sysrem+0x69>
add    $0x1,%rax
cmp    %rax,%r14
je     11e0 <decorr_image_sysrem+0xe0>
mov    (%r11,%rax,8),%rbp
cmpl   $0x0,0x0(%rbp,%rbx,4)
je     1160 <decorr_image_sysrem+0x60>
mov    (%rsi,%rax,8),%rbp
movsd  0x0(%rbp,%rbx,8),%xmm1
subsd  (%rcx,%rax,8),%xmm1
movsd  %xmm1,-0x20(%rsp)
fldl   -0x20(%rsp)
movsd  (%rdi,%rax,8),%xmm1
movsd  %xmm1,-0x8(%rsp)
fmull  -0x8(%rsp)
mov    (%rdx,%rax,8),%rbp
movsd  0x0(%rbp,%rbx,8),%xmm2
mulsd  %xmm2,%xmm2
movsd  %xmm2,-0x18(%rsp)
fldl   -0x18(%rsp)
fdivr  %st,%st(1)
fxch   %st(2)
faddp  %st,%st(1)
mulsd  %xmm1,%xmm1
movsd  %xmm1,-0x10(%rsp)
fxch   %st(1)
fdivrl -0x10(%rsp)
faddp  %st,%st(2)
add    $0x1,%r15d
jmp    1160 <decorr_image_sysrem+0x60>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %r15d,%r15d
jle    1129 <decorr_image_sysrem+0x29>
fxch   %st(1)
fucomi %st(2),%st
jbe    1136 <decorr_image_sysrem+0x36>
fxch   %st(1)
fstpl  -0x28(%rsp)
movsd  -0x28(%rsp),%xmm0
movsd  %xmm0,-0x30(%rsp)
fdivrl -0x30(%rsp)
fstpl  -0x38(%rsp)
movsd  -0x38(%rsp),%xmm0
jmp    1130 <decorr_image_sysrem+0x30>
fstp   %st(0)
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @decorr_image_sysrem(double* %0, double** %1, double** %2, double* %3, i32 %4, i32 %5, i32** %6, double %7, double %8, double* %9, double %10) {
%12 = alloca double*
%13 = alloca double**
%14 = alloca double**
%15 = alloca double*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32**
%19 = alloca double
%20 = alloca double
%21 = alloca double*
%22 = alloca double
%23 = alloca i32
%24 = alloca i32
%25 = alloca x86_fp80
%26 = alloca x86_fp80
%27 = alloca x86_fp80
%28 = alloca x86_fp80
%29 = alloca i32
store double* %0, double** %12
store double** %1, double*** %13
store double** %2, double*** %14
store double* %3, double** %15
store i32 %4, i32* %16
store i32 %5, i32* %17
store i32** %6, i32*** %18
store double %7, double* %19
store double %8, double* %20
store double* %9, double** %21
store double %10, double* %22
store i32 0, i32* %24
br label %30
30: ; preds = %161, %11
%31 = load i32, i32* %24
%32 = load i32, i32* %17
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %164
34: ; preds = %30
store i32 0, i32* %29
store x86_fp80 0xK00000000000000000000, x86_fp80* %27
store x86_fp80 0xK00000000000000000000, x86_fp80* %28
store x86_fp80 0xK00000000000000000000, x86_fp80* %25
store x86_fp80 0xK00000000000000000000, x86_fp80* %26
store i32 0, i32* %29
store i32 0, i32* %23
br label %35
35: ; preds = %135, %34
%36 = load i32, i32* %23
%37 = load i32, i32* %16
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %138
39: ; preds = %35
%40 = load i32**, i32*** %18
%41 = load i32, i32* %23
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32*, i32** %40, i64 %42
%44 = load i32*, i32** %43
%45 = load i32, i32* %24
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %134
50: ; preds = %39
%51 = load double**, double*** %13
%52 = load i32, i32* %23
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds double*, double** %51, i64 %53
%55 = load double*, double** %54
%56 = load i32, i32* %24
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds double, double* %55, i64 %57
%59 = load double, double* %58
%60 = load double*, double** %15
%61 = load i32, i32* %23
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds double, double* %60, i64 %62
%64 = load double, double* %63
%65 = fsub double %59, %64
%66 = fpext double %65 to x86_fp80
%67 = load double*, double** %12
%68 = load i32, i32* %23
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds double, double* %67, i64 %69
%71 = load double, double* %70
%72 = fpext double %71 to x86_fp80
%73 = fmul x86_fp80 %66, %72
%74 = load double**, double*** %14
%75 = load i32, i32* %23
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds double*, double** %74, i64 %76
%78 = load double*, double** %77
%79 = load i32, i32* %24
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds double, double* %78, i64 %80
%82 = load double, double* %81
%83 = load double**, double*** %14
%84 = load i32, i32* %23
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds double*, double** %83, i64 %85
%87 = load double*, double** %86
%88 = load i32, i32* %24
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds double, double* %87, i64 %89
%91 = load double, double* %90
%92 = fmul double %82, %91
%93 = fpext double %92 to x86_fp80
%94 = fdiv x86_fp80 %73, %93
%95 = load x86_fp80, x86_fp80* %27
%96 = fadd x86_fp80 %95, %94
store x86_fp80 %96, x86_fp80* %27
%97 = load double*, double** %12
%98 = load i32, i32* %23
%99 = sext i32 %98 to i64
%100 = getelementptr inbounds double, double* %97, i64 %99
%101 = load double, double* %100
%102 = load double*, double** %12
%103 = load i32, i32* %23
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds double, double* %102, i64 %104
%106 = load double, double* %105
%107 = fmul double %101, %106
%108 = fpext double %107 to x86_fp80
%109 = load double**, double*** %14
%110 = load i32, i32* %23
%111 = sext i32 %110 to i64
%112 = getelementptr inbounds double*, double** %109, i64 %111
%113 = load double*, double** %112
%114 = load i32, i32* %24
%115 = sext i32 %114 to i64
%116 = getelementptr inbounds double, double* %113, i64 %115
%117 = load double, double* %116
%118 = load double**, double*** %14
%119 = load i32, i32* %23
%120 = sext i32 %119 to i64
%121 = getelementptr inbounds double*, double** %118, i64 %120
%122 = load double*, double** %121
%123 = load i32, i32* %24
%124 = sext i32 %123 to i64
%125 = getelementptr inbounds double, double* %122, i64 %124
%126 = load double, double* %125
%127 = fmul double %117, %126
%128 = fpext double %127 to x86_fp80
%129 = fdiv x86_fp80 %108, %128
%130 = load x86_fp80, x86_fp80* %28
%131 = fadd x86_fp80 %130, %129
store x86_fp80 %131, x86_fp80* %28
%132 = load i32, i32* %29
%133 = add nsw i32 %132, 1
store i32 %133, i32* %29
br label %134
134: ; preds = %50, %39
br label %135
135: ; preds = %134
%136 = load i32, i32* %23
%137 = add nsw i32 %136, 1
store i32 %137, i32* %23
br label %35
138: ; preds = %35
%139 = load i32, i32* %29
%140 = icmp sgt i32 %139, 0
br i1 %140, label %141, label %155
141: ; preds = %138
%142 = load x86_fp80, x86_fp80* %28
%143 = fcmp ogt x86_fp80 %142, 0xK00000000000000000000
br i1 %143, label %144, label %155
144: ; preds = %141
%145 = load x86_fp80, x86_fp80* %27
%146 = fptrunc x86_fp80 %145 to double
%147 = fpext double %146 to x86_fp80
%148 = load x86_fp80, x86_fp80* %28
%149 = fdiv x86_fp80 %147, %148
%150 = fptrunc x86_fp80 %149 to double
%151 = load double*, double** %21
%152 = load i32, i32* %24
%153 = sext i32 %152 to i64
%154 = getelementptr inbounds double, double* %151, i64 %153
store double %150, double* %154
br label %160
155: ; preds = %141, %138
%156 = load double*, double** %21
%157 = load i32, i32* %24
%158 = sext i32 %157 to i64
%159 = getelementptr inbounds double, double* %156, i64 %158
store double 0.000000e+00, double* %159
br label %160
160: ; preds = %155, %144
br label %161
161: ; preds = %160
%162 = load i32, i32* %24
%163 = add nsw i32 %162, 1
store i32 %163, i32* %24
br label %30
164: ; preds = %30
ret void
}

LLVM IR (-O2):
define void @decorr_image_sysrem(double* nocapture %0, double** nocapture %1, double** nocapture %2, double* nocapture %3, i32 %4, i32 %5, i32** nocapture %6, double %7, double %8, double* nocapture writeonly %9, double %10) {
%12 = icmp sgt i32 %5, 0
br i1 %12, label %13, label %75
13: ; preds = %11
%14 = icmp sgt i32 %4, 0
%15 = zext i32 %5 to i64
%16 = zext i32 %4 to i64
br label %17
17: ; preds = %13, %70
%18 = phi i64 [ 0, %13 ], [ %73, %70 ]
br i1 %14, label %19, label %70
19: ; preds = %17, %55
%20 = phi i64 [ %59, %55 ], [ 0, %17 ]
%21 = phi i32 [ %58, %55 ], [ 0, %17 ]
%22 = phi x86_fp80 [ %57, %55 ], [ 0xK00000000000000000000, %17 ]
%23 = phi x86_fp80 [ %56, %55 ], [ 0xK00000000000000000000, %17 ]
%24 = getelementptr inbounds i32*, i32** %6, i64 %20
%25 = load i32*, i32** %24
%26 = getelementptr inbounds i32, i32* %25, i64 %18
%27 = load i32, i32* %26
%28 = icmp eq i32 %27, 0
br i1 %28, label %55, label %29
29: ; preds = %19
%30 = getelementptr inbounds double*, double** %1, i64 %20
%31 = load double*, double** %30
%32 = getelementptr inbounds double, double* %31, i64 %18
%33 = load double, double* %32
%34 = getelementptr inbounds double, double* %3, i64 %20
%35 = load double, double* %34
%36 = fsub double %33, %35
%37 = fpext double %36 to x86_fp80
%38 = getelementptr inbounds double, double* %0, i64 %20
%39 = load double, double* %38
%40 = fpext double %39 to x86_fp80
%41 = fmul x86_fp80 %37, %40
%42 = getelementptr inbounds double*, double** %2, i64 %20
%43 = load double*, double** %42
%44 = getelementptr inbounds double, double* %43, i64 %18
%45 = load double, double* %44
%46 = fmul double %45, %45
%47 = fpext double %46 to x86_fp80
%48 = fdiv x86_fp80 %41, %47
%49 = fadd x86_fp80 %23, %48
%50 = fmul double %39, %39
%51 = fpext double %50 to x86_fp80
%52 = fdiv x86_fp80 %51, %47
%53 = fadd x86_fp80 %22, %52
%54 = add nsw i32 %21, 1
br label %55
55: ; preds = %19, %29
%56 = phi x86_fp80 [ %49, %29 ], [ %23, %19 ]
%57 = phi x86_fp80 [ %53, %29 ], [ %22, %19 ]
%58 = phi i32 [ %54, %29 ], [ %21, %19 ]
%59 = add nuw nsw i64 %20, 1
%60 = icmp eq i64 %59, %16
br i1 %60, label %61, label %19
61: ; preds = %55
%62 = icmp sgt i32 %58, 0
%63 = fcmp ogt x86_fp80 %57, 0xK00000000000000000000
%64 = select i1 %62, i1 %63, i1 false
br i1 %64, label %65, label %70
65: ; preds = %61
%66 = fptrunc x86_fp80 %56 to double
%67 = fpext double %66 to x86_fp80
%68 = fdiv x86_fp80 %67, %57
%69 = fptrunc x86_fp80 %68 to double
br label %70
70: ; preds = %17, %61, %65
%71 = phi double [ %69, %65 ], [ 0.000000e+00, %61 ], [ 0.000000e+00, %17 ]
%72 = getelementptr inbounds double, double* %9, i64 %18
store double %71, double* %72
%73 = add nuw nsw i64 %18, 1
%74 = icmp eq i64 %73, %15
br i1 %74, label %75, label %17
75: ; preds = %70, %11
ret void
}