// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathmsc.cpp
// Function: add_mtie_point(int, double)
void add_mtie_point(int id, double val)
{
int i;
int j;
int k;
int p;
int kidx;
int imax;
struct MTIE_VALS *v;
struct MTIE_VALS *vm;
double x;
//double t0;

   if(id < 0) return;
   if(id > MAX_MTIE_CHANS) return;

   if(mtie_taus[id] == 0) alloc_mtie(20);   // mtie memory not allocated
   else if(mtie_buf[id] == 0) alloc_mtie(30);
   if(mtie_taus[id] == 0) return;   // mtie memory not allocated
   if(mtie_buf[id] == 0) return;

//t0 = GetMsecs();
   if((mtie_q_count[id] == 0) && (val == 0.0)) return;  // if first data point is 0.0, it is probably bogus
   save_mtie_phase(id, val);  // save new data point in the MTIE data buffer

   mtie0[id] = MAX(val, mtie0[id]);   // !!!! need ID
   if(mtie_q_count[id] > 1) {
      mtie_max[id] = MAX(val-mtie_phase(id, mtie_q_count[id]-2), mtie_max[id]);
      mtie_min[id] = MIN(val-mtie_phase(id, mtie_q_count[id]-2), mtie_min[id]);
//    mtie_max[id] = MAX(val, mtie_max[id]);
//    mtie_min[id] = MIN(val, mtie_min[id]);
   }
   mtie[id][0] = MAX(fabs(mtie_max[id]), fabs(mtie_min[id]));
   mtie_intervals[id][0] = mtie_q_count[id];

   for(kidx=0; kidx<mtie_kmax; kidx++) {
      k = kidx + 1;
      imax = mtie_q_count[id] - pow2[k] + 1;
      v = mtie_taus[id][kidx].vals;
      if(1) {  // fast "stop after buffer full mode"
         j = mtie_intervals[id][kidx+1] - 2;
         if(j < 0) j = 0;
      }
      else {  // much slower "circular buffer mode"
         j = 0;
      }

      for(i=j; i<imax; i++) {
         if(k <= 1) {
            v->max_vals[i] = MAX(mtie_phase(id, i), mtie_phase(id, i+1));
            v->min_vals[i] = MIN(mtie_phase(id, i), mtie_phase(id, i+1));
         }
         else {
            vm = mtie_taus[id][kidx-1].vals;
            p = pow2[k-1];

            v->max_vals[i] = MAX(vm->max_vals[i],
                                 vm->max_vals[i+p]);

            v->min_vals[i] = MIN(vm->min_vals[i],
                                 vm->min_vals[i+p]);
         }

         mtie_intervals[id][kidx+1] = i;
         x = v->max_vals[i] - v->min_vals[i];

         if(x > mtie[id][kidx+1]) mtie[id][kidx+1] = x;  // save current mtie value
      }
   }
//t0 = GetMsecs() - t0;
//sprintf(debug_text, "mtie_q_count %d: kmax:%d  %.12f  msecs:%f\n", mtie_q_count[id], mtie_kmax, val, t0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %edi, -0x2c(%rbp)
cmpl $0x4, %edi
ja 0xc69ea
movl -0x2c(%rbp), %r12d
leaq 0x378a3f(%rip), %rax    # 0x43f3d0
cmpq $0x0, (%rax,%r12,8)
je 0xc69a6
leaq 0x378351(%rip), %rax    # 0x43ecf0
cmpq $0x0, (%rax,%r12,8)
jne 0xc69b4
movaps %xmm0, -0x40(%rbp)
callq 0xc6922
movapd -0x40(%rbp), %xmm0
leaq 0x378a15(%rip), %rax    # 0x43f3d0
cmpq $0x0, (%rax,%r12,8)
je 0xc69ea
leaq 0x378327(%rip), %rax    # 0x43ecf0
cmpq $0x0, (%rax,%r12,8)
je 0xc69ea
leaq 0x378339(%rip), %rbx    # 0x43ed10
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xc69fc
jp 0xc69fc
cmpl $0x0, (%rbx,%r12,4)
jne 0xc69fc
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x2c(%rbp), %r15d
movl %r15d, %edi
movapd %xmm0, -0x40(%rbp)
callq 0xe84e7
leaq 0x37899c(%rip), %rax    # 0x43f3b0
movapd -0x40(%rbp), %xmm0
maxsd (%rax,%r12,8), %xmm0
movsd %xmm0, (%rax,%r12,8)
movslq (%rbx,%r12,4), %rbx
cmpq $0x1, %rbx
jle 0xc6a8d
leal -0x2(%rbx), %r14d
movl %r15d, %edi
movl %r14d, %esi
callq 0xe84b9
movapd -0x40(%rbp), %xmm1
subsd %xmm0, %xmm1
leaq 0x378922(%rip), %rax    # 0x43f370
maxsd (%rax,%r12,8), %xmm1
movapd %xmm1, -0x60(%rbp)
movsd %xmm1, (%rax,%r12,8)
movl %r15d, %edi
movl %r14d, %esi
callq 0xe84b9
movapd -0x60(%rbp), %xmm2
movapd -0x40(%rbp), %xmm1
subsd %xmm0, %xmm1
leaq 0x378911(%rip), %rax    # 0x43f390
minsd (%rax,%r12,8), %xmm1
movsd %xmm1, (%rax,%r12,8)
jmp 0xc6aa7
leaq 0x3788dc(%rip), %rax    # 0x43f370
movsd (%rax,%r12,8), %xmm2
leaq 0x3788ef(%rip), %rax    # 0x43f390
movsd (%rax,%r12,8), %xmm1
movapd 0x2c6e1(%rip), %xmm0    # 0xf3190
andpd %xmm0, %xmm2
andpd %xmm0, %xmm1
maxsd %xmm1, %xmm2
imulq $0x328, %r12, %rax      # imm = 0x328
leaq 0x377587(%rip), %rdx    # 0x43e050
movsd %xmm2, (%rax,%rdx)
imulq $0x194, %r12, %rcx      # imm = 0x194
leaq 0x378244(%rip), %rsi    # 0x43ed20
movq %rbx, -0x78(%rbp)
movl %ebx, (%rcx,%rsi)
movl 0x37890b(%rip), %edi    # 0x43f3f4
xorl %r8d, %r8d
testl %edi, %edi
cmovlel %r8d, %edi
movq %rdi, -0x88(%rbp)
addq %rcx, %rsi
movq %rsi, -0x68(%rbp)
addq %rax, %rdx
movq %rdx, -0x70(%rbp)
movq %r12, -0x80(%rbp)
movl -0x2c(%rbp), %ebx
cmpq -0x88(%rbp), %r8
je 0xc69ea
leaq 0x1(%r8), %rax
movq %rax, -0x90(%rbp)
leaq 0x377383(%rip), %rax    # 0x43deb0
movslq 0x4(%rax,%r8,4), %rax
movq -0x78(%rbp), %r11
subq %rax, %r11
leaq 0x378890(%rip), %rax    # 0x43f3d0
movq (%rax,%r12,8), %rax
movq %rax, -0xb8(%rbp)
movq (%rax,%r8,8), %rax
movq %rax, -0x40(%rbp)
movq -0x68(%rbp), %rax
leaq (%rax,%r8,4), %rdi
addq $0x4, %rdi
movl (%rdi), %r12d
cmpl $0x3, %r12d
pushq $0x2
popq %rax
cmovll %eax, %r12d
movq -0x70(%rbp), %rax
leaq (%rax,%r8,8), %r9
addq $0x8, %r9
leaq -0x2(%r12), %r10
movq %r8, -0xb0(%rbp)
movq %r11, -0xa8(%rbp)
movq %rdi, -0xa0(%rbp)
movq %r9, -0x98(%rbp)
leaq -0x2(%r12), %rax
cmpq %r11, %rax
jg 0xc6cc3
testq %r8, %r8
je 0xc6c0b
movq -0xb8(%rbp), %rax
movq -0x8(%rax,%r8,8), %rax
leaq 0x3772f0(%rip), %rcx    # 0x43deb0
movslq (%rcx,%r8,4), %rcx
movq 0x8(%rax), %rdx
movsd -0x10(%rdx,%r12,8), %xmm0
addq %r12, %rcx
maxsd -0x10(%rdx,%rcx,8), %xmm0
movq -0x40(%rbp), %rdx
movq 0x8(%rdx), %r13
movsd %xmm0, -0x10(%r13,%r12,8)
movq (%rax), %rax
movsd -0x10(%rax,%r12,8), %xmm1
minsd -0x10(%rax,%rcx,8), %xmm1
movq (%rdx), %rax
movsd %xmm1, -0x10(%rax,%r12,8)
leal -0x2(%r12), %eax
jmp 0xc6c9f
leal -0x2(%r12), %esi
movl %esi, -0x48(%rbp)
movl %ebx, %edi
movq %r10, -0x60(%rbp)
callq 0xe84b9
movsd %xmm0, -0xc0(%rbp)
leal -0x1(%r12), %r15d
movl %ebx, %edi
movl %r15d, %esi
callq 0xe84b9
movsd -0xc0(%rbp), %xmm1
maxsd %xmm0, %xmm1
movq -0x40(%rbp), %r14
movq 0x8(%r14), %r13
movsd %xmm1, -0x10(%r13,%r12,8)
movl %ebx, %edi
movl -0x48(%rbp), %esi
callq 0xe84b9
movsd %xmm0, -0x48(%rbp)
movl %ebx, %edi
movl %r15d, %esi
callq 0xe84b9
movsd -0x48(%rbp), %xmm1
movq -0x60(%rbp), %r10
movq -0x98(%rbp), %r9
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %r11
movq -0xb0(%rbp), %r8
minsd %xmm0, %xmm1
movq (%r14), %rax
movsd %xmm1, -0x10(%rax,%r12,8)
movl %r10d, %eax
movl %eax, (%rdi)
movsd -0x10(%r13,%r12,8), %xmm0
subsd %xmm1, %xmm0
ucomisd (%r9), %xmm0
jbe 0xc6cb8
movsd %xmm0, (%r9)
incq %r10
incq %r12
jmp 0xc6b9a
movq -0x90(%rbp), %r8
movq -0x80(%rbp), %r12
jmp 0xc6b0e