#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 exp(double);
double log(double);
double p_gamma(double, double, double);
int /*<<< orphan*/ rhoRubyFPrintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
double q_gamma(double a, double x, double loggamma_a) {
  int k;
  double result, w, temp, previous;
  double la = 1, lb = 1 + x - a;
  if (x < 1 + a)
    return 1 - p_gamma(a, x, loggamma_a);
  w = exp(a * log(x) - x - loggamma_a);
  result = w / lb;
  for (k = 2; k < 1000; k++) {
    temp = ((k - 1 - a) * (lb - la) + (k + x) * lb) / k;
    la = lb;
    lb = temp;
    w *= (k - 1 - a) / k;
    temp = w / (la * lb);
    previous = result;
    result += temp;
    if (result == previous)
      return result;
  }
  rhoRubyFPrintf(stderr, "erf.c:%d:q_gamma() could not converge.", 71);
  return result;
}


x86 assembly (clang, -O0):
<q_gamma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  0xea1(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  0xe94(%rip),%xmm0
addsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  0xe78(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11c4 <q_gamma+0x84>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm2
call   1030 <p_gamma@plt>
movaps %xmm0,%xmm1
movsd  0xe4a(%rip),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1337 <q_gamma+0x1f7>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1050 <log@plt>
movaps %xmm0,%xmm2
movsd  -0x60(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
subsd  -0x20(%rbp),%xmm0
call   1040 <exp@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
divsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x2,-0x24(%rbp)
cmpl   $0x3e8,-0x24(%rbp)
jge    1313 <q_gamma+0x1d3>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm2
subsd  -0x50(%rbp),%xmm2
cvtsi2sdl -0x24(%rbp),%xmm1
addsd  -0x18(%rbp),%xmm1
mulsd  -0x58(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvtsi2sdl -0x24(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
subsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x24(%rbp),%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  -0x50(%rbp),%xmm1
mulsd  -0x58(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
ucomisd -0x48(%rbp),%xmm0
jne    1300 <q_gamma+0x1c0>
jp     1300 <q_gamma+0x1c0>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1337 <q_gamma+0x1f7>
jmp    1305 <q_gamma+0x1c5>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1229 <q_gamma+0xe9>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
lea    0xce5(%rip),%rsi
mov    $0x47,%edx
call   1060 <rhoRubyFPrintf@plt>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<q_gamma>:
sub    $0x48,%rsp
movapd %xmm0,%xmm4
movsd  0xeb0(%rip),%xmm3
addsd  %xmm3,%xmm0
ucomisd %xmm1,%xmm0
jbe    1174 <q_gamma+0x34>
movapd %xmm4,%xmm0
call   1030 <p_gamma@plt>
movsd  0xe95(%rip),%xmm6
subsd  %xmm0,%xmm6
jmp    1293 <q_gamma+0x153>
movapd %xmm1,%xmm0
addsd  %xmm3,%xmm0
subsd  %xmm4,%xmm0
movapd %xmm0,0x30(%rsp)
movapd %xmm1,%xmm0
movsd  %xmm1,0x10(%rsp)
movsd  %xmm4,0x8(%rsp)
movsd  %xmm2,0x28(%rsp)
call   1050 <log@plt>
mulsd  0x8(%rsp),%xmm0
subsd  0x10(%rsp),%xmm0
subsd  0x28(%rsp),%xmm0
call   1040 <exp@plt>
movsd  0xe40(%rip),%xmm5
movsd  0x8(%rsp),%xmm8
movsd  0x10(%rsp),%xmm7
movapd %xmm0,%xmm6
movapd 0x30(%rsp),%xmm1
divsd  %xmm1,%xmm6
unpcklpd %xmm1,%xmm0
mov    $0x2,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movapd %xmm6,%xmm3
lea    -0x1(%rax),%ecx
xorps  %xmm1,%xmm1
cvtsi2sd %ecx,%xmm1
subsd  %xmm8,%xmm1
movapd %xmm0,%xmm2
unpckhpd %xmm0,%xmm2
movapd %xmm2,%xmm4
subsd  %xmm5,%xmm4
xorps  %xmm5,%xmm5
cvtsi2sd %eax,%xmm5
movapd %xmm5,%xmm6
addsd  %xmm7,%xmm6
mulsd  %xmm2,%xmm6
mulsd  %xmm1,%xmm4
addsd  %xmm6,%xmm4
unpcklpd %xmm4,%xmm1
unpcklpd %xmm5,%xmm5
divpd  %xmm5,%xmm1
mulpd  %xmm1,%xmm0
movapd %xmm0,%xmm4
unpckhpd %xmm0,%xmm4
movapd %xmm0,%xmm6
divsd  %xmm4,%xmm6
addsd  %xmm3,%xmm6
ucomisd %xmm3,%xmm6
jne    125a <q_gamma+0x11a>
jnp    1293 <q_gamma+0x153>
add    $0x1,%eax
shufpd $0x2,%xmm1,%xmm0
movapd %xmm2,%xmm5
cmp    $0x3e8,%eax
jne    11f0 <q_gamma+0xb0>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%rip),%rsi
mov    $0x47,%edx
movapd %xmm6,0x10(%rsp)
call   1060 <rhoRubyFPrintf@plt>
movapd 0x10(%rsp),%xmm6
movapd %xmm6,%xmm0
add    $0x48,%rsp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"erf.c:%d:q_gamma() could not converge.\00"
define double @q_gamma(double %0, double %1, double %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
store double %0, double* %5
store double %1, double* %6
store double %2, double* %7
store double 1.000000e+00, double* %13
%15 = load double, double* %6
%16 = fadd double 1.000000e+00, %15
%17 = load double, double* %5
%18 = fsub double %16, %17
store double %18, double* %14
%19 = load double, double* %6
%20 = load double, double* %5
%21 = fadd double 1.000000e+00, %20
%22 = fcmp olt double %19, %21
br i1 %22, label %23, label %29
23: ; preds = %3
%24 = load double, double* %5
%25 = load double, double* %6
%26 = load double, double* %7
%27 = call double @p_gamma(double %24, double %25, double %26)
%28 = fsub double 1.000000e+00, %27
store double %28, double* %4
br label %98
29: ; preds = %3
%30 = load double, double* %5
%31 = load double, double* %6
%32 = call double @log(double %31)
%33 = load double, double* %6
%34 = fneg double %33
%35 = call double @llvm.fmuladd.f64(double %30, double %32, double %34)
%36 = load double, double* %7
%37 = fsub double %35, %36
%38 = call double @exp(double %37)
store double %38, double* %10
%39 = load double, double* %10
%40 = load double, double* %14
%41 = fdiv double %39, %40
store double %41, double* %9
store i32 2, i32* %8
br label %42
42: ; preds = %91, %29
%43 = load i32, i32* %8
%44 = icmp slt i32 %43, 1000
br i1 %44, label %45, label %94
45: ; preds = %42
%46 = load i32, i32* %8
%47 = sub nsw i32 %46, 1
%48 = sitofp i32 %47 to double
%49 = load double, double* %5
%50 = fsub double %48, %49
%51 = load double, double* %14
%52 = load double, double* %13
%53 = fsub double %51, %52
%54 = load i32, i32* %8
%55 = sitofp i32 %54 to double
%56 = load double, double* %6
%57 = fadd double %55, %56
%58 = load double, double* %14
%59 = fmul double %57, %58
%60 = call double @llvm.fmuladd.f64(double %50, double %53, double %59)
%61 = load i32, i32* %8
%62 = sitofp i32 %61 to double
%63 = fdiv double %60, %62
store double %63, double* %11
%64 = load double, double* %14
store double %64, double* %13
%65 = load double, double* %11
store double %65, double* %14
%66 = load i32, i32* %8
%67 = sub nsw i32 %66, 1
%68 = sitofp i32 %67 to double
%69 = load double, double* %5
%70 = fsub double %68, %69
%71 = load i32, i32* %8
%72 = sitofp i32 %71 to double
%73 = fdiv double %70, %72
%74 = load double, double* %10
%75 = fmul double %74, %73
store double %75, double* %10
%76 = load double, double* %10
%77 = load double, double* %13
%78 = load double, double* %14
%79 = fmul double %77, %78
%80 = fdiv double %76, %79
store double %80, double* %11
%81 = load double, double* %9
store double %81, double* %12
%82 = load double, double* %11
%83 = load double, double* %9
%84 = fadd double %83, %82
store double %84, double* %9
%85 = load double, double* %9
%86 = load double, double* %12
%87 = fcmp oeq double %85, %86
br i1 %87, label %88, label %90
88: ; preds = %45
%89 = load double, double* %9
store double %89, double* %4
br label %98
90: ; preds = %45
br label %91
91: ; preds = %90
%92 = load i32, i32* %8
%93 = add nsw i32 %92, 1
store i32 %93, i32* %8
br label %42
94: ; preds = %42
%95 = load i32, i32* @stderr
%96 = call i32 @rhoRubyFPrintf(i32 %95, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 71)
%97 = load double, double* %9
store double %97, double* %4
br label %98
98: ; preds = %94, %88, %23
%99 = load double, double* %4
ret double %99
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"erf.c:%d:q_gamma() could not converge.\00"
define double @q_gamma(double %0, double %1, double %2) {
%4 = fadd double %1, 1.000000e+00
%5 = fsub double %4, %0
%6 = fadd double %0, 1.000000e+00
%7 = fcmp ogt double %6, %1
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call double @p_gamma(double %0, double %1, double %2)
%10 = fsub double 1.000000e+00, %9
br label %52
11: ; preds = %3
%12 = tail call double @log(double %1)
%13 = fneg double %1
%14 = tail call double @llvm.fmuladd.f64(double %0, double %12, double %13)
%15 = fsub double %14, %2
%16 = tail call double @exp(double %15)
%17 = fdiv double %16, %5
%18 = insertelement <2 x double> poison, double %16, i64 0
%19 = insertelement <2 x double> %18, double %5, i64 1
br label %24
20: ; preds = %24
%21 = add nuw nsw i32 %26, 1
%22 = icmp eq i32 %21, 1000
%23 = shufflevector <2 x double> %43, <2 x double> %42, <2 x i32> <i32 0, i32 3>
br i1 %22, label %49, label %24
24: ; preds = %11, %20
%25 = phi double [ 1.000000e+00, %11 ], [ %32, %20 ]
%26 = phi i32 [ 2, %11 ], [ %21, %20 ]
%27 = phi double [ %17, %11 ], [ %47, %20 ]
%28 = phi <2 x double> [ %19, %11 ], [ %23, %20 ]
%29 = add nsw i32 %26, -1
%30 = sitofp i32 %29 to double
%31 = fsub double %30, %0
%32 = extractelement <2 x double> %28, i64 1
%33 = fsub double %32, %25
%34 = sitofp i32 %26 to double
%35 = fadd double %34, %1
%36 = fmul double %35, %32
%37 = tail call double @llvm.fmuladd.f64(double %31, double %33, double %36)
%38 = insertelement <2 x double> poison, double %31, i64 0
%39 = insertelement <2 x double> %38, double %37, i64 1
%40 = insertelement <2 x double> poison, double %34, i64 0
%41 = shufflevector <2 x double> %40, <2 x double> poison, <2 x i32> zeroinitializer
%42 = fdiv <2 x double> %39, %41
%43 = fmul <2 x double> %28, %42
%44 = extractelement <2 x double> %43, i64 0
%45 = extractelement <2 x double> %43, i64 1
%46 = fdiv double %44, %45
%47 = fadd double %27, %46
%48 = fcmp oeq double %47, %27
br i1 %48, label %52, label %20
49: ; preds = %20
%50 = load i32, i32* @stderr
%51 = tail call i32 @rhoRubyFPrintf(i32 %50, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 71)
br label %52
52: ; preds = %24, %49, %8
%53 = phi double [ %10, %8 ], [ %47, %49 ], [ %47, %24 ]
ret double %53
}