// File: /workspace/github_cpp_makefile_star2_O3/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:
cmpl $0x4, %edi
ja 0xd872a
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edi, %ebx
leaq 0x37cff0(%rip), %r14    # 0x4553d0
cmpq $0x0, (%r14,%rbx,8)
je 0xd83f5
leaq 0x37c902(%rip), %rax    # 0x454cf0
cmpq $0x0, (%rax,%rbx,8)
jne 0xd8404
movsd %xmm0, -0x30(%rbp)
callq 0xd836f
movsd -0x30(%rbp), %xmm0
movq (%r14,%rbx,8), %rax
testq %rax, %rax
je 0xd871c
leaq 0x37c8d8(%rip), %rcx    # 0x454cf0
movq (%rcx,%rbx,8), %rcx
testq %rcx, %rcx
je 0xd871c
leaq 0x37c8e4(%rip), %rdx    # 0x454d10
movslq (%rdx,%rbx,4), %r9
testq %r9, %r9
sete %sil
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
setnp %dil
sete %r8b
andb %dil, %r8b
testb %sil, %r8b
jne 0xd871c
movslq 0x37cf96(%rip), %rsi    # 0x4553f0
cmpl %esi, %r9d
jge 0xd846c
movsd %xmm0, (%rcx,%r9,8)
incl %r9d
movl %r9d, (%rdx,%rbx,4)
leaq 0x37cf3d(%rip), %rdx    # 0x4553b0
movapd %xmm0, %xmm1
maxsd (%rdx,%rbx,8), %xmm1
movsd %xmm1, (%rdx,%rbx,8)
cmpl $0x2, %r9d
jl 0xd850d
leal -0x2(%r9), %edx
cmpl %edx, %esi
jle 0xd84d5
movl %edx, %edx
movapd %xmm0, %xmm1
subsd (%rcx,%rdx,8), %xmm1
leaq 0x37cecb(%rip), %rdi    # 0x455370
ucomisd (%rdi,%rbx,8), %xmm1
jbe 0xd84b1
movsd %xmm1, (%rdi,%rbx,8)
movsd (%rcx,%rdx,8), %xmm3
movapd %xmm0, %xmm2
subsd %xmm3, %xmm2
leaq 0x37cecb(%rip), %rdx    # 0x455390
leaq (%rdx,%rbx,8), %rdx
movsd (%rdx), %xmm1
ucomisd %xmm2, %xmm1
ja 0xd8501
jmp 0xd8509
leaq 0x37ce94(%rip), %rdx    # 0x455370
ucomisd (%rdx,%rbx,8), %xmm0
jbe 0xd84e8
movsd %xmm0, (%rdx,%rbx,8)
leaq 0x37cea1(%rip), %rdx    # 0x455390
leaq (%rdx,%rbx,8), %rdx
movsd (%rdx), %xmm1
xorpd %xmm3, %xmm3
ucomisd %xmm0, %xmm1
jbe 0xd8509
subsd %xmm3, %xmm0
movapd %xmm0, %xmm1
movsd %xmm1, (%rdx)
leaq 0x37ce5c(%rip), %rdx    # 0x455370
movsd (%rdx,%rbx,8), %xmm0
movapd 0x30c6f(%rip), %xmm1    # 0x109190
andpd %xmm1, %xmm0
leaq 0x37ce64(%rip), %rdx    # 0x455390
movsd (%rdx,%rbx,8), %xmm2
andpd %xmm1, %xmm2
maxsd %xmm2, %xmm0
imulq $0x328, %rbx, %rdx      # imm = 0x328
leaq 0x37bb09(%rip), %rdi    # 0x454050
movq %rdi, -0x38(%rbp)
movsd %xmm0, (%rdx,%rdi)
imulq $0x194, %rbx, %rdi      # imm = 0x194
leaq 0x37c7c2(%rip), %r10    # 0x454d20
movl %r9d, (%rdi,%r10)
movslq 0x37ce8b(%rip), %r11    # 0x4553f4
testq %r11, %r11
jle 0xd871c
leaq -0x1(%rsi), %r8
movq %r8, -0x30(%rbp)
xorl %r12d, %r12d
leaq 0x37b92c(%rip), %rbx    # 0x453eb0
addq %rdi, %r10
movl $0x2, %r14d
addq %rdx, -0x38(%rbp)
xorpd %xmm0, %xmm0
movq %r9, -0x50(%rbp)
movq %r10, -0x48(%rbp)
movq %r11, -0x40(%rbp)
movq %r12, %r15
incq %r12
movl %r9d, %edx
subl 0x4(%rbx,%r15,4), %edx
movl 0x4(%r10,%r15,4), %r8d
cmpl $0x3, %r8d
cmovll %r14d, %r8d
leal -0x2(%r8), %edi
cmpl %edx, %edi
jg 0xd8713
leaq (%r10,%r15,4), %rdi
addq $0x4, %rdi
movq %rdi, -0x58(%rbp)
movq (%rax,%r15,8), %rdi
movq (%rdi), %r13
movq 0x8(%rdi), %r14
movq -0x38(%rbp), %rdi
movq %r12, -0x60(%rbp)
leaq (%rdi,%r12,8), %rdi
movl %r8d, %r9d
movl %edx, %edx
addq $-0x3, %r9
leaq 0x1(%r9), %r12
testq %r15, %r15
je 0xd8635
movq -0x8(%rax,%r15,8), %r8
movslq (%rbx,%r15,4), %r10
movq 0x8(%r8), %r11
movsd 0x8(%r11,%r9,8), %xmm1
addq %r9, %r10
maxsd 0x8(%r11,%r10,8), %xmm1
movsd %xmm1, 0x8(%r14,%r9,8)
movq (%r8), %r8
movsd 0x8(%r8,%r9,8), %xmm1
minsd 0x8(%r8,%r10,8), %xmm1
jmp 0xd86b0
cmpq %rsi, %r12
jge 0xd866a
movsd 0x8(%rcx,%r9,8), %xmm1
cmpq -0x30(%rbp), %r12
jge 0xd8679
leaq 0x2(%r9), %r8
movl %r8d, %r10d
movsd (%rcx,%r10,8), %xmm2
ucomisd %xmm2, %xmm1
jbe 0xd868f
movsd 0x8(%rcx,%r9,8), %xmm1
movsd %xmm1, 0x8(%r14,%r9,8)
jmp 0xd869d
movq $0x0, 0x8(%r14,%r9,8)
xorpd %xmm1, %xmm1
jmp 0xd86b0
ucomisd %xmm0, %xmm1
jbe 0xd86da
movsd 0x8(%rcx,%r9,8), %xmm2
movsd %xmm2, 0x8(%r14,%r9,8)
jmp 0xd86ea
movsd %xmm2, 0x8(%r14,%r9,8)
movsd 0x8(%rcx,%r9,8), %xmm1
movl %r8d, %r8d
movsd (%rcx,%r8,8), %xmm2
ucomisd %xmm1, %xmm2
ja 0xd86b0
movapd %xmm2, %xmm1
movsd %xmm1, 0x8(%r13,%r9,8)
movsd 0x8(%r14,%r9,8), %xmm2
subsd %xmm1, %xmm2
ucomisd (%rdi), %xmm2
jbe 0xd86cc
movsd %xmm2, (%rdi)
movq %r12, %r9
cmpq %rdx, %r12
jl 0xd85f4
jmp 0xd86f6
movq $0x0, 0x8(%r14,%r9,8)
movsd 0x8(%rcx,%r9,8), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm2, %xmm1
ja 0xd86ac
jmp 0xd86b0
movq -0x58(%rbp), %rdx
movl %r12d, (%rdx)
movq -0x50(%rbp), %r9
movq -0x48(%rbp), %r10
movq -0x40(%rbp), %r11
movq -0x60(%rbp), %r12
movl $0x2, %r14d
cmpq %r11, %r12
jne 0xd85a1
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq