// File: 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
subq $0xb0, %rsp
movl %edi, -0x4(%rbp)
movsd %xmm0, -0x10(%rbp)
cmpl $0x0, -0x4(%rbp)
jge 0x126fae
jmp 0x12763c
cmpl $0x4, -0x4(%rbp)
jle 0x126fb9
jmp 0x12763c
movslq -0x4(%rbp), %rcx
leaq 0x38d9fc(%rip), %rax    # 0x4b49c0
cmpq $0x0, (%rax,%rcx,8)
jne 0x126fd7
movl $0x14, %edi
callq 0x126f10
jmp 0x126ff5
movslq -0x4(%rbp), %rcx
leaq 0x38d2fe(%rip), %rax    # 0x4b42e0
cmpq $0x0, (%rax,%rcx,8)
jne 0x126ff3
movl $0x1e, %edi
callq 0x126f10
jmp 0x126ff5
movslq -0x4(%rbp), %rcx
leaq 0x38d9c0(%rip), %rax    # 0x4b49c0
cmpq $0x0, (%rax,%rcx,8)
jne 0x12700c
jmp 0x12763c
movslq -0x4(%rbp), %rcx
leaq 0x38d2c9(%rip), %rax    # 0x4b42e0
cmpq $0x0, (%rax,%rcx,8)
jne 0x127023
jmp 0x12763c
movslq -0x4(%rbp), %rcx
leaq 0x38d2d2(%rip), %rax    # 0x4b4300
cmpl $0x0, (%rax,%rcx,4)
jne 0x127049
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x127049
jp 0x127049
jmp 0x12763c
movl -0x4(%rbp), %edi
movsd -0x10(%rbp), %xmm0
callq 0x15e0d0
movsd -0x10(%rbp), %xmm0
movslq -0x4(%rbp), %rcx
leaq 0x38d93a(%rip), %rax    # 0x4b49a0
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x127079
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x12708e
movslq -0x4(%rbp), %rcx
leaq 0x38d91c(%rip), %rax    # 0x4b49a0
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
movslq -0x4(%rbp), %rcx
leaq 0x38d902(%rip), %rax    # 0x4b49a0
movsd %xmm0, (%rax,%rcx,8)
movslq -0x4(%rbp), %rcx
leaq 0x38d252(%rip), %rax    # 0x4b4300
cmpl $0x1, (%rax,%rcx,4)
jle 0x1271fb
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x4(%rbp), %edi
movslq -0x4(%rbp), %rcx
leaq 0x38d230(%rip), %rax    # 0x4b4300
movl (%rax,%rcx,4), %esi
subl $0x2, %esi
callq 0x15e050
movaps %xmm0, %xmm1
movsd -0x50(%rbp), %xmm0
subsd %xmm1, %xmm0
movslq -0x4(%rbp), %rcx
leaq 0x38d86e(%rip), %rax    # 0x4b4960
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x12712f
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x4(%rbp), %edi
movslq -0x4(%rbp), %rcx
leaq 0x38d1ef(%rip), %rax    # 0x4b4300
movl (%rax,%rcx,4), %esi
subl $0x2, %esi
callq 0x15e050
movaps %xmm0, %xmm1
movsd -0x60(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
jmp 0x127144
movslq -0x4(%rbp), %rcx
leaq 0x38d826(%rip), %rax    # 0x4b4960
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
movslq -0x4(%rbp), %rcx
leaq 0x38d80c(%rip), %rax    # 0x4b4960
movsd %xmm0, (%rax,%rcx,8)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movl -0x4(%rbp), %edi
movslq -0x4(%rbp), %rcx
leaq 0x38d18f(%rip), %rax    # 0x4b4300
movl (%rax,%rcx,4), %esi
subl $0x2, %esi
callq 0x15e050
movsd -0x68(%rbp), %xmm1
subsd %xmm0, %xmm1
movslq -0x4(%rbp), %rcx
leaq 0x38d7f0(%rip), %rax    # 0x4b4980
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1271d1
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movl -0x4(%rbp), %edi
movslq -0x4(%rbp), %rcx
leaq 0x38d14d(%rip), %rax    # 0x4b4300
movl (%rax,%rcx,4), %esi
subl $0x2, %esi
callq 0x15e050
movaps %xmm0, %xmm1
movsd -0x78(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
jmp 0x1271e6
movslq -0x4(%rbp), %rcx
leaq 0x38d7a4(%rip), %rax    # 0x4b4980
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x70(%rbp), %xmm0
movslq -0x4(%rbp), %rcx
leaq 0x38d78a(%rip), %rax    # 0x4b4980
movsd %xmm0, (%rax,%rcx,8)
movslq -0x4(%rbp), %rcx
leaq 0x38d75a(%rip), %rax    # 0x4b4960
movsd (%rax,%rcx,8), %xmm0
movaps 0x4807e(%rip), %xmm1    # 0x16f290
pand %xmm1, %xmm0
movslq -0x4(%rbp), %rcx
leaq 0x38d75f(%rip), %rax    # 0x4b4980
movsd (%rax,%rcx,8), %xmm1
movaps 0x48063(%rip), %xmm2    # 0x16f290
pand %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x127259
movslq -0x4(%rbp), %rcx
leaq 0x38d71e(%rip), %rax    # 0x4b4960
movsd (%rax,%rcx,8), %xmm0
movaps 0x48042(%rip), %xmm1    # 0x16f290
pand %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
jmp 0x127279
movslq -0x4(%rbp), %rcx
leaq 0x38d71c(%rip), %rax    # 0x4b4980
movsd (%rax,%rcx,8), %xmm0
movaps 0x48020(%rip), %xmm1    # 0x16f290
pand %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x80(%rbp), %xmm0
movslq -0x4(%rbp), %rcx
leaq 0x38c3b7(%rip), %rax    # 0x4b3640
imulq $0x328, %rcx, %rcx      # imm = 0x328
addq %rcx, %rax
movsd %xmm0, (%rax)
movslq -0x4(%rbp), %rcx
leaq 0x38d05e(%rip), %rax    # 0x4b4300
movl (%rax,%rcx,4), %ecx
movslq -0x4(%rbp), %rdx
leaq 0x38d060(%rip), %rax    # 0x4b4310
imulq $0x194, %rdx, %rdx      # imm = 0x194
addq %rdx, %rax
movl %ecx, (%rax)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl 0x38d718(%rip), %eax    # 0x4b49e4
jge 0x12763c
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movslq -0x4(%rbp), %rcx
leaq 0x38d01a(%rip), %rax    # 0x4b4300
movl (%rax,%rcx,4), %eax
movslq -0x1c(%rbp), %rdx
leaq 0x38c1ac(%rip), %rcx    # 0x4b34a0
subl (%rcx,%rdx,4), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movslq -0x4(%rbp), %rcx
leaq 0x38d6b8(%rip), %rax    # 0x4b49c0
movq (%rax,%rcx,8), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movslq -0x4(%rbp), %rcx
leaq 0x38cfed(%rip), %rax    # 0x4b4310
imulq $0x194, %rcx, %rcx      # imm = 0x194
addq %rcx, %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
subl $0x2, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x12734c
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x12762c
cmpl $0x1, -0x1c(%rbp)
jg 0x127455
movl -0x4(%rbp), %edi
movl -0x14(%rbp), %esi
callq 0x15e050
movsd %xmm0, -0x88(%rbp)
movl -0x4(%rbp), %edi
movl -0x14(%rbp), %esi
addl $0x1, %esi
callq 0x15e050
movaps %xmm0, %xmm1
movsd -0x88(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1273af
movl -0x4(%rbp), %edi
movl -0x14(%rbp), %esi
callq 0x15e050
movsd %xmm0, -0x90(%rbp)
jmp 0x1273c5
movl -0x4(%rbp), %edi
movl -0x14(%rbp), %esi
addl $0x1, %esi
callq 0x15e050
movsd %xmm0, -0x90(%rbp)
movsd -0x90(%rbp), %xmm0
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x14(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x4(%rbp), %edi
movl -0x14(%rbp), %esi
callq 0x15e050
movsd %xmm0, -0x98(%rbp)
movl -0x4(%rbp), %edi
movl -0x14(%rbp), %esi
addl $0x1, %esi
callq 0x15e050
movsd -0x98(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x127422
movl -0x4(%rbp), %edi
movl -0x14(%rbp), %esi
callq 0x15e050
movsd %xmm0, -0xa0(%rbp)
jmp 0x127438
movl -0x4(%rbp), %edi
movl -0x14(%rbp), %esi
addl $0x1, %esi
callq 0x15e050
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm0
movq -0x30(%rbp), %rax
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x127580
movslq -0x4(%rbp), %rcx
leaq 0x38d560(%rip), %rax    # 0x4b49c0
movq (%rax,%rcx,8), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x38c01b(%rip), %rax    # 0x4b34a0
movl (%rax,%rcx,4), %eax
movl %eax, -0x20(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x14(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
addl -0x20(%rbp), %ecx
movslq %ecx, %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x1274cf
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x14(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xa8(%rbp)
jmp 0x1274ed
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %ecx
addl -0x20(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x14(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
addl -0x20(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x12754b
movq -0x38(%rbp), %rax
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xb0(%rbp)
jmp 0x127568
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
addl -0x20(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xb0(%rbp), %xmm0
movq -0x30(%rbp), %rax
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x14(%rbp), %edx
movslq -0x4(%rbp), %rcx
leaq 0x38cd82(%rip), %rax    # 0x4b4310
imulq $0x194, %rcx, %rcx      # imm = 0x194
addq %rcx, %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x14(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
movslq -0x4(%rbp), %rcx
leaq 0x38c066(%rip), %rax    # 0x4b3640
imulq $0x328, %rcx, %rcx      # imm = 0x328
addq %rcx, %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x12761c
movsd -0x40(%rbp), %xmm0
movslq -0x4(%rbp), %rcx
leaq 0x38c03c(%rip), %rax    # 0x4b3640
imulq $0x328, %rcx, %rcx      # imm = 0x328
addq %rcx, %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x12761e
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x127352
jmp 0x12762e
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1272c3
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)