// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathmsc.cpp
// Function: new_moon(double)
double new_moon(double jd)
{
double start, mid, end;
double p1, p2, p3;
int i;
double start_step, end_step;

   sp_count = 0;
   if(0 && debug_file) {
      gregorian(0, jd);
      fprintf(debug_file, "New moon pinfo for %04d/%02d/%02d\n", pri_year,pri_month,pri_day);
      fprintf(debug_file, "New moon ginfo for %04d/%02d/%02d\n", g_year,g_month,g_day);
   }

   i = lookup_new_moon(jd);
   if(i >= 0) {
      return new_moon_table[i];
   }


   // calculates the julian date of the last new moon (to around 1 second)
   // but probably accurate to around 10 minutes... maybe... perhaps

   mid = jd;
   start = mid - 1.0; // yesterday
   end = mid + 1.0;   // tomorrow

   p1 = MOON_PHASE(start);  // get the moon illumination of the three consecutive days
   p2 = MOON_PHASE(mid);
   p3 = MOON_PHASE(end);

   for(i=0; i<31; i++) {   // check back a little more than a lunar month's worth of days
      if((p3 >= p2) && (p1 >= p2)) goto refine_new_moon;  // illumination is at a minimum this day

      end = mid;           // back up a day and try again
      mid = start;
      start = start - 1.0;
      p3 = p2;
      p2 = p1;
      p1 = MOON_PHASE(start);
   }

if(0 && debug_file) fprintf(debug_file, "No new moon solution at jd:%f\n", start);
   return 0.0;        // something went wrong, no minimum found

   refine_new_moon:

   start_step = 0.6;  // start seaching +/- 0.6 days from daily minimum
   end_step = 0.6;

if(0 && debug_file) fprintf(debug_file, "refine moon %d:%6.2f  ", sp_count, (end-start)*(24.0*60.0*60.0));
   for(i=0; i<40; i++) {  // binary seach for the minimum illumnation down to the second
      if((p1 >= p2) && (p2 >= p3)) {    // 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;
         p3 = MOON_PHASE(end);
      }
      else if((p1 <= p2) && (p2 <= p3)) {  // 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;
         p1 = MOON_PHASE(start);
      }
      else {  // minimum 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;
         p1 = MOON_PHASE(start);
         p3 = MOON_PHASE(end);
      }
if(0 && debug_file) fprintf(debug_file, "refine moon %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
         mid = (start + end) / 2.0;
if(0 && debug_file) fprintf(debug_file, "\nnoon %d: %.9f\n", sp_count, mid);
         break;  // start end end are within a second
      }

      mid = (start + end) / 2.0;
      p2 = MOON_PHASE(mid);
   }
   return mid;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x38, %rsp
movl $0x0, 0x293d87(%rip)    # 0x37a5b4
movsd %xmm0, -0x10(%rbp)
callq 0xe9c54
testl %eax, %eax
js 0xe685a
movl %eax, %eax
leaq 0x8712c(%rip), %rcx     # 0x16d970
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x10(%rbp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %rbp
retq
movsd 0x22896(%rip), %xmm0    # 0x1090f8
movsd -0x10(%rbp), %xmm2
addsd %xmm2, %xmm0
movsd 0x227fd(%rip), %xmm1    # 0x109070
addsd %xmm2, %xmm1
movsd %xmm1, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
callq 0xe5033
movsd %xmm0, -0x30(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0xe5033
movsd %xmm0, -0x28(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0xe5033
movsd -0x28(%rbp), %xmm4
movapd %xmm0, %xmm1
movsd -0x30(%rbp), %xmm0
ucomisd %xmm4, %xmm0
jb 0xe68be
ucomisd %xmm4, %xmm1
jae 0xe6911
movl $0x1f, %ebx
movsd %xmm4, -0x38(%rbp)
movsd %xmm0, -0x28(%rbp)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x18(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
addsd 0x2280f(%rip), %xmm0    # 0x1090f8
movsd %xmm0, -0x20(%rbp)
callq 0xe5033
decl %ebx
je 0xe6a9b
movsd -0x28(%rbp), %xmm4
ucomisd %xmm4, %xmm0
movsd -0x38(%rbp), %xmm1
jb 0xe68c3
ucomisd %xmm4, %xmm1
jb 0xe68c3
movsd 0x250a7(%rip), %xmm2    # 0x10b9c0
movl $0x28, %ebx
movsd 0x22742(%rip), %xmm3    # 0x109068
movsd %xmm2, -0x28(%rbp)
movsd %xmm2, -0x40(%rbp)
ucomisd %xmm4, %xmm0
jb 0xe6979
ucomisd %xmm1, %xmm4
jb 0xe6979
movsd %xmm0, -0x30(%rbp)
movsd -0x28(%rbp), %xmm1
mulsd %xmm3, %xmm1
movsd -0x18(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x18(%rbp)
callq 0xe5033
movsd 0x22703(%rip), %xmm4    # 0x109068
movsd -0x18(%rbp), %xmm3
movsd %xmm0, -0x38(%rbp)
movsd -0x20(%rbp), %xmm2
jmp 0xe6a36
ucomisd %xmm0, %xmm4
movsd -0x20(%rbp), %xmm0
jb 0xe69bb
ucomisd %xmm4, %xmm1
jb 0xe69bb
movsd %xmm1, -0x38(%rbp)
movsd -0x40(%rbp), %xmm1
mulsd 0x24fec(%rip), %xmm1    # 0x10b988
addsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
callq 0xe5033
movsd -0x20(%rbp), %xmm2
movsd %xmm0, -0x30(%rbp)
movsd -0x18(%rbp), %xmm3
jmp 0xe6a2e
movsd -0x18(%rbp), %xmm2
movapd %xmm2, %xmm1
movapd %xmm2, %xmm4
movsd -0x10(%rbp), %xmm3
subsd %xmm3, %xmm1
subsd %xmm0, %xmm3
movsd 0x4531b(%rip), %xmm2    # 0x12bcf8
divsd %xmm2, %xmm3
movsd %xmm3, -0x10(%rbp)
divsd %xmm2, %xmm1
addsd %xmm3, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd %xmm1, -0x28(%rbp)
subsd %xmm1, %xmm4
movsd %xmm4, -0x18(%rbp)
callq 0xe5033
movsd %xmm0, -0x30(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0xe5033
movsd -0x20(%rbp), %xmm2
movsd -0x18(%rbp), %xmm3
movsd %xmm0, -0x38(%rbp)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd 0x22632(%rip), %xmm4    # 0x109068
movapd %xmm2, %xmm0
subsd %xmm3, %xmm0
andpd 0x2274a(%rip), %xmm0    # 0x109190
movsd 0x44daa(%rip), %xmm1    # 0x12b7f8
ucomisd %xmm0, %xmm1
movapd %xmm2, %xmm0
addsd %xmm3, %xmm0
mulsd %xmm4, %xmm0
movsd %xmm0, -0x10(%rbp)
ja 0xe684e
movsd %xmm3, -0x18(%rbp)
movsd %xmm2, -0x20(%rbp)
callq 0xe5033
movsd 0x225e8(%rip), %xmm3    # 0x109068
movapd %xmm0, %xmm4
decl %ebx
movsd -0x30(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
jne 0xe6930
jmp 0xe684e
xorpd %xmm0, %xmm0
jmp 0xe6849