// File: 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
subq $0xb0, %rsp
movsd %xmm0, -0x8(%rbp)
movsd %xmm1, -0x10(%rbp)
movsd 0x7a9db(%rip), %xmm0    # 0x16f078
mulsd -0x8(%rbp), %xmm0
movsd 0x7cf1e(%rip), %xmm1    # 0x1715c8
divsd %xmm1, %xmm0
movsd %xmm0, -0x18(%rbp)
movsd 0x7a9bd(%rip), %xmm0    # 0x16f078
mulsd -0x10(%rbp), %xmm0
movsd 0x7cf00(%rip), %xmm1    # 0x1715c8
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd 0x7a99f(%rip), %xmm1    # 0x16f078
addsd -0x20(%rbp), %xmm1
movsd 0x7a992(%rip), %xmm0    # 0x16f078
addsd -0x20(%rbp), %xmm0
movsd 0x8e43d(%rip), %xmm2    # 0x182b30
divsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
cvtsi2sd %eax, %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x8e412(%rip), %xmm2    # 0x182b30
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x7a94a(%rip), %xmm1    # 0x16f078
subsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd 0x7ce89(%rip), %xmm0    # 0x1715c8
mulsd -0x28(%rbp), %xmm0
movsd 0x7a92c(%rip), %xmm1    # 0x16f078
divsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movsd -0x18(%rbp), %xmm0
movsd -0x28(%rbp), %xmm1
callq 0xf3d80
movl %eax, 0x2e1006(%rip)    # 0x3d5770
movl 0x2e1000(%rip), %eax    # 0x3d5770
movl %eax, -0x64(%rbp)
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
callq 0xf3f20
movl -0x64(%rbp), %edx
movsbl %al, %ecx
leaq 0x2e0ff1(%rip), %rdi    # 0x3d5780
leaq 0x95328(%rip), %rsi     # 0x189abe
movb $0x0, %al
callq 0x9060
movsd -0x18(%rbp), %xmm0
movsd 0x8e9ee(%rip), %xmm1    # 0x183198
ucomisd %xmm1, %xmm0
jae 0xf47bf
movsd 0x8ea50(%rip), %xmm0    # 0x183208
ucomisd -0x18(%rbp), %xmm0
jb 0xf47d3
movsd -0x8(%rbp), %xmm0
movsd -0x10(%rbp), %xmm1
callq 0xf4480
jmp 0xf4c24
cvtsi2sdl 0x2e0f95(%rip), %xmm0 # 0x3d5770
movsd 0x7cf25(%rip), %xmm1    # 0x171708
mulsd %xmm1, %xmm0
movsd 0x8e391(%rip), %xmm1    # 0x182b80
addsd %xmm1, %xmm0
movsd 0x7a91d(%rip), %xmm1    # 0x16f118
mulsd %xmm1, %xmm0
movsd 0x7cdc1(%rip), %xmm1    # 0x1715c8
mulsd %xmm1, %xmm0
movsd 0x7a865(%rip), %xmm1    # 0x16f078
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x8(%rbp), %xmm0
callq 0x9080
movaps %xmm0, %xmm1
movsd 0x8e31f(%rip), %xmm0    # 0x182b50
mulsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0x8(%rbp), %xmm0
callq 0x9080
movaps %xmm0, %xmm1
movsd -0xa8(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm1, %xmm0
movsd 0x7a7f3(%rip), %xmm1    # 0x16f068
addsd %xmm1, %xmm0
callq 0x9280
movaps %xmm0, %xmm1
movsd 0x8e2bf(%rip), %xmm0    # 0x182b48
divsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x8(%rbp), %xmm0
callq 0x91b0
movsd %xmm0, -0xa0(%rbp)
movsd -0x8(%rbp), %xmm0
callq 0x91b0
movaps %xmm0, %xmm1
movsd -0xa0(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x8(%rbp), %xmm0
callq 0x96f0
movaps %xmm0, %xmm1
movsd 0x8e9c9(%rip), %xmm0    # 0x1832a0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x8(%rbp), %xmm0
callq 0x96f0
movaps %xmm0, %xmm1
movsd -0x98(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x8(%rbp), %xmm0
callq 0x96f0
movsd -0x10(%rbp), %xmm1
subsd -0x30(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x8(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd 0x7a81d(%rip), %xmm0    # 0x16f150
mulsd -0x8(%rbp), %xmm0
callq 0x9080
movaps %xmm0, %xmm2
movsd -0x90(%rbp), %xmm0
movsd 0x8e948(%rip), %xmm1    # 0x183298
mulsd %xmm2, %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
movsd 0x8e91d(%rip), %xmm2    # 0x183290
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd 0x7cd45(%rip), %xmm0    # 0x1716d0
mulsd -0x8(%rbp), %xmm0
callq 0x9080
movsd -0x88(%rbp), %xmm1
movsd 0x8e8e3(%rip), %xmm2    # 0x183288
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd 0x8e6b6(%rip), %xmm0    # 0x183070
mulsd -0x8(%rbp), %xmm0
callq 0x9080
movaps %xmm0, %xmm1
movsd -0x80(%rbp), %xmm0
movsd 0x8e8ac(%rip), %xmm2    # 0x183280
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x8e164(%rip), %xmm0    # 0x182b48
mulsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd 0x8e85b(%rip), %xmm0    # 0x183250
mulsd -0x38(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
movsd 0x7a661(%rip), %xmm2    # 0x16f068
subsd -0x40(%rbp), %xmm2
addsd -0x48(%rbp), %xmm2
mulsd -0x50(%rbp), %xmm2
mulsd -0x50(%rbp), %xmm2
mulsd -0x50(%rbp), %xmm2
movsd 0x8e648(%rip), %xmm3    # 0x183070
divsd %xmm3, %xmm2
addsd %xmm2, %xmm1
movsd -0x40(%rbp), %xmm2
movsd 0x7ccd3(%rip), %xmm3    # 0x171710
mulsd %xmm3, %xmm2
movsd 0x7a717(%rip), %xmm3    # 0x16f160
addsd %xmm3, %xmm2
movsd -0x40(%rbp), %xmm3
movsd -0x40(%rbp), %xmm4
mulsd %xmm4, %xmm3
addsd %xmm2, %xmm3
movsd -0x48(%rbp), %xmm2
movsd 0x8e724(%rip), %xmm4    # 0x183190
mulsd %xmm4, %xmm2
addsd %xmm3, %xmm2
movsd 0x8e7fc(%rip), %xmm3    # 0x183278
subsd %xmm3, %xmm2
mulsd -0x50(%rbp), %xmm2
mulsd -0x50(%rbp), %xmm2
mulsd -0x50(%rbp), %xmm2
mulsd -0x50(%rbp), %xmm2
mulsd -0x50(%rbp), %xmm2
movsd 0x7cc0f(%rip), %xmm3    # 0x1716b0
divsd %xmm3, %xmm2
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd 0x8e503(%rip), %xmm1    # 0x182fb8
addsd %xmm1, %xmm0
movsd %xmm0, 0x2e0ca7(%rip)   # 0x3d5768
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x8(%rbp), %xmm0
callq 0x91b0
movsd -0x78(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x70(%rbp), %xmm0
mulsd %xmm2, %xmm1
movsd -0x50(%rbp), %xmm2
mulsd -0x50(%rbp), %xmm2
movsd 0x7a64e(%rip), %xmm3    # 0x16f150
divsd %xmm3, %xmm2
movsd 0x7a652(%rip), %xmm3    # 0x16f160
subsd -0x40(%rbp), %xmm3
movsd -0x48(%rbp), %xmm4
movsd 0x8e130(%rip), %xmm5    # 0x182c50
mulsd %xmm5, %xmm4
addsd %xmm3, %xmm4
movsd 0x7cba0(%rip), %xmm3    # 0x1716d0
mulsd -0x48(%rbp), %xmm3
movsd -0x48(%rbp), %xmm5
mulsd %xmm5, %xmm3
addsd %xmm4, %xmm3
mulsd -0x50(%rbp), %xmm3
mulsd -0x50(%rbp), %xmm3
mulsd -0x50(%rbp), %xmm3
mulsd -0x50(%rbp), %xmm3
movsd 0x7a4da(%rip), %xmm4    # 0x16f038
divsd %xmm4, %xmm3
addsd %xmm3, %xmm2
movsd -0x40(%rbp), %xmm3
movsd 0x8e6f5(%rip), %xmm4    # 0x183268
mulsd %xmm4, %xmm3
movsd 0x8e6f1(%rip), %xmm4    # 0x183270
addsd %xmm4, %xmm3
movsd -0x40(%rbp), %xmm4
movsd -0x40(%rbp), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm3, %xmm4
movsd -0x48(%rbp), %xmm3
movsd 0x7cd1e(%rip), %xmm5    # 0x1718c0
mulsd %xmm5, %xmm3
addsd %xmm4, %xmm3
movsd 0x8e6ae(%rip), %xmm4    # 0x183260
subsd %xmm4, %xmm3
mulsd -0x50(%rbp), %xmm3
mulsd -0x50(%rbp), %xmm3
mulsd -0x50(%rbp), %xmm3
mulsd -0x50(%rbp), %xmm3
mulsd -0x50(%rbp), %xmm3
mulsd -0x50(%rbp), %xmm3
movsd 0x8e67c(%rip), %xmm4    # 0x183258
divsd %xmm4, %xmm3
addsd %xmm3, %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x8e65c(%rip), %xmm0    # 0x183250
mulsd %xmm1, %xmm0
movsd %xmm0, 0x2e0b60(%rip)   # 0x3d5760
xorps %xmm0, %xmm0
ucomisd -0x18(%rbp), %xmm0
jbe 0xf4c22
movsd 0x7a59e(%rip), %xmm0    # 0x16f1b0
addsd 0x2e0b46(%rip), %xmm0   # 0x3d5760
movsd %xmm0, 0x2e0b3e(%rip)   # 0x3d5760
jmp 0xf4c24
movl $0x0, -0x60(%rbp)
movl $0x0, -0x5c(%rbp)
leaq 0x2e0b67(%rip), %rdi    # 0x3d57a0
leaq 0x95a2f(%rip), %rsi     # 0x18a66f
callq 0x9350
leaq 0x277544(%rip), %rax    # 0x36c190
movzbl (%rax), %eax
cmpl $0x4, %eax
jne 0xf4d35
movl $0x1, -0x5c(%rbp)
movsd 0x2e0af9(%rip), %xmm0   # 0x3d5760
movsd 0x7a479(%rip), %xmm1    # 0x16f0e8
ucomisd %xmm1, %xmm0
jb 0xf4c9c
movsd 0x2e0ae3(%rip), %xmm0   # 0x3d5760
movsd 0x7a463(%rip), %xmm1    # 0x16f0e8
subsd %xmm1, %xmm0
movsd %xmm0, 0x2e0acf(%rip)   # 0x3d5760
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xf4c5f
movl -0x5c(%rbp), %eax
movl $0x14, %ecx
cltd
idivl %ecx
movl %edx, -0x5c(%rbp)
cvttsd2si 0x2e0aae(%rip), %eax # 0x3d5760
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x2e0aa2(%rip)   # 0x3d5760
movl $0x0, -0x60(%rbp)
movsd 0x2e0a9b(%rip), %xmm0   # 0x3d5768
movsd 0x7a413(%rip), %xmm1    # 0x16f0e8
ucomisd %xmm1, %xmm0
jb 0xf4d02
movsd 0x2e0a85(%rip), %xmm0   # 0x3d5768
movsd 0x7a3fd(%rip), %xmm1    # 0x16f0e8
subsd %xmm1, %xmm0
movsd %xmm0, 0x2e0a71(%rip)   # 0x3d5768
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xf4cc5
movl -0x60(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %edx, -0x60(%rbp)
cvttsd2si 0x2e0a50(%rip), %eax # 0x3d5768
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x2e0a44(%rip)   # 0x3d5768
movl 0x2e0a46(%rip), %edi    # 0x3d5770
movl -0x5c(%rbp), %esi
movl -0x60(%rbp), %edx
callq 0xf42e0
xorl %eax, %eax
addq $0xb0, %rsp
popq %rbp
retq