// File: /workspace/github_cpp_makefile_star2_O2/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:
movl $0x14, %eax
xorl %esi, %esi
xorpd %xmm0, %xmm0
movl $0xffffffec, %edi       # imm = 0xFFFFFFEC
leaq 0x2b0202(%rip), %rcx    # 0x360660
xorpd %xmm1, %xmm1
xorl %edx, %edx
cmpq $0xa4, %rsi
je 0xb049d
movl (%rsi,%rcx), %r8d
cvtsi2sd %r8d, %xmm2
cvtsi2sd %edi, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
leal 0x14(%rdi), %r9d
cmpl %edx, %r8d
cmovgl %r8d, %edx
cmovgl %r9d, %eax
incl %edi
addq $0x4, %rsi
jmp 0xb0464
pushq %rbp
movq %rsp, %rbp
divsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
cmpneqsd %xmm2, %xmm1
andpd %xmm0, %xmm1
movlpd %xmm1, 0x2b0186(%rip)   # 0x360640
leaq 0x154a63(%rip), %rsi    # 0x204f24
movl (%rsi), %esi
cvtsi2sd %esi, %xmm0
testl %esi, %esi
je 0xb0513
movsd 0x2b0255(%rip), %xmm3   # 0x360728
movsd 0x2b0245(%rip), %xmm1   # 0x360720
movapd %xmm3, %xmm2
mulsd %xmm3, %xmm2
divsd %xmm0, %xmm2
subsd %xmm2, %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0xb04f7
jnp 0xb0513
mulsd 0x2b0209(%rip), %xmm3   # 0x360708
movsd 0x2b0211(%rip), %xmm2   # 0x360718
divsd %xmm0, %xmm3
subsd %xmm3, %xmm2
divsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
leaq 0x154a12(%rip), %rsi    # 0x204f30
movsd %xmm2, (%rsi)
xorl %r8d, %r8d
xorl %esi, %esi
xorl %edi, %edi
cmpq $0x29, %r8
je 0xb0560
movl (%rcx), %r9d
testl %r9d, %r9d
setne %r10b
cmpl %edx, %r9d
sete %r9b
andb %r10b, %r9b
movl $0x0, %r10d
cmovnel %r8d, %r10d
addl %r10d, %esi
movzbl %r9b, %r9d
addl %r9d, %edi
incq %r8
addq $0x4, %rcx
jmp 0xb0529
cmpl $0x2, %edi
jae 0xb056e
movl %eax, %eax
cvtsi2sd %rax, %xmm0
jmp 0xb057a
cvtsi2sd %esi, %xmm0
cvtsi2sd %edi, %xmm1
divsd %xmm1, %xmm0
addsd 0x42b1e(%rip), %xmm0    # 0xf30a0
popq %rbp
retq