// File: jkingan[P]ladyheather/heathmsc.cpp
// Function: time_sync_median(double)
double time_sync_median(double delta)
{
int i;
int k, m;

   // This routine calculates the median of a string of system clock vs 
   // receiver time samples.  It returns 0 if median is not available,
   // otherwise it returns the value of the median.  This value is used
   // to determine when the system clock has diverged enough from
   // the reseiver time to cause a system clock reset.

   if(clk_sort_count < 0) clk_sort_count = 0;

   if(clk_sort_count >= CLK_SORT-1) {  // we have enough samples, get the median value
      delta = clk_sort[clk_sort_count/2];
      delta += clk_sort[clk_sort_count/2-1];
      delta += clk_sort[clk_sort_count/2+1];
      delta /= 3.0;
      clk_sort_count = 0;
      return fabs(delta);
   }

   if(clk_sort_count == 0) {   // first entry
      clk_sort[clk_sort_count] = delta;
      ++clk_sort_count;
      goto show_sort;
   }

   if(delta >= clk_sort[clk_sort_count-1]) {  // insert at end of list
      clk_sort[clk_sort_count] = delta;
      ++clk_sort_count;
      goto show_sort;
   }

   for(i=0; i<=clk_sort_count; i++) {  // insert new value into sorted list
      for(k=0; k<clk_sort_count; k++) {  // see where this value lies in the list so far
         if(delta > clk_sort[k]) continue;  // it's greater than the current value

         for(m=clk_sort_count; m>k; m--) {  // move lower value entries down one position
            clk_sort[m] = clk_sort[m-1];
         }

         clk_sort[k] = delta;   // insert new value in the list
         ++clk_sort_count;
         goto show_sort;
      }
   }
   return 0.0;

   show_sort:
// sprintf(plot_title, "sort %d: ", clk_sort_count);  // rrrrr
// for(i=0; i<clk_sort_count; i++) {
//    sprintf(out, "%.1f ", clk_sort[i]);
//    strcat(plot_title, out);
// }

   return 0.0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movsd %xmm0, -0x10(%rbp)
cmpl $0x0, 0x2a1330(%rip)    # 0x3d9ce0
jge 0x1389bc
movl $0x0, 0x2a1324(%rip)    # 0x3d9ce0
cmpl $0xa, 0x2a131d(%rip)    # 0x3d9ce0
jl 0x138a79
movl 0x2a1311(%rip), %eax    # 0x3d9ce0
movl $0x2, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
leaq 0x2a129f(%rip), %rax    # 0x3d9c80
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x10(%rbp)
movl 0x2a12ef(%rip), %eax    # 0x3d9ce0
movl $0x2, %ecx
cltd
idivl %ecx
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x2a127a(%rip), %rax    # 0x3d9c80
movsd (%rax,%rcx,8), %xmm0
addsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movl 0x2a12c5(%rip), %eax    # 0x3d9ce0
movl $0x2, %ecx
cltd
idivl %ecx
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x2a1250(%rip), %rax    # 0x3d9c80
movsd (%rax,%rcx,8), %xmm0
addsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
movsd 0x36794(%rip), %xmm1    # 0x16f1e0
divsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movl $0x0, 0x2a1281(%rip)    # 0x3d9ce0
movsd -0x10(%rbp), %xmm0
movaps 0x36825(%rip), %xmm1    # 0x16f290
pand %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x138bd5
cmpl $0x0, 0x2a1260(%rip)    # 0x3d9ce0
jne 0x138aae
movsd -0x10(%rbp), %xmm0
movslq 0x2a1252(%rip), %rcx    # 0x3d9ce0
leaq 0x2a11eb(%rip), %rax    # 0x3d9c80
movsd %xmm0, (%rax,%rcx,8)
movl 0x2a1240(%rip), %eax    # 0x3d9ce0
addl $0x1, %eax
movl %eax, 0x2a1237(%rip)    # 0x3d9ce0
jmp 0x138bcd
movsd -0x10(%rbp), %xmm0
movl 0x2a1227(%rip), %eax    # 0x3d9ce0
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x2a11ba(%rip), %rax    # 0x3d9c80
ucomisd (%rax,%rcx,8), %xmm0
jb 0x138af9
movsd -0x10(%rbp), %xmm0
movslq 0x2a1207(%rip), %rcx    # 0x3d9ce0
leaq 0x2a11a0(%rip), %rax    # 0x3d9c80
movsd %xmm0, (%rax,%rcx,8)
movl 0x2a11f5(%rip), %eax    # 0x3d9ce0
addl $0x1, %eax
movl %eax, 0x2a11ec(%rip)    # 0x3d9ce0
jmp 0x138bcd
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl 0x2a11d7(%rip), %eax    # 0x3d9ce0
jg 0x138bc3
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl 0x2a11c1(%rip), %eax    # 0x3d9ce0
jge 0x138bb3
movsd -0x10(%rbp), %xmm0
movslq -0x18(%rbp), %rcx
leaq 0x2a114b(%rip), %rax    # 0x3d9c80
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x138b3e
jmp 0x138ba5
movl 0x2a119c(%rip), %eax    # 0x3d9ce0
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jle 0x138b7f
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x2a1121(%rip), %rax    # 0x3d9c80
movsd (%rax,%rcx,8), %xmm0
movslq -0x1c(%rbp), %rcx
leaq 0x2a1111(%rip), %rax    # 0x3d9c80
movsd %xmm0, (%rax,%rcx,8)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x138b47
movsd -0x10(%rbp), %xmm0
movslq -0x18(%rbp), %rcx
leaq 0x2a10f1(%rip), %rax    # 0x3d9c80
movsd %xmm0, (%rax,%rcx,8)
movl 0x2a1146(%rip), %eax    # 0x3d9ce0
addl $0x1, %eax
movl %eax, 0x2a113d(%rip)    # 0x3d9ce0
jmp 0x138bcd
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x138b16
jmp 0x138bb5
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x138b00
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x138bd5
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
popq %rbp
retq
nopl (%rax)