#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*/ glbGetBGCenters(int, int, double *, double *);
double *glbGetBGFitRatePtr(int, int);
int /*<<< orphan*/ glbGetEnergyWindowBins(int, int, int *, int *);
double *glbGetRuleRatePtr(int, int);
double *glbGetSignalFitRatePtr(int, int);
scalar_t__ glb_likelihood(double, double);
double glbChiSpectrumOnly(int exp, int rule, int n_params, double *x,
                          double *errors, void *user_data) {
  double *true_rates = glbGetRuleRatePtr(exp, rule);
  double *signal_fit_rates = glbGetSignalFitRatePtr(exp, rule);
  double *bg_fit_rates = glbGetBGFitRatePtr(exp, rule);
  double signal_norm;
  double bg_norm_center, bg_tilt_center;
  int ew_low, ew_high;
  double fit_rate, true_rate;
  double chi2 = 0.0;
  int i;
  signal_norm = 1.0 + x[0];
  glbGetEnergyWindowBins(exp, rule, &ew_low, &ew_high);
  glbGetBGCenters(exp, rule, &bg_norm_center, &bg_tilt_center);
  for (i = ew_low; i <= ew_high; i++) {
    fit_rate =
        signal_norm * signal_fit_rates[i] + bg_norm_center * bg_fit_rates[i];
    chi2 += glb_likelihood(true_rates[i], fit_rate);
  }
  return chi2;
}


