// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathmsc.cpp
// Function: refine_solar_noon(int)
void refine_solar_noon(int do_moon)
{
int i;
double start, mid, end;
double start_step, end_step;
double start_el,mid_el,end_el;
#define X_SUN_SEARCH (do_moon?4000:600)

   // search ten minutes on each side of estimated solar noon for the peak elevation
   // if peak not found, use the estimated value

   start = solar_noon - jtime(0,0,X_SUN_SEARCH,0.0);
   mid = solar_noon;
   end = solar_noon + jtime(0,0,X_SUN_SEARCH,0.0);

   start_el = sun_peak(start, do_moon);
   mid_el = sun_peak(mid, do_moon);
   end_el = sun_peak(end, do_moon);

   start_step = X_SUN_SEARCH / 2.0;
   end_step = X_SUN_SEARCH / 2.0;

if(0 && debug_file) fprintf(debug_file, "refine noon %d:%6.2f  ", sp_count, (end-start)*(24.0*60.0*60.0));
   for(i=0; i<40; i++) {
      if((start_el < mid_el) && (mid_el < end_el)) {    // sun is rising at end time, move end forward
if(0 && debug_file) fprintf(debug_file, "  fix end:   start:%f end:%f start_step:%f, end_step:%f\n", mid-start,end-mid,start_step,end_step);
         end += end_step / 2.0;
         end_el = sun_peak(end, do_moon);
      }
      else if((start_el > mid_el) && (mid_el > end_el)) {  // sun is setting at start time, back uo start
if(0 && debug_file) fprintf(debug_file, "  fix start: start:%f end:%f start_step:%f, end_step:%f\n", mid-start,end-mid,start_step,end_step);
         start -= start_step / 2.0;
         start_el = sun_peak(start, do_moon);
      }
      else {  // noon is somewhere between start and end, narrow the range
         start_step = (mid - start) / 1.25;
         end_step = (end - mid) / 1.25;
if(0 && debug_file) fprintf(debug_file, "  fix both:  start:%f end:%f start_step:%f, end_step:%f\n", mid-start,end-mid,start_step,end_step);
         start += start_step;
         end -= end_step;
         start_el = sun_peak(start, do_moon);
         end_el = sun_peak(end, do_moon);
      }
if(0 && debug_file) fprintf(debug_file, "refine noon %d :%6.2f  ", sp_count, (end-start)*(24.0*60.0*60.0));

      if(fabs(start-end) < (1.0/(24.0*60.0*60.0))) {  // time lies with a 1 second interval
         solar_noon = (start + end) / 2.0;
if(0 && debug_file) fprintf(debug_file, "\nnoon %d: %.9f", sp_count, solar_noon);
         break;  // start end end are within a second
      }

      mid = (start + end) / 2.0;
      mid_el = sun_peak(mid, do_moon);
   }

   solar_noon = interp_solar_noon(start, end, do_moon);
if(0 && debug_file) fprintf(debug_file, "\nnoon %d: %.9f", sp_count, solar_noon);

if(0 && debug_file) {
   gregorian(0, solar_noon+time_zone());   
   sun_el = sun_posn(solar_noon, do_moon);
   if(do_moon) fprintf(debug_file, "\ntransit az:%f el:%f  (utc): %02d:%02d:%02d %g   tz:%f\n", sun_el,sun_az, g_hours,g_minutes,g_seconds,g_frac, time_zone());
   else fprintf(debug_file, "\nnoon az:%f el:%f  (utc): %02d:%02d:%02d %g  tz:%f\n", sun_el,sun_az, g_hours,g_minutes,g_seconds,g_frac, time_zone());
}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x48, %rsp
movl %edi, %ebx
testl %edi, %edi
movl $0x258, %eax            # imm = 0x258
movl $0xfa0, %ecx            # imm = 0xFA0
cmovel %eax, %ecx
leaq 0x2230ac(%rip), %r14    # 0x2f7660
cvtsi2sd %ecx, %xmm0
movsd %xmm0, -0x38(%rbp)
movapd %xmm0, %xmm1
divsd 0x1ea47(%rip), %xmm1    # 0xf3010
divsd 0x1ea6f(%rip), %xmm1    # 0xf3040
movsd (%r14), %xmm2
movsd %xmm2, -0x30(%rbp)
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
addsd %xmm2, %xmm1
movsd %xmm1, -0x28(%rbp)
movsd %xmm0, -0x20(%rbp)
callq 0xd3d29
movsd %xmm0, -0x48(%rbp)
movsd -0x30(%rbp), %xmm0
movl %ebx, %edi
callq 0xd3d29
movsd %xmm0, -0x58(%rbp)
movsd -0x28(%rbp), %xmm0
movl %ebx, %edi
callq 0xd3d29
movsd -0x58(%rbp), %xmm4
movsd -0x38(%rbp), %xmm1
mulsd 0x1ea3e(%rip), %xmm1    # 0xf3068
movsd %xmm0, -0x40(%rbp)
pushq $0x28
popq %r15
movsd 0x1ea2d(%rip), %xmm2    # 0xf3068
movsd %xmm1, -0x38(%rbp)
movsd %xmm1, -0x50(%rbp)
subl $0x1, %r15d
jb 0xd47a8
movsd -0x48(%rbp), %xmm0
ucomisd %xmm0, %xmm4
movsd -0x28(%rbp), %xmm1
jbe 0xd469e
movsd -0x40(%rbp), %xmm3
ucomisd %xmm4, %xmm3
jbe 0xd469e
movsd -0x50(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, -0x28(%rbp)
movapd %xmm1, %xmm0
movl %ebx, %edi
callq 0xd3d29
movsd 0x1e9d9(%rip), %xmm5    # 0xf3068
movsd %xmm0, -0x40(%rbp)
movsd -0x20(%rbp), %xmm4
jmp 0xd474f
ucomisd %xmm4, %xmm0
movsd -0x20(%rbp), %xmm0
jbe 0xd46de
ucomisd -0x40(%rbp), %xmm4
jbe 0xd46de
movsd -0x38(%rbp), %xmm2
mulsd 0x21343(%rip), %xmm2    # 0xf5a00
addsd %xmm2, %xmm0
movsd %xmm0, -0x20(%rbp)
movl %ebx, %edi
movsd %xmm1, -0x28(%rbp)
callq 0xd3d29
movsd -0x20(%rbp), %xmm4
movsd %xmm0, -0x48(%rbp)
jmp 0xd4747
movapd %xmm1, %xmm3
movsd -0x30(%rbp), %xmm4
subsd %xmm4, %xmm3
subsd %xmm0, %xmm4
movsd 0x41531(%rip), %xmm2    # 0x115c28
divsd %xmm2, %xmm4
movsd %xmm4, -0x30(%rbp)
divsd %xmm2, %xmm3
addsd %xmm4, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd %xmm3, -0x50(%rbp)
subsd %xmm3, %xmm1
movsd %xmm1, -0x28(%rbp)
movl %ebx, %edi
callq 0xd3d29
movsd %xmm0, -0x48(%rbp)
movsd -0x28(%rbp), %xmm0
movl %ebx, %edi
callq 0xd3d29
movsd -0x20(%rbp), %xmm4
movsd %xmm0, -0x40(%rbp)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd 0x1e919(%rip), %xmm5    # 0xf3068
movapd %xmm4, %xmm0
movsd -0x28(%rbp), %xmm3
subsd %xmm3, %xmm0
andpd 0x1ea2c(%rip), %xmm0    # 0xf3190
movsd 0x40fd4(%rip), %xmm1    # 0x115740
ucomisd %xmm0, %xmm1
movapd %xmm4, %xmm2
addsd %xmm3, %xmm2
mulsd %xmm5, %xmm2
movapd %xmm4, %xmm0
ja 0xd47b4
movsd %xmm0, -0x20(%rbp)
movsd %xmm2, -0x30(%rbp)
movapd %xmm2, %xmm0
movl %ebx, %edi
callq 0xd3d29
movsd 0x1e8c9(%rip), %xmm2    # 0xf3068
movapd %xmm0, %xmm4
jmp 0xd4645
movsd -0x28(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
jmp 0xd47bd
movapd %xmm3, %xmm1
movsd %xmm2, (%r14)
movl %ebx, %edi
callq 0xd44bf
movsd %xmm0, (%r14)
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq