#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 */
float logf(float);
float compute_distribution_entropy(float *P, int num) {
  int i;
  float log_scale = 1.0 / logf(2.0);
  float H = 0;
  for (i = 0; i < num; i++) {
    if (P[i] > 0.0)
      H += -P[i] * log_scale * logf(P[i]);
  }
  return H;
}


x86 assembly (clang, -O0):
<compute_distribution_entropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movss  0xee1(%rip),%xmm0
call   1030 <logf@plt>
cvtss2sd %xmm0,%xmm1
movsd  0xec8(%rip),%xmm0
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e1 <compute_distribution_entropy+0xd1>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11ce <compute_distribution_entropy+0xbe>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  -0x14(%rbp),%xmm0
movss  %xmm0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
call   1030 <logf@plt>
movaps %xmm0,%xmm2
movss  -0x1c(%rbp),%xmm0
movss  -0x18(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x18(%rbp)
jmp    11d3 <compute_distribution_entropy+0xc3>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1154 <compute_distribution_entropy+0x44>
movss  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_distribution_entropy>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %esi,%esi
jle    1175 <compute_distribution_entropy+0x65>
mov    %rdi,%r14
mov    %esi,%r15d
xorps  %xmm1,%xmm1
xor    %ebx,%ebx
xorps  %xmm2,%xmm2
jmp    1139 <compute_distribution_entropy+0x29>
nopl   (%rax)
add    $0x1,%rbx
cmp    %rbx,%r15
je     1178 <compute_distribution_entropy+0x68>
movss  (%r14,%rbx,4),%xmm0
ucomiss %xmm1,%xmm0
jbe    1130 <compute_distribution_entropy+0x20>
movaps %xmm0,%xmm1
mulss  0xeb1(%rip),%xmm1
movss  %xmm1,0x8(%rsp)
movss  %xmm2,0xc(%rsp)
call   1030 <logf@plt>
movss  0xc(%rsp),%xmm2
xorps  %xmm1,%xmm1
mulss  0x8(%rsp),%xmm0
addss  %xmm0,%xmm2
jmp    1130 <compute_distribution_entropy+0x20>
xorps  %xmm2,%xmm2
movaps %xmm2,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define float @compute_distribution_entropy(float* %0, i32 %1) {
%3 = alloca float*
%4 = alloca i32
%5 = alloca i32
%6 = alloca float
%7 = alloca float
store float* %0, float** %3
store i32 %1, i32* %4
%8 = call float @logf(float 2.000000e+00)
%9 = fpext float %8 to double
%10 = fdiv double 1.000000e+00, %9
%11 = fptrunc double %10 to float
store float %11, float* %6
store float 0.000000e+00, float* %7
store i32 0, i32* %5
br label %12
12: ; preds = %42, %2
%13 = load i32, i32* %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %45
16: ; preds = %12
%17 = load float*, float** %3
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds float, float* %17, i64 %19
%21 = load float, float* %20
%22 = fpext float %21 to double
%23 = fcmp ogt double %22, 0.000000e+00
br i1 %23, label %24, label %41
24: ; preds = %16
%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 = fneg float %29
%31 = load float, float* %6
%32 = fmul float %30, %31
%33 = load float*, float** %3
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds float, float* %33, i64 %35
%37 = load float, float* %36
%38 = call float @logf(float %37)
%39 = load float, float* %7
%40 = call float @llvm.fmuladd.f32(float %32, float %38, float %39)
store float %40, float* %7
br label %41
41: ; preds = %24, %16
br label %42
42: ; preds = %41
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %12
45: ; preds = %12
%46 = load float, float* %7
ret float %46
}

LLVM IR (-O2):
define float @compute_distribution_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 float [ 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 = fmul float %10, 0xBFF7154760000000
%14 = tail call float @logf(float %10)
%15 = tail call float @llvm.fmuladd.f32(float %13, float %14, float %8)
br label %16
16: ; preds = %6, %12
%17 = phi float [ %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 float [ 0.000000e+00, %2 ], [ %17, %16 ]
ret float %21
}