#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ hist1d(double const *, int const *, double *, double *, int,
                          int, int);
double log2d(double);
double entropy1d(const double *x, const int *knots, double *weights,
                 int numSamples, int splineOrder, int numBins) {
  int curBin;
  double *hist = (double *)calloc(numBins, sizeof(double));
  double H = 0;
  hist1d(x, knots, hist, weights, numSamples, splineOrder, numBins);
  for (curBin = 0; curBin < numBins; curBin++) {
    if (hist[curBin] > 0) {
      H -= hist[curBin] * log2d(hist[curBin]);
    }
  }
  free(hist);
  return H;
}


x86 assembly (clang, -O0):
<entropy1d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
mov    -0x20(%rbp),%r9d
mov    -0x24(%rbp),%eax
mov    %eax,(%rsp)
call   1040 <hist1d@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1230 <entropy1d+0xf0>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    121d <entropy1d+0xdd>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1060 <log2d@plt>
movaps %xmm0,%xmm2
movsd  -0x40(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    1222 <entropy1d+0xe2>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11a2 <entropy1d+0x62>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x38(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<entropy1d>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%ebp
mov    %r8d,0x8(%rsp)
mov    %ecx,%ebx
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
mov    %r9d,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r15
mov    %ebp,(%rsp)
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rax,%rdx
mov    %r12,%rcx
mov    %ebx,%r8d
mov    0x8(%rsp),%r9d
call   1040 <hist1d@plt>
test   %ebp,%ebp
jle    11ec <entropy1d+0xac>
mov    %ebp,%ebp
xorpd  %xmm1,%xmm1
xor    %ebx,%ebx
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
jmp    11b9 <entropy1d+0x79>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbx
cmp    %rbx,%rbp
je     11f6 <entropy1d+0xb6>
movsd  (%r15,%rbx,8),%xmm0
ucomisd %xmm1,%xmm0
jbe    11b0 <entropy1d+0x70>
movsd  %xmm0,0x10(%rsp)
call   1060 <log2d@plt>
mulsd  0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
xorpd  %xmm1,%xmm1
jmp    11b0 <entropy1d+0x70>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %r15,%rdi
call   1030 <free@plt>
movsd  0x8(%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @entropy1d(double* %0, i32* %1, double* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca double*
%8 = alloca i32*
%9 = alloca double*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca double*
%15 = alloca double
store double* %0, double** %7
store i32* %1, i32** %8
store double* %2, double** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%16 = load i32, i32* %12
%17 = call i64 @calloc(i32 %16, i32 8)
%18 = inttoptr i64 %17 to double*
store double* %18, double** %14
store double 0.000000e+00, double* %15
%19 = load double*, double** %7
%20 = load i32*, i32** %8
%21 = load double*, double** %14
%22 = load double*, double** %9
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = load i32, i32* %12
%26 = call i32 @hist1d(double* %19, i32* %20, double* %21, double* %22, i32 %23, i32 %24, i32 %25)
store i32 0, i32* %13
br label %27
27: ; preds = %54, %6
%28 = load i32, i32* %13
%29 = load i32, i32* %12
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %57
31: ; preds = %27
%32 = load double*, double** %14
%33 = load i32, i32* %13
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %32, i64 %34
%36 = load double, double* %35
%37 = fcmp ogt double %36, 0.000000e+00
br i1 %37, label %38, label %53
38: ; preds = %31
%39 = load double*, double** %14
%40 = load i32, i32* %13
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds double, double* %39, i64 %41
%43 = load double, double* %42
%44 = load double*, double** %14
%45 = load i32, i32* %13
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds double, double* %44, i64 %46
%48 = load double, double* %47
%49 = call double @log2d(double %48)
%50 = load double, double* %15
%51 = fneg double %43
%52 = call double @llvm.fmuladd.f64(double %51, double %49, double %50)
store double %52, double* %15
br label %53
53: ; preds = %38, %31
br label %54
54: ; preds = %53
%55 = load i32, i32* %13
%56 = add nsw i32 %55, 1
store i32 %56, i32* %13
br label %27
57: ; preds = %27
%58 = load double*, double** %14
%59 = call i32 @free(double* %58)
%60 = load double, double* %15
ret double %60
}

LLVM IR (-O2):
define double @entropy1d(double* %0, i32* %1, double* %2, i32 %3, i32 %4, i32 %5) {
%7 = tail call i64 @calloc(i32 %5, i32 8)
%8 = inttoptr i64 %7 to double*
%9 = tail call i32 @hist1d(double* %0, i32* %1, double* %8, double* %2, i32 %3, i32 %4, i32 %5)
%10 = icmp sgt i32 %5, 0
br i1 %10, label %11, label %27
11: ; preds = %6
%12 = zext i32 %5 to i64
br label %13
13: ; preds = %11, %23
%14 = phi i64 [ 0, %11 ], [ %25, %23 ]
%15 = phi double [ 0.000000e+00, %11 ], [ %24, %23 ]
%16 = getelementptr inbounds double, double* %8, i64 %14
%17 = load double, double* %16
%18 = fcmp ogt double %17, 0.000000e+00
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = tail call double @log2d(double %17)
%21 = fneg double %17
%22 = tail call double @llvm.fmuladd.f64(double %21, double %20, double %15)
br label %23
23: ; preds = %13, %19
%24 = phi double [ %22, %19 ], [ %15, %13 ]
%25 = add nuw nsw i64 %14, 1
%26 = icmp eq i64 %25, %12
br i1 %26, label %27, label %13
27: ; preds = %23, %6
%28 = phi double [ 0.000000e+00, %6 ], [ %24, %23 ]
%29 = tail call i32 @free(double* %8)
ret double %28
}