#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 log2f(float);
float entropy(float *xs, int size) {
  float *ip, *end, sum, e;
  for (ip = xs, end = xs + size, e = 0.0, sum = 0.0; ip != end; ip++)
    if (*ip > 0.0) {
      e -= *ip * log2f(*ip);
      sum += *ip;
    }
  return sum == 0.0 ? 0.0 : e / sum + log2f(sum);
}


x86 assembly (clang, -O0):
<entropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11da <entropy+0xca>
mov    -0x18(%rbp),%rax
movss  (%rax),%xmm0
cvtss2sd %xmm0,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11c4 <entropy+0xb4>
mov    -0x18(%rbp),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
movss  (%rax),%xmm0
call   1030 <log2f@plt>
movaps %xmm0,%xmm2
movss  -0x2c(%rbp),%xmm0
movss  -0x28(%rbp),%xmm1
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movss  (%rax),%xmm0
addss  -0x24(%rbp),%xmm0
movss  %xmm0,-0x24(%rbp)
jmp    11c9 <entropy+0xb9>
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
jmp    114a <entropy+0x3a>
movss  -0x24(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1203 <entropy+0xf3>
jp     1203 <entropy+0xf3>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    1231 <entropy+0x121>
movss  -0x28(%rbp),%xmm0
divss  -0x24(%rbp),%xmm0
movss  %xmm0,-0x3c(%rbp)
movss  -0x24(%rbp),%xmm0
call   1030 <log2f@plt>
movaps %xmm0,%xmm1
movss  -0x3c(%rbp),%xmm0
addss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<entropy>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %esi,%esi
je     11b0 <entropy+0xa0>
mov    %rdi,%r15
movslq %esi,%r14
shl    $0x2,%r14
xorps  %xmm3,%xmm3
xor    %ebx,%ebx
xorps  %xmm1,%xmm1
xorps  %xmm2,%xmm2
jmp    1149 <entropy+0x39>
nopl   0x0(%rax,%rax,1)
add    $0x4,%rbx
cmp    %rbx,%r14
je     118c <entropy+0x7c>
movss  (%r15,%rbx,1),%xmm0
ucomiss %xmm3,%xmm0
jbe    1140 <entropy+0x30>
movss  %xmm1,0x4(%rsp)
movss  %xmm2,0xc(%rsp)
movss  %xmm0,0x8(%rsp)
call   1030 <log2f@plt>
movss  0xc(%rsp),%xmm2
movss  0x4(%rsp),%xmm1
mulss  0x8(%rsp),%xmm0
subss  %xmm0,%xmm1
xorps  %xmm3,%xmm3
addss  (%r15,%rbx,1),%xmm2
jmp    1140 <entropy+0x30>
xorps  %xmm0,%xmm0
ucomiss %xmm0,%xmm2
jne    1196 <entropy+0x86>
jnp    11b3 <entropy+0xa3>
divss  %xmm2,%xmm1
movss  %xmm1,0x4(%rsp)
movaps %xmm2,%xmm0
call   1030 <log2f@plt>
addss  0x4(%rsp),%xmm0
jmp    11b3 <entropy+0xa3>
xorps  %xmm0,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define float @entropy(float* %0, i32 %1) {
%3 = alloca float*
%4 = alloca i32
%5 = alloca float*
%6 = alloca float*
%7 = alloca float
%8 = alloca float
store float* %0, float** %3
store i32 %1, i32* %4
%9 = load float*, float** %3
store float* %9, float** %5
%10 = load float*, float** %3
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds float, float* %10, i64 %12
store float* %13, float** %6
store float 0.000000e+00, float* %8
store float 0.000000e+00, float* %7
br label %14
14: ; preds = %37, %2
%15 = load float*, float** %5
%16 = load float*, float** %6
%17 = icmp ne float* %15, %16
br i1 %17, label %18, label %40
18: ; preds = %14
%19 = load float*, float** %5
%20 = load float, float* %19
%21 = fpext float %20 to double
%22 = fcmp ogt double %21, 0.000000e+00
br i1 %22, label %23, label %36
23: ; preds = %18
%24 = load float*, float** %5
%25 = load float, float* %24
%26 = load float*, float** %5
%27 = load float, float* %26
%28 = call float @log2f(float %27)
%29 = load float, float* %8
%30 = fneg float %25
%31 = call float @llvm.fmuladd.f32(float %30, float %28, float %29)
store float %31, float* %8
%32 = load float*, float** %5
%33 = load float, float* %32
%34 = load float, float* %7
%35 = fadd float %34, %33
store float %35, float* %7
br label %36
36: ; preds = %23, %18
br label %37
37: ; preds = %36
%38 = load float*, float** %5
%39 = getelementptr inbounds float, float* %38, i32 1
store float* %39, float** %5
br label %14
40: ; preds = %14
%41 = load float, float* %7
%42 = fpext float %41 to double
%43 = fcmp oeq double %42, 0.000000e+00
br i1 %43, label %44, label %45
44: ; preds = %40
br label %53
45: ; preds = %40
%46 = load float, float* %8
%47 = load float, float* %7
%48 = fdiv float %46, %47
%49 = load float, float* %7
%50 = call float @log2f(float %49)
%51 = fadd float %48, %50
%52 = fpext float %51 to double
br label %53
53: ; preds = %45, %44
%54 = phi double [ 0.000000e+00, %44 ], [ %52, %45 ]
%55 = fptrunc double %54 to float
ret float %55
}

LLVM IR (-O2):
define float @entropy(float* %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = getelementptr inbounds float, float* %0, i64 %3
%5 = icmp eq i32 %1, 0
br i1 %5, label %29, label %6
6: ; preds = %2, %18
%7 = phi float [ %20, %18 ], [ 0.000000e+00, %2 ]
%8 = phi float [ %19, %18 ], [ 0.000000e+00, %2 ]
%9 = phi float* [ %21, %18 ], [ %0, %2 ]
%10 = load float, float* %9
%11 = fcmp ogt float %10, 0.000000e+00
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = tail call float @log2f(float %10)
%14 = fneg float %10
%15 = tail call float @llvm.fmuladd.f32(float %14, float %13, float %7)
%16 = load float, float* %9
%17 = fadd float %8, %16
br label %18
18: ; preds = %6, %12
%19 = phi float [ %17, %12 ], [ %8, %6 ]
%20 = phi float [ %15, %12 ], [ %7, %6 ]
%21 = getelementptr inbounds float, float* %9, i64 1
%22 = icmp eq float* %21, %4
br i1 %22, label %23, label %6
23: ; preds = %18
%24 = fcmp oeq float %19, 0.000000e+00
br i1 %24, label %29, label %25
25: ; preds = %23
%26 = fdiv float %20, %19
%27 = tail call float @log2f(float %19)
%28 = fadd float %26, %27
br label %29
29: ; preds = %2, %23, %25
%30 = phi float [ %28, %25 ], [ 0.000000e+00, %23 ], [ 0.000000e+00, %2 ]
ret float %30
}