#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 q_gamma(double, double, double);
double p_gamma(double a, double x, double loggamma_a) {
  int k;
  double result, term, previous;
  if (x >= 1 + a)
    return 1 - q_gamma(a, x, loggamma_a);
  if (x == 0)
    return 0;
  result = term = exp(a * log(x) - x - loggamma_a) / a;
  for (k = 1; k < 1000; k++) {
    term *= x / (a + k);
    previous = result;
    result += term;
    if (result == previous)
      return result;
  }
  return result;
}


x86 assembly (clang, -O0):
<p_gamma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xeac(%rip),%xmm1
addsd  -0x10(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jb     1190 <p_gamma+0x60>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm2
call   1030 <q_gamma@plt>
movaps %xmm0,%xmm1
movsd  0xe7e(%rip),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    129e <p_gamma+0x16e>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11b5 <p_gamma+0x85>
jp     11b5 <p_gamma+0x85>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    129e <p_gamma+0x16e>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1050 <log@plt>
movaps %xmm0,%xmm2
movsd  -0x48(%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>
divsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  %xmm0,-0x30(%rbp)
movl   $0x1,-0x24(%rbp)
cmpl   $0x3e8,-0x24(%rbp)
jge    1294 <p_gamma+0x164>
movsd  -0x18(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
cvtsi2sdl -0x24(%rbp),%xmm2
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
mulsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
ucomisd -0x40(%rbp),%xmm0
jne    1281 <p_gamma+0x151>
jp     1281 <p_gamma+0x151>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    129e <p_gamma+0x16e>
jmp    1286 <p_gamma+0x156>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1215 <p_gamma+0xe5>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p_gamma>:
sub    $0x18,%rsp
movapd %xmm0,%xmm3
movsd  0xec0(%rip),%xmm0
addsd  %xmm3,%xmm0
ucomisd %xmm0,%xmm1
jae    11ec <p_gamma+0xbc>
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jne    115e <p_gamma+0x2e>
jnp    11e7 <p_gamma+0xb7>
movapd %xmm1,%xmm0
movsd  %xmm1,0x8(%rsp)
movsd  %xmm3,(%rsp)
movsd  %xmm2,0x10(%rsp)
call   1050 <log@plt>
mulsd  (%rsp),%xmm0
subsd  0x8(%rsp),%xmm0
subsd  0x10(%rsp),%xmm0
call   1040 <exp@plt>
movsd  (%rsp),%xmm6
movsd  0x8(%rsp),%xmm5
movapd %xmm0,%xmm1
divsd  %xmm6,%xmm1
mov    $0x1,%eax
movapd %xmm1,%xmm0
nopw   0x0(%rax,%rax,1)
movapd %xmm0,%xmm2
movapd %xmm1,%xmm3
xorps  %xmm4,%xmm4
cvtsi2sd %eax,%xmm4
addsd  %xmm6,%xmm4
movapd %xmm5,%xmm1
divsd  %xmm4,%xmm1
mulsd  %xmm3,%xmm1
addsd  %xmm1,%xmm0
ucomisd %xmm2,%xmm0
jne    11db <p_gamma+0xab>
jnp    11e7 <p_gamma+0xb7>
lea    0x1(%rax),%ecx
cmp    $0x3e7,%eax
mov    %ecx,%eax
jne    11b0 <p_gamma+0x80>
add    $0x18,%rsp
ret
movapd %xmm3,%xmm0
call   1030 <q_gamma@plt>
movapd %xmm0,%xmm1
movsd  0xdff(%rip),%xmm0
subsd  %xmm1,%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
define double @p_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
store double %0, double* %5
store double %1, double* %6
store double %2, double* %7
%12 = load double, double* %6
%13 = load double, double* %5
%14 = fadd double 1.000000e+00, %13
%15 = fcmp oge double %12, %14
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = load double, double* %5
%18 = load double, double* %6
%19 = load double, double* %7
%20 = call double @q_gamma(double %17, double %18, double %19)
%21 = fsub double 1.000000e+00, %20
store double %21, double* %4
br label %65
22: ; preds = %3
%23 = load double, double* %6
%24 = fcmp oeq double %23, 0.000000e+00
br i1 %24, label %25, label %26
25: ; preds = %22
store double 0.000000e+00, double* %4
br label %65
26: ; preds = %22
%27 = load double, double* %5
%28 = load double, double* %6
%29 = call double @log(double %28)
%30 = load double, double* %6
%31 = fneg double %30
%32 = call double @llvm.fmuladd.f64(double %27, double %29, double %31)
%33 = load double, double* %7
%34 = fsub double %32, %33
%35 = call double @exp(double %34)
%36 = load double, double* %5
%37 = fdiv double %35, %36
store double %37, double* %10
store double %37, double* %9
store i32 1, i32* %8
br label %38
38: ; preds = %60, %26
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 1000
br i1 %40, label %41, label %63
41: ; preds = %38
%42 = load double, double* %6
%43 = load double, double* %5
%44 = load i32, i32* %8
%45 = sitofp i32 %44 to double
%46 = fadd double %43, %45
%47 = fdiv double %42, %46
%48 = load double, double* %10
%49 = fmul double %48, %47
store double %49, double* %10
%50 = load double, double* %9
store double %50, double* %11
%51 = load double, double* %10
%52 = load double, double* %9
%53 = fadd double %52, %51
store double %53, double* %9
%54 = load double, double* %9
%55 = load double, double* %11
%56 = fcmp oeq double %54, %55
br i1 %56, label %57, label %59
57: ; preds = %41
%58 = load double, double* %9
store double %58, double* %4
br label %65
59: ; preds = %41
br label %60
60: ; preds = %59
%61 = load i32, i32* %8
%62 = add nsw i32 %61, 1
store i32 %62, i32* %8
br label %38
63: ; preds = %38
%64 = load double, double* %9
store double %64, double* %4
br label %65
65: ; preds = %63, %57, %25, %16
%66 = load double, double* %4
ret double %66
}

LLVM IR (-O2):
define double @p_gamma(double %0, double %1, double %2) {
%4 = fadd double %0, 1.000000e+00
%5 = fcmp ugt double %4, %1
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = tail call double @q_gamma(double %0, double %1, double %2)
%8 = fsub double 1.000000e+00, %7
br label %31
9: ; preds = %3
%10 = fcmp oeq double %1, 0.000000e+00
br i1 %10, label %31, label %11
11: ; preds = %9
%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, %0
br label %18
18: ; preds = %18, %11
%19 = phi double [ %17, %11 ], [ %25, %18 ]
%20 = phi double [ %17, %11 ], [ %26, %18 ]
%21 = phi i32 [ 1, %11 ], [ %28, %18 ]
%22 = sitofp i32 %21 to double
%23 = fadd double %22, %0
%24 = fdiv double %1, %23
%25 = fmul double %19, %24
%26 = fadd double %20, %25
%27 = fcmp oeq double %26, %20
%28 = add nuw nsw i32 %21, 1
%29 = icmp eq i32 %28, 1000
%30 = select i1 %27, i1 true, i1 %29
br i1 %30, label %31, label %18
31: ; preds = %18, %9, %6
%32 = phi double [ %8, %6 ], [ 0.000000e+00, %9 ], [ %26, %18 ]
ret double %32
}