#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 betacf(int, int, double);
double exp(scalar_t__);
scalar_t__ gammln(int);
int log(double);
int /*<<< orphan*/ printf(char *);
double betai(int a, int b, double x) {
  double bt;
  if (x < 0.0 || x > 1.0) {
    printf("Bad x in routine betai");
  }
  if (x == 0.0 || x == 1.0) {
    bt = 0.0;
  } else
    bt = exp(gammln(a + b) - gammln(a) - gammln(b) + a * log(x) +
             b * log(1.0 - x));
  if (x < (a + 1.0) / (a + b + 2.0)) {
    return bt * betacf(a, b, x) / a;
  } else
    return 1.0 - bt * betacf(b, a, 1.0 - x) / (double)b;
}


x86 assembly (clang, -O0):
<betai>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
ja     1188 <betai+0x38>
movsd  -0x18(%rbp),%xmm0
movsd  0xe82(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1194 <betai+0x44>
lea    0xe81(%rip),%rdi
call   1030 <printf@plt>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11b1 <betai+0x61>
jp     11b1 <betai+0x61>
jmp    11ce <betai+0x7e>
movsd  -0x18(%rbp),%xmm0
movsd  0xe42(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    11db <betai+0x8b>
jp     11db <betai+0x8b>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    126f <betai+0x11f>
mov    -0xc(%rbp),%edi
add    -0x10(%rbp),%edi
call   1060 <gammln@plt>
mov    %rax,-0x50(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <gammln@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <gammln@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1050 <log@plt>
mov    -0x3c(%rbp),%ecx
mov    %eax,%edx
mov    -0x38(%rbp),%rax
imul   %edx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
movsd  0xdb6(%rip),%xmm0
subsd  -0x18(%rbp),%xmm0
call   1050 <log@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
imul   %ecx,%eax
cltq
add    %rax,%rdi
call   1040 <exp@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm1
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xd7f(%rip),%xmm2
addsd  %xmm2,%xmm0
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
cvtsi2sd %eax,%xmm2
movsd  0xd71(%rip),%xmm3
addsd  %xmm3,%xmm2
divsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jbe    12e2 <betai+0x192>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
movsd  -0x18(%rbp),%xmm0
call   1070 <betacf@plt>
movaps %xmm0,%xmm1
movsd  -0x58(%rbp),%xmm0
mulsd  %xmm1,%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1327 <betai+0x1d7>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
movsd  0xd06(%rip),%xmm0
subsd  -0x18(%rbp),%xmm0
call   1070 <betacf@plt>
movsd  -0x60(%rbp),%xmm1
mulsd  %xmm0,%xmm1
cvtsi2sdl -0x10(%rbp),%xmm0
divsd  %xmm0,%xmm1
movsd  0xce2(%rip),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<betai>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movapd %xmm0,%xmm2
mov    %esi,%r14d
mov    %edi,%r13d
xorpd  %xmm0,%xmm0
ucomisd %xmm2,%xmm0
movsd  %xmm2,0x8(%rsp)
ja     1182 <betai+0x32>
ucomisd 0xe80(%rip),%xmm2
jbe    1194 <betai+0x44>
lea    0xe87(%rip),%rdi
call   1030 <printf@plt>
movsd  0x8(%rsp),%xmm2
lea    (%r14,%r13,1),%r15d
xorpd  %xmm3,%xmm3
ucomisd %xmm3,%xmm2
jne    11a4 <betai+0x54>
jnp    121e <betai+0xce>
ucomisd 0xe54(%rip),%xmm2
jne    11b0 <betai+0x60>
jnp    121e <betai+0xce>
mov    %r15d,%edi
call   1060 <gammln@plt>
mov    %rax,%rbp
mov    %r13d,%edi
call   1060 <gammln@plt>
mov    %rax,0x10(%rsp)
mov    %r14d,%edi
call   1060 <gammln@plt>
mov    %rax,%rbx
movsd  0x8(%rsp),%xmm0
call   1050 <log@plt>
imul   %r13d,%eax
movslq %eax,%r12
movsd  0xe13(%rip),%xmm0
subsd  0x8(%rsp),%xmm0
call   1050 <log@plt>
imul   %r14d,%eax
cltq
add    0x10(%rsp),%rbx
sub    %rbx,%rbp
add    %r12,%rbp
add    %rax,%rbp
mov    %rbp,%rdi
call   1040 <exp@plt>
movsd  0x8(%rsp),%xmm2
movapd %xmm0,%xmm3
cvtsi2sd %r13d,%xmm4
movsd  0xdd5(%rip),%xmm0
cvtsi2sd %r15d,%xmm1
addsd  0xdd0(%rip),%xmm1
addsd  %xmm4,%xmm0
divsd  %xmm1,%xmm0
ucomisd %xmm2,%xmm0
movsd  %xmm3,0x10(%rsp)
jbe    1273 <betai+0x123>
mov    %r13d,%edi
mov    %r14d,%esi
movapd %xmm2,%xmm0
movsd  %xmm4,0x8(%rsp)
call   1070 <betacf@plt>
movsd  0x10(%rsp),%xmm1
mulsd  %xmm0,%xmm1
divsd  0x8(%rsp),%xmm1
jmp    12b0 <betai+0x160>
movsd  0xd85(%rip),%xmm0
subsd  %xmm2,%xmm0
mov    %r14d,%edi
mov    %r13d,%esi
call   1070 <betacf@plt>
movsd  0x10(%rsp),%xmm1
mulsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %r14d,%xmm0
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
movsd  0xd54(%rip),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Bad x in routine betai\00"
define double @betai(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 %16
14: ; preds = %11, %3
%15 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = load double, double* %7
%18 = fcmp oeq double %17, 0.000000e+00
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load double, double* %7
%21 = fcmp oeq double %20, 1.000000e+00
br i1 %21, label %22, label %23
22: ; preds = %19, %16
store double 0.000000e+00, double* %8
br label %48
23: ; preds = %19
%24 = load i32, i32* %5
%25 = load i32, i32* %6
%26 = add nsw i32 %24, %25
%27 = call i64 @gammln(i32 %26)
%28 = load i32, i32* %5
%29 = call i64 @gammln(i32 %28)
%30 = sub nsw i64 %27, %29
%31 = load i32, i32* %6
%32 = call i64 @gammln(i32 %31)
%33 = sub nsw i64 %30, %32
%34 = load i32, i32* %5
%35 = load double, double* %7
%36 = call i32 @log(double %35)
%37 = mul nsw i32 %34, %36
%38 = sext i32 %37 to i64
%39 = add nsw i64 %33, %38
%40 = load i32, i32* %6
%41 = load double, double* %7
%42 = fsub double 1.000000e+00, %41
%43 = call i32 @log(double %42)
%44 = mul nsw i32 %40, %43
%45 = sext i32 %44 to i64
%46 = add nsw i64 %39, %45
%47 = call double @exp(i64 %46)
store double %47, double* %8
br label %48
48: ; preds = %23, %22
%49 = load double, double* %7
%50 = load i32, i32* %5
%51 = sitofp i32 %50 to double
%52 = fadd double %51, 1.000000e+00
%53 = load i32, i32* %5
%54 = load i32, i32* %6
%55 = add nsw i32 %53, %54
%56 = sitofp i32 %55 to double
%57 = fadd double %56, 2.000000e+00
%58 = fdiv double %52, %57
%59 = fcmp olt double %49, %58
br i1 %59, label %60, label %70
60: ; preds = %48
%61 = load double, double* %8
%62 = load i32, i32* %5
%63 = load i32, i32* %6
%64 = load double, double* %7
%65 = call double @betacf(i32 %62, i32 %63, double %64)
%66 = fmul double %61, %65
%67 = load i32, i32* %5
%68 = sitofp i32 %67 to double
%69 = fdiv double %66, %68
store double %69, double* %4
br label %82
70: ; preds = %48
%71 = load double, double* %8
%72 = load i32, i32* %6
%73 = load i32, i32* %5
%74 = load double, double* %7
%75 = fsub double 1.000000e+00, %74
%76 = call double @betacf(i32 %72, i32 %73, double %75)
%77 = fmul double %71, %76
%78 = load i32, i32* %6
%79 = sitofp i32 %78 to double
%80 = fdiv double %77, %79
%81 = fsub double 1.000000e+00, %80
store double %81, double* %4
br label %82
82: ; preds = %70, %60
%83 = load double, double* %4
ret double %83
}

LLVM IR (-O2):
@.str = [23 x i8] c"Bad x in routine betai\00"
define double @betai(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 %7, label %9
7: ; preds = %3
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %3, %7
%10 = fcmp oeq double %2, 0.000000e+00
%11 = fcmp oeq double %2, 1.000000e+00
%12 = or i1 %10, %11
%13 = add nsw i32 %1, %0
br i1 %12, label %30, label %14
14: ; preds = %9
%15 = tail call i64 @gammln(i32 %13)
%16 = tail call i64 @gammln(i32 %0)
%17 = tail call i64 @gammln(i32 %1)
%18 = tail call i32 @log(double %2)
%19 = mul nsw i32 %18, %0
%20 = sext i32 %19 to i64
%21 = fsub double 1.000000e+00, %2
%22 = tail call i32 @log(double %21)
%23 = mul nsw i32 %22, %1
%24 = sext i32 %23 to i64
%25 = add i64 %16, %17
%26 = sub i64 %15, %25
%27 = add i64 %26, %20
%28 = add i64 %27, %24
%29 = tail call double @exp(i64 %28)
br label %30
30: ; preds = %9, %14
%31 = phi double [ %29, %14 ], [ 0.000000e+00, %9 ]
%32 = sitofp i32 %0 to double
%33 = fadd double %32, 1.000000e+00
%34 = sitofp i32 %13 to double
%35 = fadd double %34, 2.000000e+00
%36 = fdiv double %33, %35
%37 = fcmp ogt double %36, %2
br i1 %37, label %38, label %42
38: ; preds = %30
%39 = tail call double @betacf(i32 %0, i32 %1, double %2)
%40 = fmul double %31, %39
%41 = fdiv double %40, %32
br label %49
42: ; preds = %30
%43 = fsub double 1.000000e+00, %2
%44 = tail call double @betacf(i32 %1, i32 %0, double %43)
%45 = fmul double %31, %44
%46 = sitofp i32 %1 to double
%47 = fdiv double %45, %46
%48 = fsub double 1.000000e+00, %47
br label %49
49: ; preds = %42, %38
%50 = phi double [ %41, %38 ], [ %48, %42 ]
ret double %50
}