// File: /workspace/github_cpp_makefile_star2_O1/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:
xorpd %xmm0, %xmm0
xorl %esi, %esi
movl $0x14, %eax
movl $0xffffffec, %edi       # imm = 0xFFFFFFEC
leaq 0x2b587c(%rip), %rcx    # 0x376660
xorl %edx, %edx
xorpd %xmm1, %xmm1
movl (%rsi,%rcx), %r8d
xorps %xmm2, %xmm2
cvtsi2sd %r8d, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %edi, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm1
leal 0x14(%rdi), %r9d
cmpl %edx, %r8d
cmovgl %r8d, %edx
cmovgl %r9d, %eax
addsd %xmm2, %xmm0
incl %edi
addq $0x4, %rsi
cmpq $0xa4, %rsi
jne 0xc0dea
pushq %rbp
movq %rsp, %rbp
divsd %xmm0, %xmm1
xorpd %xmm2, %xmm2
cmpneqsd %xmm2, %xmm0
andpd %xmm1, %xmm0
movlpd %xmm0, 0x2b57fc(%rip)   # 0x376640
leaq 0x15a0d9(%rip), %rsi    # 0x21af24
movl (%rsi), %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
testl %esi, %esi
je 0xc0ea0
movsd 0x2b58c8(%rip), %xmm3   # 0x376728
movsd 0x2b58b8(%rip), %xmm1   # 0x376720
movapd %xmm3, %xmm2
mulsd %xmm3, %xmm2
divsd %xmm0, %xmm2
subsd %xmm2, %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0xc0e84
jnp 0xc0ea0
mulsd 0x2b587c(%rip), %xmm3   # 0x376708
movsd 0x2b5884(%rip), %xmm2   # 0x376718
divsd %xmm0, %xmm3
subsd %xmm3, %xmm2
divsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
leaq 0x15a085(%rip), %rsi    # 0x21af30
movsd %xmm2, (%rsi)
xorl %edi, %edi
xorl %esi, %esi
xorl %r8d, %r8d
movl (%rcx), %r9d
testl %r9d, %r9d
setne %r10b
cmpl %edx, %r9d
sete %r9b
andb %r10b, %r9b
movl $0x0, %r10d
cmovnel %edi, %r10d
addl %r10d, %r8d
movzbl %r9b, %r9d
addl %r9d, %esi
incq %rdi
addq $0x4, %rcx
cmpq $0x29, %rdi
jne 0xc0eb6
cmpl $0x2, %esi
jae 0xc0efc
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
jmp 0xc0f0f
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
divsd %xmm1, %xmm0
addsd 0x48189(%rip), %xmm0    # 0x1090a0
popq %rbp
retq