x86 assembly (clang, -O0):
<glbChiSpectrumOnly>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <glbGetRuleRatePtr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <glbGetSignalFitRatePtr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1070 <glbGetBGFitRatePtr@plt>
mov    %rax,-0x40(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x78(%rbp)
mov    -0x18(%rbp),%rax
movsd  0xe3f(%rip),%xmm0
addsd  (%rax),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    -0x5c(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   1030 <glbGetEnergyWindowBins@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1080 <glbGetBGCenters@plt>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x7c(%rbp),%eax
cmp    -0x60(%rbp),%eax
jg     1267 <glbChiSpectrumOnly+0x107>
movsd  -0x48(%rbp),%xmm0
mov    -0x38(%rbp),%rax
movslq -0x7c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x50(%rbp),%xmm1
mov    -0x40(%rbp),%rax
movslq -0x7c(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x7c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  -0x68(%rbp),%xmm1
call   1060 <glb_likelihood@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x78(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
mov    -0x7c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    11f6 <glbChiSpectrumOnly+0x96>
movsd  -0x78(%rbp),%xmm0
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<glbChiSpectrumOnly>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r13
mov    %esi,%r12d
mov    %edi,%ebp
call   1040 <glbGetRuleRatePtr@plt>
mov    %rax,%r14
mov    %ebp,%edi
mov    %r12d,%esi
call   1050 <glbGetSignalFitRatePtr@plt>
mov    %rax,%r15
mov    %ebp,%edi
mov    %r12d,%esi
call   1070 <glbGetBGFitRatePtr@plt>
mov    %rax,%rbx
movsd  0x0(%r13),%xmm0
movsd  %xmm0,0x10(%rsp)
lea    0x24(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    %r12d,%esi
call   1030 <glbGetEnergyWindowBins@plt>
lea    0x28(%rsp),%rdx
lea    0x30(%rsp),%rcx
mov    %ebp,%edi
mov    %r12d,%esi
call   1080 <glbGetBGCenters@plt>
movslq 0x24(%rsp),%rbp
xorpd  %xmm0,%xmm0
cmp    0xc(%rsp),%ebp
jg     125b <glbChiSpectrumOnly+0xfb>
movsd  0x10(%rsp),%xmm0
addsd  0xe13(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
add    $0xffffffffffffffff,%rbp
xorpd  %xmm0,%xmm0
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,0x18(%rsp)
movsd  0x28(%rsp),%xmm0
mulsd  0x8(%rbx,%rbp,8),%xmm0
movsd  0x8(%r15,%rbp,8),%xmm1
mulsd  0x10(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  0x8(%r14,%rbp,8),%xmm0
call   1060 <glb_likelihood@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  0x18(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x18(%rsp)
movsd  0x18(%rsp),%xmm0
movslq 0xc(%rsp),%rax
add    $0x1,%rbp
cmp    %rax,%rbp
jl     1200 <glbChiSpectrumOnly+0xa0>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @glbChiSpectrumOnly(i32 %0, i32 %1, i32 %2, double* %3, double* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca double*
%11 = alloca double*
%12 = alloca i8*
%13 = alloca double*
%14 = alloca double*
%15 = alloca double*
%16 = alloca double
%17 = alloca double
%18 = alloca double
%19 = alloca i32
%20 = alloca i32
%21 = alloca double
%22 = alloca double
%23 = alloca double
%24 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store double* %3, double** %10
store double* %4, double** %11
store i8* %5, i8** %12
%25 = load i32, i32* %7
%26 = load i32, i32* %8
%27 = call double* @glbGetRuleRatePtr(i32 %25, i32 %26)
store double* %27, double** %13
%28 = load i32, i32* %7
%29 = load i32, i32* %8
%30 = call double* @glbGetSignalFitRatePtr(i32 %28, i32 %29)
store double* %30, double** %14
%31 = load i32, i32* %7
%32 = load i32, i32* %8
%33 = call double* @glbGetBGFitRatePtr(i32 %31, i32 %32)
store double* %33, double** %15
store double 0.000000e+00, double* %23
%34 = load double*, double** %10
%35 = getelementptr inbounds double, double* %34, i64 0
%36 = load double, double* %35
%37 = fadd double 1.000000e+00, %36
store double %37, double* %16
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = call i32 @glbGetEnergyWindowBins(i32 %38, i32 %39, i32* %19, i32* %20)
%41 = load i32, i32* %7
%42 = load i32, i32* %8
%43 = call i32 @glbGetBGCenters(i32 %41, i32 %42, double* %17, double* %18)
%44 = load i32, i32* %19
store i32 %44, i32* %24
br label %45
45: ; preds = %74, %6
%46 = load i32, i32* %24
%47 = load i32, i32* %20
%48 = icmp sle i32 %46, %47
br i1 %48, label %49, label %77
49: ; preds = %45
%50 = load double, double* %16
%51 = load double*, double** %14
%52 = load i32, i32* %24
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds double, double* %51, i64 %53
%55 = load double, double* %54
%56 = load double, double* %17
%57 = load double*, double** %15
%58 = load i32, i32* %24
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds double, double* %57, i64 %59
%61 = load double, double* %60
%62 = fmul double %56, %61
%63 = call double @llvm.fmuladd.f64(double %50, double %55, double %62)
store double %63, double* %21
%64 = load double*, double** %13
%65 = load i32, i32* %24
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds double, double* %64, i64 %66
%68 = load double, double* %67
%69 = load double, double* %21
%70 = call i64 @glb_likelihood(double %68, double %69)
%71 = sitofp i64 %70 to double
%72 = load double, double* %23
%73 = fadd double %72, %71
store double %73, double* %23
br label %74
74: ; preds = %49
%75 = load i32, i32* %24
%76 = add nsw i32 %75, 1
store i32 %76, i32* %24
br label %45
77: ; preds = %45
%78 = load double, double* %23
ret double %78
}

LLVM IR (-O2):
define double @glbChiSpectrumOnly(i32 %0, i32 %1, i32 %2, double* nocapture %3, double* nocapture readnone %4, i8* nocapture readnone %5) {
%7 = alloca double
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
%11 = tail call double* @glbGetRuleRatePtr(i32 %0, i32 %1)
%12 = tail call double* @glbGetSignalFitRatePtr(i32 %0, i32 %1)
%13 = tail call double* @glbGetBGFitRatePtr(i32 %0, i32 %1)
%14 = bitcast double* %7 to i8*
%15 = bitcast double* %8 to i8*
%16 = bitcast i32* %9 to i8*
%17 = bitcast i32* %10 to i8*
%18 = load double, double* %3
%19 = fadd double %18, 1.000000e+00
%20 = call i32 @glbGetEnergyWindowBins(i32 %0, i32 %1, i32* %9, i32* %10)
%21 = call i32 @glbGetBGCenters(i32 %0, i32 %1, double* %7, double* %8)
%22 = load i32, i32* %9
%23 = load i32, i32* %10
%24 = icmp sgt i32 %22, %23
br i1 %24, label %46, label %25
25: ; preds = %6
%26 = sext i32 %22 to i64
br label %27
27: ; preds = %25, %27
%28 = phi i64 [ %26, %25 ], [ %42, %27 ]
%29 = phi double [ 0.000000e+00, %25 ], [ %41, %27 ]
%30 = getelementptr inbounds double, double* %12, i64 %28
%31 = load double, double* %30
%32 = load double, double* %7
%33 = getelementptr inbounds double, double* %13, i64 %28
%34 = load double, double* %33
%35 = fmul double %32, %34
%36 = call double @llvm.fmuladd.f64(double %19, double %31, double %35)
%37 = getelementptr inbounds double, double* %11, i64 %28
%38 = load double, double* %37
%39 = call i64 @glb_likelihood(double %38, double %36)
%40 = sitofp i64 %39 to double
%41 = fadd double %29, %40
%42 = add nsw i64 %28, 1
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = icmp slt i64 %28, %44
br i1 %45, label %27, label %46
46: ; preds = %27, %6
%47 = phi double [ 0.000000e+00, %6 ], [ %41, %27 ]
ret double %47
}