// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathgps.cpp
// Function: lla_to_utm(double, double)
int lla_to_utm(double dLatRad, double dLonRad)
{
double dLat;
double dLon;
double dLonWork;
double dCentralMeridian_Rad;
double N;
double T;
double K;
double A;
double M;
int row,col;

   // convert radians to degrees
   dLat = dLatRad * 180.0/PI;
   dLon = dLonRad * 180.0/PI;

   // use dLonWork to make sure the longitude is between -180.00 .. 179.9
   dLonWork = (dLon + 180.0) - ((int)((dLon + 180.0) / 360.0)) * 360.0 - 180.0; // -180.00 .. 179.9;
   // convert degrees to radians
   dLonRad = dLonWork * PI / 180.0;

   // set the UTM zone string
   UTM_Zone = utm_lon_zone(dLat, dLonWork);
   sprintf(utm_zone_string, "%02d%c", UTM_Zone, utm_lat_zone(dLat, dLon));

   if((dLat >= 84.0) || (dLat <= (-80.0))) {  // polar regions
      lla_to_ups(dLatRad, dLonRad);
   }
   else { // normal regions
      // set central meridian
      dCentralMeridian_Rad = ((183.0 - (6.0 * (double)UTM_Zone)) * -1.0) * PI / 180.0;

      N = WGS84_A / sqrt(1.0 - eccSquared * sin(dLatRad) * sin(dLatRad));
      T = tan(dLatRad) * tan(dLatRad);
      K = eccPrimeSquared * cos(dLatRad) * cos(dLatRad);
      A = cos(dLatRad) * (dLonRad - dCentralMeridian_Rad);
      M = WGS84_A * ((1.0 - eccSquared / 4.0 - 3.0 * eccSquared * eccSquared / 64.0 - 5.0 * eccSquared * eccSquared * eccSquared / 256.0) * dLatRad
          - (3.0 * eccSquared / 8.0 + 3.0 * eccSquared * eccSquared / 32.0 + 45.0 * eccSquared * eccSquared * eccSquared / 1024.0) * sin(2.0 * dLatRad)
          + (15.0 * eccSquared * eccSquared / 256.0 + 45.0 * eccSquared * eccSquared * eccSquared / 1024.0) * sin(4.0 * dLatRad)
          - (35.0 * eccSquared * eccSquared * eccSquared / 3072.0) * sin(6.0 * dLatRad));

      UTMEasting = (double)(dScaleFactor * N * (A + (1.0 - T + K) * A * A * A / 6.0
                 + (5.0 - 18.0 * T + T * T + 72.0 * K - 58.0 * eccPrimeSquared) * A * A * A * A * A / 120.0) + 500000.0);

      UTMNorthing = (double)(dScaleFactor * (M + N * tan(dLatRad) * (A * A / 2.0 + (5.0 - T + 9.0 * K + 4 * K * K) * A * A * A * A / 24.0
                  + (61.0 - 58.0 * T + T * T + 600.0 * K - 330.0 * eccPrimeSquared) * A * A * A * A * A * A / 720.0)));
      if(dLat < 0.0) UTMNorthing += 10000000.0; // 10000000 meter offset for southern hemisphere
   }

   col = (0);
   row = (0);
   strcpy(mgrs_square, "??");

   if(dms == NATO_FMT) {
      row = 1;
      while(UTMNorthing >= 100000.0) {
         UTMNorthing -= 100000.0;
         ++row;
      }
      row = (row % 20);
      UTMNorthing = (double) (int) UTMNorthing;

      col = 0;
      while(UTMEasting >= 100000.0) {
         UTMEasting -= 100000.0;
         ++col;
      }
      col = (col % 8);
      UTMEasting  = (double) (int) UTMEasting;

      calc_mgrs_square(UTM_Zone, row,col);
   }
//sprintf(plot_title, "utm: E:%f(%d) N:%f(%d)  %s  mgrs:%s", UTMEasting,col, UTMNorthing,row, utm_zone_string, mgrs_square);

   return (0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x48, %rsp
movsd 0x520ba(%rip), %xmm5    # 0xf58a8
movsd %xmm0, -0x10(%rbp)
mulsd %xmm5, %xmm0
movsd 0x520a1(%rip), %xmm4    # 0xf58a0
divsd %xmm4, %xmm0
movsd %xmm0, -0x40(%rbp)
mulsd %xmm5, %xmm1
divsd %xmm4, %xmm1
movsd %xmm1, -0x20(%rbp)
addsd %xmm5, %xmm1
movsd 0x6498f(%rip), %xmm2    # 0x1081b0
movapd %xmm1, %xmm3
divsd %xmm2, %xmm3
cvttpd2dq %xmm3, %xmm3
cvtdq2pd %xmm3, %xmm3
mulsd %xmm2, %xmm3
subsd %xmm3, %xmm1
addsd 0x6509f(%rip), %xmm1    # 0x1088e0
mulsd %xmm1, %xmm4
divsd %xmm5, %xmm4
movsd %xmm4, -0x28(%rbp)
callq 0xa32af
movl %eax, %ebx
movl %eax, 0x2bcce5(%rip)    # 0x360540
movsd -0x40(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
callq 0xa33ba
movzbl %al, %ecx
leaq 0x2bccdc(%rip), %rdi    # 0x360550
leaq 0x6cdef(%rip), %rsi     # 0x11066a
movl %ebx, %edx
xorl %eax, %eax
callq 0x9060
movsd -0x40(%rbp), %xmm1
ucomisd 0x64faf(%rip), %xmm1    # 0x108840
jae 0xa3918
movsd 0x65009(%rip), %xmm0    # 0x1088a8
ucomisd %xmm1, %xmm0
jae 0xa3918
cvtsi2sdl 0x2bcc93(%rip), %xmm0 # 0x360540
mulsd 0x65033(%rip), %xmm0    # 0x1088e8
addsd 0x64943(%rip), %xmm0    # 0x108200
mulsd 0x51fdb(%rip), %xmm0    # 0xf58a0
divsd 0x51fdb(%rip), %xmm0    # 0xf58a8
movsd %xmm0, -0x18(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0x9080
movsd %xmm0, -0x20(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0x9080
movsd -0x20(%rbp), %xmm1
mulsd 0x648c8(%rip), %xmm1    # 0x1081c0
mulsd %xmm0, %xmm1
addsd 0x4f76c(%rip), %xmm1    # 0xf3070
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
movapd %xmm1, %xmm0
jb 0xa392c
sqrtsd %xmm0, %xmm0
jmp 0xa3931
movsd -0x10(%rbp), %xmm0
movsd -0x28(%rbp), %xmm1
callq 0xa36c9
jmp 0xa3bf0
callq 0x9260
movsd 0x6488f(%rip), %xmm1    # 0x1081c8
divsd %xmm0, %xmm1
movsd %xmm1, -0x20(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0x91a0
movsd %xmm0, -0x38(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0x91a0
mulsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0x9710
mulsd 0x64f79(%rip), %xmm0    # 0x1088f0
movsd %xmm0, -0x30(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0x9710
mulsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0x9710
movsd -0x28(%rbp), %xmm1
addsd -0x18(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, -0x28(%rbp)
movsd -0x10(%rbp), %xmm0
addsd %xmm0, %xmm0
callq 0x9080
mulsd 0x64f35(%rip), %xmm0    # 0x1088f8
movsd 0x64f35(%rip), %xmm2    # 0x108900
movsd -0x10(%rbp), %xmm1
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, -0x18(%rbp)
movapd %xmm1, %xmm0
mulsd 0x51fff(%rip), %xmm0    # 0xf59e8
callq 0x9080
mulsd 0x64f12(%rip), %xmm0    # 0x108908
addsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x18(%rbp)
movsd -0x10(%rbp), %xmm0
mulsd 0x64d0b(%rip), %xmm0    # 0x108718
callq 0x9080
mulsd 0x64ef6(%rip), %xmm0    # 0x108910
addsd -0x18(%rbp), %xmm0
mulsd 0x647a1(%rip), %xmm0    # 0x1081c8
movsd %xmm0, -0x18(%rbp)
movsd -0x20(%rbp), %xmm0
mulsd 0x64edf(%rip), %xmm0    # 0x108918
movsd 0x4f62f(%rip), %xmm1    # 0xf3070
movsd -0x38(%rbp), %xmm3
subsd %xmm3, %xmm1
movsd -0x30(%rbp), %xmm4
addsd %xmm4, %xmm1
movsd -0x28(%rbp), %xmm5
mulsd %xmm5, %xmm1
mulsd %xmm5, %xmm1
mulsd %xmm5, %xmm1
divsd 0x64cac(%rip), %xmm1    # 0x108718
addsd %xmm5, %xmm1
movsd 0x64ea8(%rip), %xmm2    # 0x108920
mulsd %xmm3, %xmm2
addsd 0x4f6bc(%rip), %xmm2    # 0xf3140
movapd %xmm3, %xmm6
mulsd %xmm3, %xmm6
movsd %xmm6, -0x48(%rbp)
addsd %xmm6, %xmm2
movsd 0x64d9b(%rip), %xmm3    # 0x108838
mulsd %xmm4, %xmm3
addsd %xmm2, %xmm3
addsd 0x64e7b(%rip), %xmm3    # 0x108928
mulsd %xmm5, %xmm3
mulsd %xmm5, %xmm3
mulsd %xmm5, %xmm3
mulsd %xmm5, %xmm3
mulsd %xmm5, %xmm3
divsd 0x51eff(%rip), %xmm3    # 0xf59c8
addsd %xmm1, %xmm3
mulsd %xmm0, %xmm3
addsd 0x64b7f(%rip), %xmm3    # 0x108658
movsd %xmm3, 0x2bca57(%rip)   # 0x360538
movsd -0x10(%rbp), %xmm0
callq 0x91a0
movsd -0x28(%rbp), %xmm7
movapd %xmm7, %xmm1
mulsd %xmm7, %xmm1
mulsd 0x4f568(%rip), %xmm1    # 0xf3068
mulsd -0x20(%rbp), %xmm0
movsd -0x38(%rbp), %xmm6
movsd 0x4f62e(%rip), %xmm3    # 0xf3140
subsd %xmm6, %xmm3
movsd 0x64c62(%rip), %xmm2    # 0x108780
movsd -0x30(%rbp), %xmm4
mulsd %xmm4, %xmm2
addsd %xmm3, %xmm2
movsd 0x51eb5(%rip), %xmm3    # 0xf59e8
mulsd %xmm4, %xmm3
mulsd %xmm4, %xmm3
addsd %xmm2, %xmm3
mulsd %xmm7, %xmm3
mulsd %xmm7, %xmm3
mulsd %xmm7, %xmm3
mulsd %xmm7, %xmm3
divsd 0x4f4e9(%rip), %xmm3    # 0xf3040
movapd %xmm3, %xmm5
movapd %xmm6, %xmm2
mulsd 0x64dc9(%rip), %xmm2    # 0x108930
addsd 0x64dc9(%rip), %xmm2    # 0x108938
movapd %xmm2, %xmm3
movapd %xmm4, %xmm2
mulsd 0x51f39(%rip), %xmm2    # 0xf5ab8
addsd -0x48(%rbp), %xmm3
addsd %xmm3, %xmm2
addsd 0x64db0(%rip), %xmm2    # 0x108940
mulsd %xmm7, %xmm2
mulsd %xmm7, %xmm2
mulsd %xmm7, %xmm2
mulsd %xmm7, %xmm2
mulsd %xmm7, %xmm2
mulsd %xmm7, %xmm2
divsd 0x64d98(%rip), %xmm2    # 0x108948
addsd %xmm1, %xmm5
movapd %xmm2, %xmm1
addsd %xmm5, %xmm1
mulsd %xmm0, %xmm1
addsd -0x18(%rbp), %xmm1
mulsd 0x64d4b(%rip), %xmm1    # 0x108918
movsd %xmm1, 0x2bc95b(%rip)   # 0x360530
xorpd %xmm0, %xmm0
ucomisd -0x40(%rbp), %xmm0
jbe 0xa3bf0
addsd 0x51d68(%rip), %xmm1    # 0xf5950
movsd %xmm1, 0x2bc940(%rip)   # 0x360530
movb $0x0, 0x2bc97b(%rip)    # 0x360572
movw $0x3f3f, 0x2bc970(%rip) # imm = 0x3F3F
leaq 0x2533a9(%rip), %rax    # 0x2f6fb0
cmpb $0x4, (%rax)
jne 0xa3c99
pushq $0x1
popq %rax
movsd 0x2bc915(%rip), %xmm2   # 0x360530
movsd 0x4f4a5(%rip), %xmm0    # 0xf30c8
movsd 0x64b8d(%rip), %xmm1    # 0x1087b8
ucomisd %xmm0, %xmm2
jb 0xa3c41
addsd %xmm1, %xmm2
movsd %xmm2, 0x2bc8f3(%rip)   # 0x360530
incl %eax
jmp 0xa3c2b
cvttpd2dq %xmm2, %xmm2
cvtdq2pd %xmm2, %xmm2
movlps %xmm2, 0x2bc8e0(%rip)   # 0x360530
xorl %ecx, %ecx
movsd 0x2bc8de(%rip), %xmm2   # 0x360538
ucomisd %xmm0, %xmm2
jb 0xa3c70
addsd %xmm1, %xmm2
movsd %xmm2, 0x2bc8cc(%rip)   # 0x360538
incl %ecx
jmp 0xa3c5a
pushq $0x14
popq %rsi
xorl %edx, %edx
divl %esi
cvttpd2dq %xmm2, %xmm0
cvtdq2pd %xmm0, %xmm0
andl $0x7, %ecx
movlpd %xmm0, 0x2bc8ae(%rip)   # 0x360538
movl 0x2bc8b0(%rip), %edi    # 0x360540
movl %edx, %esi
movl %ecx, %edx
callq 0xa363a
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %rbp
retq