#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(int);
double binary_entropy(double p) {
  if (p == 0 || p == 1) {
    return 0;
  }
  return -(p * log(p) + (1 - p) * log(1 - p)) / log(2);
}


x86 assembly (clang, -O0):
<binary_entropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    113a <binary_entropy+0x2a>
jp     113a <binary_entropy+0x2a>
jmp    1157 <binary_entropy+0x47>
movsd  -0x10(%rbp),%xmm0
movsd  0xeb9(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1164 <binary_entropy+0x54>
jp     1164 <binary_entropy+0x54>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11fa <binary_entropy+0xea>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
cvttsd2si -0x10(%rbp),%edi
call   1030 <log@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  0xe7b(%rip),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe69(%rip),%xmm0
subsd  -0x10(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1030 <log@plt>
movsd  -0x30(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movaps %xmm0,%xmm3
movsd  -0x20(%rbp),%xmm0
mulsd  %xmm3,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x2,%edi
call   1030 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x18(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_entropy>:
sub    $0x28,%rsp
movapd %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jne    1124 <binary_entropy+0x14>
jnp    119e <binary_entropy+0x8e>
ucomisd 0xed4(%rip),%xmm1
jne    1130 <binary_entropy+0x20>
jnp    119e <binary_entropy+0x8e>
cvttsd2si %xmm1,%edi
movsd  %xmm1,0x8(%rsp)
call   1030 <log@plt>
movapd %xmm0,0x10(%rsp)
movsd  0xeb3(%rip),%xmm0
subsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x20(%rsp)
cvttsd2si %xmm0,%edi
call   1030 <log@plt>
mulsd  0x20(%rsp),%xmm0
movapd 0x10(%rsp),%xmm1
mulsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
xorpd  0xe90(%rip),%xmm1
movapd %xmm1,0x10(%rsp)
mov    $0x2,%edi
call   1030 <log@plt>
movapd 0x10(%rsp),%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x28,%rsp
ret

LLVM IR (-O0):
define double @binary_entropy(double %0) {
%2 = alloca double
%3 = alloca double
store double %0, double* %3
%4 = load double, double* %3
%5 = fcmp oeq double %4, 0.000000e+00
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load double, double* %3
%8 = fcmp oeq double %7, 1.000000e+00
br i1 %8, label %9, label %10
9: ; preds = %6, %1
store double 0.000000e+00, double* %2
br label %26
10: ; preds = %6
%11 = load double, double* %3
%12 = load double, double* %3
%13 = fptosi double %12 to i32
%14 = call double @log(i32 %13)
%15 = load double, double* %3
%16 = fsub double 1.000000e+00, %15
%17 = load double, double* %3
%18 = fsub double 1.000000e+00, %17
%19 = fptosi double %18 to i32
%20 = call double @log(i32 %19)
%21 = fmul double %16, %20
%22 = call double @llvm.fmuladd.f64(double %11, double %14, double %21)
%23 = fneg double %22
%24 = call double @log(i32 2)
%25 = fdiv double %23, %24
store double %25, double* %2
br label %26
26: ; preds = %10, %9
%27 = load double, double* %2
ret double %27
}

LLVM IR (-O2):
define double @binary_entropy(double %0) {
%2 = fcmp oeq double %0, 0.000000e+00
%3 = fcmp oeq double %0, 1.000000e+00
%4 = or i1 %2, %3
br i1 %4, label %16, label %5
5: ; preds = %1
%6 = fptosi double %0 to i32
%7 = tail call double @log(i32 %6)
%8 = fsub double 1.000000e+00, %0
%9 = fptosi double %8 to i32
%10 = tail call double @log(i32 %9)
%11 = fmul double %8, %10
%12 = tail call double @llvm.fmuladd.f64(double %0, double %7, double %11)
%13 = fneg double %12
%14 = tail call double @log(i32 2)
%15 = fdiv double %13, %14
br label %16
16: ; preds = %1, %5
%17 = phi double [ %15, %5 ], [ 0.000000e+00, %1 ]
ret double %17
}