// File: /workspace/github_cpp_makefile_star2_O1/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, 0x2948d5(%rip)    # 0x37a5b4
movsd %xmm0, -0x10(%rbp)
callq 0xe91de
testl %eax, %eax
js 0xe5d0c
movl %eax, %eax
leaq 0x87c7a(%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 0x233e4(%rip), %xmm0    # 0x1090f8
movsd -0x10(%rbp), %xmm2
addsd %xmm2, %xmm0
movsd 0x2334b(%rip), %xmm1    # 0x109070
addsd %xmm2, %xmm1
movsd %xmm1, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
callq 0xe4462
movsd %xmm0, -0x30(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0xe4462
movsd %xmm0, -0x28(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0xe4462
movsd -0x28(%rbp), %xmm4
movapd %xmm0, %xmm1
movsd -0x30(%rbp), %xmm0
ucomisd %xmm4, %xmm0
jb 0xe5d70
ucomisd %xmm4, %xmm1
jae 0xe5dc3
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 0x2335d(%rip), %xmm0    # 0x1090f8
movsd %xmm0, -0x20(%rbp)
callq 0xe4462
decl %ebx
je 0xe5f4d
movsd -0x28(%rbp), %xmm4
ucomisd %xmm4, %xmm0
movsd -0x38(%rbp), %xmm1
jb 0xe5d75
ucomisd %xmm4, %xmm1
jb 0xe5d75
movsd 0x25bf5(%rip), %xmm2    # 0x10b9c0
movl $0x28, %ebx
movsd 0x23290(%rip), %xmm3    # 0x109068
movsd %xmm2, -0x28(%rbp)
movsd %xmm2, -0x40(%rbp)
ucomisd %xmm4, %xmm0
jb 0xe5e2b
ucomisd %xmm1, %xmm4
jb 0xe5e2b
movsd %xmm0, -0x30(%rbp)
movsd -0x28(%rbp), %xmm1
mulsd %xmm3, %xmm1
movsd -0x18(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x18(%rbp)
callq 0xe4462
movsd 0x23251(%rip), %xmm4    # 0x109068
movsd -0x18(%rbp), %xmm3
movsd %xmm0, -0x38(%rbp)
movsd -0x20(%rbp), %xmm2
jmp 0xe5ee8
ucomisd %xmm0, %xmm4
movsd -0x20(%rbp), %xmm0
jb 0xe5e6d
ucomisd %xmm4, %xmm1
jb 0xe5e6d
movsd %xmm1, -0x38(%rbp)
movsd -0x40(%rbp), %xmm1
mulsd 0x25b32(%rip), %xmm1    # 0x10b980
addsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
callq 0xe4462
movsd -0x20(%rbp), %xmm2
movsd %xmm0, -0x30(%rbp)
movsd -0x18(%rbp), %xmm3
jmp 0xe5ee0
movsd -0x18(%rbp), %xmm2
movapd %xmm2, %xmm1
movapd %xmm2, %xmm4
movsd -0x10(%rbp), %xmm3
subsd %xmm3, %xmm1
subsd %xmm0, %xmm3
movsd 0x45d89(%rip), %xmm2    # 0x12bc18
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 0xe4462
movsd %xmm0, -0x30(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0xe4462
movsd -0x20(%rbp), %xmm2
movsd -0x18(%rbp), %xmm3
movsd %xmm0, -0x38(%rbp)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd 0x23180(%rip), %xmm4    # 0x109068
movapd %xmm2, %xmm0
subsd %xmm3, %xmm0
andpd 0x23288(%rip), %xmm0    # 0x109180
movsd 0x45818(%rip), %xmm1    # 0x12b718
ucomisd %xmm0, %xmm1
movapd %xmm2, %xmm0
addsd %xmm3, %xmm0
mulsd %xmm4, %xmm0
movsd %xmm0, -0x10(%rbp)
ja 0xe5d00
movsd %xmm3, -0x18(%rbp)
movsd %xmm2, -0x20(%rbp)
callq 0xe4462
movsd 0x23136(%rip), %xmm3    # 0x109068
movapd %xmm0, %xmm4
decl %ebx
movsd -0x30(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
jne 0xe5de2
jmp 0xe5d00
xorpd %xmm0, %xmm0
jmp 0xe5cfb