// File: 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
subq $0x60, %rsp
movsd %xmm0, -0x8(%rbp)
movl %edi, -0xc(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x20, -0x44(%rbp)
jge 0x13f4ae
movslq -0x44(%rbp), %rcx
leaq 0x90d08(%rip), %rax     # 0x1d01a0
leaq 0x550f3(%rip), %rdx     # 0x194592
movq %rdx, (%rax,%rcx,8)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x13f487
leaq 0x22d4c3(%rip), %rax    # 0x36c978
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x22d4bd(%rip), %rax    # 0x36c980
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x22d4b7(%rip), %rax    # 0x36c988
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x22d4b1(%rip), %rax    # 0x36c990
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movsd -0x8(%rbp), %xmm0
xorl %edi, %edi
callq 0x11dd00
leaq 0x22d37d(%rip), %rax    # 0x36c884
movl (%rax), %edi
leaq 0x22d36c(%rip), %rax    # 0x36c87c
movl (%rax), %esi
movl $0x1, %edx
callq 0x122140
movsd %xmm0, -0x50(%rbp)
movl $0x1, %edi
xorl %edx, %edx
movsd 0x2fb38(%rip), %xmm0    # 0x16f068
movl %edx, %esi
callq 0x122210
movaps %xmm0, %xmm1
movsd -0x50(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
leaq 0x22d32d(%rip), %rax    # 0x36c87c
movl (%rax), %edi
callq 0x9c2e0
movl %eax, %ecx
leaq 0x22d31d(%rip), %rax    # 0x36c87c
movl %ecx, (%rax)
movsd -0x28(%rbp), %xmm0
leaq 0x22d30f(%rip), %rax    # 0x36c87c
movslq (%rax), %rcx
leaq 0x8a189(%rip), %rax     # 0x1c9700
cvtsi2sdl (%rax,%rcx,4), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x28(%rbp), %xmm0
callq 0x138e00
movsd %xmm0, -0x20(%rbp)
movl $0x0, -0x34(%rbp)
movsd -0x28(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13f822
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x1, %edi
xorl %edx, %edx
xorps %xmm0, %xmm0
movl %edx, %esi
callq 0x122210
movaps %xmm0, %xmm1
movsd -0x58(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
callq 0x138e00
movsd %xmm0, -0x18(%rbp)
movsd -0x8(%rbp), %xmm0
xorl %edi, %edi
callq 0x11dd00
leaq 0x22d283(%rip), %rax    # 0x36c880
cmpl $0x0, (%rax)
jl 0x13f60e
leaq 0x22d277(%rip), %rax    # 0x36c880
cmpl $0x1f, (%rax)
jle 0x13f61b
leaq 0x22d26b(%rip), %rax    # 0x36c880
movl $0x0, (%rax)
movl $0x0, -0x40(%rbp)
movsd 0x2fa66(%rip), %xmm0    # 0x16f090
ucomisd -0x20(%rbp), %xmm0
jbe 0x13f667
movsd -0x18(%rbp), %xmm0
movsd 0x2fa52(%rip), %xmm1    # 0x16f090
ucomisd %xmm1, %xmm0
jb 0x13f667
leaq 0x22d235(%rip), %rax    # 0x36c880
movslq (%rax), %rcx
leaq 0x90b4b(%rip), %rax     # 0x1d01a0
leaq 0x58149(%rip), %rdx     # 0x1977a5
movq %rdx, (%rax,%rcx,8)
movl $0x1, -0x40(%rbp)
movsd -0x20(%rbp), %xmm0
movsd 0x2fa1c(%rip), %xmm1    # 0x16f090
ucomisd %xmm1, %xmm0
jb 0x13f6ac
movsd 0x2fa0e(%rip), %xmm0    # 0x16f090
ucomisd -0x18(%rbp), %xmm0
jbe 0x13f6ac
leaq 0x22d1f0(%rip), %rax    # 0x36c880
movslq (%rax), %rcx
leaq 0x90b06(%rip), %rax     # 0x1d01a0
leaq 0x58104(%rip), %rdx     # 0x1977a5
movq %rdx, (%rax,%rcx,8)
movl $0x2, -0x40(%rbp)
cmpl $0x0, -0x34(%rbp)
jle 0x13f73b
movsd -0x18(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13f736
cmpl $0x0, -0x38(%rbp)
je 0x13f6fa
leaq 0x22d1ad(%rip), %rax    # 0x36c880
movslq (%rax), %rcx
leaq 0x90ac3(%rip), %rax     # 0x1d01a0
leaq 0x580cb(%rip), %rdx     # 0x1977af
movq %rdx, (%rax,%rcx,8)
movsd -0x8(%rbp), %xmm0
leaq 0x22d294(%rip), %rax    # 0x36c988
movsd %xmm0, (%rax)
jmp 0x13f726
leaq 0x22d17f(%rip), %rax    # 0x36c880
movslq (%rax), %rcx
leaq 0x90a95(%rip), %rax     # 0x1d01a0
leaq 0x580a7(%rip), %rdx     # 0x1977b9
movq %rdx, (%rax,%rcx,8)
movsd -0x8(%rbp), %xmm0
leaq 0x22d25e(%rip), %rax    # 0x36c980
movsd %xmm0, (%rax)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl $0x3, -0x40(%rbp)
jmp 0x13f7c1
cmpl $0x0, -0x34(%rbp)
jge 0x13f7bf
movsd -0x18(%rbp), %xmm0
ucomisd -0x20(%rbp), %xmm0
jbe 0x13f7bd
cmpl $0x0, -0x3c(%rbp)
je 0x13f781
leaq 0x22d126(%rip), %rax    # 0x36c880
movslq (%rax), %rcx
leaq 0x90a3c(%rip), %rax     # 0x1d01a0
leaq 0x58058(%rip), %rdx     # 0x1977c3
movq %rdx, (%rax,%rcx,8)
movsd -0x8(%rbp), %xmm0
leaq 0x22d215(%rip), %rax    # 0x36c990
movsd %xmm0, (%rax)
jmp 0x13f7ad
leaq 0x22d0f8(%rip), %rax    # 0x36c880
movslq (%rax), %rcx
leaq 0x90a0e(%rip), %rax     # 0x1d01a0
leaq 0x58035(%rip), %rdx     # 0x1977ce
movq %rdx, (%rax,%rcx,8)
movsd -0x8(%rbp), %xmm0
leaq 0x22d1cf(%rip), %rax    # 0x36c978
movsd %xmm0, (%rax)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl $0x4, -0x40(%rbp)
jmp 0x13f7bf
jmp 0x13f7c1
movsd -0x18(%rbp), %xmm0
ucomisd -0x20(%rbp), %xmm0
jbe 0x13f7d6
movl $0x1, -0x34(%rbp)
jmp 0x13f7ef
movsd -0x18(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13f7ed
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
jmp 0x13f7ef
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x13f813
movsd 0x43869(%rip), %xmm0    # 0x183070
addsd -0x8(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x13f81d
movsd -0x8(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x13f59b
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)