// File: /workspace/github_cpp_makefile_star2_O3/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 0x224bdc(%rip), %r14    # 0x30d660
cvtsi2sd %ecx, %xmm0
movsd %xmm0, -0x38(%rbp)
movapd %xmm0, %xmm1
divsd 0x20577(%rip), %xmm1    # 0x109010
divsd 0x2059f(%rip), %xmm1    # 0x109040
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 0xe81c0
movsd %xmm0, -0x48(%rbp)
movsd -0x30(%rbp), %xmm0
movl %ebx, %edi
callq 0xe81c0
movsd %xmm0, -0x58(%rbp)
movsd -0x28(%rbp), %xmm0
movl %ebx, %edi
callq 0xe81c0
movsd -0x58(%rbp), %xmm4
movsd -0x28(%rbp), %xmm1
movsd -0x38(%rbp), %xmm3
mulsd 0x20569(%rip), %xmm3    # 0x109068
movsd %xmm0, -0x40(%rbp)
movl $0x28, %r15d
movsd 0x20556(%rip), %xmm2    # 0x109068
movsd %xmm3, -0x38(%rbp)
movsd %xmm3, -0x50(%rbp)
movsd -0x48(%rbp), %xmm0
ucomisd %xmm0, %xmm4
jbe 0xe8b66
movsd -0x40(%rbp), %xmm3
ucomisd %xmm4, %xmm3
jbe 0xe8b66
movsd -0x38(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, -0x28(%rbp)
movapd %xmm1, %xmm0
movl %ebx, %edi
callq 0xe81c0
movsd 0x20511(%rip), %xmm4    # 0x109068
movsd %xmm0, -0x40(%rbp)
movsd -0x20(%rbp), %xmm3
jmp 0xe8c17
ucomisd %xmm4, %xmm0
movsd -0x20(%rbp), %xmm0
jbe 0xe8ba6
ucomisd -0x40(%rbp), %xmm4
jbe 0xe8ba6
movsd -0x50(%rbp), %xmm2
mulsd 0x22e03(%rip), %xmm2    # 0x10b988
addsd %xmm2, %xmm0
movsd %xmm0, -0x20(%rbp)
movl %ebx, %edi
movsd %xmm1, -0x28(%rbp)
callq 0xe81c0
movsd -0x20(%rbp), %xmm3
movsd %xmm0, -0x48(%rbp)
jmp 0xe8c0f
movapd %xmm1, %xmm2
movsd -0x30(%rbp), %xmm4
subsd %xmm4, %xmm2
subsd %xmm0, %xmm4
movsd 0x43139(%rip), %xmm3    # 0x12bcf8
divsd %xmm3, %xmm4
movsd %xmm4, -0x30(%rbp)
divsd %xmm3, %xmm2
addsd %xmm4, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd %xmm2, -0x38(%rbp)
subsd %xmm2, %xmm1
movsd %xmm1, -0x28(%rbp)
movl %ebx, %edi
callq 0xe81c0
movsd %xmm0, -0x48(%rbp)
movsd -0x28(%rbp), %xmm0
movl %ebx, %edi
callq 0xe81c0
movsd -0x20(%rbp), %xmm3
movsd %xmm0, -0x40(%rbp)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd 0x20451(%rip), %xmm4    # 0x109068
movapd %xmm3, %xmm0
movsd -0x28(%rbp), %xmm2
subsd %xmm2, %xmm0
andpd 0x20564(%rip), %xmm0    # 0x109190
movsd 0x42bc4(%rip), %xmm1    # 0x12b7f8
ucomisd %xmm0, %xmm1
movapd %xmm3, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm4, %xmm0
movsd %xmm3, -0x20(%rbp)
ja 0xe8c73
movsd %xmm0, -0x30(%rbp)
movl %ebx, %edi
callq 0xe81c0
movsd 0x20409(%rip), %xmm2    # 0x109068
movsd -0x28(%rbp), %xmm1
movapd %xmm0, %xmm4
decl %r15d
jne 0xe8b1c
jmp 0xe8c7c
movapd %xmm2, %xmm1
movsd %xmm0, (%r14)
movsd -0x20(%rbp), %xmm0
movl %ebx, %edi
callq 0xe898f
movsd %xmm0, (%r14)
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq