// File: /workspace/github_cpp_makefile_star2_O2/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 0x857f2(%rip), %r15     # 0x15afc0
leaq 0x461e1(%rip), %rcx     # 0x11b9b6
cmpq $0x20, %rax
je 0xd57e4
movq %rcx, (%r15,%rax,8)
incq %rax
jmp 0xd57d5
leaq 0x221fad(%rip), %rax    # 0x2f7798
andq $0x0, (%rax)
leaq 0x221faa(%rip), %rax    # 0x2f77a0
andq $0x0, (%rax)
leaq 0x221fa7(%rip), %rax    # 0x2f77a8
andq $0x0, (%rax)
leaq 0x221fa4(%rip), %rax    # 0x2f77b0
andq $0x0, (%rax)
xorl %r13d, %r13d
xorl %edi, %edi
callq 0xbfac2
leaq 0x221e83(%rip), %rax    # 0x2f76a4
movl (%rax), %edi
leaq 0x221e72(%rip), %r12    # 0x2f769c
movl (%r12), %r14d
pushq $0x1
popq %rbx
movl %r14d, %esi
movl %ebx, %edx
callq 0xc2f07
addsd 0x407ed(%rip), %xmm0    # 0x116030
movsd %xmm0, -0x30(%rbp)
movl %r14d, %edi
callq 0x68914
movl %eax, (%r12)
cltq
leaq 0x7eea3(%rip), %rcx     # 0x154700
cvtsi2sdl (%rcx,%rax,4), %xmm1
movsd -0x30(%rbp), %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, -0x48(%rbp)
callq 0xd18b7
movsd -0x30(%rbp), %xmm1
movapd %xmm0, %xmm2
leaq 0x221e1b(%rip), %r14    # 0x2f76a0
xorl %r12d, %r12d
movl $0x0, -0x34(%rbp)
movsd -0x48(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xd59e0
movsd %xmm2, -0x50(%rbp)
addsd 0x32f4d(%rip), %xmm1    # 0x1087f8
movsd %xmm1, -0x30(%rbp)
movapd %xmm1, %xmm0
callq 0xd18b7
movsd %xmm0, -0x40(%rbp)
xorl %edi, %edi
movsd -0x30(%rbp), %xmm0
callq 0xbfac2
movl (%r14), %ecx
cmpl $0x20, %ecx
jb 0xd58d8
andl $0x0, (%r14)
xorl %ecx, %ecx
xorl %eax, %eax
movsd 0x1d786(%rip), %xmm0    # 0xf3068
movsd -0x50(%rbp), %xmm2
ucomisd %xmm2, %xmm0
movl %ecx, %ecx
jbe 0xd590e
movsd -0x40(%rbp), %xmm3
ucomisd %xmm0, %xmm3
movsd -0x30(%rbp), %xmm1
jb 0xd5918
leaq 0x4922d(%rip), %rax     # 0x11eb33
movq %rax, (%r15,%rcx,8)
movl %ebx, %eax
jmp 0xd5918
movsd -0x30(%rbp), %xmm1
movsd -0x40(%rbp), %xmm3
ucomisd %xmm0, %xmm2
jb 0xd5932
ucomisd %xmm3, %xmm0
jbe 0xd5932
leaq 0x49208(%rip), %rax     # 0x11eb33
movq %rax, (%r15,%rcx,8)
pushq $0x2
popq %rax
testl %r13d, %r13d
jle 0xd596e
ucomisd %xmm3, %xmm2
jbe 0xd59ac
testl %r12d, %r12d
leaq 0x491f6(%rip), %rax     # 0x11eb3d
leaq 0x491f9(%rip), %rdx     # 0x11eb47
cmoveq %rdx, %rax
leaq 0x221e4f(%rip), %rdx    # 0x2f77a8
cmoveq 0x645ff(%rip), %rdx     # 0x139f60
movq %rax, (%r15,%rcx,8)
movsd %xmm1, (%rdx)
incl %r12d
jmp 0xd59a9
je 0xd59ac
ucomisd %xmm2, %xmm3
jbe 0xd59ac
movl -0x34(%rbp), %esi
testl %esi, %esi
leaq 0x491cf(%rip), %rax     # 0x11eb51
leaq 0x491d3(%rip), %rdx     # 0x11eb5c
cmoveq %rdx, %rax
leaq 0x221e1c(%rip), %rdx    # 0x2f77b0
cmoveq 0x64534(%rip), %rdx     # 0x139ed0
movq %rax, (%r15,%rcx,8)
movsd %xmm1, (%rdx)
incl %esi
movl %esi, -0x34(%rbp)
pushq $0x3
popq %rax
ucomisd %xmm2, %xmm3
movl %ebx, %ecx
ja 0xd59c0
ucomisd %xmm3, %xmm2
movl %r13d, %ecx
jbe 0xd59c0
pushq $-0x1
popq %rcx
movapd %xmm1, %xmm0
addsd 0x32d4c(%rip), %xmm0    # 0x108718
testl %eax, %eax
je 0xd59d4
movapd %xmm0, %xmm1
movapd %xmm3, %xmm2
movl %ecx, %r13d
jmp 0xd588f
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq