#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 log2(double);
double entropy_internal(int *hist, int histlen, int len) {
  double H = 0.0;
  for (int i = 0; i < histlen; i++) {
    H -= (double)hist[i] / len * log2((double)hist[i] / len);
  }
  return H;
}


x86 assembly (clang, -O0):
<entropy_internal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b2 <entropy_internal+0xa2>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cvtsi2sdl (%rax,%rcx,4),%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cvtsi2sdl (%rax,%rcx,4),%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
call   1030 <log2@plt>
movaps %xmm0,%xmm2
movsd  -0x28(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1131 <entropy_internal+0x21>
movsd  -0x18(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<entropy_internal>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
test   %esi,%esi
jle    1187 <entropy_internal+0x77>
mov    %rdi,%r14
cvtsi2sd %edx,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %esi,%r15d
xorpd  %xmm0,%xmm0
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm0,0x8(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sdl (%r14,%rbx,4),%xmm0
divsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x18(%rsp)
call   1030 <log2@plt>
mulsd  0x18(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1140 <entropy_internal+0x30>
jmp    118b <entropy_internal+0x7b>
xorpd  %xmm0,%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @entropy_internal(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store double 0.000000e+00, double* %7
store i32 0, i32* %8
br label %9
9: ; preds = %36, %3
%10 = load i32, i32* %8
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %39
13: ; preds = %9
%14 = load i32*, i32** %4
%15 = load i32, i32* %8
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %14, i64 %16
%18 = load i32, i32* %17
%19 = sitofp i32 %18 to double
%20 = load i32, i32* %6
%21 = sitofp i32 %20 to double
%22 = fdiv double %19, %21
%23 = load i32*, i32** %4
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = sitofp i32 %27 to double
%29 = load i32, i32* %6
%30 = sitofp i32 %29 to double
%31 = fdiv double %28, %30
%32 = call double @log2(double %31)
%33 = load double, double* %7
%34 = fneg double %22
%35 = call double @llvm.fmuladd.f64(double %34, double %32, double %33)
store double %35, double* %7
br label %36
36: ; preds = %13
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
br label %9
39: ; preds = %9
%40 = load double, double* %7
ret double %40
}

LLVM IR (-O2):
define double @entropy_internal(i32* nocapture %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = sitofp i32 %2 to double
%7 = zext i32 %1 to i64
br label %10
8: ; preds = %10, %3
%9 = phi double [ 0.000000e+00, %3 ], [ %19, %10 ]
ret double %9
10: ; preds = %5, %10
%11 = phi i64 [ 0, %5 ], [ %20, %10 ]
%12 = phi double [ 0.000000e+00, %5 ], [ %19, %10 ]
%13 = getelementptr inbounds i32, i32* %0, i64 %11
%14 = load i32, i32* %13
%15 = sitofp i32 %14 to double
%16 = fdiv double %15, %6
%17 = tail call double @log2(double %16)
%18 = fneg double %16
%19 = tail call double @llvm.fmuladd.f64(double %18, double %17, double %12)
%20 = add nuw nsw i64 %11, 1
%21 = icmp eq i64 %20, %7
br i1 %21, label %8, label %10
}