// File: /workspace/github_cpp_makefile_star2_O1/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 0x292de9(%rip)          # 0x37a5b4
testl %edi, %edi
je 0xe7812
callq 0xe6724
leaq 0x22600d(%rip), %rax    # 0x30d7e8
movsd (%rax), %xmm0
leaq 0x225fea(%rip), %rax    # 0x30d7d0
movsd %xmm0, (%rax)
leaq 0x225fff(%rip), %rax    # 0x30d7f0
movsd (%rax), %xmm0
leaq 0x225fbc(%rip), %rax    # 0x30d7b8
movsd %xmm0, (%rax)
leaq 0x225fc1(%rip), %rax    # 0x30d7c8
movl $0x0, (%rax)
jmp 0xe7f08
leaq 0x225ccf(%rip), %rax    # 0x30d4e8
cmpl $0x0, (%rax)
je 0xe782b
leaq 0x225ccb(%rip), %rax    # 0x30d4f0
movsd (%rax), %xmm2
jmp 0xe785b
leaq 0x225ef6(%rip), %rax    # 0x30d728
movl (%rax), %eax
addl $0x33, %eax
cvtsi2sd %eax, %xmm1
addsd 0x43e45(%rip), %xmm1    # 0x12b688
divsd 0x217c5(%rip), %xmm1    # 0x109010
xorpd %xmm2, %xmm2
addsd %xmm1, %xmm2
divsd 0x217e5(%rip), %xmm2    # 0x109040
addsd 0x4459d(%rip), %xmm0    # 0x12be00
divsd 0x217ad(%rip), %xmm2    # 0x109018
movsd %xmm0, -0x30(%rbp)
addsd %xmm0, %xmm2
movsd %xmm2, -0x8(%rbp)
movsd 0x44587(%rip), %xmm0    # 0x12be08
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 0x44518(%rip), %xmm4    # 0x12be10
movsd -0x8(%rbp), %xmm6
mulsd %xmm6, %xmm4
addsd 0x4450f(%rip), %xmm4    # 0x12be18
movsd 0x4450f(%rip), %xmm5    # 0x12be20
mulsd %xmm8, %xmm5
mulsd 0x4450a(%rip), %xmm0    # 0x12be28
addsd %xmm4, %xmm5
addsd %xmm5, %xmm0
mulsd 0x44502(%rip), %xmm7    # 0x12be30
addsd %xmm7, %xmm0
mulsd 0x444fe(%rip), %xmm2    # 0x12be38
addsd %xmm2, %xmm0
mulsd 0x444fa(%rip), %xmm1    # 0x12be40
mulsd 0x444fa(%rip), %xmm3    # 0x12be48
addsd %xmm1, %xmm0
addsd %xmm3, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd 0x444ed(%rip), %xmm0    # 0x12be50
mulsd %xmm6, %xmm0
callq 0x9080
movsd -0x20(%rbp), %xmm1
mulsd 0x444df(%rip), %xmm1    # 0x12be58
mulsd %xmm1, %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x444d1(%rip), %xmm0    # 0x12be60
mulsd -0x8(%rbp), %xmm0
addsd 0x444cc(%rip), %xmm0    # 0x12be68
callq 0x9080
mulsd 0x444c7(%rip), %xmm0    # 0x12be70
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x444bd(%rip), %xmm0    # 0x12be78
mulsd -0x8(%rbp), %xmm0
addsd 0x444b8(%rip), %xmm0    # 0x12be80
callq 0x9080
mulsd 0x444b3(%rip), %xmm0    # 0x12be88
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x444a9(%rip), %xmm0    # 0x12be90
mulsd -0x8(%rbp), %xmm0
addsd 0x444a4(%rip), %xmm0    # 0x12be98
callq 0x9080
mulsd 0x4449f(%rip), %xmm0    # 0x12bea0
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x44495(%rip), %xmm0    # 0x12bea8
mulsd -0x8(%rbp), %xmm0
addsd 0x44490(%rip), %xmm0    # 0x12beb0
callq 0x9080
mulsd 0x4448b(%rip), %xmm0    # 0x12beb8
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x44481(%rip), %xmm0    # 0x12bec0
mulsd -0x8(%rbp), %xmm0
addsd 0x4447c(%rip), %xmm0    # 0x12bec8
callq 0x9080
mulsd 0x44477(%rip), %xmm0    # 0x12bed0
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x4446d(%rip), %xmm0    # 0x12bed8
mulsd -0x8(%rbp), %xmm0
addsd 0x44468(%rip), %xmm0    # 0x12bee0
callq 0x9080
mulsd 0x44463(%rip), %xmm0    # 0x12bee8
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x44459(%rip), %xmm0    # 0x12bef0
mulsd -0x8(%rbp), %xmm0
addsd 0x44454(%rip), %xmm0    # 0x12bef8
callq 0x9080
mulsd 0x4444f(%rip), %xmm0    # 0x12bf00
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
mulsd 0x23d5d(%rip), %xmm0    # 0x10b820
divsd 0x23d4d(%rip), %xmm0    # 0x10b818
movsd 0x365bd(%rip), %xmm1    # 0x11e090
callq 0x94d0
addsd 0x23d40(%rip), %xmm0    # 0x10b820
movsd 0x365a8(%rip), %xmm1    # 0x11e090
callq 0x94d0
movsd %xmm0, 0x292273(%rip)   # 0x379d68
movsd 0x4440b(%rip), %xmm0    # 0x12bf08
mulsd -0x8(%rbp), %xmm0
addsd 0x44406(%rip), %xmm0    # 0x12bf10
movsd %xmm0, -0x40(%rbp)
callq 0x9080
mulsd 0x443fc(%rip), %xmm0    # 0x12bf18
movsd -0x20(%rbp), %xmm1
addsd 0x23cef(%rip), %xmm1    # 0x10b818
movsd %xmm0, -0x28(%rbp)
addsd %xmm0, %xmm1
movsd %xmm1, -0x20(%rbp)
movsd -0x8(%rbp), %xmm0
mulsd 0x443dc(%rip), %xmm0    # 0x12bf20
addsd 0x443dc(%rip), %xmm0    # 0x12bf28
movsd %xmm0, -0x8(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x9710
mulsd 0x443cd(%rip), %xmm0    # 0x12bf30
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 0x214c6(%rip), %xmm0    # 0x109070
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xe7bbe
sqrtsd %xmm0, %xmm0
jmp 0xe7bc3
callq 0x9270
mulsd -0x8(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
callq 0x9050
movsd 0x364fe(%rip), %xmm2    # 0x11e0d8
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 0x2154c(%rip), %xmm0    # 0x109158
leaq 0x225bcd(%rip), %rax    # 0x30d7e0
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
mulsd -0x8(%rbp), %xmm0
callq 0x9660
movsd %xmm0, -0x40(%rbp)
mulsd 0x21525(%rip), %xmm0    # 0x109158
leaq 0x225b9e(%rip), %rax    # 0x30d7d8
movsd -0x30(%rbp), %xmm1
mulsd 0x442f1(%rip), %xmm1    # 0x12bf38
movsd %xmm0, (%rax)
addsd 0x442ed(%rip), %xmm1    # 0x12bf40
leaq 0x22537e(%rip), %rax    # 0x30cfd8
addsd (%rax), %xmm1
movsd -0x28(%rbp), %xmm0
mulsd 0x442dd(%rip), %xmm0    # 0x12bf48
subsd -0x60(%rbp), %xmm1
addsd %xmm1, %xmm0
addsd 0x23b9c(%rip), %xmm0    # 0x10b818
movsd 0x36454(%rip), %xmm1    # 0x11e0d8
callq 0x94d0
movsd 0x442bf(%rip), %xmm1    # 0x12bf50
addsd %xmm1, %xmm0
movsd 0x3643b(%rip), %xmm3    # 0x11e0d8
addsd %xmm0, %xmm3
movapd %xmm0, %xmm2
cmpltsd %xmm1, %xmm2
andpd %xmm2, %xmm3
andnpd %xmm0, %xmm2
orpd %xmm3, %xmm2
movapd %xmm2, -0x20(%rbp)
leaq 0x22530e(%rip), %rax    # 0x30cfd0
movsd (%rax), %xmm0
callq 0x9080
movsd %xmm0, -0x8(%rbp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x21390(%rip), %xmm0    # 0x109070
subsd %xmm1, %xmm0
ucomisd 0x2142c(%rip), %xmm0    # 0x109118
jb 0xe7cf4
sqrtsd %xmm0, %xmm0
jmp 0xe7cf9
callq 0x9270
movsd %xmm0, -0x28(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x9080
movapd %xmm0, -0x40(%rbp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x21353(%rip), %xmm0    # 0x109070
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xe7d31
sqrtsd %xmm0, %xmm0
jmp 0xe7d36
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 0x212dd(%rip), %xmm0    # 0x109070
subsd %xmm1, %xmm0
ucomisd 0x21379(%rip), %xmm0    # 0x109118
jb 0xe7da7
sqrtsd %xmm0, %xmm0
jmp 0xe7db6
movsd %xmm4, -0x48(%rbp)
callq 0x9270
movsd -0x48(%rbp), %xmm4
mulsd 0x4419a(%rip), %xmm0    # 0x12bf58
xorpd %xmm2, %xmm2
movsd -0x30(%rbp), %xmm1
ucomisd %xmm2, %xmm1
jne 0xe7dcf
jnp 0xe7df9
movapd -0x40(%rbp), %xmm2
xorpd 0x213c4(%rip), %xmm2    # 0x1091a0
movsd -0x28(%rbp), %xmm3
mulsd %xmm2, %xmm3
divsd %xmm1, %xmm3
movsd -0x20(%rbp), %xmm1
mulsd -0x8(%rbp), %xmm1
addsd %xmm3, %xmm1
jmp 0xe7e03
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 0x2257bf(%rip), %rax    # 0x30d5e0
cmpl $0x0, (%rax)
xorpd %xmm4, %xmm4
xorpd %xmm1, %xmm1
je 0xe7e89
xorpd %xmm0, %xmm0
movsd -0x8(%rbp), %xmm1
ucomisd %xmm0, %xmm1
xorpd %xmm1, %xmm1
jbe 0xe7e89
movsd 0x44117(%rip), %xmm1    # 0x12bf60
movsd -0x8(%rbp), %xmm2
addsd %xmm2, %xmm1
movsd 0x4410e(%rip), %xmm0    # 0x12bf68
divsd %xmm1, %xmm0
addsd %xmm2, %xmm0
movapd %xmm3, -0x20(%rbp)
callq 0x91b0
xorpd %xmm4, %xmm4
movapd -0x20(%rbp), %xmm3
mulsd 0x440f3(%rip), %xmm0    # 0x12bf70
movsd 0x440f3(%rip), %xmm1    # 0x12bf78
divsd %xmm0, %xmm1
movsd %xmm1, -0x20(%rbp)
movsd 0x212c2(%rip), %xmm0    # 0x109158
mulsd %xmm0, %xmm3
addsd 0x2397e(%rip), %xmm3    # 0x10b820
movapd %xmm3, %xmm2
movsd 0x361e2(%rip), %xmm1    # 0x11e090
addsd %xmm1, %xmm2
movapd %xmm3, %xmm0
cmpltsd %xmm4, %xmm0
andpd %xmm0, %xmm2
andnpd %xmm3, %xmm0
orpd %xmm2, %xmm0
callq 0x94d0
leaq 0x2258fd(%rip), %rax    # 0x30d7d0
movsd %xmm0, (%rax)
movsd 0x440a1(%rip), %xmm1    # 0x12bf80
subsd -0x8(%rbp), %xmm1
subsd -0x20(%rbp), %xmm1
mulsd 0x21267(%rip), %xmm1    # 0x109158
movsd 0x23a17(%rip), %xmm0    # 0x10b910
subsd %xmm1, %xmm0
leaq 0x2258b4(%rip), %rax    # 0x30d7b8
movsd %xmm0, (%rax)
addq $0x60, %rsp
popq %rbp
retq