// File: /workspace/github_c_makefile_star2_O2/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, 0x246e0(%rip)     # 0x2d5e0
jne 0x8f35
leaq 0x1f6b7(%rip), %rdi     # 0x285c0
callq 0x50c0
divsd 0x1446a(%rip), %xmm0    # 0x1d380
movsd %xmm0, 0x246ca(%rip)    # 0x2d5e8
leaq 0x237a7(%rip), %rax     # 0x2c6cc
testb $0x4, (%rax)
jne 0x93c9
movb $0x1, 0x246ab(%rip)     # 0x2d5e0
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
cvtsi2sd %r15d, %xmm3
cmpl $0x62e, %ebp            # imm = 0x62E
jl 0x8fc6
sete %al
cmpl $0xa, %r14d
setl %cl
testb %al, %cl
jne 0x8fc6
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 0x8fc6
movapd %xmm3, %xmm0
divsd 0x143f6(%rip), %xmm0    # 0x1d388
movapd %xmm3, (%rsp)
callq 0x52b0
movsd 0x143ec(%rip), %xmm1    # 0x1d390
subsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
mulsd 0x143e2(%rip), %xmm0    # 0x1d398
callq 0x52b0
movapd (%rsp), %xmm3
addsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rsp)
mulsd 0x143cd(%rip), %xmm3    # 0x1d3a0
testl %r15d, %r15d
js 0x8fe3
movapd %xmm3, %xmm0
callq 0x52b0
jmp 0x903e
addsd 0x143bd(%rip), %xmm3    # 0x1d3a8
movapd 0x1449d(%rip), %xmm0    # 0x1d490
xorpd %xmm3, %xmm0
movapd %xmm0, %xmm1
cmpltsd %xmm3, %xmm1
movsd 0xb000(%rip), %xmm2     # 0x14008
andpd %xmm1, %xmm2
movapd %xmm3, %xmm1
cmpltsd %xmm0, %xmm1
movsd 0x14393(%rip), %xmm4    # 0x1d3b0
andpd %xmm1, %xmm4
andnpd %xmm2, %xmm1
orpd %xmm1, %xmm4
movapd %xmm4, 0x20(%rsp)
maxsd %xmm3, %xmm0
callq 0x52b0
mulsd 0x20(%rsp), %xmm0
addsd 0x14372(%rip), %xmm0    # 0x1d3b8
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
leal 0xc(%r14), %eax
cmpl $0x3, %r14d
cmovgel %r14d, %eax
incl %eax
cvtsi2sd %eax, %xmm0
mulsd 0x14352(%rip), %xmm0    # 0x1d3c0
callq 0x52b0
addsd (%rsp), %xmm0
cvtsi2sd %ebx, %xmm1
addsd %xmm0, %xmm1
addsd 0x14340(%rip), %xmm1    # 0x1d3c8
addsd 0x24558(%rip), %xmm1    # 0x2d5e8
addsd 0x14338(%rip), %xmm1    # 0x1d3d0
movsd 0x14338(%rip), %xmm0    # 0x1d3d8
movsd %xmm1, 0x10(%rsp)
mulsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
divsd 0x14329(%rip), %xmm0    # 0x1d3e0
callq 0x52b0
mulsd 0x14324(%rip), %xmm0    # 0x1d3e8
addsd (%rsp), %xmm0
addsd 0x1431f(%rip), %xmm0    # 0x1d3f0
addsd 0x1431f(%rip), %xmm0    # 0x1d3f8
movsd %xmm0, (%rsp)
divsd 0x142fa(%rip), %xmm0    # 0x1d3e0
callq 0x52b0
mulsd 0x142f5(%rip), %xmm0    # 0x1d3e8
addsd (%rsp), %xmm0
mulsd 0x14300(%rip), %xmm0    # 0x1d400
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, (%rsp)
callq 0x52a0
movapd %xmm0, %xmm1
movsd 0x142ec(%rip), %xmm0    # 0x1d408
mulsd %xmm0, %xmm1
addsd (%rsp), %xmm1
subsd 0x18(%rsp), %xmm1
movapd %xmm1, 0x20(%rsp)
movsd (%rsp), %xmm0
callq 0x51a0
mulsd 0x142c5(%rip), %xmm0    # 0x1d408
addsd 0xaebd(%rip), %xmm0     # 0x14008
movapd 0x20(%rsp), %xmm2
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
movsd (%rsp), %xmm0
movapd %xmm2, %xmm1
xorpd 0x14318(%rip), %xmm1    # 0x1d490
maxsd %xmm2, %xmm1
ucomisd 0x1428c(%rip), %xmm1    # 0x1d410
ja 0x9106
mulsd 0x1428a(%rip), %xmm0    # 0x1d418
callq 0x5240
mulsd 0x14285(%rip), %xmm0    # 0x1d420
callq 0x50d0
mulsd 0x14280(%rip), %xmm0    # 0x1d428
addsd %xmm0, %xmm0
addsd 0x1427c(%rip), %xmm0    # 0x1d430
movsd %xmm0, (%rsp)
movsd 0x1421f(%rip), %xmm1    # 0x1d3e0
divsd %xmm1, %xmm0
callq 0x52b0
movapd %xmm0, %xmm1
movsd 0x14212(%rip), %xmm0    # 0x1d3e8
mulsd %xmm0, %xmm1
addsd (%rsp), %xmm1
movsd %xmm1, 0x20(%rsp)
movsd 0x1424b(%rip), %xmm0    # 0x1d438
mulsd 0x10(%rsp), %xmm0
addsd 0x14245(%rip), %xmm0    # 0x1d440
movsd %xmm0, (%rsp)
divsd 0x141d8(%rip), %xmm0    # 0x1d3e0
callq 0x52b0
mulsd 0x141d3(%rip), %xmm0    # 0x1d3e8
addsd (%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x10(%rsp), %xmm1
mulsd 0x1421a(%rip), %xmm1    # 0x1d448
addsd %xmm0, %xmm1
addsd 0x14216(%rip), %xmm1    # 0x1d450
movsd %xmm1, 0x10(%rsp)
movapd %xmm1, %xmm0
divsd 0x14194(%rip), %xmm0    # 0x1d3e0
callq 0x52b0
movapd %xmm0, %xmm1
mulsd 0x1418b(%rip), %xmm1    # 0x1d3e8
addsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x38(%rsp)
movsd 0x40(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
addsd %xmm0, %xmm0
subsd %xmm1, %xmm0
mulsd 0x1417b(%rip), %xmm0    # 0x1d400
callq 0x52a0
mulsd 0x141c6(%rip), %xmm0    # 0x1d458
movsd %xmm0, (%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x52a0
mulsd 0x141b6(%rip), %xmm0    # 0x1d460
movsd %xmm0, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x52a0
movapd %xmm0, %xmm1
mulsd 0x141a1(%rip), %xmm1    # 0x1d468
movsd 0x38(%rsp), %xmm0
addsd (%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
mulsd 0x14116(%rip), %xmm1    # 0x1d400
movapd %xmm1, %xmm0
callq 0x52a0
mulsd 0x14175(%rip), %xmm0    # 0x1d470
movsd %xmm0, 0x18(%rsp)
movsd 0x38(%rsp), %xmm0
addsd %xmm0, %xmm0
mulsd 0x140ed(%rip), %xmm0    # 0x1d400
callq 0x52a0
mulsd 0x14158(%rip), %xmm0    # 0x1d478
movsd (%rsp), %xmm1
addsd 0x40(%rsp), %xmm1
addsd 0x18(%rsp), %xmm1
subsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movapd %xmm1, %xmm0
subsd 0x20(%rsp), %xmm0
addsd %xmm0, %xmm0
mulsd 0x140aa(%rip), %xmm0    # 0x1d400
callq 0x52a0
mulsd 0x1411d(%rip), %xmm0    # 0x1d480
addsd (%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rsp)
divsd 0x14065(%rip), %xmm0    # 0x1d3e0
callq 0x52b0
mulsd 0x14060(%rip), %xmm0    # 0x1d3e8
addsd (%rsp), %xmm0
divsd 0x1404b(%rip), %xmm0    # 0x1d3e0
movsd 0xac6b(%rip), %xmm2     # 0x14008
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 0x1ec28(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x140d6(%rip), %rsi     # 0x1d4b0
leaq 0x1f1df(%rip), %rdx     # 0x285c0
movb $0x1, %al
callq 0x5200
jmp 0x8f2e