#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 */
int /*<<< orphan*/ isnan(double);
double chi2(int N, double *t, double *mag, double *sig, double *aveval,
            int *ngood) {
  double avesum1, avesum2, ave, chi2val, v;
  int i;
  *aveval = -1.;
  *ngood = 0;
  if (N > 0) {
    avesum1 = 0.;
    avesum2 = 0.;
    for (i = 0; i < N; i++) {
      if (!isnan(mag[i]) && sig[i] > 0.) {
        (*ngood)++;
        v = sig[i] * sig[i];
        avesum1 += mag[i] / v;
        avesum2 += 1. / v;
      }
    }
    if (avesum2 > 0.) {
      ave = (avesum1 / avesum2);
      *aveval = ave;
      chi2val = 0.;
      for (i = 0; i < N; i++) {
        if (!isnan(mag[i]) && sig[i] > 0.) {
          v = (mag[i] - ave);
          v = v * v;
          chi2val += v / (sig[i] * sig[i]);
        }
      }
    } else
      chi2val = -1.;
  } else
    chi2val = -1.;
  return (chi2val);
}


x86 assembly (clang, -O0):
<chi2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movsd  0xec5(%rip),%xmm0
movsd  %xmm0,(%rax)
mov    -0x30(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x0,-0x4(%rbp)
jle    1316 <chi2+0x206>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    121b <chi2+0x10b>
mov    -0x18(%rbp),%rax
movslq -0x5c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1030 <isnan@plt>
cmp    $0x0,%eax
jne    1208 <chi2+0xf8>
mov    -0x20(%rbp),%rax
movslq -0x5c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1208 <chi2+0xf8>
mov    -0x30(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
movslq -0x5c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x5c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
divsd  -0x58(%rbp),%xmm0
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xe0f(%rip),%xmm0
divsd  -0x58(%rbp),%xmm0
addsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
jmp    120d <chi2+0xfd>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    116a <chi2+0x5a>
movsd  -0x40(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1304 <chi2+0x1f4>
movsd  -0x38(%rbp),%xmm0
divsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
mov    -0x28(%rbp),%rax
movsd  %xmm0,(%rax)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x50(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12ff <chi2+0x1ef>
mov    -0x18(%rbp),%rax
movslq -0x5c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1030 <isnan@plt>
cmp    $0x0,%eax
jne    12ec <chi2+0x1dc>
mov    -0x20(%rbp),%rax
movslq -0x5c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    12ec <chi2+0x1dc>
mov    -0x18(%rbp),%rax
movslq -0x5c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
mulsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x5c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x20(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm1
divsd  %xmm1,%xmm0
addsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
jmp    12f1 <chi2+0x1e1>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1258 <chi2+0x148>
jmp    1311 <chi2+0x201>
movsd  0xcf4(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
jmp    1323 <chi2+0x213>
movsd  0xce2(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chi2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movabs $0xbff0000000000000,%rax
mov    %rax,(%r8)
movl   $0x0,(%r9)
movsd  0xec6(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
test   %edi,%edi
jle    1240 <chi2+0x130>
mov    %r9,%r12
mov    %r8,%r15
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %edi,%r13d
xorpd  %xmm0,%xmm0
xor    %ebp,%ebp
jmp    116f <chi2+0x5f>
nop
movapd 0x10(%rsp),%xmm0
add    $0x1,%rbp
cmp    %rbp,%r13
je     11bd <chi2+0xad>
movapd %xmm0,0x10(%rsp)
movsd  (%rbx,%rbp,8),%xmm0
call   1030 <isnan@plt>
test   %eax,%eax
jne    1160 <chi2+0x50>
movsd  (%r14,%rbp,8),%xmm0
ucomisd 0xe77(%rip),%xmm0
jbe    1160 <chi2+0x50>
addl   $0x1,(%r12)
movapd 0xe70(%rip),%xmm1
movhpd (%rbx,%rbp,8),%xmm1
mulsd  %xmm0,%xmm0
unpcklpd %xmm0,%xmm0
divpd  %xmm0,%xmm1
movapd 0x10(%rsp),%xmm0
addpd  %xmm1,%xmm0
jmp    1166 <chi2+0x56>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1240 <chi2+0x130>
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,0x10(%rsp)
movsd  %xmm1,(%r15)
xor    %ebp,%ebp
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
jmp    11f9 <chi2+0xe9>
nopl   0x0(%rax)
add    $0x1,%rbp
cmp    %rbp,%r13
je     1240 <chi2+0x130>
movsd  (%rbx,%rbp,8),%xmm0
call   1030 <isnan@plt>
test   %eax,%eax
jne    11f0 <chi2+0xe0>
movsd  (%r14,%rbp,8),%xmm0
ucomisd 0xdf3(%rip),%xmm0
jbe    11f0 <chi2+0xe0>
movsd  (%rbx,%rbp,8),%xmm1
subsd  0x10(%rsp),%xmm1
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
divsd  %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,0x8(%rsp)
jmp    11f0 <chi2+0xe0>
movsd  0x8(%rsp),%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define double @chi2(i32 %0, double* nocapture readnone %1, double* nocapture %2, double* nocapture %3, double* nocapture writeonly %4, i32* nocapture %5) {
store double -1.000000e+00, double* %4
store i32 0, i32* %5
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %63
8: ; preds = %6
%9 = zext i32 %0 to i64
br label %10
10: ; preds = %8, %31
%11 = phi i64 [ 0, %8 ], [ %33, %31 ]
%12 = phi <2 x double> [ zeroinitializer, %8 ], [ %32, %31 ]
%13 = getelementptr inbounds double, double* %2, i64 %11
%14 = load double, double* %13
%15 = tail call i32 @isnan(double %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %10
%18 = getelementptr inbounds double, double* %3, i64 %11
%19 = load double, double* %18
%20 = fcmp ogt double %19, 0.000000e+00
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
store i32 %23, i32* %5
%24 = fmul double %19, %19
%25 = load double, double* %13
%26 = insertelement <2 x double> <double 1.000000e+00, double poison>, double %25, i64 1
%27 = insertelement <2 x double> poison, double %24, i64 0
%28 = shufflevector <2 x double> %27, <2 x double> poison, <2 x i32> zeroinitializer
%29 = fdiv <2 x double> %26, %28
%30 = fadd <2 x double> %12, %29
br label %31
31: ; preds = %10, %17, %21
%32 = phi <2 x double> [ %12, %10 ], [ %30, %21 ], [ %12, %17 ]
%33 = add nuw nsw i64 %11, 1
%34 = icmp eq i64 %33, %9
br i1 %34, label %35, label %10
35: ; preds = %31
%36 = extractelement <2 x double> %32, i64 0
%37 = fcmp ogt double %36, 0.000000e+00
br i1 %37, label %38, label %63
38: ; preds = %35
%39 = extractelement <2 x double> %32, i64 1
%40 = fdiv double %39, %36
store double %40, double* %4
br label %41
41: ; preds = %38, %59
%42 = phi i64 [ 0, %38 ], [ %61, %59 ]
%43 = phi double [ 0.000000e+00, %38 ], [ %60, %59 ]
%44 = getelementptr inbounds double, double* %2, i64 %42
%45 = load double, double* %44
%46 = tail call i32 @isnan(double %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %59
48: ; preds = %41
%49 = getelementptr inbounds double, double* %3, i64 %42
%50 = load double, double* %49
%51 = fcmp ogt double %50, 0.000000e+00
br i1 %51, label %52, label %59
52: ; preds = %48
%53 = load double, double* %44
%54 = fsub double %53, %40
%55 = fmul double %54, %54
%56 = fmul double %50, %50
%57 = fdiv double %55, %56
%58 = fadd double %43, %57
br label %59
59: ; preds = %41, %48, %52
%60 = phi double [ %43, %41 ], [ %58, %52 ], [ %43, %48 ]
%61 = add nuw nsw i64 %42, 1
%62 = icmp eq i64 %61, %9
br i1 %62, label %63, label %41
63: ; preds = %59, %6, %35
%64 = phi double [ -1.000000e+00, %35 ], [ -1.000000e+00, %6 ], [ %60, %59 ]
ret double %64
}