// File: /workspace/github_cpp_makefile_star2_O1/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 0x87c79(%rip), %r14     # 0x170fc0
leaq 0x48658(%rip), %rcx     # 0x1319a6
movq %rcx, (%r14,%rax,8)
incq %rax
cmpq $0x20, %rax
jne 0xe934e
leaq 0x224436(%rip), %rax    # 0x30d798
xorl %r12d, %r12d
movq %r12, (%rax)
leaq 0x224431(%rip), %rax    # 0x30d7a0
movq %r12, (%rax)
leaq 0x22442f(%rip), %rax    # 0x30d7a8
movq %r12, (%rax)
leaq 0x22442d(%rip), %rax    # 0x30d7b0
movq %r12, (%rax)
xorl %edi, %edi
callq 0xd0c46
leaq 0x224310(%rip), %rax    # 0x30d6a4
movl (%rax), %edi
leaq 0x2242ff(%rip), %r15    # 0x30d69c
movl (%r15), %ebx
movl %ebx, %esi
movl $0x1, %edx
callq 0xd4110
addsd 0x42c6c(%rip), %xmm0    # 0x12c020
movsd %xmm0, -0x30(%rbp)
movl %ebx, %edi
callq 0x724e5
movl %eax, (%r15)
cltq
leaq 0x81334(%rip), %rcx     # 0x16a700
cvtsi2sdl (%rcx,%rax,4), %xmm1
movsd -0x30(%rbp), %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
callq 0xe4462
movsd -0x30(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0xe9560
leaq 0x2242a1(%rip), %rbx    # 0x30d6a0
xorl %r15d, %r15d
xorl %r13d, %r13d
movsd %xmm0, -0x48(%rbp)
addsd 0x353be(%rip), %xmm1    # 0x11e7d0
movsd %xmm1, -0x30(%rbp)
movapd %xmm1, %xmm0
callq 0xe4462
movsd %xmm0, -0x40(%rbp)
xorl %edi, %edi
movsd -0x30(%rbp), %xmm0
callq 0xd0c46
cmpl $0x20, (%rbx)
jb 0xe943c
movl $0x0, (%rbx)
xorl %eax, %eax
movsd 0x1fc22(%rip), %xmm0    # 0x109068
movsd -0x48(%rbp), %xmm2
ucomisd %xmm2, %xmm0
jbe 0xe9476
movsd -0x40(%rbp), %xmm3
ucomisd %xmm0, %xmm3
movsd -0x30(%rbp), %xmm1
jb 0xe9480
movslq (%rbx), %rax
leaq 0x4b6b8(%rip), %rcx     # 0x134b23
movq %rcx, (%r14,%rax,8)
movl $0x1, %eax
jmp 0xe9480
movsd -0x30(%rbp), %xmm1
movsd -0x40(%rbp), %xmm3
ucomisd %xmm0, %xmm2
jb 0xe949f
ucomisd %xmm3, %xmm0
jbe 0xe949f
movslq (%rbx), %rax
leaq 0x4b68d(%rip), %rcx     # 0x134b23
movq %rcx, (%r14,%rax,8)
movl $0x2, %eax
testl %r13d, %r13d
jle 0xe94de
ucomisd %xmm3, %xmm2
jbe 0xe951d
testl %r15d, %r15d
leaq 0x4b679(%rip), %rax     # 0x134b2d
leaq 0x4b67c(%rip), %rcx     # 0x134b37
cmoveq %rcx, %rax
movslq (%rbx), %rcx
leaq 0x2242df(%rip), %rdx    # 0x30d7a8
cmoveq 0x66a87(%rip), %rdx     # 0x14ff58
movq %rax, (%r14,%rcx,8)
movsd %xmm1, (%rdx)
incl %r15d
jmp 0xe9518
je 0xe951d
ucomisd %xmm2, %xmm3
jbe 0xe951d
testl %r12d, %r12d
leaq 0x4b651(%rip), %rax     # 0x134b41
leaq 0x4b655(%rip), %rcx     # 0x134b4c
cmoveq %rcx, %rax
movslq (%rbx), %rcx
leaq 0x2242ab(%rip), %rdx    # 0x30d7b0
cmoveq 0x669bb(%rip), %rdx     # 0x14fec8
movq %rax, (%r14,%rcx,8)
movsd %xmm1, (%rdx)
incl %r12d
movl $0x3, %eax
movl $0x1, %ecx
ucomisd %xmm2, %xmm3
ja 0xe9536
ucomisd %xmm3, %xmm2
movl %r13d, %ecx
jbe 0xe9536
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movapd %xmm1, %xmm0
addsd 0x351ae(%rip), %xmm0    # 0x11e6f0
testl %eax, %eax
je 0xe954a
movapd %xmm0, %xmm1
movsd -0x38(%rbp), %xmm0
ucomisd %xmm1, %xmm0
movl %ecx, %r13d
movapd %xmm3, %xmm0
ja 0xe9405
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq