// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathmsc.cpp
// Function: calc_moons(double, int)
void calc_moons(double jd, int why)
{
double p, last_p;
double last_jd, jd_end;
int rising;
int blue_moon;
int black_moon;
int new_event;
int i;

   // Find all primary moon phase events for the month and flag them in
   // the array moons[].  These are used to label the analog watch face.

   for(i=0; i<32; i++) moons[i] = "";  // initalize moon phase names

   new_moon_jd = 0.0;
   full_moon_jd = 0.0;
   blue_moon_jd = 0.0;
   black_moon_jd = 0.0;
   blue_moon  = 0;       // counts full moons
   black_moon = 0;       // counts new moons

   gregorian(0, jd);
   last_jd = jdate(g_year,g_month,1) - jtime(1,0,0,1.0);
//last_jd = jdate(year,month,1) - jtime(1,0,0,0.0); 
   g_month = fix_month(g_month);
   jd_end = last_jd + (double) (dim[g_month]);

   last_p  = MOON_PHASE(last_jd);
   rising  = 0;

   while(last_jd < jd_end) {  // search the month for interesting lunar events
      jd = last_jd + jtime(1,0,0,0.0);
      p = MOON_PHASE(jd);
////  gregorian(0, jd + time_zone() + jtime(0,0,0,TSX_TWEAK));  // calculate moon time in local time zone
      gregorian(0, jd);  // calculate moon time in utc
      if((g_day < 0) || (g_day > 31)) g_day = 0;

      new_event = 0;

      if((last_p < 0.50) && (p >= 0.50)) {  // waxing quarter moon
         moons[g_day] = "Qtr Moon ";
         new_event = 1;
      }
      if((last_p >= 0.50) && (p < 0.50)) {  // waning quarter moon
         moons[g_day] = "Qtr Moon ";
         new_event = 2;
      }

      if(rising > 0) {
         if(p < last_p) {
            if(blue_moon) {  // moon started falling
               moons[g_day] = "Blue Moon";
               blue_moon_jd = jd;
            }
            else {
               moons[g_day] = "Full Moon";
               full_moon_jd = jd;
            }
            ++blue_moon;
            new_event = 3;

         }
      }
      else if(rising < 0) {  // moon started rising
         if(p > last_p) {
            if(black_moon) {
               moons[g_day] = "Black Moon";
               black_moon_jd = jd;
            }
            else {  // new moon 
               moons[g_day] = "New Moon";
               new_moon_jd = jd;
            }
            ++black_moon;
            new_event = 4;
         }
      }

      if     (p > last_p) rising = (+1);
      else if(p < last_p) rising = (-1);

      last_p = p;
      if(new_event) last_jd = jd + 6.00;  // next possible event averages 7.35 days away, we use 6 for safety
      else          last_jd = jd;         // keep seaching hour-by-hour
   }

   if(0 && debug_file) { 
      if(new_moon_jd) {
         gregorian(0, new_moon_jd + time_zone());
         fprintf(debug_file, "new moon:  %04d/%02d/%02d  %02d:%02d:%02d\n", g_year,g_month,g_day, g_hours,g_minutes,g_seconds);
      }
      if(full_moon_jd) {
         gregorian(0, full_moon_jd + time_zone());
         fprintf(debug_file, "full moon: %04d/%02d/%02d  %02d:%02d:%02d\n", g_year,g_month,g_day, g_hours,g_minutes,g_seconds);
      }
      if(blue_moon_jd) {
         gregorian(0, blue_moon_jd + time_zone());
         fprintf(debug_file, "blue moon: %04d/%02d/%02d  %02d:%02d:%02d\n", g_year,g_month,g_day, g_hours,g_minutes,g_seconds);
      }
      if(black_moon_jd) {
         gregorian(0, black_moon_jd + time_zone());
         fprintf(debug_file, "black moon: %04d/%02d/%02d  %02d:%02d:%02d\n", g_year,g_month,g_day, g_hours,g_minutes,g_seconds);
      }
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorl %eax, %eax
leaq 0x87206(%rip), %r14     # 0x170fc0
leaq 0x47cd5(%rip), %rcx     # 0x131a96
movq %rcx, (%r14,%rax,8)
incq %rax
cmpq $0x20, %rax
jne 0xe9dc1
leaq 0x2239c3(%rip), %rax    # 0x30d798
xorl %r12d, %r12d
movq %r12, (%rax)
leaq 0x2239be(%rip), %rax    # 0x30d7a0
movq %r12, (%rax)
leaq 0x2239bc(%rip), %rax    # 0x30d7a8
movq %r12, (%rax)
leaq 0x2239ba(%rip), %rax    # 0x30d7b0
movq %r12, (%rax)
xorl %edi, %edi
callq 0xd12da
leaq 0x22389d(%rip), %rax    # 0x30d6a4
movl (%rax), %edi
leaq 0x22388c(%rip), %r15    # 0x30d69c
movl (%r15), %ebx
movl %ebx, %esi
movl $0x1, %edx
callq 0xd485e
addsd 0x422d9(%rip), %xmm0    # 0x12c100
movsd %xmm0, -0x30(%rbp)
movl %ebx, %edi
callq 0x723a5
movl %eax, (%r15)
cltq
leaq 0x808c1(%rip), %rcx     # 0x16a700
cvtsi2sdl (%rcx,%rax,4), %xmm1
movsd -0x30(%rbp), %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
callq 0xe5033
movsd -0x30(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0xe9fd3
leaq 0x22382e(%rip), %rbx    # 0x30d6a0
xorl %r15d, %r15d
xorl %r13d, %r13d
movsd %xmm0, -0x48(%rbp)
addsd 0x349fb(%rip), %xmm1    # 0x11e880
movsd %xmm1, -0x30(%rbp)
movapd %xmm1, %xmm0
callq 0xe5033
movsd %xmm0, -0x40(%rbp)
xorl %edi, %edi
movsd -0x30(%rbp), %xmm0
callq 0xd12da
movl (%rbx), %ecx
cmpl $0x20, %ecx
jb 0xe9eb3
movl $0x0, (%rbx)
xorl %ecx, %ecx
xorl %eax, %eax
movsd 0x1f1ab(%rip), %xmm0    # 0x109068
movsd -0x48(%rbp), %xmm2
ucomisd %xmm2, %xmm0
jbe 0xe9eec
movsd -0x40(%rbp), %xmm3
ucomisd %xmm0, %xmm3
movsd -0x30(%rbp), %xmm1
jb 0xe9ef6
movl %ecx, %eax
leaq 0x4ad32(%rip), %rdx     # 0x134c13
movq %rdx, (%r14,%rax,8)
movl $0x1, %eax
jmp 0xe9ef6
movsd -0x30(%rbp), %xmm1
movsd -0x40(%rbp), %xmm3
ucomisd %xmm0, %xmm2
jb 0xe9f14
ucomisd %xmm3, %xmm0
jbe 0xe9f14
movl %ecx, %eax
leaq 0x4ad08(%rip), %rdx     # 0x134c13
movq %rdx, (%r14,%rax,8)
movl $0x2, %eax
testl %r13d, %r13d
jle 0xe9f52
ucomisd %xmm3, %xmm2
jbe 0xe9f90
testl %r15d, %r15d
leaq 0x4acf4(%rip), %rax     # 0x134c1d
leaq 0x4acf7(%rip), %rdx     # 0x134c27
cmoveq %rdx, %rax
movl %ecx, %ecx
leaq 0x22386b(%rip), %rdx    # 0x30d7a8
cmoveq 0x66013(%rip), %rdx     # 0x14ff58
movq %rax, (%r14,%rcx,8)
movsd %xmm1, (%rdx)
incl %r15d
jmp 0xe9f8b
je 0xe9f90
ucomisd %xmm2, %xmm3
jbe 0xe9f90
testl %r12d, %r12d
leaq 0x4accd(%rip), %rax     # 0x134c31
leaq 0x4acd1(%rip), %rdx     # 0x134c3c
cmoveq %rdx, %rax
movl %ecx, %ecx
leaq 0x223838(%rip), %rdx    # 0x30d7b0
cmoveq 0x65f48(%rip), %rdx     # 0x14fec8
movq %rax, (%r14,%rcx,8)
movsd %xmm1, (%rdx)
incl %r12d
movl $0x3, %eax
movl $0x1, %ecx
ucomisd %xmm2, %xmm3
ja 0xe9fa9
ucomisd %xmm3, %xmm2
movl %r13d, %ecx
jbe 0xe9fa9
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movapd %xmm1, %xmm0
addsd 0x347eb(%rip), %xmm0    # 0x11e7a0
testl %eax, %eax
je 0xe9fbd
movapd %xmm0, %xmm1
movsd -0x38(%rbp), %xmm0
ucomisd %xmm1, %xmm0
movl %ecx, %r13d
movapd %xmm3, %xmm0
ja 0xe9e78
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq