#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 ntail(int /*<<< orphan*/);
double pochisq(double, int);
double rtlg(double, double);
int /*<<< orphan*/ sqrt(double);
double rtlchsq(int df, double z) {
  double a, x, y;
  if (df == 1)
    return 2.0 * ntail(sqrt(z));
  if (df == 2)
    return exp(-0.5 * z);
  if (df <= 0.0)
    return 1.0;
  y = pochisq(z, df);
  if (y < 1.0e-6) {
    a = 0.5 * (double)df;
    x = 0.5 * z;
    return rtlg(a, x);
  }
  return y;
}


x86 assembly (clang, -O0):
<rtlchsq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movsd  %xmm0,-0x18(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1194 <rtlchsq+0x44>
movsd  -0x18(%rbp),%xmm0
call   1070 <sqrt@plt>
mov    %eax,%edi
call   1030 <ntail@plt>
movaps %xmm0,%xmm1
movsd  0xe9a(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    124e <rtlchsq+0xfe>
cmpl   $0x2,-0xc(%rbp)
jne    11ba <rtlchsq+0x6a>
movsd  0xe72(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
call   1050 <exp@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    124e <rtlchsq+0xfe>
cvtsi2sdl -0xc(%rbp),%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jb     11de <rtlchsq+0x8e>
movsd  0xe3c(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    124e <rtlchsq+0xfe>
movsd  -0x18(%rbp),%xmm0
mov    -0xc(%rbp),%edi
call   1040 <pochisq@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  0xe08(%rip),%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    1244 <rtlchsq+0xf4>
cvtsi2sdl -0xc(%rbp),%xmm1
movsd  0xdf8(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xde7(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
call   1060 <rtlg@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    124e <rtlchsq+0xfe>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtlchsq>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
je     1173 <rtlchsq+0x23>
mov    %edi,%ebx
cmp    $0x1,%edi
jne    1185 <rtlchsq+0x35>
call   1070 <sqrt@plt>
mov    %eax,%edi
call   1030 <ntail@plt>
addsd  %xmm0,%xmm0
jmp    11d1 <rtlchsq+0x81>
mulsd  0xe85(%rip),%xmm0
add    $0x10,%rsp
pop    %rbx
jmp    1050 <exp@plt>
jl     11c9 <rtlchsq+0x79>
movsd  %xmm0,0x8(%rsp)
mov    %ebx,%edi
call   1040 <pochisq@plt>
movsd  0xe74(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    11d1 <rtlchsq+0x81>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  0xe67(%rip),%xmm2
mulsd  %xmm2,%xmm0
movsd  0x8(%rsp),%xmm1
mulsd  %xmm2,%xmm1
add    $0x10,%rsp
pop    %rbx
jmp    1060 <rtlg@plt>
movsd  0xe37(%rip),%xmm0
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define double @rtlchsq(i32 %0, double %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
store i32 %0, i32* %4
store double %1, double* %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load double, double* %5
%13 = call i32 @sqrt(double %12)
%14 = call double @ntail(i32 %13)
%15 = fmul double 2.000000e+00, %14
store double %15, double* %3
br label %45
16: ; preds = %2
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load double, double* %5
%21 = fmul double -5.000000e-01, %20
%22 = call double @exp(double %21)
store double %22, double* %3
br label %45
23: ; preds = %16
%24 = load i32, i32* %4
%25 = sitofp i32 %24 to double
%26 = fcmp ole double %25, 0.000000e+00
br i1 %26, label %27, label %28
27: ; preds = %23
store double 1.000000e+00, double* %3
br label %45
28: ; preds = %23
%29 = load double, double* %5
%30 = load i32, i32* %4
%31 = call double @pochisq(double %29, i32 %30)
store double %31, double* %8
%32 = load double, double* %8
%33 = fcmp olt double %32, 0x3EB0C6F7A0B5ED8D
br i1 %33, label %34, label %43
34: ; preds = %28
%35 = load i32, i32* %4
%36 = sitofp i32 %35 to double
%37 = fmul double 5.000000e-01, %36
store double %37, double* %6
%38 = load double, double* %5
%39 = fmul double 5.000000e-01, %38
store double %39, double* %7
%40 = load double, double* %6
%41 = load double, double* %7
%42 = call double @rtlg(double %40, double %41)
store double %42, double* %3
br label %45
43: ; preds = %28
%44 = load double, double* %8
store double %44, double* %3
br label %45
45: ; preds = %43, %34, %27, %19, %11
%46 = load double, double* %3
ret double %46
}

LLVM IR (-O2):
define double @rtlchsq(i32 %0, double %1) {
switch i32 %0, label %10 [
i32 1, label %3
i32 2, label %7
]
3: ; preds = %2
%4 = tail call i32 @sqrt(double %1)
%5 = tail call double @ntail(i32 %4)
%6 = fmul double %5, 2.000000e+00
br label %20
7: ; preds = %2
%8 = fmul double %1, -5.000000e-01
%9 = tail call double @exp(double %8)
br label %20
10: ; preds = %2
%11 = sitofp i32 %0 to double
%12 = icmp slt i32 %0, 1
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = tail call double @pochisq(double %1, i32 %0)
%15 = fcmp olt double %14, 0x3EB0C6F7A0B5ED8D
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = fmul double %11, 5.000000e-01
%18 = fmul double %1, 5.000000e-01
%19 = tail call double @rtlg(double %17, double %18)
br label %20
20: ; preds = %13, %10, %16, %7, %3
%21 = phi double [ %6, %3 ], [ %9, %7 ], [ %19, %16 ], [ 1.000000e+00, %10 ], [ %14, %13 ]
ret double %21
}