// File: /workspace/github_cpp_makefile_star2_O2/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:
movapd %xmm1, %xmm3
leal -0x14(%rdi), %eax
cmpl $-0x13, %eax
jb 0x1351f
movl %edi, %eax
imulq $0xe8, %rax, %rcx
leaq 0x134914(%rip), %rax    # 0x147d30
cmpl $0x0, 0xcc(%rcx,%rax)
jne 0x1351f
addq %rcx, %rax
cmpl $0x0, 0xd0(%rax)
jne 0x1351f
movsd 0xa0(%rax), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0x13452
jnp 0x1351f
testl %esi, %esi
je 0x134aa
movl %edi, %ecx
andl $0x1c, %ecx
cmpl $0x4, %ecx
jne 0x134aa
cmpq $0x10000, 0x1ef465(%rip) # imm = 0x10000
jne 0x134aa
addl $-0x4, %edi
cmpl $0x3, %edi
ja 0x1351f
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movsd %xmm3, -0x8(%rbp)
leaq 0xdffb7(%rip), %rax     # 0xf3444
movslq (%rax,%rdi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
subsd 0x2137fa(%rip), %xmm0   # 0x226c98
mulsd 0xdfb72(%rip), %xmm0    # 0xf3018
xorl %edi, %edi
jmp 0x1350c
cmpl $0x0, 0x3168bb(%rip)    # 0x329d6c
je 0x134c3
movsd 0x90(%rax), %xmm2
movsd 0x98(%rax), %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
subsd %xmm1, %xmm3
jmp 0x1351f
subsd 0x2137cf(%rip), %xmm0   # 0x226ca8
mulsd 0xdfb37(%rip), %xmm0    # 0xf3018
pushq $0xa
jmp 0x1350b
subsd 0x2137c3(%rip), %xmm0   # 0x226cb0
mulsd 0xdfb23(%rip), %xmm0    # 0xf3018
pushq $0xb
jmp 0x1350b
subsd 0x21379f(%rip), %xmm0   # 0x226ca0
mulsd 0xdfb0f(%rip), %xmm0    # 0xf3018
pushq $0x1
popq %rdi
callq 0x132ec
movsd -0x8(%rbp), %xmm3
subsd %xmm0, %xmm3
addq $0x10, %rsp
popq %rbp
movapd %xmm3, %xmm0
retq