double stat_int_entropy(const int *v, int n) {
  assert(v);
  assert(n > 0);
  double s = stat_int_sum(v, n);
  double l = 0.0;
  int i;
  for (i = 0; i < n; i++) {
    if (v[i] > 0) {
      double p = v[i] / s;
      l += -p * log(p) / log(2.0);
    }
  }
  return l;
}


x86 assembly (clang, -O0):
<stat_int_entropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1040 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x0,%al
call   1030 <stat_int_sum@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    122a <stat_int_entropy+0xfa>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jle    1217 <stat_int_entropy+0xe7>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
cvtsi2sdl (%rax,%rcx,4),%xmm0
divsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1050 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xe04(%rip),%xmm0
call   1050 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
divsd  %xmm1,%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    121c <stat_int_entropy+0xec>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1183 <stat_int_entropy+0x53>
movsd  -0x20(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stat_int_entropy>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <assert@plt>
xor    %edi,%edi
test   %ebp,%ebp
setg   %dil
xor    %eax,%eax
call   1040 <assert@plt>
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <stat_int_sum@plt>
test   %ebp,%ebp
jle    11d9 <stat_int_entropy+0xa9>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %ebp,%ebp
xorpd  %xmm0,%xmm0
xor    %ebx,%ebx
jmp    1189 <stat_int_entropy+0x59>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
je     11dd <stat_int_entropy+0xad>
mov    (%r14,%rbx,4),%eax
test   %eax,%eax
jle    1180 <stat_int_entropy+0x50>
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
divsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x18(%rsp)
movsd  %xmm0,0x8(%rsp)
movapd %xmm1,%xmm0
call   1050 <log@plt>
mulsd  0x18(%rsp),%xmm0
divsd  0xe3f(%rip),%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
jmp    1180 <stat_int_entropy+0x50>
xorpd  %xmm0,%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @stat_int_entropy(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca double
store i32* %0, i32** %3
store i32 %1, i32* %4
%9 = load i32*, i32** %3
%10 = call i32 (i32*, ...) bitcast (i32 (...)* @assert to i32 (i32*, ...)*)(i32* %9)
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %13)
%15 = load i32*, i32** %3
%16 = load i32, i32* %4
%17 = call i32 (i32*, i32, ...) bitcast (i32 (...)* @stat_int_sum to i32 (i32*, i32, ...)*)(i32* %15, i32 %16)
%18 = sitofp i32 %17 to double
store double %18, double* %5
store double 0.000000e+00, double* %6
store i32 0, i32* %7
br label %19
19: ; preds = %49, %2
%20 = load i32, i32* %7
%21 = load i32, i32* %4
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %52
23: ; preds = %19
%24 = load i32*, i32** %3
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %48
30: ; preds = %23
%31 = load i32*, i32** %3
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = load i32, i32* %34
%36 = sitofp i32 %35 to double
%37 = load double, double* %5
%38 = fdiv double %36, %37
store double %38, double* %8
%39 = load double, double* %8
%40 = fneg double %39
%41 = load double, double* %8
%42 = call double @log(double %41)
%43 = fmul double %40, %42
%44 = call double @log(double 2.000000e+00)
%45 = fdiv double %43, %44
%46 = load double, double* %6
%47 = fadd double %46, %45
store double %47, double* %6
br label %48
48: ; preds = %30, %23
br label %49
49: ; preds = %48
%50 = load i32, i32* %7
%51 = add nsw i32 %50, 1
store i32 %51, i32* %7
br label %19
52: ; preds = %19
%53 = load double, double* %6
ret double %53
}

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