// File: /workspace/github_cpp_makefile_star2_O1/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, 0x291e2e(%rip)    # 0x37a5b4
leaq 0x155c03(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0xe87a3
leaq 0x4c374(%rip), %rsi     # 0x134b10
xorl %eax, %eax
callq 0x9560
leaq 0x225026(%rip), %rax    # 0x30d7d0
movsd (%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
leaq 0x224ffe(%rip), %r13    # 0x30d7b8
movsd (%r13), %xmm0
movsd %xmm0, -0x68(%rbp)
leaq 0x22501c(%rip), %rax    # 0x30d7e8
movsd (%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
leaq 0x225014(%rip), %rax    # 0x30d7f0
movsd (%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
leaq 0x224e5c(%rip), %rcx    # 0x30d648
xorl %eax, %eax
movl %eax, (%rcx)
leaq 0x224e11(%rip), %rcx    # 0x30d608
movl %eax, (%rcx)
leaq 0x224e04(%rip), %r15    # 0x30d604
movl %eax, (%r15)
leaq 0x224df6(%rip), %rbx    # 0x30d600
movl %eax, (%rbx)
leaq 0x224dfd(%rip), %r14    # 0x30d610
movq %rax, (%r14)
leaq 0x224e1b(%rip), %rcx    # 0x30d638
movl %eax, (%rcx)
leaq 0x224e0e(%rip), %rcx    # 0x30d634
movl %eax, (%rcx)
leaq 0x224e01(%rip), %rcx    # 0x30d630
movl %eax, (%rcx)
leaq 0x224e08(%rip), %rcx    # 0x30d640
movq %rax, (%rcx)
leaq 0x224e2e(%rip), %rcx    # 0x30d670
movl %eax, (%rcx)
leaq 0x224e21(%rip), %rcx    # 0x30d66c
movl %eax, (%rcx)
leaq 0x224e14(%rip), %rcx    # 0x30d668
movl %eax, (%rcx)
leaq 0x224e1b(%rip), %rcx    # 0x30d678
movq %rax, (%rcx)
leaq 0x224d61(%rip), %rcx    # 0x30d5c8
cmpq %rax, (%rcx)
je 0xe8e9d
leaq 0x224759(%rip), %rax    # 0x30cfd0
movsd (%rax), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0xe888b
jnp 0xe8e9d
leaq 0x224746(%rip), %rax    # 0x30cfd8
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jne 0xe88a2
jnp 0xe8e9d
leaq 0x224d27(%rip), %rax    # 0x30d5d0
movsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
leaq 0x224c8f(%rip), %rax    # 0x30d548
movsd (%rax), %xmm0
xorl %edi, %edi
callq 0xd0c46
leaq 0x224dd9(%rip), %rax    # 0x30d6a4
movl (%rax), %edi
leaq 0x224dc8(%rip), %rax    # 0x30d69c
movl (%rax), %esi
leaq 0x224dc3(%rip), %rax    # 0x30d6a0
movl (%rax), %edx
callq 0xd4110
addsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
leaq 0x224d8b(%rip), %r12    # 0x30d680
movl (%r12), %edi
callq 0xe77bd
movsd %xmm0, -0x30(%rbp)
movsd 0x35ec5(%rip), %xmm0    # 0x11e7d0
addsd -0x40(%rbp), %xmm0
movl (%r12), %edi
movsd %xmm0, -0x38(%rbp)
callq 0xe77bd
movsd -0x48(%rbp), %xmm1
movapd %xmm0, %xmm2
ucomisd -0x30(%rbp), %xmm1
jb 0xe8940
ucomisd %xmm1, %xmm2
jb 0xe8940
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0xe899f
movl $0x2, %r13d
movsd -0x38(%rbp), %xmm0
cmpl $0x19, %r13d
je 0xe8b01
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
divsd 0x206d6(%rip), %xmm0    # 0x109040
addsd -0x40(%rbp), %xmm0
movl (%r12), %edi
movsd %xmm0, -0x38(%rbp)
movsd %xmm2, -0x50(%rbp)
callq 0xe77bd
movsd -0x48(%rbp), %xmm1
incl %r13d
ucomisd -0x50(%rbp), %xmm1
movapd %xmm0, %xmm2
jb 0xe8946
ucomisd %xmm1, %xmm0
movapd %xmm0, %xmm2
jb 0xe8946
movl $0x4, %r13d
movsd -0x38(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
movsd %xmm1, -0x38(%rbp)
movsd %xmm0, -0x30(%rbp)
addsd %xmm0, %xmm1
mulsd 0x206a3(%rip), %xmm1    # 0x109068
movl (%r12), %edi
movsd %xmm1, -0x40(%rbp)
movapd %xmm1, %xmm0
callq 0xe77bd
movsd -0x48(%rbp), %xmm2
ucomisd %xmm0, %xmm2
jae 0xe89f9
movsd -0x40(%rbp), %xmm1
ucomisd %xmm2, %xmm0
jae 0xe89f2
movsd -0x38(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
jmp 0xe8a03
movsd -0x40(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
mulsd 0x2062d(%rip), %xmm3    # 0x109040
movsd 0x205ed(%rip), %xmm4    # 0x109008
mulsd %xmm4, %xmm3
mulsd %xmm4, %xmm3
subl $0x1, %r13d
setb %al
movsd 0x2063e(%rip), %xmm4    # 0x109070
ucomisd %xmm3, %xmm4
ja 0xe8a40
testb %al, %al
je 0xe89af
movl (%r12), %edi
callq 0xe8217
leaq 0x224ba8(%rip), %r13    # 0x30d5f8
movsd %xmm0, (%r13)
movl (%r12), %edi
callq 0xe77bd
leaq 0x224b8a(%rip), %rax    # 0x30d5f0
movsd %xmm0, (%rax)
leaq 0x224d5f(%rip), %rax    # 0x30d7d0
movsd (%rax), %xmm0
leaq 0x224b6c(%rip), %rax    # 0x30d5e8
movsd %xmm0, (%rax)
leaq 0x224df9(%rip), %rax    # 0x30d880
leaq 0x224e12(%rip), %rcx    # 0x30d8a0
xorps %xmm0, %xmm0
cvtsi2sdl (%rcx), %xmm0
divsd 0x205a3(%rip), %xmm0    # 0x109040
addsd (%rax), %xmm0
addsd (%r13), %xmm0
addsd 0x434d9(%rip), %xmm0    # 0x12bf88
xorl %edi, %edi
callq 0xd0c46
leaq 0x224beb(%rip), %rax    # 0x30d6a8
movl (%rax), %eax
movl %eax, (%rbx)
leaq 0x224be4(%rip), %rax    # 0x30d6ac
movl (%rax), %eax
movl %eax, (%r15)
leaq 0x224bdc(%rip), %rax    # 0x30d6b0
movl (%rax), %eax
leaq 0x224b2b(%rip), %rcx    # 0x30d608
movl %eax, (%rcx)
leaq 0x224bd2(%rip), %rax    # 0x30d6b8
movsd (%rax), %xmm0
movsd %xmm0, (%r14)
leaq 0x224b52(%rip), %rax    # 0x30d648
orb $0x1, (%rax)
movsd (%r13), %xmm0
jmp 0xe8b06
movsd -0x40(%rbp), %xmm0
leaq 0x224acb(%rip), %rax    # 0x30d5d8
movsd (%rax), %xmm1
movsd %xmm1, -0x48(%rbp)
movl (%r12), %edi
movsd %xmm0, -0x40(%rbp)
callq 0xe77bd
movsd %xmm0, -0x30(%rbp)
movsd 0x35c9f(%rip), %xmm0    # 0x11e7d0
addsd -0x40(%rbp), %xmm0
movl (%r12), %edi
movsd %xmm0, -0x38(%rbp)
callq 0xe77bd
movsd -0x48(%rbp), %xmm1
movapd %xmm0, %xmm2
movsd -0x30(%rbp), %xmm0
ucomisd %xmm1, %xmm0
leaq 0x224c5b(%rip), %r13    # 0x30d7b8
jb 0xe8b71
ucomisd %xmm2, %xmm1
jb 0xe8b71
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
jmp 0xe8bd0
movl $0x2, %ebx
movsd -0x38(%rbp), %xmm0
cmpl $0x19, %ebx
je 0xe8e9d
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
divsd 0x204a8(%rip), %xmm0    # 0x109040
addsd -0x40(%rbp), %xmm0
movl (%r12), %edi
movsd %xmm0, -0x38(%rbp)
movsd %xmm2, -0x50(%rbp)
callq 0xe77bd
movsd -0x48(%rbp), %xmm1
incl %ebx
movsd -0x50(%rbp), %xmm2
ucomisd %xmm1, %xmm2
movapd %xmm0, %xmm2
jb 0xe8b76
ucomisd %xmm0, %xmm1
movapd %xmm0, %xmm2
jb 0xe8b76
movl $0x4, %ebx
movsd -0x38(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
movsd %xmm1, -0x38(%rbp)
movsd %xmm0, -0x30(%rbp)
addsd %xmm0, %xmm1
movsd 0x20473(%rip), %xmm0    # 0x109068
mulsd %xmm0, %xmm1
movl (%r12), %edi
movsd %xmm1, -0x40(%rbp)
movapd %xmm1, %xmm0
callq 0xe77bd
movsd -0x48(%rbp), %xmm2
ucomisd %xmm2, %xmm0
jae 0xe8c2d
movsd -0x40(%rbp), %xmm1
ucomisd %xmm0, %xmm2
jae 0xe8c26
movsd -0x38(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
jmp 0xe8c37
movsd -0x40(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
mulsd 0x203f9(%rip), %xmm3    # 0x109040
movsd 0x203b9(%rip), %xmm4    # 0x109008
mulsd %xmm4, %xmm3
mulsd %xmm4, %xmm3
subl $0x1, %ebx
setb %al
movsd 0x2040b(%rip), %xmm4    # 0x109070
ucomisd %xmm3, %xmm4
ja 0xe8c73
testb %al, %al
je 0xe8bdf
movl (%r12), %edi
callq 0xe8217
leaq 0x2249a5(%rip), %r15    # 0x30d628
movsd %xmm0, (%r15)
movl (%r12), %edi
callq 0xe77bd
leaq 0x224988(%rip), %rax    # 0x30d620
movsd %xmm0, (%rax)
leaq 0x224b2d(%rip), %rax    # 0x30d7d0
movsd (%rax), %xmm0
leaq 0x22496a(%rip), %rax    # 0x30d618
movsd %xmm0, (%rax)
leaq 0x224be7(%rip), %rax    # 0x30d8a0
xorps %xmm0, %xmm0
cvtsi2sdl (%rax), %xmm0
leaq 0x224bb9(%rip), %rax    # 0x30d880
movsd 0x20371(%rip), %xmm1    # 0x109040
divsd %xmm1, %xmm0
addsd (%rax), %xmm0
addsd (%r15), %xmm0
addsd 0x432a4(%rip), %xmm0    # 0x12bf88
xorl %edi, %edi
callq 0xd0c46
leaq 0x2249b6(%rip), %rax    # 0x30d6a8
movl (%rax), %eax
leaq 0x224935(%rip), %rcx    # 0x30d630
movl %eax, (%rcx)
leaq 0x2249a8(%rip), %rax    # 0x30d6ac
movl (%rax), %eax
leaq 0x224927(%rip), %rcx    # 0x30d634
movl %eax, (%rcx)
leaq 0x22499a(%rip), %rbx    # 0x30d6b0
movl (%rbx), %eax
leaq 0x224919(%rip), %rcx    # 0x30d638
movl %eax, (%rcx)
leaq 0x224990(%rip), %r14    # 0x30d6b8
movsd (%r14), %xmm0
leaq 0x224914(%rip), %r13    # 0x30d648
orb $0x4, (%r13)
leaq 0x224900(%rip), %rax    # 0x30d640
movsd %xmm0, (%rax)
leaq 0x2248ad(%rip), %rax    # 0x30d5f8
movsd (%rax), %xmm0
movsd (%r15), %xmm1
movapd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd 0x20304(%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 0x2248d8(%rip), %r15    # 0x30d660
movlpd %xmm1, (%r15)
movl (%r12), %edi
callq 0xe7fde
movsd (%r15), %xmm0
movl (%r12), %edi
callq 0xe77bd
leaq 0x2248ad(%rip), %rax    # 0x30d658
movsd %xmm0, (%rax)
leaq 0x224aea(%rip), %rax    # 0x30d8a0
xorps %xmm0, %xmm0
cvtsi2sdl (%rax), %xmm0
divsd 0x2027b(%rip), %xmm0    # 0x109040
leaq 0x224a04(%rip), %rax    # 0x30d7d0
movsd (%rax), %xmm1
leaq 0x224879(%rip), %rax    # 0x30d650
leaq 0x224aa2(%rip), %rcx    # 0x30d880
addsd (%rcx), %xmm0
movsd %xmm1, (%rax)
addsd (%r15), %xmm0
addsd 0x43195(%rip), %xmm0    # 0x12bf88
xorl %edi, %edi
callq 0xd0c46
leaq 0x2248a7(%rip), %rax    # 0x30d6a8
movl (%rax), %eax
leaq 0x22485e(%rip), %rcx    # 0x30d668
movl %eax, (%rcx)
leaq 0x224899(%rip), %rax    # 0x30d6ac
movl (%rax), %eax
leaq 0x224850(%rip), %rcx    # 0x30d66c
movl %eax, (%rcx)
movl (%rbx), %eax
leaq 0x224849(%rip), %rcx    # 0x30d670
movl %eax, (%rcx)
movsd (%r14), %xmm0
leaq 0x224843(%rip), %rax    # 0x30d678
movsd %xmm0, (%rax)
movl (%r13), %eax
movl %eax, %ecx
notl %ecx
testb $0x5, %cl
jne 0xe8e52
orl $0x2, %eax
leaq 0x2247f8(%rip), %rcx    # 0x30d648
movl %eax, (%rcx)
leaq 0x12fa77(%rip), %rax    # 0x2188d0
testl $0x45018, (%rax)        # imm = 0x45018
leaq 0x224952(%rip), %r13    # 0x30d7b8
je 0xe8e9d
leaq 0x224799(%rip), %rcx    # 0x30d608
movl (%rcx), %eax
addl $-0x20, %eax
cmpl $0x5, %eax
ja 0xe8e7f
movl $0x1f, (%rcx)
leaq 0x2247b2(%rip), %rax    # 0x30d638
movl (%rax), %eax
addl $-0x20, %eax
cmpl $0x5, %eax
ja 0xe8e9d
leaq 0x2247a1(%rip), %rax    # 0x30d638
movl $0x1f, (%rax)
leaq 0x22492c(%rip), %rax    # 0x30d7d0
movsd -0x70(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0x68(%rbp), %xmm0
movsd %xmm0, (%r13)
leaq 0x224929(%rip), %rax    # 0x30d7e8
movsd -0x60(%rbp), %xmm0
movsd %xmm0, (%rax)
leaq 0x224921(%rip), %rax    # 0x30d7f0
movsd -0x58(%rbp), %xmm0
movsd %xmm0, (%rax)
leaq 0x224769(%rip), %rax    # 0x30d648
movl (%rax), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq