// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathmsc.cpp
// Function: calc_sunrise(double, int)
int calc_sunrise(double delta, int why)
{
double zenith;
int hh;
double jd, jd0;
double start, mid, end;
double start_el, mid_el, end_el;
double old_az, old_el;
double old_maz, old_mel;
#define SUN_RES 5 //was 14 - 5 approximation steps gets us within 2 minutes, then we interpolate

   // A more accurate sunrise / sunset calculator than the old one...
   //
   // Sunrise and sunset times are calculated by finding the hours that the
   // sun elevation crosses the desired horizon angles.  Then a 
   // successive-approximation binary seach of those hours is used to
   // refine the times to 2 second levels. The we interpolate to get to 100 msec.
   //
   // Solar noon is first appoximated as midway between sunrise and sunset.
   // Then the time interval +/- 600 seconds from that point is checked for
   // being the peak solar elevation.  If no peak is found the mid-point value
   // between sunrise and sunset is used.

sp_count = 0;
if(debug_file) fprintf(debug_file, "#calc_sunrise(%d)\n", why);

   old_az = sun_az;
   old_el = sun_el;
   old_maz = moon_az;
   old_mel = moon_el;

   have_sun_times = 0x00;
   rise_hh = rise_mm = rise_ss = 0;         
   rise_frac = 0.0;
   set_hh = set_mm =  set_ss = 0;
   set_frac = 0.0;
   noon_hh = noon_mm = noon_ss = 0;         
   noon_frac = 0.0;

   if((sunrise_type == 0) || (lat == 0.0) || (lon == 0.0)) {  // assume GPS not tracking yet
      goto no_sun;
   }

   zenith = sunrise_horizon;

   gregorian(0, jd_local);
   jd0 = start = jdate(g_year,g_month,g_day) + delta;

   jd = jd0; // + jtime(5,0,0,0.0);  // start sunrise search at 5:00 in the morning local time
   start_el = sun_posn(jd, do_moonrise);

   for(hh=1; hh<25; hh++) {  // find the hour of the rising zenith crossing
      end = jd + jtime((hh%25),0,0,0.0);
      end_el = sun_posn(end, do_moonrise);
if(0 && debug_file) fprintf(debug_file, "rise hr:%d  spc:%d  s_el:%f  e_el:%f\n", hh, sp_count, start_el,end_el);
      if((start_el <= zenith) && (end_el >= zenith)) {
         goto got_rise_hh;
      }
      start_el = end_el;
      start = end;
   }
   start = jd0;
   goto no_rise;

   got_rise_hh:
   for(hh=0; hh<SUN_RES; hh++) {  // do a binary search for the rising zenith crossing
      mid = (start + end) / 2.0;
      mid_el = sun_posn(mid, do_moonrise);
      if(mid_el <= zenith) start = mid;
      else if(mid_el >= zenith) end = mid;
if(0 && debug_file) fprintf(debug_file, "refine rise %d:  spc:%d  rise_el:%f  diff:%f\n", hh, sp_count, mid_el, ((end-start) * 24.0*60.0*60.0));
      if(((end-start) * 24.0*60.0*60.0) < 1.0) break;
   }
   sunrise_time = interp_sun_posn(start,end, zenith, do_moonrise);
   sunrise_el = sun_posn(sunrise_time, do_moonrise);
   sunrise_az = sun_az;

   gregorian(0, sunrise_time + time_zone() + jtime(0,0,0,0.50));
   rise_hh = g_hours;
   rise_mm = g_minutes;
   rise_ss = g_seconds;
   rise_frac = g_frac;
   have_sun_times |= 0x01;  // we have a sun rise

   // now calculate the sunset time
   if(0 && do_moonrise) start = jd0; // moon can set before it rises!
   else start = sunrise_time;   // jd0;

   no_rise:
   zenith = sunset_horizon;
   start_el = sun_posn(start, do_moonrise);
   jd = start;

   for(hh=1; hh<25; hh++) {  // find the hour of the setting zenith crossing
      end = jd + jtime(hh%25,0,0,0.0);
      end_el = sun_posn(end, do_moonrise);
      if((start_el >= zenith) && (end_el <= zenith)) {
         goto got_set_hh;
      }
if(0 && debug_file) fprintf(debug_file, "set hr:%d  spc:%d  s_el:%f  e_el:%f\n", hh, sp_count, start_el,end_el);
      start_el = end_el;
      start = end;
   }
   goto no_sun;   // sun never sets, we cant calculate a sun rise

   got_set_hh:
   for(hh=0; hh<SUN_RES; hh++) {  // do a binary search for the setting zenith crossing
      mid = (start + end) / 2.0;
      mid_el = sun_posn(mid, do_moonrise);
      if(mid_el >= zenith) start = mid;
      else if(mid_el <= zenith) end = mid;
if(0 && debug_file) fprintf(debug_file, "refine set %d:  spc:%d  set_el:%f  diff:%f\n", hh, sp_count, mid_el, ((end-start) * 24.0*60.0*60.0));
      if(((end-start) * 24.0*60.0*60.0) < 1.0) break;
   }
   sunset_time = interp_sun_posn(start,end, zenith, do_moonrise);
   sunset_el = sun_posn(sunset_time, do_moonrise);
   sunset_az = sun_az;

   gregorian(0, sunset_time + time_zone() + jtime(0,0,0,0.50));
   set_hh = g_hours;
   set_mm = g_minutes;
   set_ss = g_seconds;
   set_frac = g_frac;
   have_sun_times |= 0x04;

   solar_noon = (sunrise_time + sunset_time) / 2.0;
   if(sunset_time < sunrise_time) {
      solar_noon += 0.50;
   }
   refine_solar_noon(do_moonrise); // a better succesive-approximation refinement over a longer interval

   noon_el = sun_posn(solar_noon, do_moonrise);
   noon_az = sun_az;

   gregorian(0, solar_noon + time_zone() + jtime(0,0,0,0.50));
   noon_hh = g_hours;
   noon_mm = g_minutes;
   noon_ss = g_seconds;
   noon_frac = g_frac;
   if((have_sun_times & 0x01) && (have_sun_times & 0x04)) {  // we have a rise and set
      have_sun_times |= 0x02;
   }

   if(STATUS_RCVR) { // make sure we can trigger alarms during status message
      if((rise_ss >= SCPI_STATUS_SECOND) && (rise_ss <= SCPI_STATUS_SECOND+5)) {
         rise_ss = SCPI_STATUS_SECOND-1;
      }
      if((set_ss >= SCPI_STATUS_SECOND) && (set_ss <= SCPI_STATUS_SECOND+5)) {
         set_ss = SCPI_STATUS_SECOND-1;
      }
   }

   no_sun:
   if(0 && !have_local_time) have_sun_times = 0;  // we don't need to do this anymore since we use jd_local to calc sunrise/sunset
   if(!do_moonrise) log_sun_times();

   sun_az = old_az;
   sun_el = old_el;
   moon_az = old_maz;
   moon_el = old_mel;
   return have_sun_times;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edi, %edx
movsd %xmm0, -0x38(%rbp)
movl $0x0, 0x2913ad(%rip)    # 0x37a5b4
leaq 0x155182(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0xe9224
leaq 0x4b9e3(%rip), %rsi     # 0x134c00
xorl %eax, %eax
callq 0x9560
leaq 0x2245a5(%rip), %rbx    # 0x30d7d0
movsd (%rbx), %xmm0
movsd %xmm0, -0x70(%rbp)
leaq 0x22457d(%rip), %rdx    # 0x30d7b8
movsd (%rdx), %xmm0
movsd %xmm0, -0x68(%rbp)
leaq 0x22459d(%rip), %rsi    # 0x30d7e8
movsd (%rsi), %xmm0
movsd %xmm0, -0x60(%rbp)
leaq 0x224595(%rip), %rdi    # 0x30d7f0
movsd (%rdi), %xmm0
movsd %xmm0, -0x58(%rbp)
leaq 0x2243dd(%rip), %rcx    # 0x30d648
xorl %eax, %eax
movl %eax, (%rcx)
leaq 0x224392(%rip), %rcx    # 0x30d608
movl %eax, (%rcx)
leaq 0x224385(%rip), %r12    # 0x30d604
movl %eax, (%r12)
leaq 0x224376(%rip), %r13    # 0x30d600
movl %eax, (%r13)
leaq 0x22437b(%rip), %r15    # 0x30d610
movq %rax, (%r15)
leaq 0x224399(%rip), %rcx    # 0x30d638
movl %eax, (%rcx)
leaq 0x22438c(%rip), %rcx    # 0x30d634
movl %eax, (%rcx)
leaq 0x22437f(%rip), %rcx    # 0x30d630
movl %eax, (%rcx)
leaq 0x224386(%rip), %rcx    # 0x30d640
movq %rax, (%rcx)
leaq 0x2243ac(%rip), %rcx    # 0x30d670
movl %eax, (%rcx)
leaq 0x22439f(%rip), %rcx    # 0x30d66c
movl %eax, (%rcx)
leaq 0x224392(%rip), %rcx    # 0x30d668
movl %eax, (%rcx)
leaq 0x224399(%rip), %rcx    # 0x30d678
movq %rax, (%rcx)
leaq 0x2242df(%rip), %rcx    # 0x30d5c8
cmpq %rax, (%rcx)
je 0xe9933
leaq 0x223cd7(%rip), %rcx    # 0x30cfd0
movsd (%rcx), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0xe930d
jnp 0xe9933
leaq 0x223cc4(%rip), %rcx    # 0x30cfd8
movsd (%rcx), %xmm1
ucomisd %xmm0, %xmm1
jne 0xe9324
jnp 0xe9933
leaq 0x2242a5(%rip), %rax    # 0x30d5d0
movsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
leaq 0x22420d(%rip), %rax    # 0x30d548
movsd (%rax), %xmm0
xorl %edi, %edi
callq 0xd12da
leaq 0x224357(%rip), %rax    # 0x30d6a4
movl (%rax), %edi
leaq 0x224346(%rip), %rax    # 0x30d69c
movl (%rax), %esi
leaq 0x224341(%rip), %rax    # 0x30d6a0
movl (%rax), %edx
callq 0xd485e
addsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
leaq 0x224309(%rip), %r14    # 0x30d680
movl (%r14), %edi
callq 0xe823e
movsd %xmm0, -0x30(%rbp)
movsd 0x354f4(%rip), %xmm0    # 0x11e880
addsd -0x40(%rbp), %xmm0
movl (%r14), %edi
movsd %xmm0, -0x38(%rbp)
callq 0xe823e
movsd -0x48(%rbp), %xmm1
movapd %xmm0, %xmm2
ucomisd -0x30(%rbp), %xmm1
jb 0xe93c0
ucomisd %xmm1, %xmm2
jb 0xe93c0
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0xe941a
movl $0x2, %ebx
movsd -0x38(%rbp), %xmm0
cmpl $0x19, %ebx
je 0xe9574
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
divsd 0x1fc59(%rip), %xmm0    # 0x109040
addsd -0x40(%rbp), %xmm0
movl (%r14), %edi
movsd %xmm0, -0x38(%rbp)
movsd %xmm2, -0x50(%rbp)
callq 0xe823e
movsd -0x48(%rbp), %xmm1
incl %ebx
ucomisd -0x50(%rbp), %xmm1
movapd %xmm0, %xmm2
jb 0xe93c5
ucomisd %xmm1, %xmm0
movapd %xmm0, %xmm2
jb 0xe93c5
movl $0x4, %ebx
movsd -0x38(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
movsd %xmm1, -0x38(%rbp)
movsd %xmm0, -0x30(%rbp)
addsd %xmm0, %xmm1
mulsd 0x1fc29(%rip), %xmm1    # 0x109068
movl (%r14), %edi
movsd %xmm1, -0x40(%rbp)
movapd %xmm1, %xmm0
callq 0xe823e
movsd -0x48(%rbp), %xmm2
ucomisd %xmm0, %xmm2
jae 0xe9472
movsd -0x40(%rbp), %xmm1
ucomisd %xmm2, %xmm0
jae 0xe946b
movsd -0x38(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
jmp 0xe947c
movsd -0x40(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
mulsd 0x1fbb4(%rip), %xmm3    # 0x109040
movsd 0x1fb74(%rip), %xmm4    # 0x109008
mulsd %xmm4, %xmm3
mulsd %xmm4, %xmm3
subl $0x1, %ebx
setb %al
movsd 0x1fbc6(%rip), %xmm4    # 0x109070
ucomisd %xmm3, %xmm4
ja 0xe94b8
testb %al, %al
je 0xe9429
movl (%r14), %edi
callq 0xe8c98
leaq 0x224131(%rip), %rbx    # 0x30d5f8
movsd %xmm0, (%rbx)
movl (%r14), %edi
callq 0xe823e
leaq 0x224116(%rip), %rax    # 0x30d5f0
movsd %xmm0, (%rax)
leaq 0x2242eb(%rip), %rax    # 0x30d7d0
movsd (%rax), %xmm0
leaq 0x2240f8(%rip), %rax    # 0x30d5e8
movsd %xmm0, (%rax)
leaq 0x224385(%rip), %rax    # 0x30d880
leaq 0x22439e(%rip), %rcx    # 0x30d8a0
xorps %xmm0, %xmm0
cvtsi2sdl (%rcx), %xmm0
divsd 0x1fb2f(%rip), %xmm0    # 0x109040
addsd (%rax), %xmm0
addsd (%rbx), %xmm0
addsd 0x42b47(%rip), %xmm0    # 0x12c068
xorl %edi, %edi
callq 0xd12da
leaq 0x224179(%rip), %rax    # 0x30d6a8
movl (%rax), %eax
movl %eax, (%r13)
leaq 0x224170(%rip), %rax    # 0x30d6ac
movl (%rax), %eax
movl %eax, (%r12)
leaq 0x224167(%rip), %rax    # 0x30d6b0
movl (%rax), %eax
leaq 0x2240b6(%rip), %rcx    # 0x30d608
movl %eax, (%rcx)
leaq 0x22415d(%rip), %rax    # 0x30d6b8
movsd (%rax), %xmm0
movsd %xmm0, (%r15)
leaq 0x2240dd(%rip), %rax    # 0x30d648
orb $0x1, (%rax)
movsd (%rbx), %xmm0
jmp 0xe9579
movsd -0x40(%rbp), %xmm0
leaq 0x224058(%rip), %rax    # 0x30d5d8
movsd (%rax), %xmm1
movsd %xmm1, -0x48(%rbp)
movl (%r14), %edi
movsd %xmm0, -0x40(%rbp)
callq 0xe823e
movsd %xmm0, -0x30(%rbp)
movsd 0x352dd(%rip), %xmm0    # 0x11e880
addsd -0x40(%rbp), %xmm0
movl (%r14), %edi
movsd %xmm0, -0x38(%rbp)
callq 0xe823e
movsd -0x48(%rbp), %xmm1
movapd %xmm0, %xmm2
movsd -0x30(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0xe95db
ucomisd %xmm2, %xmm1
jb 0xe95db
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0xe9639
movl $0x2, %ebx
movsd -0x38(%rbp), %xmm0
cmpl $0x19, %ebx
je 0xe990e
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
divsd 0x1fa3e(%rip), %xmm0    # 0x109040
addsd -0x40(%rbp), %xmm0
movl (%r14), %edi
movsd %xmm0, -0x38(%rbp)
movsd %xmm2, -0x50(%rbp)
callq 0xe823e
movsd -0x48(%rbp), %xmm1
incl %ebx
movsd -0x50(%rbp), %xmm2
ucomisd %xmm1, %xmm2
movapd %xmm0, %xmm2
jb 0xe95e0
ucomisd %xmm0, %xmm1
movapd %xmm0, %xmm2
jb 0xe95e0
movl $0x4, %ebx
movsd -0x38(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
movsd %xmm1, -0x38(%rbp)
movsd %xmm0, -0x30(%rbp)
addsd %xmm0, %xmm1
movsd 0x1fa0a(%rip), %xmm0    # 0x109068
mulsd %xmm0, %xmm1
movl (%r14), %edi
movsd %xmm1, -0x40(%rbp)
movapd %xmm1, %xmm0
callq 0xe823e
movsd -0x48(%rbp), %xmm2
ucomisd %xmm2, %xmm0
jae 0xe9695
movsd -0x40(%rbp), %xmm1
ucomisd %xmm0, %xmm2
jae 0xe968e
movsd -0x38(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
jmp 0xe969f
movsd -0x40(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
mulsd 0x1f991(%rip), %xmm3    # 0x109040
movsd 0x1f951(%rip), %xmm4    # 0x109008
mulsd %xmm4, %xmm3
mulsd %xmm4, %xmm3
subl $0x1, %ebx
setb %al
movsd 0x1f9a3(%rip), %xmm4    # 0x109070
ucomisd %xmm3, %xmm4
ja 0xe96db
testb %al, %al
je 0xe9648
movl (%r14), %edi
callq 0xe8c98
leaq 0x223f3e(%rip), %r13    # 0x30d628
movsd %xmm0, (%r13)
movl (%r14), %edi
callq 0xe823e
leaq 0x223f21(%rip), %rax    # 0x30d620
movsd %xmm0, (%rax)
leaq 0x2240c6(%rip), %rbx    # 0x30d7d0
movsd (%rbx), %xmm0
leaq 0x223f03(%rip), %rax    # 0x30d618
movsd %xmm0, (%rax)
leaq 0x224180(%rip), %rax    # 0x30d8a0
xorps %xmm0, %xmm0
cvtsi2sdl (%rax), %xmm0
leaq 0x224152(%rip), %rax    # 0x30d880
movsd 0x1f90a(%rip), %xmm1    # 0x109040
divsd %xmm1, %xmm0
addsd (%rax), %xmm0
addsd (%r13), %xmm0
addsd 0x4291c(%rip), %xmm0    # 0x12c068
xorl %edi, %edi
callq 0xd12da
leaq 0x223f4e(%rip), %rax    # 0x30d6a8
movl (%rax), %eax
leaq 0x223ecd(%rip), %rcx    # 0x30d630
movl %eax, (%rcx)
leaq 0x223f40(%rip), %rax    # 0x30d6ac
movl (%rax), %eax
leaq 0x223ebf(%rip), %rcx    # 0x30d634
movl %eax, (%rcx)
leaq 0x223f32(%rip), %rax    # 0x30d6b0
movl (%rax), %eax
leaq 0x223eb1(%rip), %rcx    # 0x30d638
movl %eax, (%rcx)
leaq 0x223f28(%rip), %r12    # 0x30d6b8
movsd (%r12), %xmm0
leaq 0x223eab(%rip), %r15    # 0x30d648
orb $0x4, (%r15)
leaq 0x223e98(%rip), %rax    # 0x30d640
movsd %xmm0, (%rax)
leaq 0x223e45(%rip), %rax    # 0x30d5f8
movsd (%rax), %xmm0
movsd (%r13), %xmm1
movapd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd 0x1f89b(%rip), %xmm3    # 0x109068
mulsd %xmm3, %xmm2
addsd %xmm2, %xmm3
cmpltsd %xmm0, %xmm1
movapd %xmm1, %xmm0
andnpd %xmm2, %xmm0
andpd %xmm3, %xmm1
orpd %xmm0, %xmm1
leaq 0x223e6f(%rip), %r13    # 0x30d660
movlpd %xmm1, (%r13)
movl (%r14), %edi
callq 0xe8a5f
movsd (%r13), %xmm0
movl (%r14), %edi
callq 0xe823e
leaq 0x223e44(%rip), %rax    # 0x30d658
movsd %xmm0, (%rax)
leaq 0x224081(%rip), %rax    # 0x30d8a0
xorps %xmm0, %xmm0
cvtsi2sdl (%rax), %xmm0
divsd 0x1f812(%rip), %xmm0    # 0x109040
movsd (%rbx), %xmm1
leaq 0x223e17(%rip), %rax    # 0x30d650
leaq 0x224040(%rip), %rcx    # 0x30d880
addsd (%rcx), %xmm0
movsd %xmm1, (%rax)
addsd (%r13), %xmm0
addsd 0x42812(%rip), %xmm0    # 0x12c068
xorl %edi, %edi
callq 0xd12da
leaq 0x223e44(%rip), %rax    # 0x30d6a8
movl (%rax), %eax
leaq 0x223dfb(%rip), %rcx    # 0x30d668
movl %eax, (%rcx)
leaq 0x223e36(%rip), %rax    # 0x30d6ac
movl (%rax), %eax
leaq 0x223ded(%rip), %rcx    # 0x30d66c
movl %eax, (%rcx)
leaq 0x223e28(%rip), %rax    # 0x30d6b0
movl (%rax), %eax
leaq 0x223ddf(%rip), %rcx    # 0x30d670
movl %eax, (%rcx)
movsd (%r12), %xmm0
leaq 0x223dd8(%rip), %rax    # 0x30d678
movsd %xmm0, (%rax)
movl (%r15), %eax
movl %eax, %ecx
notl %ecx
testb $0x5, %cl
jne 0xe98b6
orl $0x2, %eax
movl %eax, (%r15)
leaq 0x223efb(%rip), %rdx    # 0x30d7b8
leaq 0x223f24(%rip), %rsi    # 0x30d7e8
leaq 0x223f25(%rip), %rdi    # 0x30d7f0
leaq 0x223d36(%rip), %r8     # 0x30d608
leaq 0x12eff7(%rip), %rcx    # 0x2188d0
testl $0x45018, (%rcx)        # imm = 0x45018
je 0xe9933
movl (%r8), %ecx
addl $-0x20, %ecx
cmpl $0x5, %ecx
ja 0xe98f3
movl $0x1f, (%r8)
leaq 0x223d3e(%rip), %r8     # 0x30d638
movl (%r8), %ecx
addl $-0x20, %ecx
cmpl $0x5, %ecx
ja 0xe9933
movl $0x1f, (%r8)
jmp 0xe9933
leaq 0x223d33(%rip), %rax    # 0x30d648
movl (%rax), %eax
leaq 0x223eb2(%rip), %rbx    # 0x30d7d0
leaq 0x223e93(%rip), %rdx    # 0x30d7b8
leaq 0x223ebc(%rip), %rsi    # 0x30d7e8
leaq 0x223ebd(%rip), %rdi    # 0x30d7f0
movsd -0x70(%rbp), %xmm0
movsd %xmm0, (%rbx)
movsd -0x68(%rbp), %xmm0
movsd %xmm0, (%rdx)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, (%rsi)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, (%rdi)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq