#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 */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ isnan(double);
unsigned int log(int);
double log_combinations_d(unsigned int, unsigned int);
double log_dbinom(unsigned int x, unsigned int n, double p) {
  if ((x == 0) && (p == 0))
    return 0;
  if ((x == n) && (p == 1))
    return 0;
  double c = log_combinations_d(n, x);
  assert(!isnan(c));
  double p0 = log(p) * x;
  assert(!isnan(p0));
  double p1 = log(1 - p) * (n - x);
  assert(!isnan(p1));
  return c + p0 + p1;
}


x86 assembly (clang, -O0):
<log_dbinom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1182 <log_dbinom+0x42>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1182 <log_dbinom+0x42>
jp     1182 <log_dbinom+0x42>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    126f <log_dbinom+0x12f>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jne    11b8 <log_dbinom+0x78>
movsd  -0x18(%rbp),%xmm0
movsd  0xe65(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    11b8 <log_dbinom+0x78>
jp     11b8 <log_dbinom+0x78>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    126f <log_dbinom+0x12f>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <log_combinations_d@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1050 <isnan@plt>
test   %eax,%eax
sete   %al
movzbl %al,%edi
call   1030 <assert@plt>
cvttsd2si -0x18(%rbp),%edi
call   1060 <log@plt>
mov    -0xc(%rbp),%ecx
imul   %ecx,%eax
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1050 <isnan@plt>
test   %eax,%eax
sete   %al
movzbl %al,%edi
call   1030 <assert@plt>
movsd  -0x18(%rbp),%xmm1
movsd  0xde3(%rip),%xmm0
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1060 <log@plt>
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%edx
sub    %edx,%ecx
imul   %ecx,%eax
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1050 <isnan@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movsd  -0x20(%rbp),%xmm0
addsd  -0x28(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_dbinom>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movapd %xmm0,%xmm2
mov    %edi,%ebx
xorpd  %xmm0,%xmm0
test   %edi,%edi
jne    1164 <log_dbinom+0x24>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm2
jne    1164 <log_dbinom+0x24>
jnp    1216 <log_dbinom+0xd6>
mov    %esi,%ebp
sub    %ebx,%ebp
jne    117a <log_dbinom+0x3a>
ucomisd 0xe8e(%rip),%xmm2
jne    117a <log_dbinom+0x3a>
jnp    1216 <log_dbinom+0xd6>
mov    %esi,%edi
mov    %ebx,%esi
movsd  %xmm2,(%rsp)
call   1040 <log_combinations_d@plt>
movsd  %xmm0,0x10(%rsp)
call   1050 <isnan@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1030 <assert@plt>
cvttsd2si (%rsp),%edi
call   1060 <log@plt>
imul   %ebx,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  %xmm0,0x8(%rsp)
call   1050 <isnan@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1030 <assert@plt>
movsd  0xe2b(%rip),%xmm0
subsd  (%rsp),%xmm0
cvttsd2si %xmm0,%edi
call   1060 <log@plt>
imul   %ebp,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  %xmm0,(%rsp)
call   1050 <isnan@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1030 <assert@plt>
movsd  0x10(%rsp),%xmm0
addsd  0x8(%rsp),%xmm0
addsd  (%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define double @log_dbinom(i32 %0, i32 %1, double %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
store i32 %0, i32* %5
store i32 %1, i32* %6
store double %2, double* %7
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load double, double* %7
%15 = fcmp oeq double %14, 0.000000e+00
br i1 %15, label %16, label %17
16: ; preds = %13
store double 0.000000e+00, double* %4
br label %67
17: ; preds = %13, %3
%18 = load i32, i32* %5
%19 = load i32, i32* %6
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load double, double* %7
%23 = fcmp oeq double %22, 1.000000e+00
br i1 %23, label %24, label %25
24: ; preds = %21
store double 0.000000e+00, double* %4
br label %67
25: ; preds = %21, %17
%26 = load i32, i32* %6
%27 = load i32, i32* %5
%28 = call double @log_combinations_d(i32 %26, i32 %27)
store double %28, double* %8
%29 = load double, double* %8
%30 = call i32 @isnan(double %29)
%31 = icmp ne i32 %30, 0
%32 = xor i1 %31, true
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load double, double* %7
%36 = fptosi double %35 to i32
%37 = call i32 @log(i32 %36)
%38 = load i32, i32* %5
%39 = mul i32 %37, %38
%40 = uitofp i32 %39 to double
store double %40, double* %9
%41 = load double, double* %9
%42 = call i32 @isnan(double %41)
%43 = icmp ne i32 %42, 0
%44 = xor i1 %43, true
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = load double, double* %7
%48 = fsub double 1.000000e+00, %47
%49 = fptosi double %48 to i32
%50 = call i32 @log(i32 %49)
%51 = load i32, i32* %6
%52 = load i32, i32* %5
%53 = sub i32 %51, %52
%54 = mul i32 %50, %53
%55 = uitofp i32 %54 to double
store double %55, double* %10
%56 = load double, double* %10
%57 = call i32 @isnan(double %56)
%58 = icmp ne i32 %57, 0
%59 = xor i1 %58, true
%60 = zext i1 %59 to i32
%61 = call i32 @assert(i32 %60)
%62 = load double, double* %8
%63 = load double, double* %9
%64 = fadd double %62, %63
%65 = load double, double* %10
%66 = fadd double %64, %65
store double %66, double* %4
br label %67
67: ; preds = %25, %24, %16
%68 = load double, double* %4
ret double %68
}

LLVM IR (-O2):
define double @log_dbinom(i32 %0, i32 %1, double %2) {
%4 = icmp eq i32 %0, 0
%5 = fcmp oeq double %2, 0.000000e+00
%6 = and i1 %4, %5
br i1 %6, label %37, label %7
7: ; preds = %3
%8 = icmp eq i32 %1, %0
%9 = fcmp oeq double %2, 1.000000e+00
%10 = and i1 %8, %9
br i1 %10, label %37, label %11
11: ; preds = %7
%12 = tail call double @log_combinations_d(i32 %1, i32 %0)
%13 = tail call i32 @isnan(double %12)
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = fptosi double %2 to i32
%18 = tail call i32 @log(i32 %17)
%19 = mul i32 %18, %0
%20 = uitofp i32 %19 to double
%21 = tail call i32 @isnan(double %20)
%22 = icmp eq i32 %21, 0
%23 = zext i1 %22 to i32
%24 = tail call i32 @assert(i32 %23)
%25 = fsub double 1.000000e+00, %2
%26 = fptosi double %25 to i32
%27 = tail call i32 @log(i32 %26)
%28 = sub i32 %1, %0
%29 = mul i32 %27, %28
%30 = uitofp i32 %29 to double
%31 = tail call i32 @isnan(double %30)
%32 = icmp eq i32 %31, 0
%33 = zext i1 %32 to i32
%34 = tail call i32 @assert(i32 %33)
%35 = fadd double %12, %20
%36 = fadd double %35, %30
br label %37
37: ; preds = %7, %3, %11
%38 = phi double [ %36, %11 ], [ 0.000000e+00, %3 ], [ 0.000000e+00, %7 ]
ret double %38
}