#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(float const);
double fvec_entropy(const float *pmf, int n) {
  int i;
  double minusent = 0;
  for (i = 0; i < n; i++) {
    if (pmf[i] > 0) {
      minusent += pmf[i] * log(pmf[i]);
    }
  }
  return -minusent / log(2);
}


x86 assembly (clang, -O0):
<fvec_entropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a8 <fvec_entropy+0x98>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    1195 <fvec_entropy+0x85>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
call   1030 <log@plt>
movaps %xmm0,%xmm2
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    119a <fvec_entropy+0x8a>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112e <fvec_entropy+0x1e>
movsd  -0x18(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x28(%rbp)
movss  0xe2f(%rip),%xmm0
call   1030 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
divsd  %xmm1,%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fvec_entropy>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
test   %esi,%esi
jle    116f <fvec_entropy+0x5f>
mov    %rdi,%r14
mov    %esi,%r15d
xorpd  %xmm1,%xmm1
xor    %ebx,%ebx
xorps  %xmm2,%xmm2
jmp    1139 <fvec_entropy+0x29>
xchg   %ax,%ax
add    $0x1,%rbx
cmp    %rbx,%r15
je     1173 <fvec_entropy+0x63>
movss  (%r14,%rbx,4),%xmm0
ucomiss %xmm2,%xmm0
jbe    1130 <fvec_entropy+0x20>
xorps  %xmm2,%xmm2
cvtss2sd %xmm0,%xmm2
movsd  %xmm2,0x18(%rsp)
movapd %xmm1,(%rsp)
call   1030 <log@plt>
xorps  %xmm2,%xmm2
movapd (%rsp),%xmm1
mulsd  0x18(%rsp),%xmm0
addsd  %xmm0,%xmm1
jmp    1130 <fvec_entropy+0x20>
xorpd  %xmm1,%xmm1
xorpd  0xe85(%rip),%xmm1
movapd %xmm1,(%rsp)
movss  0xe88(%rip),%xmm0
call   1030 <log@plt>
movapd (%rsp),%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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