// File: /workspace/github_cpp_makefile_star2_O2/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
andl $0x0, 0x28f8a0(%rip)    # 0x3645b4
movsd %xmm0, -0x38(%rbp)
leaq 0x153670(%rip), %rax    # 0x228390
movq (%rax), %rdi
testq %rdi, %rdi
je 0xd4d36
leaq 0x49df1(%rip), %rsi     # 0x11eb20
xorl %eax, %eax
callq 0x9560
leaq 0x222a93(%rip), %r14    # 0x2f77d0
movsd (%r14), %xmm2
leaq 0x222a6f(%rip), %rdx    # 0x2f77b8
movsd (%rdx), %xmm3
leaq 0x222a94(%rip), %rsi    # 0x2f77e8
movsd (%rsi), %xmm4
leaq 0x222a91(%rip), %rdi    # 0x2f77f0
movsd (%rdi), %xmm5
leaq 0x2228de(%rip), %rax    # 0x2f7648
andl $0x0, (%rax)
leaq 0x222894(%rip), %rax    # 0x2f7608
andl $0x0, (%rax)
leaq 0x222886(%rip), %r15    # 0x2f7604
andl $0x0, (%r15)
leaq 0x222877(%rip), %r13    # 0x2f7600
andl $0x0, (%r13)
leaq 0x22287b(%rip), %r12    # 0x2f7610
andq $0x0, (%r12)
leaq 0x222897(%rip), %rax    # 0x2f7638
andl $0x0, (%rax)
leaq 0x222889(%rip), %rax    # 0x2f7634
andl $0x0, (%rax)
leaq 0x22287b(%rip), %rax    # 0x2f7630
andl $0x0, (%rax)
leaq 0x222881(%rip), %rax    # 0x2f7640
andq $0x0, (%rax)
leaq 0x2228a6(%rip), %rax    # 0x2f7670
andl $0x0, (%rax)
leaq 0x222898(%rip), %rax    # 0x2f766c
andl $0x0, (%rax)
leaq 0x22288a(%rip), %rax    # 0x2f7668
andl $0x0, (%rax)
leaq 0x222890(%rip), %rax    # 0x2f7678
andq $0x0, (%rax)
leaq 0x2227d5(%rip), %rcx    # 0x2f75c8
xorl %eax, %eax
cmpq %rax, (%rcx)
je 0xd53e5
leaq 0x2221cb(%rip), %rcx    # 0x2f6fd0
movsd (%rcx), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0xd4e19
jnp 0xd53e5
leaq 0x2221b8(%rip), %rcx    # 0x2f6fd8
movsd (%rcx), %xmm1
ucomisd %xmm0, %xmm1
jne 0xd4e30
jnp 0xd53e5
movsd %xmm5, -0x50(%rbp)
movsd %xmm4, -0x58(%rbp)
movsd %xmm3, -0x60(%rbp)
movsd %xmm2, -0x68(%rbp)
leaq 0x222785(%rip), %rax    # 0x2f75d0
movsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
leaq 0x2226ed(%rip), %rax    # 0x2f7548
movsd (%rax), %xmm0
xorl %edi, %edi
callq 0xbfac2
leaq 0x222837(%rip), %rax    # 0x2f76a4
movl (%rax), %edi
leaq 0x222826(%rip), %rax    # 0x2f769c
movl (%rax), %esi
leaq 0x222821(%rip), %rax    # 0x2f76a0
movl (%rax), %edx
callq 0xc2f07
addsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
leaq 0x2227e9(%rip), %r14    # 0x2f7680
movl (%r14), %edi
callq 0xd3da7
movapd %xmm0, %xmm3
pushq $0x1
popq %rbx
movsd -0x30(%rbp), %xmm1
cmpl $0x19, %ebx
je 0xd504b
movsd %xmm1, -0x40(%rbp)
cvtsi2sd %ebx, %xmm0
divsd 0x1e17b(%rip), %xmm0    # 0xf3040
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movl (%r14), %edi
movsd %xmm3, -0x70(%rbp)
callq 0xd3da7
movsd -0x38(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
incl %ebx
ucomisd -0x70(%rbp), %xmm2
movapd %xmm0, %xmm3
jb 0xd4eab
ucomisd %xmm2, %xmm0
movapd %xmm0, %xmm3
jb 0xd4eab
pushq $0x6
popq %rbx
movsd -0x40(%rbp), %xmm0
decl %ebx
je 0xd4f92
movsd %xmm1, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
addsd %xmm0, %xmm1
mulsd 0x1e145(%rip), %xmm1    # 0xf3068
movl (%r14), %edi
movsd %xmm1, -0x30(%rbp)
movapd %xmm1, %xmm0
callq 0xd3da7
movsd -0x48(%rbp), %xmm2
ucomisd %xmm0, %xmm2
jae 0xd4f56
movsd -0x30(%rbp), %xmm1
ucomisd %xmm2, %xmm0
jae 0xd4f4f
movsd -0x38(%rbp), %xmm1
movsd -0x40(%rbp), %xmm0
jmp 0xd4f60
movsd -0x30(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
mulsd 0x1e0d0(%rip), %xmm3    # 0xf3040
movsd 0x1e090(%rip), %xmm4    # 0xf3008
mulsd %xmm4, %xmm3
mulsd %xmm4, %xmm3
movsd 0x1e0e8(%rip), %xmm4    # 0xf3070
ucomisd %xmm3, %xmm4
jbe 0xd4f05
movl (%r14), %edi
callq 0xd47d4
leaq 0x222657(%rip), %rbx    # 0x2f75f8
movsd %xmm0, (%rbx)
movl (%r14), %edi
callq 0xd3da7
leaq 0x22263c(%rip), %rax    # 0x2f75f0
movsd %xmm0, (%rax)
leaq 0x222811(%rip), %rax    # 0x2f77d0
movsd (%rax), %xmm0
leaq 0x22261e(%rip), %rax    # 0x2f75e8
movsd %xmm0, (%rax)
leaq 0x2228ab(%rip), %rax    # 0x2f7880
leaq 0x2228c4(%rip), %rcx    # 0x2f78a0
cvtsi2sdl (%rcx), %xmm0
divsd 0x1e058(%rip), %xmm0    # 0xf3040
addsd (%rax), %xmm0
addsd (%rbx), %xmm0
addsd 0x40fa0(%rip), %xmm0    # 0x115f98
xorl %edi, %edi
callq 0xbfac2
leaq 0x2226a2(%rip), %rax    # 0x2f76a8
movl (%rax), %eax
movl %eax, (%r13)
leaq 0x222699(%rip), %rax    # 0x2f76ac
movl (%rax), %eax
movl %eax, (%r15)
leaq 0x222691(%rip), %rax    # 0x2f76b0
movl (%rax), %eax
leaq 0x2225e0(%rip), %rcx    # 0x2f7608
movl %eax, (%rcx)
leaq 0x222687(%rip), %rax    # 0x2f76b8
movsd (%rax), %xmm0
movsd %xmm0, (%r12)
leaq 0x222606(%rip), %rax    # 0x2f7648
orb $0x1, (%rax)
movsd (%rbx), %xmm0
jmp 0xd5050
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
leaq 0x22257c(%rip), %rax    # 0x2f75d8
movsd (%rax), %xmm1
movsd %xmm1, -0x48(%rbp)
movl (%r14), %edi
callq 0xd3da7
movapd %xmm0, %xmm3
pushq $0x1
popq %rbx
movsd -0x30(%rbp), %xmm1
cmpl $0x19, %ebx
je 0xd53ac
movsd %xmm1, -0x40(%rbp)
cvtsi2sd %ebx, %xmm0
divsd 0x1dfad(%rip), %xmm0    # 0xf3040
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movl (%r14), %edi
movsd %xmm3, -0x70(%rbp)
callq 0xd3da7
movsd -0x38(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
incl %ebx
movsd -0x70(%rbp), %xmm3
ucomisd %xmm2, %xmm3
movapd %xmm0, %xmm3
jb 0xd5079
ucomisd %xmm0, %xmm2
movapd %xmm0, %xmm3
jb 0xd5079
pushq $0x6
popq %rbx
movsd -0x40(%rbp), %xmm0
decl %ebx
je 0xd5168
movsd %xmm1, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
addsd %xmm0, %xmm1
movsd 0x1df73(%rip), %xmm0    # 0xf3068
mulsd %xmm0, %xmm1
movl (%r14), %edi
movsd %xmm1, -0x30(%rbp)
movapd %xmm1, %xmm0
callq 0xd3da7
movsd -0x48(%rbp), %xmm2
ucomisd %xmm2, %xmm0
jae 0xd512c
movsd -0x30(%rbp), %xmm1
ucomisd %xmm0, %xmm2
jae 0xd5125
movsd -0x38(%rbp), %xmm1
movsd -0x40(%rbp), %xmm0
jmp 0xd5136
movsd -0x30(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
mulsd 0x1defa(%rip), %xmm3    # 0xf3040
movsd 0x1deba(%rip), %xmm4    # 0xf3008
mulsd %xmm4, %xmm3
mulsd %xmm4, %xmm3
movsd 0x1df12(%rip), %xmm4    # 0xf3070
ucomisd %xmm3, %xmm4
jbe 0xd50d7
movl (%r14), %edi
callq 0xd47d4
leaq 0x2224b1(%rip), %r13    # 0x2f7628
movsd %xmm0, (%r13)
movl (%r14), %edi
callq 0xd3da7
leaq 0x222494(%rip), %rax    # 0x2f7620
movsd %xmm0, (%rax)
leaq 0x222639(%rip), %rbx    # 0x2f77d0
movsd (%rbx), %xmm0
leaq 0x222476(%rip), %rax    # 0x2f7618
movsd %xmm0, (%rax)
leaq 0x2226f3(%rip), %rax    # 0x2f78a0
cvtsi2sdl (%rax), %xmm0
leaq 0x2226c8(%rip), %rax    # 0x2f7880
movsd 0x1de80(%rip), %xmm1    # 0xf3040
divsd %xmm1, %xmm0
addsd (%rax), %xmm0
addsd (%r13), %xmm0
addsd 0x40dc2(%rip), %xmm0    # 0x115f98
xorl %edi, %edi
callq 0xbfac2
leaq 0x2224c4(%rip), %rax    # 0x2f76a8
movl (%rax), %eax
leaq 0x222443(%rip), %rcx    # 0x2f7630
movl %eax, (%rcx)
leaq 0x2224b6(%rip), %rax    # 0x2f76ac
movl (%rax), %eax
leaq 0x222435(%rip), %rcx    # 0x2f7634
movl %eax, (%rcx)
leaq 0x2224a8(%rip), %rax    # 0x2f76b0
movl (%rax), %eax
leaq 0x222427(%rip), %rcx    # 0x2f7638
movl %eax, (%rcx)
leaq 0x22249e(%rip), %r12    # 0x2f76b8
movsd (%r12), %xmm0
leaq 0x222421(%rip), %r15    # 0x2f7648
orb $0x4, (%r15)
leaq 0x22240e(%rip), %rax    # 0x2f7640
movsd %xmm0, (%rax)
leaq 0x2223bb(%rip), %rax    # 0x2f75f8
movsd (%rax), %xmm0
movsd (%r13), %xmm1
movapd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd 0x1de11(%rip), %xmm3    # 0xf3068
mulsd %xmm3, %xmm2
addsd %xmm2, %xmm3
cmpltsd %xmm0, %xmm1
movapd %xmm1, %xmm0
andnpd %xmm2, %xmm0
andpd %xmm3, %xmm1
orpd %xmm0, %xmm1
leaq 0x2223e5(%rip), %r13    # 0x2f7660
movlpd %xmm1, (%r13)
movl (%r14), %edi
callq 0xd458f
movsd (%r13), %xmm0
movl (%r14), %edi
movq %rbx, %r14
callq 0xd3da7
leaq 0x2223b7(%rip), %rax    # 0x2f7658
movsd %xmm0, (%rax)
leaq 0x2225f4(%rip), %rax    # 0x2f78a0
cvtsi2sdl (%rax), %xmm0
divsd 0x1dd88(%rip), %xmm0    # 0xf3040
movsd (%rbx), %xmm1
leaq 0x22238d(%rip), %rax    # 0x2f7650
leaq 0x2225b6(%rip), %rcx    # 0x2f7880
addsd (%rcx), %xmm0
movsd %xmm1, (%rax)
addsd (%r13), %xmm0
addsd 0x40cb8(%rip), %xmm0    # 0x115f98
xorl %edi, %edi
callq 0xbfac2
leaq 0x2223ba(%rip), %rax    # 0x2f76a8
movl (%rax), %eax
leaq 0x222371(%rip), %rcx    # 0x2f7668
movl %eax, (%rcx)
leaq 0x2223ac(%rip), %rax    # 0x2f76ac
movl (%rax), %eax
leaq 0x222363(%rip), %rcx    # 0x2f766c
movl %eax, (%rcx)
leaq 0x22239e(%rip), %rax    # 0x2f76b0
movl (%rax), %eax
leaq 0x222355(%rip), %rcx    # 0x2f7670
movl %eax, (%rcx)
movsd (%r12), %xmm0
leaq 0x22234e(%rip), %rax    # 0x2f7678
movsd %xmm0, (%rax)
movl (%r15), %eax
movl %eax, %ecx
notl %ecx
testb $0x5, %cl
jne 0xd5340
orl $0x2, %eax
movl %eax, (%r15)
movsd -0x68(%rbp), %xmm2
leaq 0x22246c(%rip), %rdx    # 0x2f77b8
movsd -0x60(%rbp), %xmm3
leaq 0x222490(%rip), %rsi    # 0x2f77e8
movsd -0x58(%rbp), %xmm4
leaq 0x22248c(%rip), %rdi    # 0x2f77f0
movsd -0x50(%rbp), %xmm5
leaq 0x222298(%rip), %r8     # 0x2f7608
leaq 0x12d559(%rip), %rcx    # 0x2028d0
testl $0x45018, (%rcx)        # imm = 0x45018
je 0xd53e5
movl (%r8), %ecx
addl $-0x20, %ecx
cmpl $0x5, %ecx
ja 0xd5391
movl $0x1f, (%r8)
leaq 0x2222a0(%rip), %r8     # 0x2f7638
movl (%r8), %ecx
addl $-0x20, %ecx
cmpl $0x5, %ecx
ja 0xd53e5
movl $0x1f, (%r8)
jmp 0xd53e5
leaq 0x222295(%rip), %rax    # 0x2f7648
movl (%rax), %eax
leaq 0x222414(%rip), %r14    # 0x2f77d0
movsd -0x68(%rbp), %xmm2
leaq 0x2223f0(%rip), %rdx    # 0x2f77b8
movsd -0x60(%rbp), %xmm3
leaq 0x222414(%rip), %rsi    # 0x2f77e8
movsd -0x58(%rbp), %xmm4
leaq 0x222410(%rip), %rdi    # 0x2f77f0
movsd -0x50(%rbp), %xmm5
movsd %xmm2, (%r14)
movsd %xmm3, (%rdx)
movsd %xmm4, (%rsi)
movsd %xmm5, (%rdi)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq