// File: jkingan[P]ladyheather/heathmsc.cpp
// Function: calc_plot_hist(int)
long calc_plot_hist(int id)
{
long i, j, k;
long last_i;
long count;
int col;
struct PLOT_Q q;
DATA_SIZE val, aval;
int bin;
int max_bin;
long max_count;

   // calculate a histogram of a plot queue value

   if(queue_interval == 0) return 0;
   if(stat_count == 0) return 0;
   if(id < 0) return 0;
   if(id > (NUM_PLOTS+DERIVED_PLOTS)) return 0;

   last_i = 0;
   plot_column = 0;
   hist_size = plot_q_count;
   if(hist_size > PLOT_WIDTH) hist_size = PLOT_WIDTH;
   if(hist_size >= MAX_HIST) hist_size = MAX_HIST;

   fft_id = id;
   set_fft_scale();

   hist_minv = plot[id].min_disp_val;
   hist_maxv = plot[id].max_disp_val;
   aval = DATA_SIZE (fabs(hist_maxv-hist_minv) * 0.01);
   hist_minv -= aval;   // expand value range by 1% of the span
   hist_maxv += aval;

   hist_bin_width = (hist_maxv - hist_minv) / (DATA_SIZE) hist_size; 

   for(i=0; i<hist_size; i++) plot_hist[i] = 0;

   max_bin = 0;
   max_count = 0;
   count = 0;
   col = 0;
   i = plot_q_col0;   // hist displayed points
   fft_queue_0 = i;
////i = 0;            // hist all queue points
   while(i != plot_q_in) {  // scan the data that is in the plot queue
      q = get_plot_q(i);    // get next point to histogram
      val = (DATA_SIZE) q.data[id] / (DATA_SIZE) queue_interval;
      val = scale_hist_val(id, val);
      bin = (int) ((val - hist_minv) / hist_bin_width);
      if((bin >= 0) && (bin < hist_size)) {
         ++plot_hist[bin];
//       if(plot_hist[bin] > max_count) {   // find first peak bin
         if(plot_hist[bin] >= max_count) {  // find last peak bin
            max_count = plot_hist[bin];
            max_bin = bin;
         }
      }

      ++i;
      if(i >= plot_q_size) i = 0;
      if(++count > plot_q_count) break; // all points in the queue have been processed

      col += plot_mag; // end histogram at end of displayed data
      if(col >= (PLOT_WIDTH*view_interval)) break;
   }

   plot_column = 0;
   j = 0;
   i = last_i = plot_q_col0;

   fft_scale = view_interval;
   if(fft_scale < 1) fft_scale = 1;

   // place hist results into the plot queue FFT plot data
   while(i != plot_q_in) {  
      for(k=0; k<fft_scale; k++) { // expand plot horizontally so that it is easier to read
         q = get_plot_q(i);
         val = (DATA_SIZE) plot_hist[j];
         if(fft_db && (val > 0)) {
            val = (float) (10.0 * log10(MAX((double) val, 0.0))) ;
         }
         q.data[FFT] = val;
         last_i = i;
         if(j == 1) mark_q_entry[1] = i;

         put_plot_q(i, q);
         if(++i == plot_q_in) break;
         while(i >= plot_q_size) i -= plot_q_size;
      }

      ++j;
      if(j >= hist_size) break;
   }
   mark_q_entry[2] = last_i;

// dump_hist_plot();

   if(1 && (title_type != USER)) {
      if((max_bin >= 0) && (max_bin < hist_size)) {
         val = hist_minv + hist_bin_width*(DATA_SIZE) max_bin;
//sprintf(debug_text4, "bin:%d  step:%.9f  count:%ld   min:%.9f max:%.9f  val:%.9f", mouse_x, hist_bin_width, plot_hist[mouse_x], hist_minv, hist_maxv, val);
      }
      else val = 0;

      if(show_live_fft) {
         sprintf(plot_title, "%ld bin live %s plot histogram.  Max count:%ld  bin:%d  val:%f", hist_size, plot[fft_id].plot_id, max_count, max_bin, val);
      }
      else {
         sprintf(plot_title, "%ld bin single %s plot histogram.  Max count:%ld  bin:%d  val:%f", hist_size, plot[fft_id].plot_id, max_count, max_bin, val);
      }
//sprintf(debug_text3, "hist point count:%ld  scale:%ld", count, fft_scale);

      if(mouse_plot_valid && (mouse_x >= 0) && (mouse_x < hist_size)) {
         val = hist_minv + hist_bin_width*(DATA_SIZE) mouse_x;
//sprintf(debug_text, "bin:%d  step:%.9f  count:%ld   min:%.9f max:%.9f  val:%.9f", mouse_x, hist_bin_width, plot_hist[mouse_x], hist_minv, hist_maxv, val);
      }
      title_type = OTHER;
   }

   return hist_size;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3e0, %rsp            # imm = 0x3E0
movl %edi, -0xc(%rbp)
leaq 0x10c973(%rip), %rax    # 0x27a578
cmpq $0x0, (%rax)
jne 0x16dc18
movq $0x0, -0x8(%rbp)
jmp 0x16e3b8
leaq 0x1fe551(%rip), %rax    # 0x36c170
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x16dc3b
jp 0x16dc3b
movq $0x0, -0x8(%rbp)
jmp 0x16e3b8
cmpl $0x0, -0xc(%rbp)
jge 0x16dc4e
movq $0x0, -0x8(%rbp)
jmp 0x16e3b8
cmpl $0x14, -0xc(%rbp)
jle 0x16dc61
movq $0x0, -0x8(%rbp)
jmp 0x16e3b8
movq $0x0, -0x30(%rbp)
leaq 0x10c93c(%rip), %rax    # 0x27a5ac
movl $0x0, (%rax)
leaq 0x10c89b(%rip), %rax    # 0x27a518
movq (%rax), %rcx
leaq 0x1149e9(%rip), %rax    # 0x282670
movq %rcx, (%rax)
leaq 0x1149df(%rip), %rax    # 0x282670
movq (%rax), %rax
leaq 0x109dc9(%rip), %rcx    # 0x277a64
movslq (%rcx), %rcx
cmpq %rcx, %rax
jle 0x16dcb7
leaq 0x109dba(%rip), %rax    # 0x277a64
movslq (%rax), %rcx
leaq 0x1149bc(%rip), %rax    # 0x282670
movq %rcx, (%rax)
leaq 0x1149b2(%rip), %rax    # 0x282670
cmpq $0x1000, (%rax)         # imm = 0x1000
jl 0x16dcd5
leaq 0x1149a2(%rip), %rax    # 0x282670
movq $0x1000, (%rax)         # imm = 0x1000
movl -0xc(%rbp), %eax
movb %al, %cl
leaq 0x10c97d(%rip), %rax    # 0x27a65e
movb %cl, (%rax)
callq 0x16d560
movslq -0xc(%rbp), %rcx
leaq 0x4f02d(%rip), %rax     # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movsd 0xb0(%rax), %xmm0
leaq 0x11496c(%rip), %rax    # 0x282678
movsd %xmm0, (%rax)
movslq -0xc(%rbp), %rcx
leaq 0x4f005(%rip), %rax     # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movsd 0xb8(%rax), %xmm0
leaq 0x11494c(%rip), %rax    # 0x282680
movsd %xmm0, (%rax)
leaq 0x114941(%rip), %rax    # 0x282680
movsd (%rax), %xmm0
leaq 0x11492e(%rip), %rax    # 0x282678
subsd (%rax), %xmm0
movaps 0x153b(%rip), %xmm1     # 0x16f290
pand %xmm1, %xmm0
movsd 0x13bf(%rip), %xmm1     # 0x16f120
mulsd %xmm1, %xmm0
movsd %xmm0, -0x100(%rbp)
movsd -0x100(%rbp), %xmm1
leaq 0x1148fc(%rip), %rax    # 0x282678
movsd (%rax), %xmm0
subsd %xmm1, %xmm0
leaq 0x1148ed(%rip), %rax    # 0x282678
movsd %xmm0, (%rax)
movsd -0x100(%rbp), %xmm0
leaq 0x1148e2(%rip), %rax    # 0x282680
addsd (%rax), %xmm0
leaq 0x1148d7(%rip), %rax    # 0x282680
movsd %xmm0, (%rax)
leaq 0x1148cc(%rip), %rax    # 0x282680
movsd (%rax), %xmm0
leaq 0x1148b9(%rip), %rax    # 0x282678
subsd (%rax), %xmm0
leaq 0x1148a6(%rip), %rax    # 0x282670
cvtsi2sdq (%rax), %xmm1
divsd %xmm1, %xmm0
leaq 0x1148ae(%rip), %rax    # 0x282688
movsd %xmm0, (%rax)
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
leaq 0x11487f(%rip), %rcx    # 0x282670
cmpq (%rcx), %rax
jge 0x16de17
movq -0x18(%rbp), %rcx
leaq 0x10c86f(%rip), %rax    # 0x27a670
movq $0x0, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x16dde6
movl $0x0, -0x108(%rbp)
movq $0x0, -0x110(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
leaq 0x10c606(%rip), %rax    # 0x27a448
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rcx
leaq 0x231124(%rip), %rax    # 0x39ef78
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
leaq 0x10c6a6(%rip), %rcx    # 0x27a508
cmpq (%rcx), %rax
je 0x16dfe9
movq -0x18(%rbp), %rsi
leaq -0x1c0(%rbp), %rdi
callq 0x133f0
leaq -0xf0(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
movl $0xb0, %edx
callq 0x92c0
movslq -0xc(%rbp), %rax
movsd -0xe0(%rbp,%rax,8), %xmm0
leaq 0x10c6d1(%rip), %rax    # 0x27a578
cvtsi2sdq (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xf8(%rbp)
movl -0xc(%rbp), %edi
movsd -0xf8(%rbp), %xmm0
callq 0x16e550
movsd %xmm0, -0xf8(%rbp)
movsd -0xf8(%rbp), %xmm0
leaq 0x114799(%rip), %rax    # 0x282678
subsd (%rax), %xmm0
leaq 0x11479e(%rip), %rax    # 0x282688
divsd (%rax), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x104(%rbp)
cmpl $0x0, -0x104(%rbp)
jl 0x16df77
movslq -0x104(%rbp), %rax
leaq 0x114761(%rip), %rcx    # 0x282670
cmpq (%rcx), %rax
jge 0x16df77
movslq -0x104(%rbp), %rcx
leaq 0x10c74e(%rip), %rax    # 0x27a670
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
leaq 0x10c73f(%rip), %rax    # 0x27a670
movq %rdx, (%rax,%rcx,8)
movslq -0x104(%rbp), %rcx
leaq 0x10c72d(%rip), %rax    # 0x27a670
movq (%rax,%rcx,8), %rax
cmpq -0x110(%rbp), %rax
jl 0x16df75
movslq -0x104(%rbp), %rcx
leaq 0x10c712(%rip), %rax    # 0x27a670
movq (%rax,%rcx,8), %rax
movq %rax, -0x110(%rbp)
movl -0x104(%rbp), %eax
movl %eax, -0x108(%rbp)
jmp 0x16df77
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
leaq 0x10c572(%rip), %rcx    # 0x27a500
cmpq (%rcx), %rax
jl 0x16df9b
movq $0x0, -0x18(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
leaq 0x10c56a(%rip), %rcx    # 0x27a518
cmpq (%rcx), %rax
jle 0x16dfb5
jmp 0x16dfe9
leaq 0x10c5f4(%rip), %rax    # 0x27a5b0
movl (%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
leaq 0x109a95(%rip), %rcx    # 0x277a64
movslq (%rcx), %rcx
leaq 0x10c5a7(%rip), %rdx    # 0x27a580
imulq (%rdx), %rcx
cmpq %rcx, %rax
jl 0x16dfe4
jmp 0x16dfe9
jmp 0x16de57
leaq 0x10c5bc(%rip), %rax    # 0x27a5ac
movl $0x0, (%rax)
movq $0x0, -0x20(%rbp)
leaq 0x10c443(%rip), %rax    # 0x27a448
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq %rax, -0x18(%rbp)
leaq 0x10c569(%rip), %rax    # 0x27a580
movq (%rax), %rcx
leaq 0x230f47(%rip), %rax    # 0x39ef68
movq %rcx, (%rax)
leaq 0x230f3d(%rip), %rax    # 0x39ef68
cmpq $0x1, (%rax)
jge 0x16e03f
leaq 0x230f30(%rip), %rax    # 0x39ef68
movq $0x1, (%rax)
jmp 0x16e041
movq -0x18(%rbp), %rax
leaq 0x10c4bc(%rip), %rcx    # 0x27a508
cmpq (%rcx), %rax
je 0x16e210
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
leaq 0x230f00(%rip), %rcx    # 0x39ef68
cmpq (%rcx), %rax
jge 0x16e1ed
movq -0x18(%rbp), %rsi
leaq -0x270(%rbp), %rdi
callq 0x133f0
leaq -0xf0(%rbp), %rdi
leaq -0x270(%rbp), %rsi
movl $0xb0, %edx
callq 0x92c0
movq -0x20(%rbp), %rcx
leaq 0x10c5cc(%rip), %rax    # 0x27a670
cvtsi2sdq (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xf8(%rbp)
leaq 0x230ebb(%rip), %rax    # 0x39ef74
cmpb $0x0, (%rax)
je 0x16e12b
movsd -0xf8(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x16e12b
movsd -0xf8(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x16e0f2
movsd -0xf8(%rbp), %xmm0
movsd %xmm0, -0x328(%rbp)
jmp 0x16e0ff
xorps %xmm0, %xmm0
movsd %xmm0, -0x328(%rbp)
jmp 0x16e0ff
movsd -0x328(%rbp), %xmm0
callq 0x9160
movaps %xmm0, %xmm1
movsd 0xfa9(%rip), %xmm0      # 0x16f0c0
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0xf8(%rbp)
movsd -0xf8(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x1, -0x20(%rbp)
jne 0x16e156
movq -0x18(%rbp), %rcx
leaq 0x10c33e(%rip), %rax    # 0x27a490
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x330(%rbp)
leaq -0x320(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movl $0xb0, %edx
callq 0x92c0
movq %rsp, %rdi
movl $0x16, %ecx
leaq -0x320(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq -0x330(%rbp), %rdi
callq 0x13680
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
leaq 0x10c35e(%rip), %rcx    # 0x27a508
cmpq (%rcx), %rax
jne 0x16e1b1
jmp 0x16e1ed
jmp 0x16e1b3
movq -0x18(%rbp), %rax
leaq 0x10c342(%rip), %rcx    # 0x27a500
cmpq (%rcx), %rax
jl 0x16e1da
leaq 0x10c336(%rip), %rax    # 0x27a500
movq (%rax), %rcx
movq -0x18(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x18(%rbp)
jmp 0x16e1b3
jmp 0x16e1dc
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x16e05d
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
leaq 0x11446c(%rip), %rcx    # 0x282670
cmpq (%rcx), %rax
jl 0x16e20b
jmp 0x16e210
jmp 0x16e041
movq -0x30(%rbp), %rcx
leaq 0x10c275(%rip), %rax    # 0x27a490
movq %rcx, 0x10(%rax)
leaq 0x230286(%rip), %rax    # 0x39e4ac
cmpl $0x2, (%rax)
je 0x16e3aa
cmpl $0x0, -0x108(%rbp)
jl 0x16e27b
movslq -0x108(%rbp), %rax
leaq 0x11442a(%rip), %rcx    # 0x282670
cmpq (%rcx), %rax
jge 0x16e27b
leaq 0x114426(%rip), %rax    # 0x282678
movsd (%rax), %xmm1
leaq 0x11442b(%rip), %rax    # 0x282688
movsd (%rax), %xmm0
cvtsi2sdl -0x108(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xf8(%rbp)
jmp 0x16e286
xorps %xmm0, %xmm0
movsd %xmm0, -0xf8(%rbp)
leaq 0x10c37d(%rip), %rax    # 0x27a60a
cmpb $0x0, (%rax)
je 0x16e2e9
leaq 0x1143d7(%rip), %rax    # 0x282670
movq (%rax), %rdx
leaq 0x10c3bb(%rip), %rax    # 0x27a65e
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x4ea71(%rip), %rax     # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x110(%rbp), %r8
movl -0x108(%rbp), %r9d
movsd -0xf8(%rbp), %xmm0
leaq 0x22f757(%rip), %rdi    # 0x39da30
leaq 0x2ab4c(%rip), %rsi     # 0x198e2c
movb $0x1, %al
callq 0x9060
jmp 0x16e33e
leaq 0x114380(%rip), %rax    # 0x282670
movq (%rax), %rdx
leaq 0x10c364(%rip), %rax    # 0x27a65e
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0x4ea1a(%rip), %rax     # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x110(%rbp), %r8
movl -0x108(%rbp), %r9d
movsd -0xf8(%rbp), %xmm0
leaq 0x22f700(%rip), %rdi    # 0x39da30
leaq 0x2ab34(%rip), %rsi     # 0x198e6b
movb $0x1, %al
callq 0x9060
leaq 0x10c0f3(%rip), %rax    # 0x27a438
cmpb $0x0, (%rax)
je 0x16e39d
leaq 0x10c0cf(%rip), %rax    # 0x27a420
cmpl $0x0, (%rax)
jl 0x16e39d
leaq 0x10c0c3(%rip), %rax    # 0x27a420
movslq (%rax), %rax
leaq 0x114309(%rip), %rcx    # 0x282670
cmpq (%rcx), %rax
jge 0x16e39d
leaq 0x114305(%rip), %rax    # 0x282678
movsd (%rax), %xmm1
leaq 0x11430a(%rip), %rax    # 0x282688
movsd (%rax), %xmm0
leaq 0x10c097(%rip), %rax    # 0x27a420
cvtsi2sdl (%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xf8(%rbp)
leaq 0x230108(%rip), %rax    # 0x39e4ac
movl $0x3, (%rax)
leaq 0x1142bf(%rip), %rax    # 0x282670
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x3e0, %rsp            # imm = 0x3E0
popq %rbp
retq
nopw %cs:(%rax,%rax)