// File: /workspace/github_cpp_makefile_star2_O1/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
cmpl $0x0, 0x29688c(%rip)    # 0x37aa80
jns 0xe4200
movl $0x0, 0x296880(%rip)    # 0x37aa80
movslq 0x296879(%rip), %rax    # 0x37aa80
movl %eax, %ecx
cmpq $0xa, %rax
jl 0xe424b
shrl %ecx
leaq 0x296808(%rip), %rax    # 0x37aa20
movsd (%rax,%rcx,8), %xmm0
movl %ecx, %edx
decl %edx
addsd (%rax,%rdx,8), %xmm0
addsd 0x8(%rax,%rcx,8), %xmm0
divsd 0x277d4(%rip), %xmm0    # 0x10ba08
movl $0x0, 0x296842(%rip)    # 0x37aa80
andpd 0x24f3a(%rip), %xmm0    # 0x109180
jmp 0xe42f1
testq %rcx, %rcx
je 0xe429f
leaq 0x2967c9(%rip), %rdx    # 0x37aa20
ucomisd -0x8(%rdx,%rax,8), %xmm0
jae 0xe42b3
testl %eax, %eax
js 0xe42ed
xorl %r9d, %r9d
movabsq $0x100000000, %r8       # imm = 0x100000000
movq %rdx, %rsi
xorl %edi, %edi
xorl %r10d, %r10d
ucomisd (%rsi), %xmm0
jbe 0xe42ba
incq %r10
addq %r8, %rdi
addq $0x8, %rsi
cmpq %r10, %rcx
jne 0xe427c
leal 0x1(%r9), %esi
cmpl %ecx, %r9d
movl %esi, %r9d
jne 0xe4274
jmp 0xe42ed
movsd %xmm0, 0x296779(%rip)   # 0x37aa20
movl $0x1, 0x2967cf(%rip)    # 0x37aa80
jmp 0xe42ed
movsd %xmm0, (%rdx,%rax,8)
jmp 0xe42e5
cmpl %r10d, %eax
jle 0xe42e1
sarq $0x20, %rdi
leaq 0x296756(%rip), %rdx    # 0x37aa20
movsd -0x8(%rdx,%rcx,8), %xmm1
movsd %xmm1, (%rdx,%rcx,8)
leaq -0x1(%rcx), %r8
movq %r8, %rcx
cmpq %rdi, %r8
jg 0xe42ca
movsd %xmm0, (%rsi)
incl %eax
movl %eax, 0x296793(%rip)    # 0x37aa80
xorpd %xmm0, %xmm0
popq %rbp
retq