#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*/ hist2d(double const *, double const *, int const *,
                          double const *, double const *, double *, int, int,
                          int);
double log2d(double);
double entropy2d(const double *x, const double *y, const int *knots,
                 const double *wx, const double *wy, int numSamples,
                 int splineOrder, int numBins) {
  int curBinX, curBinY;
  double *hist = (double *)calloc(numBins * numBins, sizeof(double));
  double H = 0;
  double incr;
  hist2d(x, y, knots, wx, wy, hist, numSamples, splineOrder, numBins);
  for (curBinX = 0; curBinX < numBins; curBinX++) {
    for (curBinY = 0; curBinY < numBins; curBinY++) {
      incr = hist[curBinX * numBins + curBinY];
      if (incr > 0) {
        H -= incr * log2d(incr);
      }
    }
  }
  free(hist);
  return H;
}


x86 assembly (clang, -O0):
<entropy2d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    0x18(%rbp),%edi
imul   0x18(%rbp),%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x40(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x40(%rbp),%r9
mov    -0x2c(%rbp),%r11d
mov    0x10(%rbp),%r10d
mov    0x18(%rbp),%eax
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1060 <hist2d@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    0x18(%rbp),%eax
jge    1276 <entropy2d+0x136>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    0x18(%rbp),%eax
jge    1263 <entropy2d+0x123>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%ecx
imul   0x18(%rbp),%ecx
add    -0x34(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1250 <entropy2d+0x110>
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x50(%rbp),%xmm0
call   1050 <log2d@plt>
movaps %xmm0,%xmm2
movsd  -0x58(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
jmp    1255 <entropy2d+0x115>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d2 <entropy2d+0x92>
jmp    1268 <entropy2d+0x128>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11bf <entropy2d+0x7f>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x48(%rbp),%xmm0
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<entropy2d>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x8(%rsp)
mov    %r8,%r12
mov    %rcx,%r13
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r14
mov    0x58(%rsp),%r15d
mov    %r15d,%edi
imul   %r15d,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
sub    $0x8,%rsp
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r13,%rcx
mov    %rax,%r13
mov    %r12,%r8
mov    %rax,%r9
push   %r15
mov    0x60(%rsp),%eax
push   %rax
push   0x20(%rsp)
call   1060 <hist2d@plt>
add    $0x20,%rsp
test   %r15d,%r15d
jle    121c <entropy2d+0xdc>
mov    %r15d,%r12d
lea    0x0(,%r12,8),%r14
xorpd  %xmm1,%xmm1
xor    %r15d,%r15d
mov    %r13,%rbp
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
jmp    11dc <entropy2d+0x9c>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15
add    %r14,%rbp
cmp    %r12,%r15
je     1226 <entropy2d+0xe6>
xor    %ebx,%ebx
jmp    11e9 <entropy2d+0xa9>
add    $0x1,%rbx
cmp    %rbx,%r12
je     11d0 <entropy2d+0x90>
movsd  0x0(%rbp,%rbx,8),%xmm0
ucomisd %xmm1,%xmm0
jbe    11e0 <entropy2d+0xa0>
movsd  %xmm0,0x10(%rsp)
call   1050 <log2d@plt>
mulsd  0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
xorpd  %xmm1,%xmm1
jmp    11e0 <entropy2d+0xa0>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %r13,%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 @entropy2d(double* %0, double* %1, i32* %2, double* %3, double* %4, i32 %5, i32 %6, i32 %7) {
%9 = alloca double*
%10 = alloca double*
%11 = alloca i32*
%12 = alloca double*
%13 = alloca double*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca double*
%20 = alloca double
%21 = alloca double
store double* %0, double** %9
store double* %1, double** %10
store i32* %2, i32** %11
store double* %3, double** %12
store double* %4, double** %13
store i32 %5, i32* %14
store i32 %6, i32* %15
store i32 %7, i32* %16
%22 = load i32, i32* %16
%23 = load i32, i32* %16
%24 = mul nsw i32 %22, %23
%25 = call i64 @calloc(i32 %24, i32 8)
%26 = inttoptr i64 %25 to double*
store double* %26, double** %19
store double 0.000000e+00, double* %20
%27 = load double*, double** %9
%28 = load double*, double** %10
%29 = load i32*, i32** %11
%30 = load double*, double** %12
%31 = load double*, double** %13
%32 = load double*, double** %19
%33 = load i32, i32* %14
%34 = load i32, i32* %15
%35 = load i32, i32* %16
%36 = call i32 @hist2d(double* %27, double* %28, i32* %29, double* %30, double* %31, double* %32, i32 %33, i32 %34, i32 %35)
store i32 0, i32* %17
br label %37
37: ; preds = %70, %8
%38 = load i32, i32* %17
%39 = load i32, i32* %16
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %73
41: ; preds = %37
store i32 0, i32* %18
br label %42
42: ; preds = %66, %41
%43 = load i32, i32* %18
%44 = load i32, i32* %16
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %69
46: ; preds = %42
%47 = load double*, double** %19
%48 = load i32, i32* %17
%49 = load i32, i32* %16
%50 = mul nsw i32 %48, %49
%51 = load i32, i32* %18
%52 = add nsw i32 %50, %51
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds double, double* %47, i64 %53
%55 = load double, double* %54
store double %55, double* %21
%56 = load double, double* %21
%57 = fcmp ogt double %56, 0.000000e+00
br i1 %57, label %58, label %65
58: ; preds = %46
%59 = load double, double* %21
%60 = load double, double* %21
%61 = call double @log2d(double %60)
%62 = load double, double* %20
%63 = fneg double %59
%64 = call double @llvm.fmuladd.f64(double %63, double %61, double %62)
store double %64, double* %20
br label %65
65: ; preds = %58, %46
br label %66
66: ; preds = %65
%67 = load i32, i32* %18
%68 = add nsw i32 %67, 1
store i32 %68, i32* %18
br label %42
69: ; preds = %42
br label %70
70: ; preds = %69
%71 = load i32, i32* %17
%72 = add nsw i32 %71, 1
store i32 %72, i32* %17
br label %37
73: ; preds = %37
%74 = load double*, double** %19
%75 = call i32 @free(double* %74)
%76 = load double, double* %20
ret double %76
}

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