#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 */
typedef size_t int64_t;

/* Variables and functions */
double nfw_menc(float, float);
float chi2_scale(float rs, float *bin_r, float *weights, int64_t num_bins) {
  int64_t i;
  float bin_mass = nfw_menc(bin_r[num_bins], rs) / ((double)num_bins);
  float last_menc = 0;
  float chi2 = 0, dx;
  for (i = 0; i < num_bins; i++) {
    float menc = nfw_menc(bin_r[i + 1], rs);
    dx = weights[i] * ((menc - last_menc) - bin_mass) / bin_mass;
    chi2 += dx * dx;
    last_menc = menc;
  }
  return chi2;
}


x86 assembly (clang, -O0):
<chi2_scale>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movss  %xmm0,-0x4(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
movss  -0x4(%rbp),%xmm1
call   1030 <nfw_menc@plt>
movaps 0xeb9(%rip),%xmm2
movq   -0x20(%rbp),%xmm1
punpckldq %xmm2,%xmm1
movapd 0xeb8(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x2c(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    121c <chi2_scale+0x10c>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movss  0x4(%rax,%rcx,4),%xmm0
movss  -0x4(%rbp),%xmm1
call   1030 <nfw_menc@plt>
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
movss  -0x3c(%rbp),%xmm1
subss  -0x30(%rbp),%xmm1
subss  -0x2c(%rbp),%xmm1
mulss  %xmm1,%xmm0
divss  -0x2c(%rbp),%xmm0
movss  %xmm0,-0x38(%rbp)
movss  -0x38(%rbp),%xmm0
movss  -0x38(%rbp),%xmm2
movss  -0x34(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x34(%rbp)
movss  -0x3c(%rbp),%xmm0
movss  %xmm0,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    118c <chi2_scale+0x7c>
movss  -0x34(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chi2_scale>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
movapd %xmm0,%xmm1
movss  (%rdi,%rdx,4),%xmm0
movss  %xmm1,0x8(%rsp)
call   1030 <nfw_menc@plt>
test   %r12,%r12
je     11e6 <chi2_scale+0xd6>
movq   %r12,%xmm1
punpckldq 0xeb0(%rip),%xmm1
subpd  0xeb8(%rip),%xmm1
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
divsd  %xmm2,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,0x4(%rsp)
xorps  %xmm3,%xmm3
xor    %ebx,%ebx
xorps  %xmm0,%xmm0
nopw   0x0(%rax,%rax,1)
movss  %xmm3,0xc(%rsp)
movss  %xmm0,(%rsp)
lea    0x1(%rbx),%r13
movss  0x4(%r15,%rbx,4),%xmm0
movss  0x8(%rsp),%xmm1
call   1030 <nfw_menc@plt>
xorps  %xmm3,%xmm3
cvtsd2ss %xmm0,%xmm3
movaps %xmm3,%xmm1
subss  0xc(%rsp),%xmm1
movss  0x4(%rsp),%xmm2
subss  %xmm2,%xmm1
mulss  (%r14,%rbx,4),%xmm1
divss  %xmm2,%xmm1
mulss  %xmm1,%xmm1
movss  (%rsp),%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,(%rsp)
movss  (%rsp),%xmm0
mov    %r13,%rbx
cmp    %r13,%r12
jne    1180 <chi2_scale+0x70>
jmp    11ea <chi2_scale+0xda>
xorpd  %xmm0,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define float @chi2_scale(float %0, float* %1, float* %2, i64 %3) {
%5 = alloca float
%6 = alloca float*
%7 = alloca float*
%8 = alloca i64
%9 = alloca i64
%10 = alloca float
%11 = alloca float
%12 = alloca float
%13 = alloca float
%14 = alloca float
store float %0, float* %5
store float* %1, float** %6
store float* %2, float** %7
store i64 %3, i64* %8
%15 = load float*, float** %6
%16 = load i64, i64* %8
%17 = getelementptr inbounds float, float* %15, i64 %16
%18 = load float, float* %17
%19 = load float, float* %5
%20 = call double @nfw_menc(float %18, float %19)
%21 = load i64, i64* %8
%22 = uitofp i64 %21 to double
%23 = fdiv double %20, %22
%24 = fptrunc double %23 to float
store float %24, float* %10
store float 0.000000e+00, float* %11
store float 0.000000e+00, float* %12
store i64 0, i64* %9
br label %25
25: ; preds = %55, %4
%26 = load i64, i64* %9
%27 = load i64, i64* %8
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %58
29: ; preds = %25
%30 = load float*, float** %6
%31 = load i64, i64* %9
%32 = add i64 %31, 1
%33 = getelementptr inbounds float, float* %30, i64 %32
%34 = load float, float* %33
%35 = load float, float* %5
%36 = call double @nfw_menc(float %34, float %35)
%37 = fptrunc double %36 to float
store float %37, float* %14
%38 = load float*, float** %7
%39 = load i64, i64* %9
%40 = getelementptr inbounds float, float* %38, i64 %39
%41 = load float, float* %40
%42 = load float, float* %14
%43 = load float, float* %11
%44 = fsub float %42, %43
%45 = load float, float* %10
%46 = fsub float %44, %45
%47 = fmul float %41, %46
%48 = load float, float* %10
%49 = fdiv float %47, %48
store float %49, float* %13
%50 = load float, float* %13
%51 = load float, float* %13
%52 = load float, float* %12
%53 = call float @llvm.fmuladd.f32(float %50, float %51, float %52)
store float %53, float* %12
%54 = load float, float* %14
store float %54, float* %11
br label %55
55: ; preds = %29
%56 = load i64, i64* %9
%57 = add i64 %56, 1
store i64 %57, i64* %9
br label %25
58: ; preds = %25
%59 = load float, float* %12
ret float %59
}

LLVM IR (-O2):
define float @chi2_scale(float %0, float* nocapture %1, float* nocapture %2, i64 %3) {
%5 = getelementptr inbounds float, float* %1, i64 %3
%6 = load float, float* %5
%7 = tail call double @nfw_menc(float %6, float %0)
%8 = uitofp i64 %3 to double
%9 = fdiv double %7, %8
%10 = fptrunc double %9 to float
%11 = icmp eq i64 %3, 0
br i1 %11, label %29, label %12
12: ; preds = %4, %12
%13 = phi i64 [ %16, %12 ], [ 0, %4 ]
%14 = phi float [ %20, %12 ], [ 0.000000e+00, %4 ]
%15 = phi float [ %27, %12 ], [ 0.000000e+00, %4 ]
%16 = add nuw i64 %13, 1
%17 = getelementptr inbounds float, float* %1, i64 %16
%18 = load float, float* %17
%19 = tail call double @nfw_menc(float %18, float %0)
%20 = fptrunc double %19 to float
%21 = getelementptr inbounds float, float* %2, i64 %13
%22 = load float, float* %21
%23 = fsub float %20, %14
%24 = fsub float %23, %10
%25 = fmul float %22, %24
%26 = fdiv float %25, %10
%27 = tail call float @llvm.fmuladd.f32(float %26, float %26, float %15)
%28 = icmp eq i64 %16, %3
br i1 %28, label %29, label %12
29: ; preds = %12, %4
%30 = phi float [ 0.000000e+00, %4 ], [ %27, %12 ]
ret float %30
}