#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 glbChiNoSysSpectrum(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 bg_norm_center, bg_tilt_center;
  int ew_low, ew_high;
  double fit_rate, true_rate;
  double chi2 = 0.0;
  int i;
  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_fit_rates[i] + bg_norm_center * bg_fit_rates[i];
    chi2 += glb_likelihood(true_rates[i], fit_rate);
  }
  return chi2;
}


x86 assembly (clang, -O0):
<glbChiNoSysSpectrum>:
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,-0x70(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    -0x54(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1030 <glbGetEnergyWindowBins@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1080 <glbGetBGCenters@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%eax
cmp    -0x58(%rbp),%eax
jg     124d <glbChiNoSysSpectrum+0xed>
mov    -0x38(%rbp),%rax
movslq -0x74(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x48(%rbp),%xmm0
mov    -0x40(%rbp),%rax
movslq -0x74(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x74(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  -0x60(%rbp),%xmm1
call   1060 <glb_likelihood@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x70(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    11e1 <glbChiNoSysSpectrum+0x81>
movsd  -0x70(%rbp),%xmm0
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<glbChiNoSysSpectrum>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
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
lea    0x1c(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    %r12d,%esi
call   1030 <glbGetEnergyWindowBins@plt>
lea    0x20(%rsp),%rdx
lea    0x28(%rsp),%rcx
mov    %ebp,%edi
mov    %r12d,%esi
call   1080 <glbGetBGCenters@plt>
movslq 0x1c(%rsp),%rbp
xorps  %xmm0,%xmm0
cmp    0xc(%rsp),%ebp
jg     1221 <glbChiNoSysSpectrum+0xc1>
add    $0xffffffffffffffff,%rbp
xorps  %xmm0,%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  0x20(%rsp),%xmm1
mulsd  0x8(%rbx,%rbp,8),%xmm1
addsd  0x8(%r15,%rbp,8),%xmm1
movsd  0x8(%r14,%rbp,8),%xmm0
call   1060 <glb_likelihood@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  0x10(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
movsd  0x10(%rsp),%xmm0
movslq 0xc(%rsp),%rax
add    $0x1,%rbp
cmp    %rax,%rbp
jl     11d0 <glbChiNoSysSpectrum+0x70>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @glbChiNoSysSpectrum(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 i32
%19 = alloca i32
%20 = alloca double
%21 = alloca double
%22 = alloca double
%23 = 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
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = call double* @glbGetRuleRatePtr(i32 %24, i32 %25)
store double* %26, double** %13
%27 = load i32, i32* %7
%28 = load i32, i32* %8
%29 = call double* @glbGetSignalFitRatePtr(i32 %27, i32 %28)
store double* %29, double** %14
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = call double* @glbGetBGFitRatePtr(i32 %30, i32 %31)
store double* %32, double** %15
store double 0.000000e+00, double* %22
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = call i32 @glbGetEnergyWindowBins(i32 %33, i32 %34, i32* %18, i32* %19)
%36 = load i32, i32* %7
%37 = load i32, i32* %8
%38 = call i32 @glbGetBGCenters(i32 %36, i32 %37, double* %16, double* %17)
%39 = load i32, i32* %18
store i32 %39, i32* %23
br label %40
40: ; preds = %67, %6
%41 = load i32, i32* %23
%42 = load i32, i32* %19
%43 = icmp sle i32 %41, %42
br i1 %43, label %44, label %70
44: ; preds = %40
%45 = load double*, double** %14
%46 = load i32, i32* %23
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %45, i64 %47
%49 = load double, double* %48
%50 = load double, double* %16
%51 = load double*, double** %15
%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 = call double @llvm.fmuladd.f64(double %50, double %55, double %49)
store double %56, double* %20
%57 = load double*, double** %13
%58 = load i32, i32* %23
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds double, double* %57, i64 %59
%61 = load double, double* %60
%62 = load double, double* %20
%63 = call i64 @glb_likelihood(double %61, double %62)
%64 = sitofp i64 %63 to double
%65 = load double, double* %22
%66 = fadd double %65, %64
store double %66, double* %22
br label %67
67: ; preds = %44
%68 = load i32, i32* %23
%69 = add nsw i32 %68, 1
store i32 %69, i32* %23
br label %40
70: ; preds = %40
%71 = load double, double* %22
ret double %71
}

LLVM IR (-O2):
define double @glbChiNoSysSpectrum(i32 %0, i32 %1, i32 %2, double* nocapture readnone %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 = call i32 @glbGetEnergyWindowBins(i32 %0, i32 %1, i32* %9, i32* %10)
%19 = call i32 @glbGetBGCenters(i32 %0, i32 %1, double* %7, double* %8)
%20 = load i32, i32* %9
%21 = load i32, i32* %10
%22 = icmp sgt i32 %20, %21
br i1 %22, label %43, label %23
23: ; preds = %6
%24 = sext i32 %20 to i64
br label %25
25: ; preds = %23, %25
%26 = phi i64 [ %24, %23 ], [ %39, %25 ]
%27 = phi double [ 0.000000e+00, %23 ], [ %38, %25 ]
%28 = getelementptr inbounds double, double* %12, i64 %26
%29 = load double, double* %28
%30 = load double, double* %7
%31 = getelementptr inbounds double, double* %13, i64 %26
%32 = load double, double* %31
%33 = call double @llvm.fmuladd.f64(double %30, double %32, double %29)
%34 = getelementptr inbounds double, double* %11, i64 %26
%35 = load double, double* %34
%36 = call i64 @glb_likelihood(double %35, double %33)
%37 = sitofp i64 %36 to double
%38 = fadd double %27, %37
%39 = add nsw i64 %26, 1
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = icmp slt i64 %26, %41
br i1 %42, label %25, label %43
43: ; preds = %25, %6
%44 = phi double [ 0.000000e+00, %6 ], [ %38, %25 ]
ret double %44
}