#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 const log(double const);
double calcEntropy(const double *p, const unsigned n) {
  unsigned i;
  double sum;
  for (sum = i = 0; i < n; i++)
    sum -= p[i] * log(p[i]) / log(2);
  return sum;
}


x86 assembly (clang, -O0):
<calcEntropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    119f <calcEntropy+0x8f>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
movsd  (%rax,%rcx,8),%xmm0
call   1030 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xe8b(%rip),%xmm0
call   1030 <log@plt>
movsd  -0x20(%rbp),%xmm1
divsd  %xmm0,%xmm1
movsd  -0x18(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112e <calcEntropy+0x1e>
movsd  -0x18(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calcEntropy>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %esi,%esi
je     1172 <calcEntropy+0x62>
mov    %rdi,%r14
mov    %esi,%r15d
xorps  %xmm0,%xmm0
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,(%rsp)
movsd  (%r14,%rbx,8),%xmm0
movsd  %xmm0,0x8(%rsp)
call   1030 <log@plt>
mulsd  0x8(%rsp),%xmm0
divsd  0xeac(%rip),%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1130 <calcEntropy+0x20>
jmp    1175 <calcEntropy+0x65>
xorps  %xmm0,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @calcEntropy(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
store double* %0, double** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store double 0.000000e+00, double* %6
br label %7
7: ; preds = %28, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = icmp ult i32 %8, %9
br i1 %10, label %11, label %31
11: ; preds = %7
%12 = load double*, double** %3
%13 = load i32, i32* %5
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds double, double* %12, i64 %14
%16 = load double, double* %15
%17 = load double*, double** %3
%18 = load i32, i32* %5
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds double, double* %17, i64 %19
%21 = load double, double* %20
%22 = call double @log(double %21)
%23 = fmul double %16, %22
%24 = call double @log(double 2.000000e+00)
%25 = fdiv double %23, %24
%26 = load double, double* %6
%27 = fsub double %26, %25
store double %27, double* %6
br label %28
28: ; preds = %11
%29 = load i32, i32* %5
%30 = add i32 %29, 1
store i32 %30, i32* %5
br label %7
31: ; preds = %7
%32 = load double, double* %6
ret double %32
}

LLVM IR (-O2):
define double @calcEntropy(double* nocapture %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %17, label %4
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %6
%7 = phi i64 [ 0, %4 ], [ %15, %6 ]
%8 = phi double [ 0.000000e+00, %4 ], [ %14, %6 ]
%9 = getelementptr inbounds double, double* %0, i64 %7
%10 = load double, double* %9
%11 = tail call double @log(double %10)
%12 = fmul double %10, %11
%13 = fdiv double %12, 0x3FE62E42FEFA39EF
%14 = fsub double %8, %13
%15 = add nuw nsw i64 %7, 1
%16 = icmp eq i64 %15, %5
br i1 %16, label %17, label %6
17: ; preds = %6, %2
%18 = phi double [ 0.000000e+00, %2 ], [ %14, %6 ]
ret double %18
}