// File: /workspace/github_cpp_makefile_star2_O2/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
andl $0x0, 0x291d0a(%rip)    # 0x3645b4
movsd %xmm0, -0x18(%rbp)
callq 0xd5672
testl %eax, %eax
js 0xd28cb
movl %eax, %eax
leaq 0x850af(%rip), %rcx     # 0x157970
movsd (%rcx,%rax,8), %xmm0
jmp 0xd2b14
movsd 0x20825(%rip), %xmm0    # 0xf30f8
movsd -0x18(%rbp), %xmm2
addsd %xmm2, %xmm0
movsd 0x2078c(%rip), %xmm1    # 0xf3070
addsd %xmm2, %xmm1
movsd %xmm1, -0x10(%rbp)
movsd %xmm0, -0x28(%rbp)
callq 0xd18b7
movsd %xmm0, -0x20(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0xd18b7
movsd %xmm0, -0x30(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0xd18b7
movsd -0x10(%rbp), %xmm2
movapd %xmm0, %xmm3
movsd -0x20(%rbp), %xmm0
pushq $0x1f
popq %rbx
movsd -0x30(%rbp), %xmm4
movsd -0x18(%rbp), %xmm1
movsd %xmm1, -0x20(%rbp)
movsd -0x28(%rbp), %xmm1
subl $0x1, %ebx
jb 0xd297c
ucomisd %xmm4, %xmm0
movsd %xmm1, -0x18(%rbp)
jb 0xd2950
ucomisd %xmm4, %xmm3
jae 0xd2985
movsd %xmm0, -0x30(%rbp)
movapd %xmm1, %xmm0
addsd 0x20797(%rip), %xmm0    # 0xf30f8
movsd %xmm0, -0x28(%rbp)
movsd %xmm4, -0x10(%rbp)
callq 0xd18b7
movsd -0x20(%rbp), %xmm2
movsd -0x10(%rbp), %xmm3
jmp 0xd2926
xorpd %xmm0, %xmm0
jmp 0xd2b14
movsd %xmm2, -0x10(%rbp)
movsd 0x230a6(%rip), %xmm1    # 0xf5a38
pushq $0x28
popq %rbx
movsd %xmm1, -0x38(%rbp)
movsd %xmm1, -0x40(%rbp)
subl $0x1, %ebx
jb 0xd2b0f
ucomisd %xmm4, %xmm0
jb 0xd29f8
ucomisd %xmm3, %xmm4
jb 0xd29f8
movsd %xmm0, -0x30(%rbp)
movsd -0x40(%rbp), %xmm2
movsd 0x206a2(%rip), %xmm1    # 0xf3068
mulsd %xmm1, %xmm2
movsd -0x10(%rbp), %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, -0x10(%rbp)
callq 0xd18b7
movsd 0x20683(%rip), %xmm5    # 0xf3068
movsd -0x10(%rbp), %xmm4
movapd %xmm0, %xmm2
movsd -0x18(%rbp), %xmm3
jmp 0xd2ab9
ucomisd %xmm0, %xmm4
movsd -0x18(%rbp), %xmm0
jb 0xd2a3f
ucomisd %xmm4, %xmm3
jb 0xd2a3f
movsd -0x38(%rbp), %xmm1
mulsd 0x22fea(%rip), %xmm1    # 0xf5a00
addsd %xmm1, %xmm0
movsd %xmm0, -0x18(%rbp)
movsd %xmm3, -0x28(%rbp)
callq 0xd18b7
movsd -0x18(%rbp), %xmm3
movsd -0x28(%rbp), %xmm2
movsd %xmm0, -0x30(%rbp)
movsd -0x10(%rbp), %xmm4
jmp 0xd2ab1
movsd -0x10(%rbp), %xmm1
movapd %xmm1, %xmm2
movapd %xmm1, %xmm4
movsd -0x20(%rbp), %xmm3
subsd %xmm3, %xmm2
subsd %xmm0, %xmm3
movsd 0x431c7(%rip), %xmm1    # 0x115c28
divsd %xmm1, %xmm3
movsd %xmm3, -0x20(%rbp)
divsd %xmm1, %xmm2
addsd %xmm3, %xmm0
movsd %xmm0, -0x18(%rbp)
movsd %xmm2, -0x40(%rbp)
subsd %xmm2, %xmm4
movsd %xmm4, -0x10(%rbp)
callq 0xd18b7
movsd %xmm0, -0x30(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0xd18b7
movsd -0x18(%rbp), %xmm3
movsd -0x10(%rbp), %xmm4
movapd %xmm0, %xmm2
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd 0x205af(%rip), %xmm5    # 0xf3068
movapd %xmm3, %xmm0
subsd %xmm4, %xmm0
andpd 0x206c7(%rip), %xmm0    # 0xf3190
movsd 0x42c6f(%rip), %xmm1    # 0x115740
ucomisd %xmm0, %xmm1
movapd %xmm3, %xmm0
addsd %xmm4, %xmm0
mulsd %xmm5, %xmm0
ja 0xd2b14
movsd %xmm4, -0x10(%rbp)
movsd %xmm3, -0x18(%rbp)
movsd %xmm0, -0x20(%rbp)
movsd %xmm2, -0x28(%rbp)
callq 0xd18b7
movsd -0x28(%rbp), %xmm3
movapd %xmm0, %xmm4
movsd -0x30(%rbp), %xmm0
jmp 0xd299f
movsd -0x20(%rbp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %rbp
retq