// File: /workspace/github_c_makefile_star2_O3/mmayer[P]pcal/src/moonphas.c
// Function: calc_phase
double calc_phase (int month, int inday, int year)
{
   double Day, N, M, Ec, Lambdasun, ml, MM;
   double Ev, Ae, A3, MmP, mEc, A4, lP, V, lPP, MoonAge, pdate, moon_phase;
   static int first = TRUE;
   static double utc_offset_days;

   /* Get the UTC offset on the first pass. 

      The original code used to normalize the UTC offset to +/- 12 hours.  But
      it was bug-ridden and also failed to take into account that some parts
      of the world have offsets from UTC greater than 12 hours!  Therefore,
      beginning with v2.0.0, we don't attempt to normalize the user-specified
      UTC timezone offset at all.
   */
   if (first) {
      
      utc_offset_days = atof(time_zone) / 24.0;
      
      if (DEBUG(DEBUG_MOON)) {
         fprintf(stderr, "time_zone='%s'  utc_offset_days = %.5lf\n", time_zone, utc_offset_days);
      }
      
      first = FALSE;
   }
   
   /*  need to convert month, day, year into a Julian pdate */
   pdate = julday(month, inday, year) + utc_offset_days;
   
   /*  Calculation of the Sun's position. */
   
   Day = pdate - epoch;   /* date within epoch */
   N = fixangle((360 / 365.2422) * Day);   /* mean anomaly of the Sun */
   M = fixangle(N + elonge - elongp);      /* convert from perigee
                                              co-ordinates to epoch 1980.0 */
   Ec = kepler(M, eccent);   /* solve equation of Kepler */
   Ec = sqrt((1 + eccent) / (1 - eccent)) * tan(Ec / 2);
   Ec = 2 * todeg(atan(Ec));   /* true anomaly */
   Lambdasun = fixangle(Ec + elongp);   /* Sun's geocentric ecliptic longitude */
   
   /*  Calculation of the Moon's position. */
   
   /*  Moon's mean longitude. */
   ml = fixangle(13.1763966 * Day + mmlong);
   
   /*  Moon's mean anomaly. */
   MM = fixangle(ml - 0.1114041 * Day - mmlongp);
   
   /*  Moon's ascending node mean longitude. */
   /*  Not used -- commented out. */
   /* MN = fixangle(mlnode - 0.0529539 * Day); */
   
   /*  Evection. */
   Ev = 1.2739 * sin(torad(2 * (ml - Lambdasun) - MM));
   
   /*  Annual equation. */
   Ae = 0.1858 * sin(torad(M));
   
   /*  Correction term. */
   A3 = 0.37 * sin(torad(M));
   
   /*  Corrected anomaly. */
   MmP = MM + Ev - Ae - A3;
   
   /*  Correction for the equation of the centre. */
   mEc = 6.2886 * sin(torad(MmP));
   
   /*  Another correction term. */
   A4 = 0.214 * sin(torad(2 * MmP));
   
   /*  Corrected longitude. */
   lP = ml + Ev + mEc - Ae + A4;
   
   /*  Variation. */
   V = 0.6583 * sin(torad(2 * (lP - Lambdasun)));
   
   /*  True longitude. */
   lPP = lP + V;
   
   /*  Calculation of the phase of the Moon. */
   
   /*  Age of the Moon in degrees. */
   MoonAge = lPP - Lambdasun;
   
   moon_phase = fixangle(MoonAge) / 360.0;
   if (moon_phase < 0.0) moon_phase += 1.0;

   /* fprintf(stderr, "Moon phase on %04d-%02d-%02d: %.5lf\n", year, month, inday, moon_phase); */

   return (moon_phase);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebp
movl %esi, %ebx
movl %edi, %r14d
cmpb $0x0, 0x24a70(%rip)     # 0x2e5f0
jne 0x9bb5
leaq 0x1fa47(%rip), %rdi     # 0x295d0
callq 0x50c0
divsd 0x15842(%rip), %xmm0    # 0x1f3d8
movsd %xmm0, 0x24a5a(%rip)    # 0x2e5f8
leaq 0x23b37(%rip), %rax     # 0x2d6dc
testb $0x4, (%rax)
jne 0xa075
movb $0x1, 0x24a3b(%rip)     # 0x2e5f0
movl %ebp, %eax
shrl $0x1f, %eax
xorl %ecx, %ecx
cmpl $0x3, %r14d
setl %cl
movl %ebp, %r15d
subl %ecx, %r15d
addl %eax, %r15d
xorpd %xmm0, %xmm0
cmpl $0x62e, %ebp            # imm = 0x62E
jl 0x9c3b
sete %al
cmpl $0xa, %r14d
setl %cl
testb %al, %cl
jne 0x9c3b
xorl $0x62e, %ebp            # imm = 0x62E
movl %r14d, %eax
xorl $0xa, %eax
cmpl $0xf, %ebx
setl %cl
orl %ebp, %eax
sete %al
testb %cl, %al
jne 0x9c3b
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
divsd 0x157cf(%rip), %xmm0    # 0x1f3e0
callq 0x52c0
movsd 0x157ca(%rip), %xmm1    # 0x1f3e8
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
mulsd 0x157c0(%rip), %xmm0    # 0x1f3f0
callq 0x52c0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
testl %r15d, %r15d
js 0x9c5d
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0x157a2(%rip), %xmm0    # 0x1f3f8
callq 0x52c0
jmp 0x9cc8
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
mulsd 0x1578b(%rip), %xmm1    # 0x1f3f8
addsd 0x1578b(%rip), %xmm1    # 0x1f400
movapd 0x15863(%rip), %xmm0    # 0x1f4e0
xorpd %xmm1, %xmm0
movapd %xmm0, %xmm2
cmpltsd %xmm1, %xmm2
movsd 0xc3ce(%rip), %xmm3     # 0x16060
andpd %xmm2, %xmm3
movapd %xmm1, %xmm2
cmpltsd %xmm0, %xmm2
movsd 0x15761(%rip), %xmm4    # 0x1f408
andpd %xmm2, %xmm4
andnpd %xmm3, %xmm2
orpd %xmm2, %xmm4
movapd %xmm4, 0x20(%rsp)
maxsd %xmm1, %xmm0
callq 0x52c0
mulsd 0x20(%rsp), %xmm0
addsd 0x15740(%rip), %xmm0    # 0x1f410
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
leal 0xc(%r14), %eax
cmpl $0x3, %r14d
cmovgel %r14d, %eax
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x1571b(%rip), %xmm0    # 0x1f418
callq 0x52c0
addsd 0x8(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
addsd %xmm0, %xmm1
addsd 0x15705(%rip), %xmm1    # 0x1f420
addsd 0x248d5(%rip), %xmm1    # 0x2e5f8
addsd 0x156fd(%rip), %xmm1    # 0x1f428
movsd 0x156fd(%rip), %xmm0    # 0x1f430
movsd %xmm1, 0x10(%rsp)
mulsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
divsd 0x156ed(%rip), %xmm0    # 0x1f438
callq 0x52c0
mulsd 0x156e8(%rip), %xmm0    # 0x1f440
addsd 0x8(%rsp), %xmm0
addsd 0x156e2(%rip), %xmm0    # 0x1f448
addsd 0x156e2(%rip), %xmm0    # 0x1f450
movsd %xmm0, 0x8(%rsp)
divsd 0x156bc(%rip), %xmm0    # 0x1f438
callq 0x52c0
mulsd 0x156b7(%rip), %xmm0    # 0x1f440
addsd 0x8(%rsp), %xmm0
mulsd 0x156c1(%rip), %xmm0    # 0x1f458
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, 0x8(%rsp)
callq 0x52b0
movapd %xmm0, %xmm1
movsd 0x156ac(%rip), %xmm0    # 0x1f460
mulsd %xmm0, %xmm1
addsd 0x8(%rsp), %xmm1
subsd 0x18(%rsp), %xmm1
movapd %xmm1, 0x20(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x51b0
mulsd 0x15683(%rip), %xmm0    # 0x1f460
addsd 0xc27b(%rip), %xmm0     # 0x16060
movapd 0x20(%rsp), %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
movapd %xmm2, %xmm1
xorpd 0x156cb(%rip), %xmm1    # 0x1f4e0
maxsd %xmm2, %xmm1
ucomisd 0x15647(%rip), %xmm1    # 0x1f468
ja 0x9d9d
mulsd 0x15641(%rip), %xmm0    # 0x1f470
callq 0x5250
mulsd 0x1563c(%rip), %xmm0    # 0x1f478
callq 0x50d0
mulsd 0x15637(%rip), %xmm0    # 0x1f480
addsd %xmm0, %xmm0
addsd 0x15633(%rip), %xmm0    # 0x1f488
movsd %xmm0, 0x8(%rsp)
movsd 0x155d5(%rip), %xmm1    # 0x1f438
divsd %xmm1, %xmm0
callq 0x52c0
movapd %xmm0, %xmm1
movsd 0x155c8(%rip), %xmm0    # 0x1f440
mulsd %xmm0, %xmm1
addsd 0x8(%rsp), %xmm1
movsd %xmm1, 0x20(%rsp)
movsd 0x15600(%rip), %xmm0    # 0x1f490
mulsd 0x10(%rsp), %xmm0
addsd 0x155fa(%rip), %xmm0    # 0x1f498
movsd %xmm0, 0x8(%rsp)
divsd 0x1558c(%rip), %xmm0    # 0x1f438
callq 0x52c0
mulsd 0x15587(%rip), %xmm0    # 0x1f440
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x10(%rsp), %xmm1
mulsd 0x155cd(%rip), %xmm1    # 0x1f4a0
addsd %xmm0, %xmm1
addsd 0x155c9(%rip), %xmm1    # 0x1f4a8
movsd %xmm1, 0x10(%rsp)
movapd %xmm1, %xmm0
divsd 0x15547(%rip), %xmm0    # 0x1f438
callq 0x52c0
movapd %xmm0, %xmm1
mulsd 0x1553e(%rip), %xmm1    # 0x1f440
addsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x38(%rsp)
movsd 0x40(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
addsd %xmm0, %xmm0
subsd %xmm1, %xmm0
mulsd 0x1552e(%rip), %xmm0    # 0x1f458
callq 0x52b0
mulsd 0x15579(%rip), %xmm0    # 0x1f4b0
movsd %xmm0, 0x8(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x52b0
mulsd 0x15568(%rip), %xmm0    # 0x1f4b8
movsd %xmm0, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x52b0
movapd %xmm0, %xmm1
mulsd 0x15553(%rip), %xmm1    # 0x1f4c0
movsd 0x38(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
mulsd 0x154c7(%rip), %xmm1    # 0x1f458
movapd %xmm1, %xmm0
callq 0x52b0
mulsd 0x15526(%rip), %xmm0    # 0x1f4c8
movsd %xmm0, 0x18(%rsp)
movsd 0x38(%rsp), %xmm0
addsd %xmm0, %xmm0
mulsd 0x1549e(%rip), %xmm0    # 0x1f458
callq 0x52b0
mulsd 0x15509(%rip), %xmm0    # 0x1f4d0
movsd 0x8(%rsp), %xmm1
addsd 0x40(%rsp), %xmm1
addsd 0x18(%rsp), %xmm1
subsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movapd %xmm1, %xmm0
subsd 0x20(%rsp), %xmm0
addsd %xmm0, %xmm0
mulsd 0x15459(%rip), %xmm0    # 0x1f458
callq 0x52b0
mulsd 0x154cc(%rip), %xmm0    # 0x1f4d8
addsd 0x8(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
divsd 0x15412(%rip), %xmm0    # 0x1f438
callq 0x52c0
mulsd 0x1540d(%rip), %xmm0    # 0x1f440
addsd 0x8(%rsp), %xmm0
divsd 0x153f7(%rip), %xmm0    # 0x1f438
movsd 0xc017(%rip), %xmm2     # 0x16060
addsd %xmm0, %xmm2
xorpd %xmm3, %xmm3
movapd %xmm0, %xmm1
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
movapd %xmm1, %xmm0
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x1ef74(%rip), %rax     # 0x28ff0
movq (%rax), %rdi
leaq 0x1547a(%rip), %rsi     # 0x1f500
leaq 0x1f543(%rip), %rdx     # 0x295d0
movb $0x1, %al
callq 0x5210
jmp 0x9bae