// File: /workspace/github_cpp_makefile_star2_O2/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 0x2907ff(%rip)          # 0x3645b4
testl %edi, %edi
je 0xd3df9
callq 0xd3202
leaq 0x223a23(%rip), %rax    # 0x2f77e8
movsd (%rax), %xmm0
leaq 0x223a00(%rip), %rax    # 0x2f77d0
movsd %xmm0, (%rax)
leaq 0x223a15(%rip), %rax    # 0x2f77f0
movsd (%rax), %xmm0
leaq 0x2239d2(%rip), %rax    # 0x2f77b8
movsd %xmm0, (%rax)
leaq 0x2239d7(%rip), %rax    # 0x2f77c8
andl $0x0, (%rax)
jmp 0xd44b9
movsd %xmm0, -0x30(%rbp)
callq 0xbcc92
movsd -0x30(%rbp), %xmm1
addsd 0x42000(%rip), %xmm1    # 0x115e10
divsd 0x1f200(%rip), %xmm0    # 0xf3018
movsd %xmm1, -0x30(%rbp)
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
movsd 0x41fea(%rip), %xmm1    # 0x115e18
mulsd %xmm0, %xmm1
movsd %xmm1, -0x28(%rbp)
movapd %xmm1, %xmm0
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 0x41f77(%rip), %xmm4    # 0x115e20
movsd -0x8(%rbp), %xmm6
mulsd %xmm6, %xmm4
addsd 0x41f6e(%rip), %xmm4    # 0x115e28
movsd 0x41f6e(%rip), %xmm5    # 0x115e30
mulsd %xmm8, %xmm5
mulsd 0x41f69(%rip), %xmm0    # 0x115e38
addsd %xmm4, %xmm5
addsd %xmm5, %xmm0
mulsd 0x41f61(%rip), %xmm7    # 0x115e40
addsd %xmm7, %xmm0
mulsd 0x41f5d(%rip), %xmm2    # 0x115e48
addsd %xmm2, %xmm0
mulsd 0x41f59(%rip), %xmm1    # 0x115e50
mulsd 0x41f59(%rip), %xmm3    # 0x115e58
addsd %xmm1, %xmm0
addsd %xmm3, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd 0x41f4c(%rip), %xmm0    # 0x115e60
mulsd %xmm6, %xmm0
callq 0x9080
movsd -0x20(%rbp), %xmm1
mulsd 0x41f3e(%rip), %xmm1    # 0x115e68
mulsd %xmm1, %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x41f30(%rip), %xmm0    # 0x115e70
mulsd -0x8(%rbp), %xmm0
addsd 0x41f2b(%rip), %xmm0    # 0x115e78
callq 0x9080
mulsd 0x41f26(%rip), %xmm0    # 0x115e80
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x41f1c(%rip), %xmm0    # 0x115e88
mulsd -0x8(%rbp), %xmm0
addsd 0x41f17(%rip), %xmm0    # 0x115e90
callq 0x9080
mulsd 0x41f12(%rip), %xmm0    # 0x115e98
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x41f08(%rip), %xmm0    # 0x115ea0
mulsd -0x8(%rbp), %xmm0
addsd 0x41f03(%rip), %xmm0    # 0x115ea8
callq 0x9080
mulsd 0x41efe(%rip), %xmm0    # 0x115eb0
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x41ef4(%rip), %xmm0    # 0x115eb8
mulsd -0x8(%rbp), %xmm0
addsd 0x41eef(%rip), %xmm0    # 0x115ec0
callq 0x9080
mulsd 0x41eea(%rip), %xmm0    # 0x115ec8
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x41ee0(%rip), %xmm0    # 0x115ed0
mulsd -0x8(%rbp), %xmm0
addsd 0x41edb(%rip), %xmm0    # 0x115ed8
callq 0x9080
mulsd 0x41ed6(%rip), %xmm0    # 0x115ee0
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x41ecc(%rip), %xmm0    # 0x115ee8
mulsd -0x8(%rbp), %xmm0
addsd 0x41ec7(%rip), %xmm0    # 0x115ef0
callq 0x9080
mulsd 0x41ec2(%rip), %xmm0    # 0x115ef8
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x41eb8(%rip), %xmm0    # 0x115f00
mulsd -0x8(%rbp), %xmm0
addsd 0x41eb3(%rip), %xmm0    # 0x115f08
callq 0x9080
mulsd 0x41eae(%rip), %xmm0    # 0x115f10
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
mulsd 0x21834(%rip), %xmm0    # 0xf58a8
divsd 0x21824(%rip), %xmm0    # 0xf58a0
movsd 0x3412c(%rip), %xmm1    # 0x1081b0
callq 0x94d0
addsd 0x21817(%rip), %xmm0    # 0xf58a8
movsd 0x34117(%rip), %xmm1    # 0x1081b0
callq 0x94d0
movsd %xmm0, 0x28fcc2(%rip)   # 0x363d68
movsd 0x41e6a(%rip), %xmm0    # 0x115f18
mulsd -0x8(%rbp), %xmm0
addsd 0x41e65(%rip), %xmm0    # 0x115f20
movsd %xmm0, -0x40(%rbp)
callq 0x9080
mulsd 0x41e5b(%rip), %xmm0    # 0x115f28
movsd -0x20(%rbp), %xmm1
addsd 0x217c6(%rip), %xmm1    # 0xf58a0
movsd %xmm0, -0x28(%rbp)
addsd %xmm0, %xmm1
movsd %xmm1, -0x20(%rbp)
movsd -0x8(%rbp), %xmm0
mulsd 0x41e3b(%rip), %xmm0    # 0x115f30
addsd 0x41e3b(%rip), %xmm0    # 0x115f38
movsd %xmm0, -0x8(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x9710
mulsd 0x41e2c(%rip), %xmm0    # 0x115f40
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 0x1ef15(%rip), %xmm0    # 0xf3070
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xd416f
sqrtsd %xmm0, %xmm0
jmp 0xd4174
callq 0x9260
mulsd -0x8(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
callq 0x9050
movsd 0x3406d(%rip), %xmm2    # 0x1081f8
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 0x1efa3(%rip), %xmm0    # 0xf3160
leaq 0x22361c(%rip), %rax    # 0x2f77e0
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
mulsd -0x8(%rbp), %xmm0
callq 0x9660
movsd %xmm0, -0x40(%rbp)
mulsd 0x1ef7c(%rip), %xmm0    # 0xf3160
leaq 0x2235ed(%rip), %rax    # 0x2f77d8
movsd -0x30(%rbp), %xmm1
mulsd 0x41d50(%rip), %xmm1    # 0x115f48
movsd %xmm0, (%rax)
addsd 0x41d4c(%rip), %xmm1    # 0x115f50
leaq 0x222dcd(%rip), %rax    # 0x2f6fd8
addsd (%rax), %xmm1
movsd -0x28(%rbp), %xmm0
mulsd 0x41d3c(%rip), %xmm0    # 0x115f58
subsd -0x60(%rbp), %xmm1
addsd %xmm1, %xmm0
addsd 0x21673(%rip), %xmm0    # 0xf58a0
movsd 0x33fc3(%rip), %xmm1    # 0x1081f8
callq 0x94d0
movsd 0x41d1e(%rip), %xmm1    # 0x115f60
addsd %xmm1, %xmm0
movsd 0x33faa(%rip), %xmm3    # 0x1081f8
addsd %xmm0, %xmm3
movapd %xmm0, %xmm2
cmpltsd %xmm1, %xmm2
andpd %xmm2, %xmm3
andnpd %xmm0, %xmm2
orpd %xmm3, %xmm2
movapd %xmm2, -0x20(%rbp)
leaq 0x222d5d(%rip), %rax    # 0x2f6fd0
movsd (%rax), %xmm0
callq 0x9080
movsd %xmm0, -0x8(%rbp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x1eddf(%rip), %xmm0    # 0xf3070
subsd %xmm1, %xmm0
ucomisd 0x1ee7b(%rip), %xmm0    # 0xf3118
jb 0xd42a5
sqrtsd %xmm0, %xmm0
jmp 0xd42aa
callq 0x9260
movsd %xmm0, -0x28(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x9080
movapd %xmm0, -0x40(%rbp)
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x1eda2(%rip), %xmm0    # 0xf3070
subsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xd42e2
sqrtsd %xmm0, %xmm0
jmp 0xd42e7
callq 0x9260
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 0x1ed2c(%rip), %xmm0    # 0xf3070
subsd %xmm1, %xmm0
ucomisd 0x1edc8(%rip), %xmm0    # 0xf3118
jb 0xd4358
sqrtsd %xmm0, %xmm0
jmp 0xd4367
movsd %xmm4, -0x48(%rbp)
callq 0x9260
movsd -0x48(%rbp), %xmm4
mulsd 0x41bf9(%rip), %xmm0    # 0x115f68
xorpd %xmm2, %xmm2
movsd -0x30(%rbp), %xmm1
ucomisd %xmm2, %xmm1
jne 0xd4380
jnp 0xd43aa
movapd -0x40(%rbp), %xmm2
xorpd 0x1ee23(%rip), %xmm2    # 0xf31b0
movsd -0x28(%rbp), %xmm3
mulsd %xmm2, %xmm3
divsd %xmm1, %xmm3
movsd -0x20(%rbp), %xmm1
mulsd -0x8(%rbp), %xmm1
addsd %xmm3, %xmm1
jmp 0xd43b4
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 0x22320e(%rip), %rax    # 0x2f75e0
cmpl $0x0, (%rax)
xorpd %xmm4, %xmm4
xorpd %xmm1, %xmm1
je 0xd443a
xorpd %xmm0, %xmm0
movsd -0x8(%rbp), %xmm1
ucomisd %xmm0, %xmm1
xorpd %xmm1, %xmm1
jbe 0xd443a
movsd 0x41b76(%rip), %xmm1    # 0x115f70
movsd -0x8(%rbp), %xmm2
addsd %xmm2, %xmm1
movsd 0x41b6d(%rip), %xmm0    # 0x115f78
divsd %xmm1, %xmm0
addsd %xmm2, %xmm0
movapd %xmm3, -0x20(%rbp)
callq 0x91a0
xorpd %xmm4, %xmm4
movapd -0x20(%rbp), %xmm3
mulsd 0x41b52(%rip), %xmm0    # 0x115f80
movsd 0x41b52(%rip), %xmm1    # 0x115f88
divsd %xmm0, %xmm1
movsd %xmm1, -0x20(%rbp)
movsd 0x1ed19(%rip), %xmm0    # 0xf3160
mulsd %xmm0, %xmm3
addsd 0x21455(%rip), %xmm3    # 0xf58a8
movapd %xmm3, %xmm2
movsd 0x33d51(%rip), %xmm1    # 0x1081b0
addsd %xmm1, %xmm2
movapd %xmm3, %xmm0
cmpltsd %xmm4, %xmm0
andpd %xmm0, %xmm2
andnpd %xmm3, %xmm0
orpd %xmm2, %xmm0
callq 0x94d0
leaq 0x22334c(%rip), %rax    # 0x2f77d0
movsd %xmm0, (%rax)
movsd 0x41b00(%rip), %xmm1    # 0x115f90
subsd -0x8(%rbp), %xmm1
subsd -0x20(%rbp), %xmm1
mulsd 0x1ecbe(%rip), %xmm1    # 0xf3160
movsd 0x214e6(%rip), %xmm0    # 0xf5990
subsd %xmm1, %xmm0
leaq 0x223303(%rip), %rax    # 0x2f77b8
movsd %xmm0, (%rax)
addq $0x60, %rsp
popq %rbp
retq