// File: /workspace/github_cpp_makefile_star2_O1/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 0xd801d
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edi, %ebx
leaq 0x37d810(%rip), %r14    # 0x4554b0
cmpq $0x0, (%r14,%rbx,8)
je 0xd7cb5
leaq 0x37d122(%rip), %rax    # 0x454dd0
cmpq $0x0, (%rax,%rbx,8)
jne 0xd7cc4
movsd %xmm0, -0x30(%rbp)
callq 0xd7c2f
movsd -0x30(%rbp), %xmm0
movq (%r14,%rbx,8), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0xd800f
leaq 0x37d0f4(%rip), %rax    # 0x454dd0
movq (%rax,%rbx,8), %rcx
testq %rcx, %rcx
je 0xd800f
leaq 0x37d100(%rip), %rdx    # 0x454df0
movslq (%rdx,%rbx,4), %rsi
testq %rsi, %rsi
sete %al
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
setnp %dil
sete %r8b
andb %dil, %r8b
testb %al, %r8b
jne 0xd800f
cmpl 0x37d7b4(%rip), %esi    # 0x4554d0
jge 0xd7d29
movsd %xmm0, (%rcx,%rsi,8)
leal 0x1(%rsi), %eax
movl %eax, (%rdx,%rbx,4)
leaq 0x37d760(%rip), %rax    # 0x455490
movapd %xmm0, %xmm1
maxsd (%rax,%rbx,8), %xmm1
movsd %xmm1, (%rax,%rbx,8)
movslq (%rdx,%rbx,4), %r8
cmpq $0x2, %r8
jl 0xd7de0
leal -0x2(%r8), %edx
movl 0x37d77a(%rip), %esi    # 0x4554d0
xorpd %xmm2, %xmm2
cmpl %edx, %esi
jle 0xd7d65
movl %edx, %eax
movsd (%rcx,%rax,8), %xmm2
movapd %xmm0, %xmm1
subsd %xmm2, %xmm1
leaq 0x37d6dc(%rip), %rdi    # 0x455450
movsd (%rdi,%rbx,8), %xmm2
ucomisd %xmm2, %xmm1
jbe 0xd7d96
xorpd %xmm1, %xmm1
cmpl %edx, %esi
jle 0xd7d8e
movl %edx, %eax
movsd (%rcx,%rax,8), %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, (%rdi,%rbx,8)
xorpd %xmm2, %xmm2
cmpl %edx, %esi
jle 0xd7daa
movl %edx, %eax
movsd (%rcx,%rax,8), %xmm2
movapd %xmm0, %xmm1
subsd %xmm2, %xmm1
leaq 0x37d6b7(%rip), %rdi    # 0x455470
movsd (%rdi,%rbx,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0xd7ddb
xorpd %xmm1, %xmm1
cmpl %edx, %esi
jle 0xd7dd3
movl %edx, %eax
movsd (%rcx,%rax,8), %xmm1
subsd %xmm1, %xmm0
movapd %xmm0, %xmm2
movsd %xmm2, (%rdi,%rbx,8)
leaq 0x37d669(%rip), %rax    # 0x455450
movsd (%rax,%rbx,8), %xmm0
movapd 0x3138c(%rip), %xmm1    # 0x109180
andpd %xmm1, %xmm0
leaq 0x37d671(%rip), %rax    # 0x455470
movsd (%rax,%rbx,8), %xmm2
andpd %xmm1, %xmm2
maxsd %xmm2, %xmm0
imulq $0x328, %rbx, %rdx      # imm = 0x328
leaq 0x37c316(%rip), %rax    # 0x454130
movq %rax, -0x38(%rbp)
movsd %xmm0, (%rdx,%rax)
imulq $0x194, %rbx, %rsi      # imm = 0x194
leaq 0x37cfcf(%rip), %rdi    # 0x454e00
movl %r8d, (%rsi,%rdi)
movslq 0x37d698(%rip), %r10    # 0x4554d4
testq %r10, %r10
jle 0xd800f
movslq 0x37d684(%rip), %r9     # 0x4554d0
xorl %r11d, %r11d
addq %rsi, %rdi
movl $0x2, %esi
addq %rdx, -0x38(%rbp)
movq %r8, -0x50(%rbp)
movq %rdi, -0x48(%rbp)
movq %r10, -0x40(%rbp)
movq %r11, %r14
incq %r11
leaq 0x37c11c(%rip), %rax    # 0x453f90
movslq 0x4(%rax,%r14,4), %rax
movq %r8, %r15
subq %rax, %r15
movl 0x4(%rdi,%r14,4), %edx
cmpl $0x3, %edx
cmovll %esi, %edx
leal -0x2(%rdx), %eax
cmpl %r15d, %eax
jg 0xd8006
movq -0x30(%rbp), %rax
movq (%rax,%r14,8), %r13
leaq (%rdi,%r14,4), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movq (%r13), %rbx
movq 0x8(%r13), %rsi
movq -0x38(%rbp), %rax
movq %r11, -0x60(%rbp)
leaq (%rax,%r11,8), %r8
movl %edx, %edx
leaq -0x2(%rdx), %r12
addq $-0x3, %rdx
testq %r14, %r14
je 0xd7f15
movq -0x30(%rbp), %rax
movq -0x8(%rax,%r14,8), %rax
leaq 0x37c0b3(%rip), %rdi    # 0x453f90
movslq (%rdi,%r14,4), %rdi
movq 0x8(%rax), %r10
movsd 0x8(%r10,%rdx,8), %xmm0
addq %rdx, %rdi
maxsd 0x8(%r10,%rdi,8), %xmm0
movq 0x8(%r13), %r10
movsd %xmm0, 0x8(%r10,%rdx,8)
movq (%rax), %rax
movsd 0x8(%rax,%rdx,8), %xmm0
minsd 0x8(%rax,%rdi,8), %xmm0
jmp 0xd7fbe
leaq 0x1(%rdx), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
cmpq %r9, %rdi
jge 0xd7f2c
movsd 0x8(%rcx,%rdx,8), %xmm1
leaq 0x2(%rdx), %r10
cmpq %r9, %r10
jge 0xd7f3b
movsd 0x10(%rcx,%rdx,8), %xmm0
leaq 0x1(%r12), %rax
ucomisd %xmm0, %xmm1
jbe 0xd7f61
movq %rdi, %r11
cmpq %r9, %rdi
jl 0xd7f6d
movq 0x8(%r13), %r11
movq $0x0, 0x8(%r11,%rdx,8)
xorpd %xmm0, %xmm0
jmp 0xd7f8d
xorpd %xmm0, %xmm0
movq %rax, %r11
cmpq %r9, %r10
jge 0xd7f73
movsd (%rcx,%r11,8), %xmm0
movq 0x8(%r13), %r11
movsd %xmm0, 0x8(%r11,%rdx,8)
xorpd %xmm0, %xmm0
cmpq %r9, %rdi
jge 0xd7f8d
movsd 0x8(%rcx,%rdx,8), %xmm0
xorpd %xmm1, %xmm1
cmpq %r9, %r10
jge 0xd7f9c
movsd 0x10(%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0xd7fad
xorpd %xmm0, %xmm0
cmpq %r9, %rdi
jl 0xd7fb9
jmp 0xd7fbe
xorpd %xmm0, %xmm0
movq %rax, %rdi
cmpq %r9, %r10
jge 0xd7fbe
movsd (%rcx,%rdi,8), %xmm0
movsd %xmm0, 0x8(%rbx,%rdx,8)
movsd 0x8(%rsi,%rdx,8), %xmm0
subsd 0x8(%rbx,%rdx,8), %xmm0
ucomisd (%r8), %xmm0
jbe 0xd7fdc
movsd %xmm0, (%r8)
incq %r12
incq %rdx
cmpq %r15, %rdx
jl 0xd7ec8
movq -0x58(%rbp), %rax
movl %edx, (%rax)
movq -0x50(%rbp), %r8
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %r10
movq -0x60(%rbp), %r11
movl $0x2, %esi
cmpq %r10, %r11
jne 0xd7e67
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq