// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathmsc.cpp
// Function: sun_posn(double, int)
double sun_posn(double jd, int moon_flag)
{
double RightAscension;
double Declination;
double HourAngle;
double Zenith;
double Azimuth;
double t, te, wte, s1, c1, s2, c2, s3, c3;
double sp, cp, sd, cd, sH, cH, se0, ep, De, lambda, epsi;
double sl, cl, se, ce, L, nu, Dlam;
double Temperature = 15.0;  // degrees C
double Pressure = 1.0;      // atmospheres
double tt;

   // Calculate sun position using Grena's Algorithm 5
   // Very accurate results (<0.0027 degrees) for years 2010 to 2110.
   // jd is jd_utc.

++sp_count;

   if(moon_flag) {       // royal kludge to calculate moon rise/transit/set
      moon_posn(jd);     // ... this kludge lets us use the calc_sunrise()
      sun_az = moon_az;  // ... code to do the moon, but the results are
      sun_el = moon_el;  // ... saved in the sun related variables !!!!!
      have_sun_el = 0;
      return moon_el;
   }

   
   tt = utc_delta_t();

   t = jd - jdate(2060,1,1);    // year 2060 is middle of algorithm validity range
   te = t + (tt / (24.0*60.0*60.0));  // convert to terresrial time

   wte = 0.0172019715*te;

   s1 = sin(wte);
   c1 = cos(wte);
   s2 = 2.0*s1*c1;
   c2 = (c1+s1)*(c1-s1);
   s3 = s2*c1 + c2*s1;
   c3 = c2*c1 - s2*s1;

   // Heliocentric longitude
   L = 1.7527901 + 1.7202792159e-2*te + 3.33024e-2*s1 - 2.0582e-3*c1 
     + 3.512e-4*s2 - 4.07e-5*c2 + 5.2e-6*s3 - 9e-7*c3 
     - 8.23e-5*s1*sin(2.92e-5*te) + 1.27e-5*sin(1.49e-3*te - 2.337)
     + 1.21e-5*sin(4.31e-3*te + 3.065) + 2.33e-5*sin(1.076e-2*te - 1.533)
     + 3.49e-5*sin(1.575e-2*te - 2.358) + 2.67e-5*sin(2.152e-2*te + 0.074)
     + 1.28e-5*sin(3.152e-2*te + 1.547) + 3.14e-5*sin(2.1277e-1*te - 0.488);

   sun_hlon = (L*180.0/PI);                     // used as possible alternate input to gravity tide sun position code
   sun_hlon = fmod(sun_hlon, 360.0) + 180.0;
   sun_hlon = fmod(sun_hlon, 360.0);
//sprintf(debug_text2, "hlon: %.9f  r:%.9f", sun_hlon, (earth_sun_dist(jd)+EQU_RADIUS*0.0)*1000.0);

   nu = 9.282e-4*te - 0.8;  // nutation
   Dlam = 8.34e-5*sin(nu);
   lambda = L + PI + Dlam;

   epsi = 4.089567e-1 - 6.19e-9*te + 4.46e-5*cos(nu);

   sl = sin(lambda);
   cl = cos(lambda);
   se = sin(epsi);
   ce = sqrt(1.0-se*se);

   RightAscension = atan2(sl*ce, cl);
   if(RightAscension < 0.0) RightAscension += (PI*2.0);
   sun_ra = RightAscension * (180.0 / PI);

   Declination = asin(sl*se);
   sun_decl = Declination * (180.0 / PI);

   HourAngle = 1.7528311 + 6.300388099*t + lon - RightAscension + 0.92*Dlam;
   HourAngle = fmod(HourAngle + PI, (PI*2.0)) - PI;
   if(HourAngle < (-PI)) HourAngle += (PI*2.0);

   sp = sin(lat);
   cp = sqrt((1.0-sp*sp));
   sd = sin(Declination);
   cd = sqrt(1.0-sd*sd);
   sH = sin(HourAngle);
   cH = cos(HourAngle);
   se0 = sp*sd + cp*cd*cH;
   ep = asin(se0) - 4.26e-5*sqrt(1.0-se0*se0);
   if(cd) Azimuth = atan2(sH, cH*sp - sd*cp/cd);
   else   Azimuth = atan2(sH, cH*sp);

   De = 0.0;
   if(apply_refraction && (ep > 0.0)) {  // apply refraction correction
      De = (0.08422*Pressure) / ((273.0+Temperature)*tan(ep + 0.003138/(ep + 0.08919)));
   }

   // convert results to our desired orientation in degrees
   sun_az = (Azimuth * (180.0/PI)) + 180.0;
   if(sun_az < 0.0) sun_az += 360.0;
   sun_az = fmod(sun_az, 360.0);

   Zenith = (PI/2.0) - ep - De;
   sun_el = 90.0 - ((Zenith * (180.0/PI)));

   return sun_el;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
incl 0x292368(%rip)          # 0x37a5b4
testl %edi, %edi
je 0xe8293
callq 0xe7272
leaq 0x22558c(%rip), %rax    # 0x30d7e8
movsd (%rax), %xmm0
leaq 0x225569(%rip), %rax    # 0x30d7d0
movsd %xmm0, (%rax)
leaq 0x22557e(%rip), %rax    # 0x30d7f0
movsd (%rax), %xmm0
leaq 0x22553b(%rip), %rax    # 0x30d7b8
movsd %xmm0, (%rax)
leaq 0x225540(%rip), %rax    # 0x30d7c8
movl $0x0, (%rax)
jmp 0xe8989
leaq 0x22524e(%rip), %rax    # 0x30d4e8
cmpl $0x0, (%rax)
je 0xe82ac
leaq 0x22524a(%rip), %rax    # 0x30d4f0
movsd (%rax), %xmm2
jmp 0xe82dc
leaq 0x225475(%rip), %rax    # 0x30d728
movl (%rax), %eax
addl $0x33, %eax
cvtsi2sd %eax, %xmm1
addsd 0x434a4(%rip), %xmm1    # 0x12b768
divsd 0x20d44(%rip), %xmm1    # 0x109010
xorpd %xmm2, %xmm2
addsd %xmm1, %xmm2
divsd 0x20d64(%rip), %xmm2    # 0x109040
addsd 0x43bfc(%rip), %xmm0    # 0x12bee0
divsd 0x20d2c(%rip), %xmm2    # 0x109018
movsd %xmm0, -0x30(%rbp)
addsd %xmm0, %xmm2
movsd %xmm2, -0x8(%rbp)
movsd 0x43be6(%rip), %xmm0    # 0x12bee8
mulsd %xmm2, %xmm0
movsd %xmm0, -0x28(%rbp)
callq 0x9080
movsd %xmm0, -0x20(%rbp)
movsd -0x28(%rbp), %xmm0
callq 0x9710
movsd -0x20(%rbp), %xmm5
movapd %xmm5, %xmm7
addsd %xmm5, %xmm7
mulsd %xmm0, %xmm7
movapd %xmm5, %xmm1
addsd %xmm0, %xmm1
movapd %xmm0, %xmm2
subsd %xmm5, %xmm2
mulsd %xmm1, %xmm2
movapd %xmm5, %xmm3
mulsd %xmm2, %xmm3
movapd %xmm7, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm3, %xmm1
movapd %xmm5, %xmm4
movapd %xmm5, %xmm8
mulsd %xmm7, %xmm4
movapd %xmm2, %xmm3
mulsd %xmm0, %xmm3
subsd %xmm4, %xmm3
movsd 0x43b77(%rip), %xmm4    # 0x12bef0
movsd -0x8(%rbp), %xmm6
mulsd %xmm6, %xmm4
addsd 0x43b6e(%rip), %xmm4    # 0x12bef8
movsd 0x43b6e(%rip), %xmm5    # 0x12bf00
mulsd %xmm8, %xmm5
mulsd 0x43b69(%rip), %xmm0    # 0x12bf08
addsd %xmm4, %xmm5
addsd %xmm5, %xmm0
mulsd 0x43b61(%rip), %xmm7    # 0x12bf10
addsd %xmm7, %xmm0
mulsd 0x43b5d(%rip), %xmm2    # 0x12bf18
addsd %xmm2, %xmm0
mulsd 0x43b59(%rip), %xmm1    # 0x12bf20
mulsd 0x43b59(%rip), %xmm3    # 0x12bf28
addsd %xmm1, %xmm0
addsd %xmm3, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd 0x43b4c(%rip), %xmm0    # 0x12bf30
mulsd %xmm6, %xmm0
callq 0x9080
movsd -0x20(%rbp), %xmm1
mulsd 0x43b3e(%rip), %xmm1    # 0x12bf38
mulsd %xmm1, %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x43b30(%rip), %xmm0    # 0x12bf40
mulsd -0x8(%rbp), %xmm0
addsd 0x43b2b(%rip), %xmm0    # 0x12bf48
callq 0x9080
mulsd 0x43b26(%rip), %xmm0    # 0x12bf50
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x43b1c(%rip), %xmm0    # 0x12bf58
mulsd -0x8(%rbp), %xmm0
addsd 0x43b17(%rip), %xmm0    # 0x12bf60
callq 0x9080
mulsd 0x43b12(%rip), %xmm0    # 0x12bf68
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x43b08(%rip), %xmm0    # 0x12bf70
mulsd -0x8(%rbp), %xmm0
addsd 0x43b03(%rip), %xmm0    # 0x12bf78
callq 0x9080
mulsd 0x43afe(%rip), %xmm0    # 0x12bf80
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x43af4(%rip), %xmm0    # 0x12bf88
mulsd -0x8(%rbp), %xmm0
addsd 0x43aef(%rip), %xmm0    # 0x12bf90
callq 0x9080
mulsd 0x43aea(%rip), %xmm0    # 0x12bf98
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x43ae0(%rip), %xmm0    # 0x12bfa0
mulsd -0x8(%rbp), %xmm0
addsd 0x43adb(%rip), %xmm0    # 0x12bfa8
callq 0x9080
mulsd 0x43ad6(%rip), %xmm0    # 0x12bfb0
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x43acc(%rip), %xmm0    # 0x12bfb8
mulsd -0x8(%rbp), %xmm0
addsd 0x43ac7(%rip), %xmm0    # 0x12bfc0
callq 0x9080
mulsd 0x43ac2(%rip), %xmm0    # 0x12bfc8
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x43ab8(%rip), %xmm0    # 0x12bfd0
mulsd -0x8(%rbp), %xmm0
addsd 0x43ab3(%rip), %xmm0    # 0x12bfd8
callq 0x9080
mulsd 0x43aae(%rip), %xmm0    # 0x12bfe0
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
mulsd 0x232ec(%rip), %xmm0    # 0x10b830
divsd 0x232dc(%rip), %xmm0    # 0x10b828
movsd 0x35be4(%rip), %xmm1    # 0x11e138
callq 0x94d0
addsd 0x232cf(%rip), %xmm0    # 0x10b830
movsd 0x35bcf(%rip), %xmm1    # 0x11e138
callq 0x94d0
movsd %xmm0, 0x2917f2(%rip)   # 0x379d68
movsd 0x43a6a(%rip), %xmm0    # 0x12bfe8
mulsd -0x8(%rbp), %xmm0
addsd 0x43a65(%rip), %xmm0    # 0x12bff0
movsd %xmm0, -0x40(%rbp)
callq 0x9080
mulsd 0x43a5b(%rip), %xmm0    # 0x12bff8
movsd -0x20(%rbp), %xmm1
addsd 0x2327e(%rip), %xmm1    # 0x10b828
movsd %xmm0, -0x28(%rbp)
addsd %xmm0, %xmm1
movsd %xmm1, -0x20(%rbp)
movsd -0x8(%rbp), %xmm0
mulsd 0x43a3b(%rip), %xmm0    # 0x12c000
addsd 0x43a3b(%rip), %xmm0    # 0x12c008
movsd %xmm0, -0x8(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x9710
mulsd 0x43a2c(%rip), %xmm0    # 0x12c010
addsd -0x8(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x20(%rbp), %xmm0
callq 0x9080
movsd %xmm0, -0x8(%rbp)
movsd -0x20(%rbp), %xmm0
callq 0x9710
movsd %xmm0, -0x40(%rbp)
movsd -0x60(%rbp), %xmm0
callq 0x9080
movsd %xmm0, -0x20(%rbp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x20a45(%rip), %xmm0    # 0x109070
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xe863f
sqrtsd %xmm0, %xmm0
jmp 0xe8644
callq 0x9270
mulsd -0x8(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
callq 0x9050
movsd 0x35b25(%rip), %xmm2    # 0x11e180
movapd %xmm0, %xmm1
addsd %xmm2, %xmm1
movapd %xmm0, %xmm3
xorpd %xmm2, %xmm2
cmpltsd %xmm2, %xmm3
andpd %xmm3, %xmm1
andnpd %xmm0, %xmm3
orpd %xmm1, %xmm3
movapd %xmm3, -0x60(%rbp)
movapd %xmm3, %xmm0
mulsd 0x20ad3(%rip), %xmm0    # 0x109160
leaq 0x22514c(%rip), %rax    # 0x30d7e0
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
mulsd -0x8(%rbp), %xmm0
callq 0x9660
movsd %xmm0, -0x40(%rbp)
mulsd 0x20aac(%rip), %xmm0    # 0x109160
leaq 0x22511d(%rip), %rax    # 0x30d7d8
movsd -0x30(%rbp), %xmm1
mulsd 0x43950(%rip), %xmm1    # 0x12c018
movsd %xmm0, (%rax)
addsd 0x4394c(%rip), %xmm1    # 0x12c020
leaq 0x2248fd(%rip), %rax    # 0x30cfd8
addsd (%rax), %xmm1
movsd -0x28(%rbp), %xmm0
mulsd 0x4393c(%rip), %xmm0    # 0x12c028
subsd -0x60(%rbp), %xmm1
addsd %xmm1, %xmm0
addsd 0x2312b(%rip), %xmm0    # 0x10b828
movsd 0x35a7b(%rip), %xmm1    # 0x11e180
callq 0x94d0
movsd 0x4391e(%rip), %xmm1    # 0x12c030
addsd %xmm1, %xmm0
movsd 0x35a62(%rip), %xmm3    # 0x11e180
addsd %xmm0, %xmm3
movapd %xmm0, %xmm2
cmpltsd %xmm1, %xmm2
andpd %xmm2, %xmm3
andnpd %xmm0, %xmm2
orpd %xmm3, %xmm2
movapd %xmm2, -0x20(%rbp)
leaq 0x22488d(%rip), %rax    # 0x30cfd0
movsd (%rax), %xmm0
callq 0x9080
movsd %xmm0, -0x8(%rbp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x2090f(%rip), %xmm0    # 0x109070
subsd %xmm1, %xmm0
ucomisd 0x209ab(%rip), %xmm0    # 0x109118
jb 0xe8775
sqrtsd %xmm0, %xmm0
jmp 0xe877a
callq 0x9270
movsd %xmm0, -0x28(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x9080
movapd %xmm0, -0x40(%rbp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x208d2(%rip), %xmm0    # 0x109070
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xe87b2
sqrtsd %xmm0, %xmm0
jmp 0xe87b7
callq 0x9270
movsd %xmm0, -0x30(%rbp)
movaps -0x20(%rbp), %xmm0
callq 0x9080
movsd %xmm0, -0x60(%rbp)
movapd -0x20(%rbp), %xmm0
callq 0x9710
movsd -0x28(%rbp), %xmm1
mulsd -0x30(%rbp), %xmm1
movsd %xmm0, -0x20(%rbp)
mulsd %xmm0, %xmm1
movsd -0x8(%rbp), %xmm0
mulsd -0x40(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
callq 0x9660
movapd %xmm0, %xmm4
movsd -0x48(%rbp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x2085c(%rip), %xmm0    # 0x109070
subsd %xmm1, %xmm0
ucomisd 0x208f8(%rip), %xmm0    # 0x109118
jb 0xe8828
sqrtsd %xmm0, %xmm0
jmp 0xe8837
movsd %xmm4, -0x48(%rbp)
callq 0x9270
movsd -0x48(%rbp), %xmm4
mulsd 0x437f9(%rip), %xmm0    # 0x12c038
xorpd %xmm2, %xmm2
movsd -0x30(%rbp), %xmm1
ucomisd %xmm2, %xmm1
jne 0xe8850
jnp 0xe887a
movapd -0x40(%rbp), %xmm2
xorpd 0x20953(%rip), %xmm2    # 0x1091b0
movsd -0x28(%rbp), %xmm3
mulsd %xmm2, %xmm3
divsd %xmm1, %xmm3
movsd -0x20(%rbp), %xmm1
mulsd -0x8(%rbp), %xmm1
addsd %xmm3, %xmm1
jmp 0xe8884
movsd -0x20(%rbp), %xmm1
mulsd -0x8(%rbp), %xmm1
addsd %xmm4, %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x60(%rbp), %xmm0
callq 0x9050
movapd %xmm0, %xmm3
leaq 0x224d3e(%rip), %rax    # 0x30d5e0
cmpl $0x0, (%rax)
xorpd %xmm4, %xmm4
xorpd %xmm1, %xmm1
je 0xe890a
xorpd %xmm0, %xmm0
movsd -0x8(%rbp), %xmm1
ucomisd %xmm0, %xmm1
xorpd %xmm1, %xmm1
jbe 0xe890a
movsd 0x43776(%rip), %xmm1    # 0x12c040
movsd -0x8(%rbp), %xmm2
addsd %xmm2, %xmm1
movsd 0x4376d(%rip), %xmm0    # 0x12c048
divsd %xmm1, %xmm0
addsd %xmm2, %xmm0
movapd %xmm3, -0x20(%rbp)
callq 0x91b0
xorpd %xmm4, %xmm4
movapd -0x20(%rbp), %xmm3
mulsd 0x43752(%rip), %xmm0    # 0x12c050
movsd 0x43752(%rip), %xmm1    # 0x12c058
divsd %xmm0, %xmm1
movsd %xmm1, -0x20(%rbp)
movsd 0x20849(%rip), %xmm0    # 0x109160
mulsd %xmm0, %xmm3
addsd 0x22f0d(%rip), %xmm3    # 0x10b830
movapd %xmm3, %xmm2
movsd 0x35809(%rip), %xmm1    # 0x11e138
addsd %xmm1, %xmm2
movapd %xmm3, %xmm0
cmpltsd %xmm4, %xmm0
andpd %xmm0, %xmm2
andnpd %xmm3, %xmm0
orpd %xmm2, %xmm0
callq 0x94d0
leaq 0x224e7c(%rip), %rax    # 0x30d7d0
movsd %xmm0, (%rax)
movsd 0x43700(%rip), %xmm1    # 0x12c060
subsd -0x8(%rbp), %xmm1
subsd -0x20(%rbp), %xmm1
mulsd 0x207ee(%rip), %xmm1    # 0x109160
movsd 0x22f9e(%rip), %xmm0    # 0x10b918
subsd %xmm1, %xmm0
leaq 0x224e33(%rip), %rax    # 0x30d7b8
movsd %xmm0, (%rax)
addq $0x60, %rsp
popq %rbp
retq