// File: jkingan[P]ladyheather/heather.cpp
// Function: remove_trend(int, int, double, double)
DATA_SIZE remove_trend(int id, int phase_flag, double jd, DATA_SIZE val)
{
double jd0;
DATA_SIZE r;
double a0, a1;

   // remove linear regression trend line value from reading

   if(id <= 0) return val;
   if(id >= (NUM_PLOTS+DERIVED_PLOTS)) return val;

   if(plot[id].show_deriv) return val;       // plot derivative enabled
   if(plot[id].show_freq) return val;        // plot frequency enabled
   if(plot[id].drift_rate == 0) return val;  // trend removal not active for the plot

   if(phase_flag && phase_plot(id)) {  // it's a TICC_RCVR phase plot
      if(id == ONE) {
         jd0 = jd - pps_adev_jd0;    // time of first queue entry
         jd0 *= (24.0*60.0*60.0);
         r = trend_value(PPS, jd0);
//r -= resid[PPS].a0;    // for better match with TimeLab zero-based values
         val = val - r;
      }
      else if(id == TWO) {
         jd0 = jd - osc_adev_jd0;
         jd0 *= (24.0*60.0*60.0);
         r = trend_value(OSC, jd0);
//r -= resid[OSC].a0;
         val = val - r;
      }
      else if(id == THREE) {
         jd0 = jd - chc_adev_jd0;
         jd0 *= (24.0*60.0*60.0);
         r = trend_value(CHC, jd0);
//r -= resid[CHC].a0;
         val = val - r;
      }
      else if(id == FOUR) {
         jd0 = jd - chd_adev_jd0;
         jd0 *= (24.0*60.0*60.0);
         r = trend_value(CHD, jd0);
//r -= resid[CHD].a0;
         val = val - r;
      }
   }
   else {
      if(dynamic_trend_line) {   // dynamic trend removal
         a1 = plot[id].a1;
         a0 = plot[id].a0;
      }
      else {                     // static trend removal
         a1 = plot[id].drift_rate;
         a0 = 0.0;
      }
      val -= (DATA_SIZE) (a0 + (a1 * jd));
   }

   return val;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movsd %xmm1, -0x20(%rbp)
cmpl $0x0, -0xc(%rbp)
jg 0x1987d
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x19b2e
cmpl $0x14, -0xc(%rbp)
jl 0x19892
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x19b2e
movslq -0xc(%rbp), %rcx
leaq 0x1a3483(%rip), %rax    # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0xcc(%rax)
je 0x198bf
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x19b2e
movslq -0xc(%rbp), %rcx
leaq 0x1a3456(%rip), %rax    # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0xd0(%rax)
je 0x198ec
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x19b2e
movslq -0xc(%rbp), %rcx
leaq 0x1a3429(%rip), %rax    # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movsd 0xa0(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x19923
jp 0x19923
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x19b2e
cmpl $0x0, -0x10(%rbp)
je 0x19a86
movl -0xc(%rbp), %edi
callq 0x19800
cmpl $0x0, %eax
je 0x19a86
cmpl $0x4, -0xc(%rbp)
jne 0x1998d
movsd -0x18(%rbp), %xmm0
subsd 0x282527(%rip), %xmm0   # 0x29be78
movsd %xmm0, -0x28(%rbp)
movsd 0x1556aa(%rip), %xmm0   # 0x16f008
mulsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
xorl %edi, %edi
callq 0x19500
movsd %xmm0, -0x30(%rbp)
movsd -0x20(%rbp), %xmm0
subsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0x19a81
cmpl $0x5, -0xc(%rbp)
jne 0x199df
movsd -0x18(%rbp), %xmm0
subsd 0x2824e0(%rip), %xmm0   # 0x29be80
movsd %xmm0, -0x28(%rbp)
movsd 0x15565b(%rip), %xmm0   # 0x16f008
mulsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
movl $0x1, %edi
callq 0x19500
movsd %xmm0, -0x30(%rbp)
movsd -0x20(%rbp), %xmm0
subsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0x19a7f
cmpl $0x6, -0xc(%rbp)
jne 0x19a2e
movsd -0x18(%rbp), %xmm0
subsd 0x282496(%rip), %xmm0   # 0x29be88
movsd %xmm0, -0x28(%rbp)
movsd 0x155609(%rip), %xmm0   # 0x16f008
mulsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
movl $0xa, %edi
callq 0x19500
movsd %xmm0, -0x30(%rbp)
movsd -0x20(%rbp), %xmm0
subsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0x19a7d
cmpl $0x7, -0xc(%rbp)
jne 0x19a7b
movsd -0x18(%rbp), %xmm0
subsd 0x28244f(%rip), %xmm0   # 0x29be90
movsd %xmm0, -0x28(%rbp)
movsd 0x1555ba(%rip), %xmm0   # 0x16f008
mulsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
movl $0xb, %edi
callq 0x19500
movsd %xmm0, -0x30(%rbp)
movsd -0x20(%rbp), %xmm0
subsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0x19a7d
jmp 0x19a7f
jmp 0x19a81
jmp 0x19b24
cmpl $0x0, 0x3854bf(%rip)    # 0x39ef4c
je 0x19ad5
movslq -0xc(%rbp), %rcx
leaq 0x1a3286(%rip), %rax    # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movsd 0x98(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movslq -0xc(%rbp), %rcx
leaq 0x1a3264(%rip), %rax    # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movsd 0x90(%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x19aff
movslq -0xc(%rbp), %rcx
leaq 0x1a3240(%rip), %rax    # 0x1bcd20
imulq $0xe8, %rcx, %rcx
addq %rcx, %rax
movsd 0xa0(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movsd -0x18(%rbp), %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x20(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)