// File: /workspace/github_cpp_makefile_star2_O1/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 0x1a536c(%rip), %rbx    # 0x21b468
leaq 0x2975ed(%rip), %rax    # 0x30d6f0
movl (%rax), %eax
orl (%rbx), %eax
jne 0x76294
leaq 0x297304(%rip), %r14    # 0x30d418
xorpd %xmm0, %xmm0
ucomisd (%r14), %xmm0
setp %al
setne %cl
orb %al, %cl
leaq 0x2973e8(%rip), %r15    # 0x30d514
movl (%r15), %edx
cmpl $0x7e2, %edx            # imm = 0x7E2
setge %al
andb %cl, %al
cmpb $0x1, %al
jne 0x76181
leaq 0x1a531b(%rip), %rax    # 0x21b460
xorl %ecx, %ecx
movq %rcx, (%rax)
movq %rcx, (%r14)
leaq 0x2972bc(%rip), %rax    # 0x30d410
movl $0x0, (%rax)
leaq 0x2975a3(%rip), %rax    # 0x30d704
movl $0x4d2, (%rax)          # imm = 0x4D2
leaq 0x1c77f2(%rip), %rdi    # 0x23d960
leaq 0xabed9(%rip), %rsi     # 0x12204e
xorl %eax, %eax
callq 0x9060
jmp 0x7626f
decl %edx
cmpl $0x7e0, %edx            # imm = 0x7E0
ja 0x76209
callq 0xa495
movl (%r15), %edi
leaq 0x297376(%rip), %rax    # 0x30d510
movl (%rax), %esi
leaq 0x297369(%rip), %rax    # 0x30d50c
movl (%rax), %edx
callq 0xd4110
movsd %xmm0, -0x20(%rbp)
leaq 0x297352(%rip), %rax    # 0x30d508
movl (%rax), %edi
leaq 0x297345(%rip), %rax    # 0x30d504
movl (%rax), %esi
leaq 0x297338(%rip), %rax    # 0x30d500
movl (%rax), %edx
leaq 0x297347(%rip), %rax    # 0x30d518
movsd (%rax), %xmm0
callq 0xd41e0
addsd -0x20(%rbp), %xmm0
leaq 0x2974fa(%rip), %rax    # 0x30d6e0
movsd (%rax), %xmm1
subsd %xmm0, %xmm1
divsd 0xa7fb2(%rip), %xmm1    # 0x11e1a8
cvttsd2si %xmm1, %eax
cmpl $0x2, %eax
jl 0x76215
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
jmp 0x7621d
movl $0x0, 0x2f5a7d(%rip)    # 0x36bc90
jmp 0x76294
movsd 0x92e53(%rip), %xmm0    # 0x109070
movsd %xmm0, 0x2faad3(%rip)   # 0x370cf8
movsd 0x958f3(%rip), %xmm1    # 0x10bb20
mulsd %xmm0, %xmm1
leaq 0x1a5228(%rip), %rax    # 0x21b460
movsd %xmm1, (%rax)
ucomisd (%r14), %xmm1
jne 0x76245
jnp 0x76294
movsd %xmm1, (%r14)
leaq 0x2974b3(%rip), %rax    # 0x30d704
movl $0x4d2, (%rax)          # imm = 0x4D2
movl (%r15), %edx
leaq 0x1c76ff(%rip), %rdi    # 0x23d960
leaq 0xabe21(%rip), %rsi     # 0x122089
movb $0x1, %al
callq 0x9060
movl $0x1, %edi
callq 0xc3105
leaq 0x2971c0(%rip), %rax    # 0x30d440
cmpl $0x13, (%rax)
ja 0x76294
movl $0x7, %edi
movl $0x11f6, %esi           # imm = 0x11F6
callq 0x10c56
leaq 0x1a51c5(%rip), %rax    # 0x21b460
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x762b0
jp 0x762b0
cmpl $0x0, (%rbx)
je 0x762c7
callq 0x5c907
leaq 0x297154(%rip), %rax    # 0x30d410
cmpl $0x0, (%rax)
jne 0x762c7
movl $0x1, (%rax)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq