#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);
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;
  }
  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  0xeb1(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  0xea4(%rip),%xmm0
addsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  0xe88(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11b4 <q_gamma+0x84>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm2
call   1030 <p_gamma@plt>
movaps %xmm0,%xmm1
movsd  0xe5a(%rip),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    130d <q_gamma+0x1dd>
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    1303 <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    12f0 <q_gamma+0x1c0>
jp     12f0 <q_gamma+0x1c0>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    130d <q_gamma+0x1dd>
jmp    12f5 <q_gamma+0x1c5>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1219 <q_gamma+0xe9>
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    $0x38,%rsp
movapd %xmm0,%xmm4
movsd  0xec0(%rip),%xmm3
addsd  %xmm3,%xmm0
ucomisd %xmm1,%xmm0
jbe    1164 <q_gamma+0x34>
movapd %xmm4,%xmm0
call   1030 <p_gamma@plt>
movsd  0xea5(%rip),%xmm1
subsd  %xmm0,%xmm1
jmp    1266 <q_gamma+0x136>
movapd %xmm1,%xmm0
addsd  %xmm3,%xmm0
subsd  %xmm4,%xmm0
movapd %xmm0,0x20(%rsp)
movapd %xmm1,%xmm0
movsd  %xmm1,0x10(%rsp)
movsd  %xmm4,0x8(%rsp)
movsd  %xmm2,0x18(%rsp)
call   1050 <log@plt>
mulsd  0x8(%rsp),%xmm0
subsd  0x10(%rsp),%xmm0
subsd  0x18(%rsp),%xmm0
call   1040 <exp@plt>
movsd  0xe50(%rip),%xmm5
movsd  0x8(%rsp),%xmm8
movsd  0x10(%rsp),%xmm7
movapd %xmm0,%xmm1
movapd 0x20(%rsp),%xmm2
divsd  %xmm2,%xmm1
unpcklpd %xmm2,%xmm0
mov    $0x2,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movapd %xmm1,%xmm4
lea    -0x1(%rax),%ecx
xorps  %xmm2,%xmm2
cvtsi2sd %ecx,%xmm2
subsd  %xmm8,%xmm2
movapd %xmm0,%xmm3
unpckhpd %xmm0,%xmm3
movapd %xmm3,%xmm1
subsd  %xmm5,%xmm1
xorps  %xmm5,%xmm5
cvtsi2sd %eax,%xmm5
movapd %xmm5,%xmm6
addsd  %xmm7,%xmm6
mulsd  %xmm3,%xmm6
mulsd  %xmm2,%xmm1
addsd  %xmm6,%xmm1
unpcklpd %xmm1,%xmm2
unpcklpd %xmm5,%xmm5
divpd  %xmm5,%xmm2
movapd %xmm0,%xmm1
mulpd  %xmm2,%xmm1
movapd %xmm1,%xmm5
unpckhpd %xmm1,%xmm5
divsd  %xmm5,%xmm1
addsd  %xmm4,%xmm1
ucomisd %xmm4,%xmm1
jne    124a <q_gamma+0x11a>
jnp    1266 <q_gamma+0x136>
lea    0x1(%rax),%ecx
mulsd  %xmm0,%xmm2
movapd %xmm3,%xmm5
movapd %xmm2,%xmm0
cmp    $0x3e7,%eax
mov    %ecx,%eax
jne    11e0 <q_gamma+0xb0>
movapd %xmm1,%xmm0
add    $0x38,%rsp
ret

LLVM IR (-O0):
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 %96
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 %96
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 double, double* %9
store double %95, double* %4
br label %96
96: ; preds = %94, %88, %23
%97 = load double, double* %4
ret double %97
}

LLVM IR (-O2):
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 %49
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 %20
20: ; preds = %20, %11
%21 = phi double [ 1.000000e+00, %11 ], [ %28, %20 ]
%22 = phi i32 [ 2, %11 ], [ %45, %20 ]
%23 = phi double [ %17, %11 ], [ %43, %20 ]
%24 = phi <2 x double> [ %19, %11 ], [ %48, %20 ]
%25 = add nsw i32 %22, -1
%26 = sitofp i32 %25 to double
%27 = fsub double %26, %0
%28 = extractelement <2 x double> %24, i64 1
%29 = fsub double %28, %21
%30 = sitofp i32 %22 to double
%31 = fadd double %30, %1
%32 = fmul double %31, %28
%33 = tail call double @llvm.fmuladd.f64(double %27, double %29, double %32)
%34 = insertelement <2 x double> poison, double %27, i64 0
%35 = insertelement <2 x double> %34, double %33, i64 1
%36 = insertelement <2 x double> poison, double %30, i64 0
%37 = shufflevector <2 x double> %36, <2 x double> poison, <2 x i32> zeroinitializer
%38 = fdiv <2 x double> %35, %37
%39 = fmul <2 x double> %24, %38
%40 = extractelement <2 x double> %39, i64 0
%41 = extractelement <2 x double> %39, i64 1
%42 = fdiv double %40, %41
%43 = fadd double %23, %42
%44 = fcmp oeq double %43, %23
%45 = add nuw nsw i32 %22, 1
%46 = icmp eq i32 %45, 1000
%47 = select i1 %44, i1 true, i1 %46
%48 = shufflevector <2 x double> %39, <2 x double> %38, <2 x i32> <i32 0, i32 3>
br i1 %47, label %49, label %20
49: ; preds = %20, %8
%50 = phi double [ %10, %8 ], [ %43, %20 ]
ret double %50
}