#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 betai(int, int, int);
double log(double);
double bdtr(int k, int n, double p) {
  double dk;
  if ((p < 0.0) || (p > 1.0))
    goto domerr;
  if (k < 0)
    return (1.0);
  if (n < k) {
  domerr:
    return (-0);
  }
  if (k == n)
    return (0.0);
  dk = k + 1;
  dk = betai(n - k, k + 1, 1 - p);
  return (log(dk));
}


x86 assembly (clang, -O0):
<bdtr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
ja     1158 <bdtr+0x38>
movsd  -0x18(%rbp),%xmm0
movsd  0xeb2(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    115d <bdtr+0x3d>
jmp    118a <bdtr+0x6a>
cmpl   $0x0,-0xc(%rbp)
jge    1179 <bdtr+0x59>
movsd  0xe91(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f5 <bdtr+0xd5>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1197 <bdtr+0x77>
jmp    118a <bdtr+0x6a>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f5 <bdtr+0xd5>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jne    11b0 <bdtr+0x90>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f5 <bdtr+0xd5>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%edi
sub    -0xc(%rbp),%edi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
movsd  0xe2d(%rip),%xmm0
subsd  -0x18(%rbp),%xmm0
cvttsd2si %xmm0,%edx
call   1030 <betai@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1040 <log@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bdtr>:
push   %rax
movapd %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
ja     1165 <bdtr+0x45>
ucomisd 0xec9(%rip),%xmm1
ja     1165 <bdtr+0x45>
mov    %edi,%eax
test   %edi,%edi
js     1167 <bdtr+0x47>
sub    %eax,%esi
jle    1165 <bdtr+0x45>
add    $0x1,%eax
movsd  0xeb2(%rip),%xmm0
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edx
mov    %esi,%edi
mov    %eax,%esi
call   1030 <betai@plt>
pop    %rax
jmp    1040 <log@plt>
pop    %rax
ret
movsd  0xe91(%rip),%xmm0
pop    %rax
ret

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

LLVM IR (-O2):
define double @bdtr(i32 %0, i32 %1, double %2) {
%4 = fcmp olt double %2, 0.000000e+00
%5 = fcmp ogt double %2, 1.000000e+00
%6 = or i1 %4, %5
br i1 %6, label %18, label %7
7: ; preds = %3
%8 = icmp slt i32 %0, 0
br i1 %8, label %18, label %9
9: ; preds = %7
%10 = icmp sgt i32 %1, %0
br i1 %10, label %11, label %18
11: ; preds = %9
%12 = add nuw nsw i32 %0, 1
%13 = sub nsw i32 %1, %0
%14 = fsub double 1.000000e+00, %2
%15 = fptosi double %14 to i32
%16 = tail call double @betai(i32 %13, i32 %12, i32 %15)
%17 = tail call double @log(double %16)
br label %18
18: ; preds = %3, %9, %7, %11
%19 = phi double [ %17, %11 ], [ 1.000000e+00, %7 ], [ 0.000000e+00, %9 ], [ 0.000000e+00, %3 ]
ret double %19
}