// File: 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
subq $0x60, %rsp
movl %edi, -0x4(%rbp)
leaq 0x22ee5e(%rip), %rax    # 0x36c840
movsd (%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x4(%rbp), %ecx
movl $0x258, %edx            # imm = 0x258
movl $0xfa0, %eax            # imm = 0xFA0
cmpl $0x0, %ecx
cmovnel %eax, %edx
xorl %esi, %esi
xorps %xmm0, %xmm0
movl %esi, %edi
callq 0x122210
movaps %xmm0, %xmm1
movsd -0x58(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
leaq 0x22ee1e(%rip), %rax    # 0x36c840
movsd (%rax), %xmm0
movsd %xmm0, -0x18(%rbp)
leaq 0x22ee0e(%rip), %rax    # 0x36c840
movsd (%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x4(%rbp), %ecx
movl $0x258, %edx            # imm = 0x258
movl $0xfa0, %eax            # imm = 0xFA0
cmpl $0x0, %ecx
cmovnel %eax, %edx
xorl %esi, %esi
xorps %xmm0, %xmm0
movl %esi, %edi
callq 0x122210
movaps %xmm0, %xmm1
movsd -0x50(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd -0x10(%rbp), %xmm0
movl -0x4(%rbp), %edi
callq 0x13cbf0
movsd %xmm0, -0x38(%rbp)
movsd -0x18(%rbp), %xmm0
movl -0x4(%rbp), %edi
callq 0x13cbf0
movsd %xmm0, -0x40(%rbp)
movsd -0x20(%rbp), %xmm0
movl -0x4(%rbp), %edi
callq 0x13cbf0
movsd %xmm0, -0x48(%rbp)
movl -0x4(%rbp), %edx
movl $0x258, %eax            # imm = 0x258
movl $0xfa0, %ecx            # imm = 0xFA0
cmpl $0x0, %edx
cmovnel %ecx, %eax
cvtsi2sd %eax, %xmm0
movsd 0x31690(%rip), %xmm1    # 0x16f150
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movl -0x4(%rbp), %edx
movl $0x258, %eax            # imm = 0x258
movl $0xfa0, %ecx            # imm = 0xFA0
cmpl $0x0, %edx
cmovnel %ecx, %eax
cvtsi2sd %eax, %xmm0
movsd 0x31668(%rip), %xmm1    # 0x16f150
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0x0, -0x8(%rbp)
cmpl $0x28, -0x8(%rbp)
jge 0x13dc9e
movsd -0x38(%rbp), %xmm1
movsd -0x40(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13db54
movsd -0x40(%rbp), %xmm1
movsd -0x48(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13db54
movsd -0x30(%rbp), %xmm0
movsd 0x31621(%rip), %xmm1    # 0x16f150
divsd %xmm1, %xmm0
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd -0x20(%rbp), %xmm0
movl -0x4(%rbp), %edi
callq 0x13cbf0
movsd %xmm0, -0x48(%rbp)
jmp 0x13dc1d
movsd -0x38(%rbp), %xmm0
ucomisd -0x40(%rbp), %xmm0
jbe 0x13db9f
movsd -0x40(%rbp), %xmm0
ucomisd -0x48(%rbp), %xmm0
jbe 0x13db9f
movsd -0x28(%rbp), %xmm1
movsd 0x315d7(%rip), %xmm0    # 0x16f150
divsd %xmm0, %xmm1
movsd -0x10(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
movl -0x4(%rbp), %edi
callq 0x13cbf0
movsd %xmm0, -0x38(%rbp)
jmp 0x13dc1b
movsd -0x18(%rbp), %xmm0
subsd -0x10(%rbp), %xmm0
movsd 0x50a37(%rip), %xmm1    # 0x18e5e8
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x20(%rbp), %xmm0
subsd -0x18(%rbp), %xmm0
movsd 0x50a1c(%rip), %xmm1    # 0x18e5e8
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x28(%rbp), %xmm0
addsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x30(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd -0x10(%rbp), %xmm0
movl -0x4(%rbp), %edi
callq 0x13cbf0
movsd %xmm0, -0x38(%rbp)
movsd -0x20(%rbp), %xmm0
movl -0x4(%rbp), %edi
callq 0x13cbf0
movsd %xmm0, -0x48(%rbp)
jmp 0x13dc1d
movsd -0x10(%rbp), %xmm1
subsd -0x20(%rbp), %xmm1
movaps 0x31662(%rip), %xmm0    # 0x16f290
pand %xmm0, %xmm1
movsd 0x50ba6(%rip), %xmm0    # 0x18e7e0
ucomisd %xmm1, %xmm0
jbe 0x13dc63
movsd -0x10(%rbp), %xmm0
addsd -0x20(%rbp), %xmm0
movsd 0x314fe(%rip), %xmm1    # 0x16f150
divsd %xmm1, %xmm0
leaq 0x22ebe3(%rip), %rax    # 0x36c840
movsd %xmm0, (%rax)
jmp 0x13dc9e
movsd -0x10(%rbp), %xmm0
addsd -0x20(%rbp), %xmm0
movsd 0x314db(%rip), %xmm1    # 0x16f150
divsd %xmm1, %xmm0
movsd %xmm0, -0x18(%rbp)
movsd -0x18(%rbp), %xmm0
movl -0x4(%rbp), %edi
callq 0x13cbf0
movsd %xmm0, -0x40(%rbp)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x13daf8
movsd -0x10(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
movl -0x4(%rbp), %edi
callq 0x13d830
leaq 0x22eb89(%rip), %rax    # 0x36c840
movsd %xmm0, (%rax)
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)