#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 ChiSq_1(int, float);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, int, double, double);
float ChiSq(int v, double D, double tol) {
  int i = 0;
  float bot, top, mid;
  double d;
  bot = (float)0.5;
  top = (float)1. - (float)tol;
  if ((D > ChiSq_1(v, top)) || (D < ChiSq_1(v, bot))) {
    printf("Cannot find chiSq(%d) for %lf (tol=%lf)\n", v, D, tol);
    exit(1);
  }
  mid = (top + bot) / (float)2.;
  while ((((((d = (double)ChiSq_1(v, mid)) - D) < 0.)
               ? -((d = (double)ChiSq_1(v, mid)) - D)
               : ((d = (double)ChiSq_1(v, mid)) - D)) > tol) &&
         (++i < 20)) {
    if (d > D)
      top = mid;
    else
      bot = mid;
    mid = (top + bot) / (float)2.;
  }
  return (mid);
}


x86 assembly (clang, -O0):
<ChiSq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movss  0xeb0(%rip),%xmm0
movss  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm1
movss  0xe96(%rip),%xmm0
subss  %xmm1,%xmm0
movss  %xmm0,-0x24(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x4(%rbp),%edi
movss  -0x24(%rbp),%xmm0
call   1050 <ChiSq_1@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
ucomisd %xmm1,%xmm0
ja     11c2 <ChiSq+0x92>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x4(%rbp),%edi
movss  -0x20(%rbp),%xmm0
call   1050 <ChiSq_1@plt>
movsd  -0x40(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jbe    11e5 <ChiSq+0xb5>
mov    -0x4(%rbp),%esi
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
lea    0xe36(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
movss  -0x24(%rbp),%xmm0
addss  -0x20(%rbp),%xmm0
movss  0xe11(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
movss  -0x28(%rbp),%xmm0
call   1050 <ChiSq_1@plt>
movaps %xmm0,%xmm1
movsd  %xmm1,-0x30(%rbp)
subsd  -0x10(%rbp),%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    125f <ChiSq+0x12f>
mov    -0x4(%rbp),%edi
movss  -0x28(%rbp),%xmm0
call   1050 <ChiSq_1@plt>
movsd  %xmm0,-0x30(%rbp)
subsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x48(%rbp)
jmp    127b <ChiSq+0x14b>
mov    -0x4(%rbp),%edi
movss  -0x28(%rbp),%xmm0
call   1050 <ChiSq_1@plt>
movsd  %xmm0,-0x30(%rbp)
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
xor    %eax,%eax
ucomisd -0x18(%rbp),%xmm0
mov    %al,-0x49(%rbp)
jbe    12a2 <ChiSq+0x172>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x14,%eax
setl   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    12b2 <ChiSq+0x182>
jmp    12fb <ChiSq+0x1cb>
movsd  -0x30(%rbp),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    12d1 <ChiSq+0x1a1>
movss  -0x28(%rbp),%xmm0
movss  %xmm0,-0x24(%rbp)
jmp    12db <ChiSq+0x1ab>
movss  -0x28(%rbp),%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x24(%rbp),%xmm0
addss  -0x20(%rbp),%xmm0
movss  0xd1b(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x28(%rbp)
jmp    1200 <ChiSq+0xd0>
movss  -0x28(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ChiSq>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
movsd  %xmm0,0x8(%rsp)
mov    %edi,%ebp
movsd  %xmm1,0x18(%rsp)
cvtsd2ss %xmm1,%xmm1
movss  0xeb0(%rip),%xmm0
subss  %xmm1,%xmm0
movss  %xmm0,0x10(%rsp)
call   1050 <ChiSq_1@plt>
movsd  0x8(%rsp),%xmm1
ucomisd %xmm0,%xmm1
ja     12f6 <ChiSq+0x1c6>
movss  0xe8d(%rip),%xmm0
mov    %ebp,%edi
call   1050 <ChiSq_1@plt>
ucomisd 0x8(%rsp),%xmm0
ja     12f6 <ChiSq+0x1c6>
movss  0x10(%rsp),%xmm1
movss  0xe6c(%rip),%xmm0
addss  %xmm0,%xmm1
mulss  %xmm0,%xmm1
movss  %xmm1,0x4(%rsp)
mov    %ebp,%edi
movaps %xmm1,%xmm0
call   1050 <ChiSq_1@plt>
subsd  0x8(%rsp),%xmm0
movapd %xmm0,0x20(%rsp)
mov    %ebp,%edi
movss  0x4(%rsp),%xmm0
call   1050 <ChiSq_1@plt>
movapd %xmm0,%xmm1
movsd  0x8(%rsp),%xmm5
subsd  %xmm5,%xmm1
movapd 0xe31(%rip),%xmm2
xorpd  %xmm1,%xmm2
xorpd  %xmm3,%xmm3
movapd 0x20(%rsp),%xmm4
cmpltsd %xmm3,%xmm4
movapd %xmm4,%xmm3
andnpd %xmm1,%xmm3
andpd  %xmm2,%xmm4
orpd   %xmm3,%xmm4
ucomisd 0x18(%rsp),%xmm4
jbe    12e9 <ChiSq+0x1b9>
movapd %xmm5,%xmm3
xor    %ebx,%ebx
movss  0xde8(%rip),%xmm1
movss  %xmm1,0x14(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
ucomisd %xmm3,%xmm0
ja     1250 <ChiSq+0x120>
movss  0x4(%rsp),%xmm2
movss  0x10(%rsp),%xmm0
jmp    1262 <ChiSq+0x132>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movss  0x4(%rsp),%xmm0
movss  %xmm0,0x10(%rsp)
movss  0x14(%rsp),%xmm2
movss  %xmm2,0x14(%rsp)
addss  %xmm2,%xmm0
mulss  %xmm1,%xmm0
mov    %ebp,%edi
movss  %xmm0,0x4(%rsp)
call   1050 <ChiSq_1@plt>
subsd  0x8(%rsp),%xmm0
movapd %xmm0,0x20(%rsp)
mov    %ebp,%edi
movss  0x4(%rsp),%xmm0
call   1050 <ChiSq_1@plt>
movsd  0x8(%rsp),%xmm3
movapd %xmm0,%xmm1
subsd  %xmm3,%xmm1
movapd 0x20(%rsp),%xmm4
cmpltsd 0xd6d(%rip),%xmm4
movapd %xmm4,%xmm2
andnpd %xmm1,%xmm2
xorpd  0xd4d(%rip),%xmm1
andpd  %xmm4,%xmm1
orpd   %xmm2,%xmm1
ucomisd 0x18(%rsp),%xmm1
jbe    12e9 <ChiSq+0x1b9>
lea    0x1(%rbx),%eax
cmp    $0x12,%ebx
mov    %eax,%ebx
movss  0xd21(%rip),%xmm1
jb     1230 <ChiSq+0x100>
movss  0x4(%rsp),%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd2b(%rip),%rdi
mov    %ebp,%esi
movsd  0x8(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"Cannot find chiSq(%d) for %lf (tol=%lf)\0A\00"
define float @ChiSq(i32 %0, double %1, double %2) {
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca float
%9 = alloca float
%10 = alloca float
%11 = alloca double
store i32 %0, i32* %4
store double %1, double* %5
store double %2, double* %6
store i32 0, i32* %7
store float 5.000000e-01, float* %8
%12 = load double, double* %6
%13 = fptrunc double %12 to float
%14 = fsub float 1.000000e+00, %13
store float %14, float* %9
%15 = load double, double* %5
%16 = load i32, i32* %4
%17 = load float, float* %9
%18 = call double @ChiSq_1(i32 %16, float %17)
%19 = fcmp ogt double %15, %18
br i1 %19, label %26, label %20
20: ; preds = %3
%21 = load double, double* %5
%22 = load i32, i32* %4
%23 = load float, float* %8
%24 = call double @ChiSq_1(i32 %22, float %23)
%25 = fcmp olt double %21, %24
br i1 %25, label %26, label %32
26: ; preds = %20, %3
%27 = load i32, i32* %4
%28 = load double, double* %5
%29 = load double, double* %6
%30 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %27, double %28, double %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %20
%33 = load float, float* %9
%34 = load float, float* %8
%35 = fadd float %33, %34
%36 = fdiv float %35, 2.000000e+00
store float %36, float* %10
br label %37
37: ; preds = %75, %32
%38 = load i32, i32* %4
%39 = load float, float* %10
%40 = call double @ChiSq_1(i32 %38, float %39)
store double %40, double* %11
%41 = load double, double* %5
%42 = fsub double %40, %41
%43 = fcmp olt double %42, 0.000000e+00
br i1 %43, label %44, label %51
44: ; preds = %37
%45 = load i32, i32* %4
%46 = load float, float* %10
%47 = call double @ChiSq_1(i32 %45, float %46)
store double %47, double* %11
%48 = load double, double* %5
%49 = fsub double %47, %48
%50 = fneg double %49
br label %57
51: ; preds = %37
%52 = load i32, i32* %4
%53 = load float, float* %10
%54 = call double @ChiSq_1(i32 %52, float %53)
store double %54, double* %11
%55 = load double, double* %5
%56 = fsub double %54, %55
br label %57
57: ; preds = %51, %44
%58 = phi double [ %50, %44 ], [ %56, %51 ]
%59 = load double, double* %6
%60 = fcmp ogt double %58, %59
br i1 %60, label %61, label %65
61: ; preds = %57
%62 = load i32, i32* %7
%63 = add nsw i32 %62, 1
store i32 %63, i32* %7
%64 = icmp slt i32 %63, 20
br label %65
65: ; preds = %61, %57
%66 = phi i1 [ false, %57 ], [ %64, %61 ]
br i1 %66, label %67, label %80
67: ; preds = %65
%68 = load double, double* %11
%69 = load double, double* %5
%70 = fcmp ogt double %68, %69
br i1 %70, label %71, label %73
71: ; preds = %67
%72 = load float, float* %10
store float %72, float* %9
br label %75
73: ; preds = %67
%74 = load float, float* %10
store float %74, float* %8
br label %75
75: ; preds = %73, %71
%76 = load float, float* %9
%77 = load float, float* %8
%78 = fadd float %76, %77
%79 = fdiv float %78, 2.000000e+00
store float %79, float* %10
br label %37
80: ; preds = %65
%81 = load float, float* %10
ret float %81
}

LLVM IR (-O2):
@.str = [41 x i8] c"Cannot find chiSq(%d) for %lf (tol=%lf)\0A\00"
define float @ChiSq(i32 %0, double %1, double %2) {
%4 = fptrunc double %2 to float
%5 = fsub float 1.000000e+00, %4
%6 = tail call double @ChiSq_1(i32 %0, float %5)
%7 = fcmp olt double %6, %1
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = tail call double @ChiSq_1(i32 %0, float 5.000000e-01)
%10 = fcmp ogt double %9, %1
br i1 %10, label %11, label %14
11: ; preds = %8, %3
%12 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %0, double %1, double %2)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = fadd float %5, 5.000000e-01
%16 = fmul float %15, 5.000000e-01
%17 = tail call double @ChiSq_1(i32 %0, float %16)
%18 = fsub double %17, %1
%19 = fcmp olt double %18, 0.000000e+00
%20 = tail call double @ChiSq_1(i32 %0, float %16)
%21 = fsub double %20, %1
%22 = fneg double %21
%23 = select i1 %19, double %22, double %21
%24 = fcmp ogt double %23, %2
br i1 %24, label %25, label %47
25: ; preds = %14, %25
%26 = phi double [ %40, %25 ], [ %20, %14 ]
%27 = phi float [ %36, %25 ], [ %16, %14 ]
%28 = phi float [ %34, %25 ], [ %5, %14 ]
%29 = phi float [ %33, %25 ], [ 5.000000e-01, %14 ]
%30 = phi i32 [ %31, %25 ], [ 0, %14 ]
%31 = add nuw nsw i32 %30, 1
%32 = fcmp ogt double %26, %1
%33 = select i1 %32, float %29, float %27
%34 = select i1 %32, float %27, float %28
%35 = fadd float %34, %33
%36 = fmul float %35, 5.000000e-01
%37 = tail call double @ChiSq_1(i32 %0, float %36)
%38 = fsub double %37, %1
%39 = fcmp olt double %38, 0.000000e+00
%40 = tail call double @ChiSq_1(i32 %0, float %36)
%41 = fsub double %40, %1
%42 = fneg double %41
%43 = select i1 %39, double %42, double %41
%44 = fcmp ogt double %43, %2
%45 = icmp ult i32 %30, 18
%46 = select i1 %44, i1 %45, i1 false
br i1 %46, label %25, label %47
47: ; preds = %25, %14
%48 = phi float [ %16, %14 ], [ %36, %25 ]
ret float %48
}