// File: /workspace/github_c_makefile_star2_O1/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, 0x24048(%rip)     # 0x2d5f0
jne 0x95dd
leaq 0x1f01f(%rip), %rdi     # 0x285d0
callq 0x50c0
divsd 0x14dc2(%rip), %xmm0    # 0x1e380
movsd %xmm0, 0x24032(%rip)    # 0x2d5f8
leaq 0x2310f(%rip), %rax     # 0x2c6dc
testb $0x4, (%rax)
jne 0x9a9d
movb $0x1, 0x24013(%rip)     # 0x2d5f0
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 0x9663
sete %al
cmpl $0xa, %r14d
setl %cl
testb %al, %cl
jne 0x9663
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 0x9663
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
divsd 0x14d4f(%rip), %xmm0    # 0x1e388
callq 0x52c0
movsd 0x14d4a(%rip), %xmm1    # 0x1e390
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
mulsd 0x14d40(%rip), %xmm0    # 0x1e398
callq 0x52c0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
testl %r15d, %r15d
js 0x9685
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0x14d22(%rip), %xmm0    # 0x1e3a0
callq 0x52c0
jmp 0x96f0
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
mulsd 0x14d0b(%rip), %xmm1    # 0x1e3a0
addsd 0x14d0b(%rip), %xmm1    # 0x1e3a8
movapd 0x14deb(%rip), %xmm0    # 0x1e490
xorpd %xmm1, %xmm0
movapd %xmm0, %xmm2
cmpltsd %xmm1, %xmm2
movsd 0xb94e(%rip), %xmm3     # 0x15008
andpd %xmm2, %xmm3
movapd %xmm1, %xmm2
cmpltsd %xmm0, %xmm2
movsd 0x14ce1(%rip), %xmm4    # 0x1e3b0
andpd %xmm2, %xmm4
andnpd %xmm3, %xmm2
orpd %xmm2, %xmm4
movapd %xmm4, 0x20(%rsp)
maxsd %xmm1, %xmm0
callq 0x52c0
mulsd 0x20(%rsp), %xmm0
addsd 0x14cc0(%rip), %xmm0    # 0x1e3b8
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 0x14c9b(%rip), %xmm0    # 0x1e3c0
callq 0x52c0
addsd 0x8(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
addsd %xmm0, %xmm1
addsd 0x14c85(%rip), %xmm1    # 0x1e3c8
addsd 0x23ead(%rip), %xmm1    # 0x2d5f8
addsd 0x14c7d(%rip), %xmm1    # 0x1e3d0
movsd 0x14c7d(%rip), %xmm0    # 0x1e3d8
movsd %xmm1, 0x10(%rsp)
mulsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
divsd 0x14c6d(%rip), %xmm0    # 0x1e3e0
callq 0x52c0
mulsd 0x14c68(%rip), %xmm0    # 0x1e3e8
addsd 0x8(%rsp), %xmm0
addsd 0x14c62(%rip), %xmm0    # 0x1e3f0
addsd 0x14c62(%rip), %xmm0    # 0x1e3f8
movsd %xmm0, 0x8(%rsp)
divsd 0x14c3c(%rip), %xmm0    # 0x1e3e0
callq 0x52c0
mulsd 0x14c37(%rip), %xmm0    # 0x1e3e8
addsd 0x8(%rsp), %xmm0
mulsd 0x14c41(%rip), %xmm0    # 0x1e400
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, 0x8(%rsp)
callq 0x52b0
movapd %xmm0, %xmm1
movsd 0x14c2c(%rip), %xmm0    # 0x1e408
mulsd %xmm0, %xmm1
addsd 0x8(%rsp), %xmm1
subsd 0x18(%rsp), %xmm1
movapd %xmm1, 0x20(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x51b0
mulsd 0x14c03(%rip), %xmm0    # 0x1e408
addsd 0xb7fb(%rip), %xmm0     # 0x15008
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 0x14c53(%rip), %xmm1    # 0x1e490
maxsd %xmm2, %xmm1
ucomisd 0x14bc7(%rip), %xmm1    # 0x1e410
ja 0x97c5
mulsd 0x14bc1(%rip), %xmm0    # 0x1e418
callq 0x5250
mulsd 0x14bbc(%rip), %xmm0    # 0x1e420
callq 0x50d0
mulsd 0x14bb7(%rip), %xmm0    # 0x1e428
addsd %xmm0, %xmm0
addsd 0x14bb3(%rip), %xmm0    # 0x1e430
movsd %xmm0, 0x8(%rsp)
movsd 0x14b55(%rip), %xmm1    # 0x1e3e0
divsd %xmm1, %xmm0
callq 0x52c0
movapd %xmm0, %xmm1
movsd 0x14b48(%rip), %xmm0    # 0x1e3e8
mulsd %xmm0, %xmm1
addsd 0x8(%rsp), %xmm1
movsd %xmm1, 0x20(%rsp)
movsd 0x14b80(%rip), %xmm0    # 0x1e438
mulsd 0x10(%rsp), %xmm0
addsd 0x14b7a(%rip), %xmm0    # 0x1e440
movsd %xmm0, 0x8(%rsp)
divsd 0x14b0c(%rip), %xmm0    # 0x1e3e0
callq 0x52c0
mulsd 0x14b07(%rip), %xmm0    # 0x1e3e8
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x10(%rsp), %xmm1
mulsd 0x14b4d(%rip), %xmm1    # 0x1e448
addsd %xmm0, %xmm1
addsd 0x14b49(%rip), %xmm1    # 0x1e450
movsd %xmm1, 0x10(%rsp)
movapd %xmm1, %xmm0
divsd 0x14ac7(%rip), %xmm0    # 0x1e3e0
callq 0x52c0
movapd %xmm0, %xmm1
mulsd 0x14abe(%rip), %xmm1    # 0x1e3e8
addsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x38(%rsp)
movsd 0x40(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
addsd %xmm0, %xmm0
subsd %xmm1, %xmm0
mulsd 0x14aae(%rip), %xmm0    # 0x1e400
callq 0x52b0
mulsd 0x14af9(%rip), %xmm0    # 0x1e458
movsd %xmm0, 0x8(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x52b0
mulsd 0x14ae8(%rip), %xmm0    # 0x1e460
movsd %xmm0, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x52b0
movapd %xmm0, %xmm1
mulsd 0x14ad3(%rip), %xmm1    # 0x1e468
movsd 0x38(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
mulsd 0x14a47(%rip), %xmm1    # 0x1e400
movapd %xmm1, %xmm0
callq 0x52b0
mulsd 0x14aa6(%rip), %xmm0    # 0x1e470
movsd %xmm0, 0x18(%rsp)
movsd 0x38(%rsp), %xmm0
addsd %xmm0, %xmm0
mulsd 0x14a1e(%rip), %xmm0    # 0x1e400
callq 0x52b0
mulsd 0x14a89(%rip), %xmm0    # 0x1e478
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 0x149d9(%rip), %xmm0    # 0x1e400
callq 0x52b0
mulsd 0x14a4c(%rip), %xmm0    # 0x1e480
addsd 0x8(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
divsd 0x14992(%rip), %xmm0    # 0x1e3e0
callq 0x52c0
mulsd 0x1498d(%rip), %xmm0    # 0x1e3e8
addsd 0x8(%rsp), %xmm0
divsd 0x14977(%rip), %xmm0    # 0x1e3e0
movsd 0xb597(%rip), %xmm2     # 0x15008
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 0x1e54c(%rip), %rax     # 0x27ff0
movq (%rax), %rdi
leaq 0x14a02(%rip), %rsi     # 0x1e4b0
leaq 0x1eb1b(%rip), %rdx     # 0x285d0
movb $0x1, %al
callq 0x5210
jmp 0x95d6