// File: /workspace/github_cpp_makefile_star2_O3/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 0x13df9
movl %edi, %eax
imulq $0xe8, %rax, %rcx
leaq 0x14a042(%rip), %rax    # 0x15dd30
cmpl $0x0, 0xcc(%rcx,%rax)
jne 0x13df9
addq %rcx, %rax
cmpl $0x0, 0xd0(%rax)
jne 0x13df9
movsd 0xa0(%rax), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0x13d24
jnp 0x13df9
testl %esi, %esi
je 0x13d7c
movl %edi, %ecx
andl $0x1c, %ecx
cmpl $0x4, %ecx
jne 0x13d7c
cmpq $0x10000, 0x204b93(%rip) # imm = 0x10000
jne 0x13d7c
addl $-0x4, %edi
cmpl $0x3, %edi
ja 0x13df9
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movsd %xmm3, -0x8(%rbp)
leaq 0xf56e5(%rip), %rax     # 0x109444
movslq (%rax,%rdi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
subsd 0x228f28(%rip), %xmm0   # 0x23cc98
mulsd 0xf52a0(%rip), %xmm0    # 0x109018
xorl %edi, %edi
jmp 0x13de6
cmpl $0x0, 0x32bfe9(%rip)    # 0x33fd6c
je 0x13d95
movsd 0x90(%rax), %xmm2
movsd 0x98(%rax), %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
subsd %xmm1, %xmm3
jmp 0x13df9
subsd 0x228efd(%rip), %xmm0   # 0x23cca8
mulsd 0xf5265(%rip), %xmm0    # 0x109018
movl $0xa, %edi
jmp 0x13de6
subsd 0x228eee(%rip), %xmm0   # 0x23ccb0
mulsd 0xf524e(%rip), %xmm0    # 0x109018
movl $0xb, %edi
jmp 0x13de6
subsd 0x228ec7(%rip), %xmm0   # 0x23cca0
mulsd 0xf5237(%rip), %xmm0    # 0x109018
movl $0x1, %edi
callq 0x13bb4
movsd -0x8(%rbp), %xmm3
subsd %xmm0, %xmm3
addq $0x10, %rsp
popq %rbp
movapd %xmm3, %xmm0
retq