// File: /workspace/github_cpp_makefile_star2_O3/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 0x1a54f9(%rip), %rbx    # 0x21b468
leaq 0x29777a(%rip), %rax    # 0x30d6f0
movl (%rax), %eax
orl (%rbx), %eax
jne 0x76107
leaq 0x297491(%rip), %r14    # 0x30d418
xorpd %xmm0, %xmm0
ucomisd (%r14), %xmm0
setp %al
setne %cl
orb %al, %cl
leaq 0x297575(%rip), %r15    # 0x30d514
movl (%r15), %edx
cmpl $0x7e2, %edx            # imm = 0x7E2
setge %al
andb %cl, %al
cmpb $0x1, %al
jne 0x75ff4
leaq 0x1a54a8(%rip), %rax    # 0x21b460
xorl %ecx, %ecx
movq %rcx, (%rax)
movq %rcx, (%r14)
leaq 0x297449(%rip), %rax    # 0x30d410
movl $0x0, (%rax)
leaq 0x297730(%rip), %rax    # 0x30d704
movl $0x4d2, (%rax)          # imm = 0x4D2
leaq 0x1c797f(%rip), %rdi    # 0x23d960
leaq 0xac146(%rip), %rsi     # 0x12212e
xorl %eax, %eax
callq 0x9060
jmp 0x760e2
decl %edx
cmpl $0x7e0, %edx            # imm = 0x7E0
ja 0x7607c
callq 0xa477
movl (%r15), %edi
leaq 0x297503(%rip), %rax    # 0x30d510
movl (%rax), %esi
leaq 0x2974f6(%rip), %rax    # 0x30d50c
movl (%rax), %edx
callq 0xd485e
movsd %xmm0, -0x20(%rbp)
leaq 0x2974df(%rip), %rax    # 0x30d508
movl (%rax), %edi
leaq 0x2974d2(%rip), %rax    # 0x30d504
movl (%rax), %esi
leaq 0x2974c5(%rip), %rax    # 0x30d500
movl (%rax), %edx
leaq 0x2974d4(%rip), %rax    # 0x30d518
movsd (%rax), %xmm0
callq 0xd492e
addsd -0x20(%rbp), %xmm0
leaq 0x297687(%rip), %rax    # 0x30d6e0
movsd (%rax), %xmm1
subsd %xmm0, %xmm1
divsd 0xa81e7(%rip), %xmm1    # 0x11e250
cvttsd2si %xmm1, %eax
cmpl $0x2, %eax
jl 0x76088
cvttpd2dq %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm0
jmp 0x76090
movl $0x0, 0x2f5c0a(%rip)    # 0x36bc90
jmp 0x76107
movsd 0x92fe0(%rip), %xmm0    # 0x109070
movsd %xmm0, 0x2fac60(%rip)   # 0x370cf8
movsd 0x95a80(%rip), %xmm1    # 0x10bb20
mulsd %xmm0, %xmm1
leaq 0x1a53b5(%rip), %rax    # 0x21b460
movsd %xmm1, (%rax)
ucomisd (%r14), %xmm1
jne 0x760b8
jnp 0x76107
movsd %xmm1, (%r14)
leaq 0x297640(%rip), %rax    # 0x30d704
movl $0x4d2, (%rax)          # imm = 0x4D2
movl (%r15), %edx
leaq 0x1c788c(%rip), %rdi    # 0x23d960
leaq 0xac08e(%rip), %rsi     # 0x122169
movb $0x1, %al
callq 0x9060
movl $0x1, %edi
callq 0xc36e2
leaq 0x29734d(%rip), %rax    # 0x30d440
cmpl $0x13, (%rax)
ja 0x76107
movl $0x7, %edi
movl $0x11f6, %esi           # imm = 0x11F6
callq 0x10bd1
leaq 0x1a5352(%rip), %rax    # 0x21b460
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x76123
jp 0x76123
cmpl $0x0, (%rbx)
je 0x7613a
callq 0x5c7ed
leaq 0x2972e1(%rip), %rax    # 0x30d410
cmpl $0x0, (%rax)
jne 0x7613a
movl $0x1, (%rax)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq