// File: jkingan[P]ladyheather/heathgps.cpp
// Function: x72_pps_stats(int)
double x72_pps_stats(int dump)
{
unsigned i;
unsigned max_bin;
int max_val;
int max_count;
int max_sum;
double val;
double sum;
double count;
double avg;
double mcv;   // most common value seen
double sxx, syy, sxy;
double a0, a1;
double t;

   // Find the most common PPS value seen during the interval
   // and the average PPS value during the interval.  Return the most
   // common value, set global var x72_pps_avg to the interval average.

   max_val = 0;
   max_bin = X72_PPS_SPREAD;
   count = sum = avg = mcv = 0.0;

   for(i=0; i<X72_PPS_BINS; i++) {     // find the bin with the largest count
      sum += (double) x72_pps_list[i] * (double) ((int) i-X72_PPS_SPREAD);
      count += (double) x72_pps_list[i];

if(0 && debug_file && dump) fprintf(debug_file, "i:%d list:%d   bin:%d  sum:%g  count:%g\n", i, x72_pps_list[i], i-X72_PPS_SPREAD, sum, count);
      if(x72_pps_list[i] > max_val) {
         max_bin = i;
         max_val = x72_pps_list[i];
      }
   }

   if(count) avg = sum / count;
   else avg = 0.0;
   x72_pps_avg = avg;

   a1 = a0 = 0.0;
   t = (double) x72_pps_interval;
   if(x72_pps_interval) {
      sxy = x72_sum_xy - ((x72_sum_x*x72_sum_y) / t);
      syy = x72_sum_yy - ((x72_sum_y*x72_sum_y) / t);
      sxx = x72_sum_xx - ((x72_sum_x*x72_sum_x) / t);
      if(sxx) {
         a1 = (sxy / sxx);
         a0 = (x72_sum_y / t) - (a1*(x72_sum_x/t));
      }
   }
   x72_pps_trend = a1*t;

///sprintf(debug_text2, "sum:%g  count:%g  avg:%g  a1:%g a0:%g   trend:%g -> %g", sum, count, avg, a1,a0, x72_pps_trend,x72_pps_trend/t);

   max_count = 0;
   max_sum = 0;
   for(i=0; i<X72_PPS_BINS; i++) {  // how many bins have the max count?
      if(x72_pps_list[i] == 0) continue;
      if(x72_pps_list[i] == max_val) {
         ++max_count;
         max_sum += i;
      }
   }

   val = (double) max_bin;
   if(max_count > 1) {  // more than one bin with max count... average them
      val = (double) max_sum / (double) max_count;
   }

   mcv = (val - (double) X72_PPS_SPREAD);
if(0 && (last_x72_dmode > 2)) {
   sprintf(debug_text4, "max_sum:%d  max_count:%d  avg:%g  mcv:%g", max_sum, max_count, avg, mcv);
   if(debug_file && dump) fprintf(debug_file, "! max_sum:%d  max_count:%d  avg:%g  mcv:%g\n", max_sum, max_count, avg, mcv);
}

   return mcv;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movl %edi, -0x4(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x14, -0xc(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0x0, -0x8(%rbp)
cmpl $0x29, -0x8(%rbp)
jae 0x109cea
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2cbc1e(%rip), %rax    # 0x3d5890
cvtsi2sdl (%rax,%rcx,4), %xmm0
movl -0x8(%rbp), %eax
subl $0x14, %eax
cvtsi2sd %eax, %xmm2
movsd -0x28(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2cbbf1(%rip), %rax    # 0x3d5890
cvtsi2sdl (%rax,%rcx,4), %xmm0
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2cbbd6(%rip), %rax    # 0x3d5890
movl (%rax,%rcx,4), %eax
cmpl -0x10(%rbp), %eax
jle 0x109cda
movl -0x8(%rbp), %eax
movl %eax, -0xc(%rbp)
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2cbbbc(%rip), %rax    # 0x3d5890
movl (%rax,%rcx,4), %eax
movl %eax, -0x10(%rbp)
jmp 0x109cdc
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x109c5c
movsd -0x30(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x109cfc
jp 0x109cfc
jmp 0x109d0d
movsd -0x28(%rbp), %xmm0
divsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x109d15
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, 0x2cbb4e(%rip)   # 0x3d5870
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
leaq 0x1703cb(%rip), %rax    # 0x27a104
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
leaq 0x1703bb(%rip), %rax    # 0x27a104
cmpl $0x0, (%rax)
je 0x109e2a
movsd 0x2cbbee(%rip), %xmm0   # 0x3d5948
movsd 0x2cbbf6(%rip), %xmm1   # 0x3d5958
mulsd 0x2cbbce(%rip), %xmm1   # 0x3d5938
divsd -0x70(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd 0x2cbbc0(%rip), %xmm0   # 0x3d5940
movsd 0x2cbbb0(%rip), %xmm1   # 0x3d5938
mulsd 0x2cbba8(%rip), %xmm1   # 0x3d5938
divsd -0x70(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd 0x2cbbaa(%rip), %xmm0   # 0x3d5950
movsd 0x2cbbaa(%rip), %xmm1   # 0x3d5958
mulsd 0x2cbba2(%rip), %xmm1   # 0x3d5958
divsd -0x70(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x109dd6
jp 0x109dd6
jmp 0x109e28
movsd -0x58(%rbp), %xmm0
divsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd 0x2cbb4b(%rip), %xmm1   # 0x3d5938
divsd -0x70(%rbp), %xmm1
movsd -0x68(%rbp), %xmm0
movsd 0x2cbb59(%rip), %xmm2   # 0x3d5958
divsd -0x70(%rbp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
jmp 0x109e2a
movsd -0x68(%rbp), %xmm0
mulsd -0x70(%rbp), %xmm0
leaq 0x1702d5(%rip), %rax    # 0x27a110
movsd %xmm0, (%rax)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x8(%rbp)
cmpl $0x29, -0x8(%rbp)
jae 0x109ea1
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2cba2a(%rip), %rax    # 0x3d5890
cmpl $0x0, (%rax,%rcx,4)
jne 0x109e6e
jmp 0x109e96
movl -0x8(%rbp), %eax
movl %eax, %ecx
leaq 0x2cba16(%rip), %rax    # 0x3d5890
movl (%rax,%rcx,4), %eax
cmpl -0x10(%rbp), %eax
jne 0x109e94
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
jmp 0x109e96
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x109e54
movl -0xc(%rbp), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x20(%rbp)
cmpl $0x1, -0x14(%rbp)
jle 0x109ec7
cvtsi2sdl -0x18(%rbp), %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd -0x20(%rbp), %xmm0
movsd 0x651f4(%rip), %xmm1    # 0x16f0c8
subsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
popq %rbp
retq
nopw %cs:(%rax,%rax)