// File: /workspace/github_cpp_makefile_star2_O3/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 0x108da0
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
leaq 0x112a28(%rip), %rcx    # 0x21b398
cmpq $0x0, (%rcx)
je 0x108d8f
leaq 0x20460f(%rip), %rcx    # 0x30cf90
movsd (%rcx), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x108995
jnp 0x108d8f
movl %edi, %r12d
leaq 0x112a2d(%rip), %rax    # 0x21b3cc
movl $0x0, (%rax)
leaq 0x11298c(%rip), %rax    # 0x21b338
movq (%rax), %rax
leaq 0x10fece(%rip), %rcx    # 0x218884
movslq (%rcx), %rcx
cmpq %rcx, %rax
cmovlq %rax, %rcx
movl $0x1000, %eax           # imm = 0x1000
cmpq %rax, %rcx
cmovgeq %rax, %rcx
leaq 0x11aabd(%rip), %rbx    # 0x223490
movq %rcx, (%rbx)
leaq 0x112aa1(%rip), %rax    # 0x21b47e
movb %r12b, (%rax)
callq 0x1084f5
movl %r12d, %r13d
imulq $0xe8, %r13, %rax
leaq 0x5533a(%rip), %rcx     # 0x15dd30
movsd 0xb0(%rcx,%rax), %xmm0
movsd 0xb8(%rcx,%rax), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
andpd 0x778(%rip), %xmm2      # 0x109190
mulsd 0x6e0(%rip), %xmm2      # 0x109100
subsd %xmm2, %xmm0
leaq 0x11aa6d(%rip), %rax    # 0x223498
movsd %xmm0, (%rax)
addsd %xmm1, %xmm2
leaq 0x11aa66(%rip), %rax    # 0x2234a0
movsd %xmm2, (%rax)
subsd %xmm0, %xmm2
movq (%rbx), %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
divsd %xmm0, %xmm2
leaq 0x11aa50(%rip), %rax    # 0x2234a8
movsd %xmm2, (%rax)
testq %rdx, %rdx
jle 0x108a73
shlq $0x3, %rdx
leaq 0x112a24(%rip), %rdi    # 0x21b490
xorl %esi, %esi
callq 0x91a0
leaq 0x1127ee(%rip), %rax    # 0x21b268
movq (%rax), %r15
leaq 0x237314(%rip), %rax    # 0x33fd98
movq %r15, (%rax)
movq $-0x1, %r14
xorl %ebx, %ebx
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
leaq 0x112882(%rip), %rax    # 0x21b328
cmpq (%rax), %r15
je 0x108b9b
leaq -0xe8(%rbp), %rdi
movq %r15, %rsi
callq 0x100be
movsd -0xd8(%rbp,%r13,8), %xmm0
leaq 0x1128c9(%rip), %rax    # 0x21b398
xorps %xmm1, %xmm1
cvtsi2sdq (%rax), %xmm1
divsd %xmm1, %xmm0
movl %r12d, %edi
callq 0x108eba
leaq 0x11a9ae(%rip), %rax    # 0x223498
subsd (%rax), %xmm0
leaq 0x11a9b3(%rip), %rax    # 0x2234a8
divsd (%rax), %xmm0
cvttsd2si %xmm0, %eax
testl %eax, %eax
js 0x108b39
movl %eax, %ecx
leaq 0x11a986(%rip), %rdx    # 0x223490
cmpq %rcx, (%rdx)
jle 0x108b39
leaq 0x11297a(%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)
leaq 0x112860(%rip), %rax    # 0x21b3a0
movq (%rax), %rax
incq %r14
leaq 0x1127eb(%rip), %rcx    # 0x21b338
cmpq (%rcx), %r14
jge 0x108b8f
incq %r15
leaq 0x1127c4(%rip), %rcx    # 0x21b320
cmpq (%rcx), %r15
movl $0x0, %ecx
cmovgeq %rcx, %r15
leaq 0x112861(%rip), %rcx    # 0x21b3d0
movslq (%rcx), %rcx
movslq %ebx, %rbx
addq %rcx, %rbx
leaq 0x10fd05(%rip), %rcx    # 0x218884
movslq (%rcx), %rcx
imulq %rax, %rcx
cmpq %rbx, %rcx
jg 0x108a9f
leaq 0x112792(%rip), %rcx    # 0x21b328
movq (%rcx), %r15
jmp 0x108ba5
leaq 0x1127fe(%rip), %rax    # 0x21b3a0
movq (%rax), %rax
leaq 0x112820(%rip), %rcx    # 0x21b3cc
movl $0x0, (%rcx)
leaq 0x1126af(%rip), %rcx    # 0x21b268
movq (%rcx), %r12
cmpq $0x2, %rax
movl $0x1, %ecx
cmovgeq %rax, %rcx
leaq 0x2371b8(%rip), %rax    # 0x33fd88
movq %rcx, (%rax)
xorl %r14d, %r14d
leaq -0xe8(%rbp), %rbx
movq %r12, %rcx
cmpq %r15, %rcx
je 0x108cdf
leaq 0x237198(%rip), %rax    # 0x33fd88
cmpq $0x0, (%rax)
jle 0x108cc7
xorl %r13d, %r13d
movq %rcx, %r12
leaq -0x198(%rbp), %r15
movq %r15, %rdi
movq %rcx, %rsi
callq 0x100be
movl $0xb0, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x92b0
leaq 0x112867(%rip), %rax    # 0x21b490
movq (%rax,%r14,8), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
leaq 0x237158(%rip), %rcx    # 0x33fd94
cmpb $0x0, (%rcx)
je 0x108c5b
testq %rax, %rax
jle 0x108c5b
callq 0x9160
mulsd 0x455(%rip), %xmm0      # 0x1090a8
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
cmpq $0x1, %r14
jne 0x108c71
leaq 0x112643(%rip), %rax    # 0x21b2b0
movq %r12, 0x8(%rax)
movl $0x16, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r12, %rdi
callq 0x1028a
leaq 0x1(%r12), %rcx
leaq 0x112695(%rip), %rax    # 0x21b328
movq (%rax), %r15
cmpq %r15, %rcx
je 0x108cc7
leaq 0x11267e(%rip), %rax    # 0x21b320
movq (%rax), %rax
leaq (%r12,%rax), %rcx
incq %rcx
subq %rax, %rcx
cmpq %rax, %rcx
jge 0x108cac
incq %r13
leaq 0x2370ca(%rip), %rax    # 0x33fd88
cmpq (%rax), %r13
jl 0x108bfd
incq %r14
leaq 0x11a7bf(%rip), %rax    # 0x223490
movq (%rax), %rax
cmpq %rax, %r14
jl 0x108be0
jmp 0x108ce9
leaq 0x11a7aa(%rip), %rax    # 0x223490
movq (%rax), %rax
leaq 0x1125c0(%rip), %rcx    # 0x21b2b0
movq %r12, 0x10(%rcx)
leaq 0x2365d1(%rip), %rbx    # 0x33f2cc
cmpl $0x2, (%rbx)
je 0x108d8f
movl -0x2c(%rbp), %r9d
movl %r9d, %ecx
xorpd %xmm0, %xmm0
cmpq %rcx, %rax
jle 0x108d32
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
leaq 0x11a785(%rip), %rcx    # 0x2234a8
mulsd (%rcx), %xmm0
leaq 0x11a76a(%rip), %rcx    # 0x223498
addsd (%rcx), %xmm0
leaq 0x1126f1(%rip), %rcx    # 0x21b42a
leaq 0x11273e(%rip), %rdx    # 0x21b47e
movzbl (%rdx), %edx
imulq $0xe8, %rdx, %rdx
cmpb $0x0, (%rcx)
leaq 0x54fdc(%rip), %rcx     # 0x15dd30
movq (%rcx,%rdx), %rcx
leaq 0x2d593(%rip), %rdx     # 0x1362f2
leaq 0x2d54d(%rip), %rsi     # 0x1362b3
cmoveq %rdx, %rsi
leaq 0x235adf(%rip), %rdi    # 0x33e850
movq %rax, %rdx
movq -0x38(%rbp), %r8
movb $0x1, %al
callq 0x9060
movl $0x3, (%rbx)
leaq 0x11a704(%rip), %rax    # 0x223490
movq (%rax), %rax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq