// File: /workspace/github_cpp_makefile_star2_O1/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:
xorl %eax, %eax
cmpl $0x14, %edi
ja 0x10867e
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
leaq 0x11313a(%rip), %rcx    # 0x21b398
cmpq $0x0, (%rcx)
je 0x10866d
leaq 0x204d21(%rip), %rcx    # 0x30cf90
movsd (%rcx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x108283
jnp 0x10866d
movl %edi, %r15d
leaq 0x11313f(%rip), %rax    # 0x21b3cc
movl $0x0, (%rax)
leaq 0x11309e(%rip), %rax    # 0x21b338
movq (%rax), %rax
leaq 0x1105e0(%rip), %rcx    # 0x218884
movslq (%rcx), %rcx
cmpq %rcx, %rax
cmovlq %rax, %rcx
movl $0x1000, %eax           # imm = 0x1000
cmpq %rax, %rcx
cmovgeq %rax, %rcx
leaq 0x11b1cf(%rip), %rbx    # 0x223490
movq %rcx, (%rbx)
leaq 0x1131b3(%rip), %rax    # 0x21b47e
movb %r15b, (%rax)
callq 0x107dd8
movl %r15d, %r13d
imulq $0xe8, %r13, %rax
leaq 0x55a4c(%rip), %rcx     # 0x15dd30
movsd 0xb0(%rcx,%rax), %xmm0
leaq 0x11b1a4(%rip), %rdx    # 0x223498
movsd %xmm0, (%rdx)
movsd 0xb8(%rcx,%rax), %xmm1
leaq 0x11b198(%rip), %rax    # 0x2234a0
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
andpd 0xe68(%rip), %xmm2      # 0x109180
mulsd 0xde0(%rip), %xmm2      # 0x109100
subsd %xmm2, %xmm0
movsd %xmm0, (%rdx)
addsd %xmm1, %xmm2
movsd %xmm2, (%rax)
subsd %xmm0, %xmm2
movq (%rbx), %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
divsd %xmm0, %xmm2
leaq 0x11b15e(%rip), %rax    # 0x2234a8
movsd %xmm2, (%rax)
testq %rdx, %rdx
jle 0x108365
shlq $0x3, %rdx
leaq 0x113132(%rip), %rdi    # 0x21b490
xorl %esi, %esi
callq 0x91a0
leaq 0x112efc(%rip), %rax    # 0x21b268
movq (%rax), %r12
leaq 0x237a22(%rip), %rax    # 0x33fd98
movq %r12, (%rax)
movq $-0x1, %rbx
xorl %r14d, %r14d
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
leaq 0x112f8f(%rip), %rax    # 0x21b328
cmpq (%rax), %r12
je 0x10847f
leaq -0xe8(%rbp), %rdi
movq %r12, %rsi
callq 0x10141
movsd -0xd8(%rbp,%r13,8), %xmm0
leaq 0x112fd6(%rip), %rax    # 0x21b398
xorps %xmm1, %xmm1
cvtsi2sdq (%rax), %xmm1
divsd %xmm1, %xmm0
movl %r15d, %edi
callq 0x10878c
leaq 0x11b0bb(%rip), %rax    # 0x223498
subsd (%rax), %xmm0
leaq 0x11b0c0(%rip), %rax    # 0x2234a8
divsd (%rax), %xmm0
cvttsd2si %xmm0, %eax
testl %eax, %eax
js 0x10842c
movl %eax, %ecx
leaq 0x11b093(%rip), %rdx    # 0x223490
cmpq %rcx, (%rdx)
jle 0x10842c
leaq 0x113087(%rip), %rsi    # 0x21b490
movq (%rsi,%rcx,8), %rdx
incq %rdx
movq %rdx, (%rsi,%rcx,8)
movq -0x38(%rbp), %rsi
cmpq %rsi, %rdx
movl -0x2c(%rbp), %ecx
cmovgel %eax, %ecx
movl %ecx, -0x2c(%rbp)
cmovgq %rdx, %rsi
movq %rsi, -0x38(%rbp)
incq %rbx
leaq 0x112f02(%rip), %rax    # 0x21b338
cmpq (%rax), %rbx
jge 0x10847f
incq %r12
leaq 0x112edb(%rip), %rax    # 0x21b320
cmpq (%rax), %r12
movl $0x0, %eax
cmovgeq %rax, %r12
leaq 0x112f78(%rip), %rax    # 0x21b3d0
movslq (%rax), %rax
movslq %r14d, %r14
addq %rax, %r14
leaq 0x11041c(%rip), %rax    # 0x218884
movslq (%rax), %rax
leaq 0x112f2e(%rip), %rcx    # 0x21b3a0
imulq (%rcx), %rax
cmpq %r14, %rax
jg 0x108392
leaq 0x112f46(%rip), %rax    # 0x21b3cc
movl $0x0, (%rax)
leaq 0x112dd5(%rip), %rax    # 0x21b268
movq (%rax), %r15
leaq 0x112f03(%rip), %rax    # 0x21b3a0
movq (%rax), %rax
cmpq $0x2, %rax
movl $0x1, %ecx
cmovgeq %rax, %rcx
leaq 0x2378d4(%rip), %rax    # 0x33fd88
movq %rcx, (%rax)
xorl %ebx, %ebx
leaq -0x198(%rbp), %r12
leaq -0xe8(%rbp), %r13
movq %r15, %rax
leaq 0x112e57(%rip), %rcx    # 0x21b328
cmpq (%rcx), %rax
je 0x1085c0
leaq 0x2378a7(%rip), %rcx    # 0x33fd88
cmpq $0x0, (%rcx)
jle 0x1085ad
xorl %r14d, %r14d
movq %rax, %r15
movq %r12, %rdi
movq %rax, %rsi
callq 0x10141
movl $0xb0, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x92b0
leaq 0x112f7d(%rip), %rax    # 0x21b490
movq (%rax,%rbx,8), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
leaq 0x23786e(%rip), %rcx    # 0x33fd94
cmpb $0x0, (%rcx)
je 0x108545
testq %rax, %rax
jle 0x108545
callq 0x9160
mulsd 0xb6b(%rip), %xmm0      # 0x1090a8
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
cmpq $0x1, %rbx
jne 0x10855b
leaq 0x112d59(%rip), %rax    # 0x21b2b0
movq %r15, 0x8(%rax)
movl $0x16, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
callq 0x1030d
leaq 0x1(%r15), %rax
leaq 0x112dac(%rip), %rcx    # 0x21b328
cmpq (%rcx), %rax
je 0x1085ad
leaq 0x112d98(%rip), %rax    # 0x21b320
movq (%rax), %rcx
leaq (%r15,%rcx), %rax
incq %rax
subq %rcx, %rax
cmpq %rcx, %rax
jge 0x108592
incq %r14
leaq 0x2377e4(%rip), %rcx    # 0x33fd88
cmpq (%rcx), %r14
jl 0x1084ee
incq %rbx
leaq 0x11aed9(%rip), %rcx    # 0x223490
cmpq (%rcx), %rbx
jl 0x1084ca
leaq 0x112ce9(%rip), %rax    # 0x21b2b0
movq %r15, 0x10(%rax)
leaq 0x236cfa(%rip), %rbx    # 0x33f2cc
cmpl $0x2, (%rbx)
je 0x108663
movl -0x2c(%rbp), %r9d
movl %r9d, %eax
leaq 0x11aea7(%rip), %rcx    # 0x223490
movq (%rcx), %rdx
xorpd %xmm0, %xmm0
cmpq %rax, %rdx
jle 0x108613
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
leaq 0x11aea4(%rip), %rax    # 0x2234a8
mulsd (%rax), %xmm0
leaq 0x11ae89(%rip), %rax    # 0x223498
addsd (%rax), %xmm0
leaq 0x112e10(%rip), %rax    # 0x21b42a
leaq 0x112e5d(%rip), %rcx    # 0x21b47e
movzbl (%rcx), %ecx
imulq $0xe8, %rcx, %rcx
cmpb $0x0, (%rax)
leaq 0x556fb(%rip), %rax     # 0x15dd30
movq (%rax,%rcx), %rcx
leaq 0x2dbb9(%rip), %rax     # 0x1361f9
leaq 0x2db73(%rip), %rsi     # 0x1361ba
cmoveq %rax, %rsi
leaq 0x2361fe(%rip), %rdi    # 0x33e850
movq -0x38(%rbp), %r8
movb $0x1, %al
callq 0x9060
movl $0x3, (%rbx)
leaq 0x11ae26(%rip), %rax    # 0x223490
movq (%rax), %rax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq