// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathmsc.cpp
// Function: calc_precise_lla()
void calc_precise_lla()
{
u08 have_lla;

   // calculate simple average of all surveyed points

   have_lla = 0;
   if(precision_samples) {
      xlat = lat_sum / (double)precision_samples;
      xlon = lon_sum / (double)precision_samples;
      xalt = alt_sum / (double)precision_samples;
      precise_lat = xlat;
      precise_lon = xlon;
      precise_alt = xalt;
      have_precise_lla = 2;
      ref_lat = precise_lat;
      ref_lon = precise_lon;
      ref_alt = precise_alt;
      cos_factor = cos(ref_lat);
      if(cos_factor == 0.0) cos_factor = 0.001;
      lla_to_ecef(xlat,xlon,xalt);

      xlat *= RAD_TO_DEG;
      xlon *= RAD_TO_DEG;
      if(lla_file) {
         fprintf(lla_file, "# Simple average of %.0f points:\n", (double)precision_samples);
         fprintf(lla_file, "#   lat:%14.10f  %.10f  ecef_x:%.5f  dms:%s\n",  xlat, (xlat-LAT_REF)/ANGLE_SCALE, ecef_x, dms_fmt(xlat));
         fprintf(lla_file, "#   lon:%14.10f  %.10f  ecef_y:%.5f  dms:%s\n",  xlon, (xlon-LON_REF)/ANGLE_SCALE*COS_FACTOR(xlat), ecef_y, dms_fmt(xlon));
         fprintf(lla_file, "#   alt:%14.10f  %.10f  ecef_z:%.5f\n",  xalt, (xalt-ALT_REF), ecef_z);
         fprintf(lla_file, "#\n");
      }
      have_lla |= 0x01;
   }


   // calculate average of all 24 hour median intervals
   if(best_count && best_lat && best_lon && best_alt) {
      xlat = best_lat / best_count;
      xlon = best_lon / best_count;
      xalt = best_alt / best_count;
      precise_lat = xlat;
      precise_lon = xlon;
      precise_alt = xalt;
      have_precise_lla = 3;
      ref_lat = xlat;
      ref_lon = xlon;
      ref_alt = xalt;
      cos_factor = cos(ref_lat);
      if(cos_factor == 0.0) cos_factor = 0.001;
      lla_to_ecef(xlat,xlon,xalt);

      xlat *= RAD_TO_DEG;
      xlon *= RAD_TO_DEG;
      if(lla_file) {
         fprintf(lla_file, "# REF: %.10f  %.10f  %.10f  cos=%.10f\n", LAT_REF, LON_REF, ALT_REF, COS_FACTOR(xlat));
         fprintf(lla_file, "# Average of %.0f 24 hour medians:\n", best_count);
         fprintf(lla_file, "#   lat:%14.10f  %.10f  ecef_x:%.5f  dms:%s\n",  xlat, (xlat-LAT_REF)/ANGLE_SCALE, ecef_x, dms_fmt(xlat));
         fprintf(lla_file, "#   lon:%14.10f  %.10f  ecef_y:%.5f  dms:%s\n",  xlon, (xlon-LON_REF)/ANGLE_SCALE*COS_FACTOR(xlat), ecef_y, dms_fmt(xlon));
         fprintf(lla_file, "#   alt:%14.10f  %.10f  ecef_z:%.5f\n",  xalt, (xalt-ALT_REF), ecef_z);
      }
      have_lla |= 0x02;
   }

   if(have_lla) {            // we have a good idea where we are
      save_precise_posn(-1);  // save it in the receiver (directly or via repeated single point surveys)
   }
//if(lla_file) {
//   fprintf(lla_file, "### calc_precise_lla: have:%d  ps:%d bc:%d bla:%d blo:%d bal:%d\n", 
//   have_lla, precision_samples, best_count,best_lat,best_lon,best_alt);
//}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0x23e607(%rip), %rbx    # 0x3252e4
movl (%rbx), %r14d
testl %r14d, %r14d
je 0xe6f6b
movsd 0x355bcf(%rip), %xmm0   # 0x43c8c0
movl %r14d, %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x355ba3(%rip)   # 0x43c8a8
movsd 0x355bbb(%rip), %xmm2   # 0x43c8c8
divsd %xmm1, %xmm2
movsd %xmm2, 0x355b97(%rip)   # 0x43c8b0
movsd 0x355baf(%rip), %xmm3   # 0x43c8d0
divsd %xmm1, %xmm3
movsd %xmm3, 0x355b8b(%rip)   # 0x43c8b8
leaq 0x23e5bc(%rip), %r15    # 0x3252f0
movsd %xmm0, (%r15)
leaq 0x23e5b8(%rip), %r13    # 0x3252f8
movsd %xmm2, (%r13)
leaq 0x23e5b3(%rip), %rax    # 0x325300
movsd %xmm3, (%rax)
leaq 0x23e5b0(%rip), %rax    # 0x325308
movl $0x2, (%rax)
leaq 0x23e5ab(%rip), %rax    # 0x325310
movsd %xmm0, (%rax)
leaq 0x23e5a8(%rip), %rax    # 0x325318
movsd %xmm2, (%rax)
leaq 0x23e5a5(%rip), %rax    # 0x325320
movsd %xmm3, (%rax)
callq 0x9710
xorpd %xmm1, %xmm1
cmpeqsd %xmm0, %xmm1
movsd 0xec13(%rip), %xmm2     # 0xf59a8
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm1, %xmm2
leaq 0x210220(%rip), %rax    # 0x2f6fc8
movlpd %xmm2, (%rax)
movsd 0x355af4(%rip), %xmm0   # 0x43c8a8
movsd 0x355af4(%rip), %xmm1   # 0x43c8b0
movsd 0x355af4(%rip), %xmm2   # 0x43c8b8
callq 0x55535
movsd 0xc38f(%rip), %xmm1     # 0xf3160
movsd 0x355acf(%rip), %xmm0   # 0x43c8a8
mulsd %xmm1, %xmm0
movsd %xmm0, 0x355ac3(%rip)   # 0x43c8a8
movsd 0x355ac3(%rip), %xmm0   # 0x43c8b0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x355ab7(%rip)   # 0x43c8b0
leaq 0x1415a0(%rip), %r12    # 0x2283a0
movq (%r12), %rdi
testq %rdi, %rdi
je 0xe6f6b
movl (%rbx), %eax
cvtsi2sd %rax, %xmm0
leaq 0x38849(%rip), %rsi     # 0x11f664
movb $0x1, %al
callq 0x9560
movq (%r12), %rbx
movsd 0x355a7a(%rip), %xmm0   # 0x43c8a8
movsd %xmm0, -0x38(%rbp)
movsd (%r15), %xmm1
mulsd 0xc320(%rip), %xmm1     # 0xf3160
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
leaq 0x210169(%rip), %r15    # 0x2f6fb8
divsd (%r15), %xmm2
movsd %xmm2, -0x30(%rbp)
leaq 0x210188(%rip), %rax    # 0x2f6fe8
movsd (%rax), %xmm1
movsd %xmm1, -0x40(%rbp)
callq 0xa3ca2
leaq 0x38811(%rip), %rsi     # 0x11f686
movq %rbx, %rdi
movsd -0x38(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
movq %rax, %rdx
movb $0x3, %al
callq 0x9560
movq (%r12), %rbx
movsd 0x355a13(%rip), %xmm2   # 0x43c8b0
movsd %xmm2, -0x40(%rbp)
movsd (%r13), %xmm0
movsd 0xc2b0(%rip), %xmm1     # 0xf3160
mulsd %xmm1, %xmm0
subsd %xmm0, %xmm2
divsd (%r15), %xmm2
movsd %xmm2, -0x30(%rbp)
movsd 0x3559de(%rip), %xmm0   # 0x43c8a8
divsd %xmm1, %xmm0
callq 0x9710
movsd -0x30(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
leaq 0x210108(%rip), %rax    # 0x2f6ff0
movsd (%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd 0x3559b7(%rip), %xmm0   # 0x43c8b0
callq 0xa3ca2
leaq 0x387ae(%rip), %rsi     # 0x11f6b3
movq %rbx, %rdi
movsd -0x40(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
movq %rax, %rdx
movb $0x3, %al
callq 0x9560
movq (%r12), %rdi
movsd 0x35598b(%rip), %xmm0   # 0x43c8b8
movapd %xmm0, %xmm1
leaq 0x23e3c8(%rip), %rax    # 0x325300
subsd (%rax), %xmm1
leaq 0x2100b5(%rip), %rax    # 0x2f6ff8
movsd (%rax), %xmm2
leaq 0x38792(%rip), %rsi     # 0x11f6e0
movb $0x3, %al
callq 0x9560
movq (%r12), %rcx
leaq 0x3492d(%rip), %rdi     # 0x11b88d
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x9750
movsd 0x35597d(%rip), %xmm1   # 0x43c8f0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0xe6f83
jnp 0xe7267
movsd 0x35594d(%rip), %xmm0   # 0x43c8d8
ucomisd %xmm2, %xmm0
jne 0xe6f97
jnp 0xe7267
movsd 0x355941(%rip), %xmm2   # 0x43c8e0
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm2
jne 0xe6faf
jnp 0xe7267
movsd 0x355931(%rip), %xmm3   # 0x43c8e8
ucomisd %xmm4, %xmm3
jne 0xe6fc3
jnp 0xe7267
divsd %xmm1, %xmm0
movsd %xmm0, 0x3558d9(%rip)   # 0x43c8a8
divsd %xmm1, %xmm2
movsd %xmm2, 0x3558d5(%rip)   # 0x43c8b0
divsd %xmm1, %xmm3
movsd %xmm3, 0x3558d1(%rip)   # 0x43c8b8
leaq 0x23e302(%rip), %r13    # 0x3252f0
movsd %xmm0, (%r13)
leaq 0x23e2fd(%rip), %r15    # 0x3252f8
movsd %xmm2, (%r15)
leaq 0x23e2f9(%rip), %r14    # 0x325300
movsd %xmm3, (%r14)
leaq 0x23e2f5(%rip), %rax    # 0x325308
movl $0x3, (%rax)
leaq 0x23e2f0(%rip), %rax    # 0x325310
movsd %xmm0, (%rax)
leaq 0x23e2ed(%rip), %rax    # 0x325318
movsd %xmm2, (%rax)
leaq 0x23e2ea(%rip), %rax    # 0x325320
movsd %xmm3, (%rax)
callq 0x9710
xorpd %xmm1, %xmm1
cmpeqsd %xmm0, %xmm1
movsd 0xe958(%rip), %xmm2     # 0xf59a8
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm1, %xmm2
leaq 0x20ff65(%rip), %rax    # 0x2f6fc8
movlpd %xmm2, (%rax)
movsd 0x355839(%rip), %xmm0   # 0x43c8a8
movsd 0x355839(%rip), %xmm1   # 0x43c8b0
movsd 0x355839(%rip), %xmm2   # 0x43c8b8
callq 0x55535
movsd 0xc0d4(%rip), %xmm2     # 0xf3160
movsd 0x355814(%rip), %xmm0   # 0x43c8a8
mulsd %xmm2, %xmm0
movsd %xmm0, 0x355808(%rip)   # 0x43c8a8
movsd 0x355808(%rip), %xmm1   # 0x43c8b0
mulsd %xmm2, %xmm1
movsd %xmm1, 0x3557fc(%rip)   # 0x43c8b0
leaq 0x1412e5(%rip), %r12    # 0x2283a0
movq (%r12), %rbx
testq %rbx, %rbx
je 0xe726c
movsd (%r13), %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, -0x30(%rbp)
movsd (%r15), %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, -0x38(%rbp)
movsd (%r14), %xmm1
movsd %xmm1, -0x40(%rbp)
divsd %xmm2, %xmm0
callq 0x9710
movapd %xmm0, %xmm3
leaq 0x38602(%rip), %rsi     # 0x11f705
movq %rbx, %rdi
movsd -0x30(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
movb $0x4, %al
callq 0x9560
movq (%r12), %rdi
movsd 0x3557c8(%rip), %xmm0   # 0x43c8f0
leaq 0x385fd(%rip), %rsi     # 0x11f72c
movb $0x1, %al
callq 0x9560
movq (%r12), %rbx
movsd 0x355766(%rip), %xmm0   # 0x43c8a8
movsd %xmm0, -0x38(%rbp)
movsd (%r13), %xmm1
mulsd 0xc00b(%rip), %xmm1     # 0xf3160
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
leaq 0x20fe54(%rip), %r13    # 0x2f6fb8
divsd (%r13), %xmm2
movsd %xmm2, -0x30(%rbp)
leaq 0x20fe72(%rip), %rax    # 0x2f6fe8
movsd (%rax), %xmm1
movsd %xmm1, -0x40(%rbp)
callq 0xa3ca2
leaq 0x384fb(%rip), %rsi     # 0x11f686
movq %rbx, %rdi
movsd -0x38(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
movq %rax, %rdx
movb $0x3, %al
callq 0x9560
movq (%r12), %rbx
movsd 0x3556fd(%rip), %xmm2   # 0x43c8b0
movsd %xmm2, -0x40(%rbp)
movsd (%r15), %xmm0
movsd 0xbf9b(%rip), %xmm1     # 0xf3160
mulsd %xmm1, %xmm0
subsd %xmm0, %xmm2
divsd (%r13), %xmm2
movsd %xmm2, -0x30(%rbp)
movsd 0x3556c8(%rip), %xmm0   # 0x43c8a8
divsd %xmm1, %xmm0
callq 0x9710
movsd -0x30(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
leaq 0x20fdf2(%rip), %rax    # 0x2f6ff0
movsd (%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd 0x3556a1(%rip), %xmm0   # 0x43c8b0
callq 0xa3ca2
leaq 0x38498(%rip), %rsi     # 0x11f6b3
movq %rbx, %rdi
movsd -0x40(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
movq %rax, %rdx
movb $0x3, %al
callq 0x9560
movq (%r12), %rdi
movsd 0x355675(%rip), %xmm0   # 0x43c8b8
movapd %xmm0, %xmm1
subsd (%r14), %xmm1
leaq 0x20fda5(%rip), %rax    # 0x2f6ff8
movsd (%rax), %xmm2
leaq 0x38482(%rip), %rsi     # 0x11f6e0
movb $0x3, %al
callq 0x9560
jmp 0xe726c
testl %r14d, %r14d
je 0xe7282
pushq $-0x1
popq %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq