#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(double);
double nentropy(const int n, const double *z) {
  int i;
  double ret = 0.0;
  for (i = 0; i < n; i++) {
    double y1, y2;
    y1 = n * z[i];
    y2 = 1 - y1;
    ret -= (y1 > 0.0 && y2 > 0.0) ? y1 * log(y1) + y2 * log(y2) : 0.0;
  }
  return ret;
}


x86 assembly (clang, -O0):
<nentropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    120a <nentropy+0xfa>
cvtsi2sdl -0x4(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xea7(%rip),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11dc <nentropy+0xcc>
movsd  -0x30(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11dc <nentropy+0xcc>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1030 <log@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1030 <log@plt>
movsd  -0x50(%rbp),%xmm1
movsd  -0x48(%rbp),%xmm2
movaps %xmm0,%xmm3
movsd  -0x40(%rbp),%xmm0
mulsd  %xmm3,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    11e9 <nentropy+0xd9>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    11e9 <nentropy+0xd9>
movsd  -0x38(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    112e <nentropy+0x1e>
movsd  -0x20(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nentropy>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
test   %edi,%edi
jle    11ce <nentropy+0xbe>
mov    %rsi,%r14
cvtsi2sd %edi,%xmm4
mov    %edi,%r15d
xorpd  %xmm1,%xmm1
xor    %ebx,%ebx
xorpd  %xmm2,%xmm2
movsd  %xmm4,0x10(%rsp)
jmp    1151 <nentropy+0x41>
nopl   (%rax)
subsd  %xmm3,%xmm2
add    $0x1,%rbx
cmp    %rbx,%r15
je     11d2 <nentropy+0xc2>
movsd  (%r14,%rbx,8),%xmm0
mulsd  %xmm4,%xmm0
ucomisd %xmm1,%xmm0
xorpd  %xmm3,%xmm3
jbe    1140 <nentropy+0x30>
movsd  0xe93(%rip),%xmm5
subsd  %xmm0,%xmm5
ucomisd %xmm1,%xmm5
xorpd  %xmm3,%xmm3
jbe    1140 <nentropy+0x30>
movsd  %xmm2,0x28(%rsp)
movsd  %xmm0,0x20(%rsp)
movsd  %xmm5,0x8(%rsp)
call   1030 <log@plt>
movsd  %xmm0,0x18(%rsp)
movsd  0x8(%rsp),%xmm0
call   1030 <log@plt>
movsd  0x18(%rsp),%xmm3
movsd  0x28(%rsp),%xmm2
xorpd  %xmm1,%xmm1
mulsd  0x8(%rsp),%xmm0
mulsd  0x20(%rsp),%xmm3
addsd  %xmm0,%xmm3
movsd  0x10(%rsp),%xmm4
jmp    1140 <nentropy+0x30>
xorpd  %xmm2,%xmm2
movapd %xmm2,%xmm0
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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