// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathgps.cpp
// Function: fix_rollover()
void fix_rollover()
{
double jd;

   // See if date from the receiver is likely incorrect due to week rollover
   // issues.  Attempt to fix it if it is wrong.

   if(user_set_rollover) ;    // user forced a rollover offset
   else if(fake_time_stamp) ; // faked time stamp (for Symmetricom UCCM-P)
   else if(auto_rollover && (user_set_rollover == 0) && (pri_year >= ROLLOVER_YEAR)) {  // receiver is now sending valid years
      rollover = 0.0;       // cancel the rollover fix
      auto_rollover = 0.0;
      rolled = 0;

      last_log_hours = 1234;  // force new log header write
      sprintf(log_text, "#! automatic week rollover compensation triggered. year:%d", pri_year);
      write_log_comment(1);

      if(1 && (roll_timing_msg < (ROLL_THRESH+5))) {  // reset the plot queue if rollover detection occured at Heather startup
         reset_queues(RESET_ALL_QUEUES, 4598);
      }
   }
   else if(ROLL_FILT && (time_flags & TFLAGS_NO_UTC_OFS)) {  // GPS time is not valid
      roll_filter = 0;
   }
   else if(ROLL_FILT && !have_utc_ofs) {              // no UTC offset seen yet
      roll_filter = 0;
   }
   else if(ROLL_FILT && (pri_year < 1980)) {            // should never happen
      roll_filter = 0;
   }
   else if(0 && (gps_week == 0) && ((pri_year == 1980) || (pri_year == 1999))) {  // years of first two rollover events
      get_clock_time();       // ... probably a receiver in startup mode
      pri_year = clk_year;    // ... so substitute system clock date/time
      pri_month = clk_month;
      pri_day = clk_day;
      pri_hours = clk_hours;
      pri_minutes = clk_minutes;
      pri_seconds = clk_seconds;
      pri_frac = raw_frac = clk_frac;
   }
   else if((pri_year > 0) && (pri_year < ROLLOVER_YEAR)) {   // GPS year looks bogus
      if(ROLL_FILT && (roll_filter < ROLL_THRESH)) {    // we need a few consecutive bogo years
         ++roll_filter;
      }
      else {
         get_clock_time();  // figure out how many times we rolled over from the system clock
         jd = jdate(pri_year,pri_month,pri_day) + jtime(pri_hours,pri_minutes,pri_seconds, pri_frac);
         rolls = (clk_jd -jd) / (19.0*365.0);  // 19.0 is (1024/52) rounded down (to allow for some system clock error)
//sprintf(debug_text3, "clkjd:%.9f rjd:%.9f  diff:%.9f rolls:%f", clk_jd, jd, clk_jd-jd, rolls);
         rolls = (double) (int) rolls;
         if(rolls <= 1.0) rolls = 1.0;
         rollover = rolls * (1024.0 * 7.0 * 24.0*60.0*60.0);  // rollover one 1024 week epoch
         if(rollover != auto_rollover) {  // new rollover value
            auto_rollover = rollover;

            last_log_hours = 1234;  // force log header write
            sprintf(log_text, "#! automatic week rollover compensation triggered. year:%d  rolls:%g", pri_year, rolls);
            write_log_comment(1);

            if(1 && (roll_timing_msg < (ROLL_THRESH+5))) {  // reset the plot queue if rollover detection occured at Heather startup
               reset_queues(RESET_ALL_QUEUES, 4598);
            }
//          setup_calendars(10);
         }
      }
   }
   else roll_filter = 0;

   if(rollover || user_set_rollover) {  // adjust GPS receiver time by xxx seconds
// sprintf(debug_text, "rover:%f  rolled:%d  pyear:%d", rollover, user_set_rollover, pri_year);
      adjust_rcvr_time(rollover);
      if(rolled == 0) rolled = 1;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
leaq 0x199786(%rip), %rbx    # 0x205468
leaq 0x28ba07(%rip), %rax    # 0x2f76f0
movl (%rax), %eax
orl (%rbx), %eax
jne 0x6be70
leaq 0x28b71e(%rip), %r14    # 0x2f7418
xorpd %xmm0, %xmm0
ucomisd (%r14), %xmm0
setp %al
setne %cl
orb %al, %cl
leaq 0x28b802(%rip), %r15    # 0x2f7514
movl (%r15), %edx
cmpl $0x7e2, %edx            # imm = 0x7E2
setge %al
andb %cl, %al
cmpb $0x1, %al
jne 0x6bd64
leaq 0x199735(%rip), %rax    # 0x205460
andq $0x0, (%rax)
andq $0x0, (%r14)
leaq 0x28b6d6(%rip), %rax    # 0x2f7410
andl $0x0, (%rax)
leaq 0x28b9c0(%rip), %rax    # 0x2f7704
movl $0x4d2, (%rax)          # imm = 0x4D2
leaq 0x1bbc0f(%rip), %rdi    # 0x227960
leaq 0xa031e(%rip), %rsi     # 0x10c076
xorl %eax, %eax
callq 0x9060
jmp 0x6be4f
decl %edx
cmpl $0x7e0, %edx            # imm = 0x7E0
ja 0x6bdec
callq 0xa386
movl (%r15), %edi
leaq 0x28b793(%rip), %rax    # 0x2f7510
movl (%rax), %esi
leaq 0x28b786(%rip), %rax    # 0x2f750c
movl (%rax), %edx
callq 0xc2f07
movsd %xmm0, -0x20(%rbp)
leaq 0x28b76f(%rip), %rax    # 0x2f7508
movl (%rax), %edi
leaq 0x28b762(%rip), %rax    # 0x2f7504
movl (%rax), %esi
leaq 0x28b755(%rip), %rax    # 0x2f7500
movl (%rax), %edx
leaq 0x28b764(%rip), %rax    # 0x2f7518
movsd (%rax), %xmm0
callq 0xc2faf
addsd -0x20(%rbp), %xmm0
leaq 0x28b917(%rip), %rax    # 0x2f76e0
movsd (%rax), %xmm1
subsd %xmm0, %xmm1
divsd 0x9c4ef(%rip), %xmm1    # 0x1082c8
cvttsd2si %xmm1, %eax
cmpl $0x2, %eax
jl 0x6bdf5
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
jmp 0x6bdfd
andl $0x0, 0x2e9e9d(%rip)    # 0x355c90
jmp 0x6be70
movsd 0x87273(%rip), %xmm0    # 0xf3070
movsd %xmm0, 0x2eeef3(%rip)   # 0x35acf8
movsd 0x89d8b(%rip), %xmm1    # 0xf5b98
mulsd %xmm0, %xmm1
leaq 0x199648(%rip), %rax    # 0x205460
movsd %xmm1, (%rax)
ucomisd (%r14), %xmm1
jne 0x6be25
jnp 0x6be70
movsd %xmm1, (%r14)
leaq 0x28b8d3(%rip), %rax    # 0x2f7704
movl $0x4d2, (%rax)          # imm = 0x4D2
movl (%r15), %edx
leaq 0x1bbb1f(%rip), %rdi    # 0x227960
leaq 0xa0269(%rip), %rsi     # 0x10c0b1
movb $0x1, %al
callq 0x9060
pushq $0x1
popq %rdi
callq 0xb25f5
leaq 0x28b5e2(%rip), %rax    # 0x2f7440
cmpl $0x13, (%rax)
ja 0x6be70
pushq $0x7
popq %rdi
movl $0x11f6, %esi           # imm = 0x11F6
callq 0x104b9
leaq 0x1995e9(%rip), %rax    # 0x205460
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x6be8c
jp 0x6be8c
cmpl $0x0, (%rbx)
je 0x6bea3
callq 0x55462
leaq 0x28b578(%rip), %rax    # 0x2f7410
cmpl $0x0, (%rax)
jne 0x6bea3
movl $0x1, (%rax)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq