#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 */
double log(size_t);
scalar_t__ plogp_sane(double);
double calc_entropy(double **p, size_t n1, size_t n2) {
  size_t i, j;
  double s = 0;
  for (i = 0; i < n1; i++)
    for (j = 0; j < n2; j++)
      s += plogp_sane(p[i][j]);
  s = -s / n1 / log(n2);
  return s;
}


x86 assembly (clang, -O0):
<calc_entropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11b8 <calc_entropy+0x98>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11a2 <calc_entropy+0x82>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x28(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1040 <plogp_sane@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    115a <calc_entropy+0x3a>
jmp    11a7 <calc_entropy+0x87>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1144 <calc_entropy+0x24>
movaps 0xe41(%rip),%xmm1
movsd  -0x30(%rbp),%xmm0
pxor   %xmm1,%xmm0
movaps 0xe41(%rip),%xmm2
movq   -0x10(%rbp),%xmm1
punpckldq %xmm2,%xmm1
movapd 0xe40(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_entropy>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
test   %rsi,%rsi
je     1199 <calc_entropy+0x79>
mov    %rdi,%r12
xorpd  %xmm0,%xmm0
xor    %r13d,%r13d
jmp    1159 <calc_entropy+0x39>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r13
cmp    %r14,%r13
je     119d <calc_entropy+0x7d>
test   %r15,%r15
je     1150 <calc_entropy+0x30>
xor    %ebx,%ebx
movapd %xmm0,(%rsp)
mov    (%r12,%r13,8),%rax
movsd  (%rax,%rbx,8),%xmm0
call   1040 <plogp_sane@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movapd (%rsp),%xmm1
addsd  %xmm0,%xmm1
movapd %xmm1,(%rsp)
movapd (%rsp),%xmm0
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1160 <calc_entropy+0x40>
jmp    1150 <calc_entropy+0x30>
xorpd  %xmm0,%xmm0
xorpd  0xe5b(%rip),%xmm0
movapd %xmm0,%xmm2
movq   %r14,%xmm0
punpckldq 0xe5a(%rip),%xmm0
subpd  0xe62(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
divsd  %xmm1,%xmm2
movapd %xmm2,(%rsp)
mov    %r15,%rdi
call   1030 <log@plt>
movapd (%rsp),%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @calc_entropy(double** %0, i64 %1, i64 %2) {
%4 = alloca double**
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca double
store double** %0, double*** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
store double 0.000000e+00, double* %9
store i64 0, i64* %7
br label %10
10: ; preds = %35, %3
%11 = load i64, i64* %7
%12 = load i64, i64* %5
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %38
14: ; preds = %10
store i64 0, i64* %8
br label %15
15: ; preds = %31, %14
%16 = load i64, i64* %8
%17 = load i64, i64* %6
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %34
19: ; preds = %15
%20 = load double**, double*** %4
%21 = load i64, i64* %7
%22 = getelementptr inbounds double*, double** %20, i64 %21
%23 = load double*, double** %22
%24 = load i64, i64* %8
%25 = getelementptr inbounds double, double* %23, i64 %24
%26 = load double, double* %25
%27 = call i64 @plogp_sane(double %26)
%28 = sitofp i64 %27 to double
%29 = load double, double* %9
%30 = fadd double %29, %28
store double %30, double* %9
br label %31
31: ; preds = %19
%32 = load i64, i64* %8
%33 = add i64 %32, 1
store i64 %33, i64* %8
br label %15
34: ; preds = %15
br label %35
35: ; preds = %34
%36 = load i64, i64* %7
%37 = add i64 %36, 1
store i64 %37, i64* %7
br label %10
38: ; preds = %10
%39 = load double, double* %9
%40 = fneg double %39
%41 = load i64, i64* %5
%42 = uitofp i64 %41 to double
%43 = fdiv double %40, %42
%44 = load i64, i64* %6
%45 = call double @log(i64 %44)
%46 = fdiv double %43, %45
store double %46, double* %9
%47 = load double, double* %9
ret double %47
}

LLVM IR (-O2):
define double @calc_entropy(double** nocapture %0, i64 %1, i64 %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %27, label %5
5: ; preds = %3
%6 = icmp eq i64 %2, 0
br label %7
7: ; preds = %5, %23
%8 = phi double [ 0.000000e+00, %5 ], [ %24, %23 ]
%9 = phi i64 [ 0, %5 ], [ %25, %23 ]
br i1 %6, label %23, label %10
10: ; preds = %7
%11 = getelementptr inbounds double*, double** %0, i64 %9
br label %12
12: ; preds = %10, %12
%13 = phi double [ %8, %10 ], [ %20, %12 ]
%14 = phi i64 [ 0, %10 ], [ %21, %12 ]
%15 = load double*, double** %11
%16 = getelementptr inbounds double, double* %15, i64 %14
%17 = load double, double* %16
%18 = tail call i64 @plogp_sane(double %17)
%19 = sitofp i64 %18 to double
%20 = fadd double %13, %19
%21 = add nuw i64 %14, 1
%22 = icmp eq i64 %21, %2
br i1 %22, label %23, label %12
23: ; preds = %12, %7
%24 = phi double [ %8, %7 ], [ %20, %12 ]
%25 = add nuw i64 %9, 1
%26 = icmp eq i64 %25, %1
br i1 %26, label %27, label %7
27: ; preds = %23, %3
%28 = phi double [ 0.000000e+00, %3 ], [ %24, %23 ]
%29 = fneg double %28
%30 = uitofp i64 %1 to double
%31 = fdiv double %29, %30
%32 = tail call double @log(i64 %2)
%33 = fdiv double %31, %32
ret double %33
}