// File: /workspace/github_cpp_makefile_star2_O2/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 0xf2186
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
leaq 0x11362f(%rip), %rcx    # 0x205398
cmpq $0x0, (%rcx)
je 0xf2175
leaq 0x205216(%rip), %rcx    # 0x2f6f90
movsd (%rcx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xf1d8e
jnp 0xf2175
movl %edi, %r12d
leaq 0x113634(%rip), %rax    # 0x2053cc
andl $0x0, (%rax)
leaq 0x113596(%rip), %rax    # 0x205338
movq (%rax), %rax
leaq 0x110ad8(%rip), %rcx    # 0x202884
movslq (%rcx), %rcx
cmpq %rcx, %rax
cmovlq %rax, %rcx
movl $0x1000, %eax           # imm = 0x1000
cmpq %rax, %rcx
cmovgeq %rax, %rcx
leaq 0x11b6c7(%rip), %rbx    # 0x20d490
movq %rcx, (%rbx)
leaq 0x1136ab(%rip), %rax    # 0x20547e
movb %r12b, (%rax)
callq 0xf192c
movl %r12d, %r14d
imulq $0xe8, %r14, %rax
leaq 0x55f44(%rip), %rcx     # 0x147d30
movsd 0xb0(%rcx,%rax), %xmm0
movsd 0xb8(%rcx,%rax), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
andpd 0x1382(%rip), %xmm2     # 0xf3190
mulsd 0x12ea(%rip), %xmm2     # 0xf3100
subsd %xmm2, %xmm0
leaq 0x11b677(%rip), %rax    # 0x20d498
movsd %xmm0, (%rax)
addsd %xmm1, %xmm2
leaq 0x11b670(%rip), %rax    # 0x20d4a0
movsd %xmm2, (%rax)
subsd %xmm0, %xmm2
movq (%rbx), %rax
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm2
leaq 0x11b65d(%rip), %rcx    # 0x20d4a8
movsd %xmm2, (%rcx)
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
leaq 0x113631(%rip), %rdx    # 0x205490
cmpq %rcx, %rax
je 0xf1e6e
andq $0x0, (%rdx,%rcx,8)
incq %rcx
jmp 0xf1e5f
leaq 0x1133f3(%rip), %rax    # 0x205268
movq (%rax), %r15
leaq 0x237f19(%rip), %rax    # 0x329d98
movq %r15, (%rax)
pushq $-0x1
popq %r13
xorl %ebx, %ebx
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
leaq 0x11348a(%rip), %rax    # 0x205328
cmpq (%rax), %r15
je 0xf1f90
leaq -0xe8(%rbp), %rdi
movq %r15, %rsi
callq 0xf9c7
movsd -0xd8(%rbp,%r14,8), %xmm0
leaq 0x1134d1(%rip), %rax    # 0x205398
cvtsi2sdq (%rax), %xmm1
divsd %xmm1, %xmm0
movl %r12d, %edi
callq 0xf2298
leaq 0x11b5b9(%rip), %rax    # 0x20d498
subsd (%rax), %xmm0
leaq 0x11b5be(%rip), %rax    # 0x20d4a8
divsd (%rax), %xmm0
cvttsd2si %xmm0, %eax
testl %eax, %eax
js 0xf1f2e
movl %eax, %ecx
leaq 0x11b591(%rip), %rdx    # 0x20d490
cmpq %rcx, (%rdx)
jle 0xf1f2e
leaq 0x113585(%rip), %rsi    # 0x205490
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)
leaq 0x11346b(%rip), %rax    # 0x2053a0
movq (%rax), %rax
incq %r13
leaq 0x1133f6(%rip), %rcx    # 0x205338
cmpq (%rcx), %r13
jge 0xf1f84
incq %r15
leaq 0x1133cf(%rip), %rcx    # 0x205320
cmpq (%rcx), %r15
movl $0x0, %ecx
cmovgeq %rcx, %r15
leaq 0x11346c(%rip), %rcx    # 0x2053d0
movslq (%rcx), %rcx
movslq %ebx, %rbx
addq %rcx, %rbx
leaq 0x110910(%rip), %rcx    # 0x202884
movslq (%rcx), %rcx
imulq %rax, %rcx
cmpq %rbx, %rcx
jg 0xf1e97
leaq 0x11339d(%rip), %rcx    # 0x205328
movq (%rcx), %r15
jmp 0xf1f9a
leaq 0x113409(%rip), %rax    # 0x2053a0
movq (%rax), %rax
leaq 0x11342b(%rip), %rcx    # 0x2053cc
andl $0x0, (%rcx)
leaq 0x1132bd(%rip), %rcx    # 0x205268
movq (%rcx), %rcx
cmpq $0x2, %rax
pushq $0x1
popq %rdx
cmovgeq %rax, %rdx
leaq 0x237dc8(%rip), %rax    # 0x329d88
movq %rdx, (%rax)
xorl %ebx, %ebx
leaq -0xe8(%rbp), %r13
movq %rcx, %r14
cmpq %r15, %r14
je 0xf20c8
xorl %r12d, %r12d
leaq 0x237da6(%rip), %rax    # 0x329d88
cmpq (%rax), %r12
jge 0xf20b0
leaq -0x198(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0xf9c7
movl $0xb0, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x92a0
leaq 0x11347c(%rip), %rax    # 0x205490
movq (%rax,%rbx,8), %rax
cvtsi2sd %rax, %xmm0
leaq 0x237d70(%rip), %rcx    # 0x329d94
cmpb $0x0, (%rcx)
je 0xf2043
testq %rax, %rax
jle 0xf2043
callq 0x9160
mulsd 0x106d(%rip), %xmm0     # 0xf30a8
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
cmpq $0x1, %rbx
jne 0xf2059
leaq 0x11325b(%rip), %rax    # 0x2052b0
movq %r14, 0x8(%rax)
pushq $0x16
popq %rcx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
callq 0xfb93
leaq 0x1(%r14), %rax
leaq 0x1132b0(%rip), %rcx    # 0x205328
movq (%rcx), %r15
cmpq %r15, %rax
je 0xf20a7
leaq 0x113299(%rip), %rax    # 0x205320
movq (%rax), %rcx
leaq (%r14,%rcx), %rax
incq %rax
subq %rcx, %rax
cmpq %rcx, %rax
jge 0xf2091
incq %r12
movq %r14, %rcx
movq %rax, %r14
jmp 0xf1fdb
movq %r14, %rcx
movq %rax, %r14
movq %rax, %r15
incq %rbx
leaq 0x11b3d6(%rip), %rax    # 0x20d490
movq (%rax), %rax
cmpq %rax, %rbx
jl 0xf1fcf
jmp 0xf20d2
leaq 0x11b3c1(%rip), %rax    # 0x20d490
movq (%rax), %rax
leaq 0x1131d7(%rip), %rdx    # 0x2052b0
movq %rcx, 0x10(%rdx)
leaq 0x2371e8(%rip), %rbx    # 0x3292cc
cmpl $0x2, (%rbx)
je 0xf2175
movl -0x2c(%rbp), %r9d
movl %r9d, %ecx
xorpd %xmm0, %xmm0
cmpq %rcx, %rax
jle 0xf2118
cvtsi2sd %r9d, %xmm0
leaq 0x11b39f(%rip), %rcx    # 0x20d4a8
mulsd (%rcx), %xmm0
leaq 0x11b384(%rip), %rcx    # 0x20d498
addsd (%rcx), %xmm0
leaq 0x11330b(%rip), %rcx    # 0x20542a
leaq 0x113358(%rip), %rdx    # 0x20547e
movzbl (%rdx), %edx
imulq $0xe8, %rdx, %rdx
cmpb $0x0, (%rcx)
leaq 0x55bf6(%rip), %rcx     # 0x147d30
movq (%rcx,%rdx), %rcx
leaq 0x2e0b9(%rip), %rdx     # 0x1201fe
leaq 0x2e073(%rip), %rsi     # 0x1201bf
cmoveq %rdx, %rsi
leaq 0x2366f9(%rip), %rdi    # 0x328850
movq %rax, %rdx
movq -0x38(%rbp), %r8
movb $0x1, %al
callq 0x9060
movl $0x3, (%rbx)
leaq 0x11b31e(%rip), %rax    # 0x20d490
movq (%rax), %rax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq