// File: /workspace/github_cpp_makefile_star2_O1/jkingan[P]ladyheather/heathui.cpp
// Function: calc_prop_delay(char*)
int calc_prop_delay(char *s)
{
double old_lat, old_lon, old_alt;
struct STATION station;
char lat_s[SLEN+1];
char lon_s[SLEN+1];
char alt_s[SLEN+1];
int n;
char *comma;
int hops;
double dist;
double az;
double delay;
double x_lat,x_lon,x_alt;

   // set lat/lon/altitude
   if(s == 0) return (-1);
   strupr(s);

   old_lat = prop_lat;
   old_lon = prop_lon;
   old_alt = prop_alt;

   comma = s;
   while(comma) {  // convert commas to spaces
      comma = strchr(comma, ',');
      if(comma) *comma = ' ';
      else break;
   }

   if(strstr(s, "LOC")) {
      x_lat = lat;
      x_lon = lon;
      x_alt = alt;
      n = read_default_lla();  // get location from heather.loc file
      lat = x_lat;
      lon = x_lon;
      alt = x_alt;
      if(n != LLA_OK) goto bad_loc;

      sprintf(lat_s, "%.9f", precise_lat*180.0/PI);
      sprintf(lon_s, "%.9f", precise_lon*180.0/PI);
      sprintf(alt_s, "%.9f", 0.0);
      n = 3;
   }
   else {
      n = sscanf(s, "%s %s %s", &lat_s[0],&lon_s[0],&alt_s[0]);
      if(n == 1) {  // iono height not given, default it
         sprintf(lon_s, "%g", iono_height());
         ++n;
      }
   }

   dist = 0.0;
   if(n == 2) {  // location is a time station name, distance, or lat/lon
      strcpy(alt_s, lon_s);
      station = find_station(lat_s);
      if(station.id && station.lat && station.lon) {  // station id given as location
         strcpy(lat_s, station.lat);
         strcpy(lon_s, station.lon);
      }
      else if(strchr(lat_s, 'K')) {  // distance in km
         dist = atof(lat_s);
         strcpy(lat_s, "0.0");
         strcpy(lon_s, "0.0");
      }
      else {  // lat/lon given, fill in ionosphere height
         sprintf(alt_s, "%g", iono_height());
         ++n;
      }
   }
   else if(n != 3) {
      bad_loc:
      prop_lat = old_lat;
      prop_lon = old_lon;
      prop_alt = old_alt;
      return LLA_MISSING;
   }

   prop_lat = parse_coord(0, lat_s);
   prop_lon = parse_coord(1, lon_s);
   prop_alt = parse_alt(alt_s);
   if(prop_alt == 0.0) prop_alt = 0.001;

   if((prop_lat < -90.0) || (prop_lat > 90.0)) {
      prop_lat = old_lat;
      prop_lon = old_lon;
      prop_alt = old_alt;
      return BAD_LAT;
   }
   else if((prop_lon < -180.0) || (prop_lon > 180.0)) {
      prop_lat = old_lat;
      prop_lon = old_lon;
      prop_alt = old_alt;
      return BAD_LON;
   }
   else if((prop_alt < 0.0) || (prop_alt > 1.0E6)) {
      prop_lat = old_lat;
      prop_lon = old_lon;
      prop_alt = old_alt;
      return BAD_ALT;
   }

   prop_lat /= RAD_TO_DEG;
   prop_lon /= RAD_TO_DEG;
   have_prop_lla = (-5);

   hops = find_delay(lat,lon, prop_lat,prop_lon,prop_alt, dist, &dist,&delay);
   az = az_angle(lat,lon, prop_lat,prop_lon);

   if(dist < 1.0) {  // less than 1km away, show meters/feet
      sprintf(debug_text, "%.3f m (%.2f feet)  az=%.3f deg   prop delay %.6f seconds  ionosphere:%.0f km  hops:%d",
          dist*1000.0, dist/1.609344*5280.0, az, delay, prop_alt, hops);
   }
   else {  // show km/miles
      sprintf(debug_text, "%.3f km (%.3f miles)  az=%.3f deg   prop delay %.6f seconds  ionosphere:%.0f km  hops:%d",
          dist, dist/1.609344, az, delay, prop_alt, hops);
   }

   return LLA_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x348, %rsp            # imm = 0x348
testq %rdi, %rdi
je 0x31a17
movq %rdi, %rbx
callq 0x9899
leaq 0x309a7e(%rip), %r13    # 0x33b348
movsd (%r13), %xmm0
movsd %xmm0, -0x38(%rbp)
leaq 0x309a74(%rip), %r12    # 0x33b350
movsd (%r12), %xmm0
movsd %xmm0, -0x40(%rbp)
leaq 0x309a6a(%rip), %r15    # 0x33b358
movsd (%r15), %xmm0
movsd %xmm0, -0x30(%rbp)
movq %rbx, %rdi
movl $0x2c, %esi
callq 0x9100
testq %rax, %rax
je 0x31912
movb $0x20, (%rax)
movq %rax, %rdi
jmp 0x318fb
leaq 0xd94af(%rip), %rsi     # 0x10adc8
movq %rbx, %rdi
callq 0x9090
testq %rax, %rax
je 0x31a21
leaq 0x2db69f(%rip), %rbx    # 0x30cfd0
movsd (%rbx), %xmm0
movsd %xmm0, -0x68(%rbp)
leaq 0x2db697(%rip), %r14    # 0x30cfd8
movsd (%r14), %xmm0
movsd %xmm0, -0x60(%rbp)
movq %r12, %r13
movq %r15, %r12
leaq 0x2db688(%rip), %r15    # 0x30cfe0
movsd (%r15), %xmm0
movsd %xmm0, -0x58(%rbp)
callq 0x202d4
movsd -0x68(%rbp), %xmm0
movsd %xmm0, (%rbx)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, (%r14)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, (%r15)
movq %r12, %r15
movq %r13, %r12
leaq 0x3099b7(%rip), %r13    # 0x33b348
testl %eax, %eax
jne 0x31b0c
leaq 0x309950(%rip), %rax    # 0x33b2f0
movsd (%rax), %xmm0
mulsd 0xd9e74(%rip), %xmm0    # 0x10b820
divsd 0xd9e64(%rip), %xmm0    # 0x10b818
leaq 0x100e9c(%rip), %rbx    # 0x132857
leaq -0x270(%rbp), %rdi
movq %rbx, %rsi
movb $0x1, %al
callq 0x9060
leaq 0x309925(%rip), %rax    # 0x33b2f8
movsd 0xd9e45(%rip), %xmm0    # 0x10b820
mulsd (%rax), %xmm0
divsd 0xd9e31(%rip), %xmm0    # 0x10b818
leaq -0x170(%rbp), %rdi
movq %rbx, %rsi
movb $0x1, %al
callq 0x9060
leaq -0x370(%rbp), %rdi
xorpd %xmm0, %xmm0
movq %rbx, %rsi
movb $0x1, %al
callq 0x9060
movl $0x3, %eax
jmp 0x31ab3
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x31e48
leaq 0xe3361(%rip), %rsi     # 0x114d89
leaq -0x270(%rbp), %rdx
leaq -0x170(%rbp), %rcx
leaq -0x370(%rbp), %r8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9360
cmpl $0x1, %eax
jne 0x31ab3
leaq 0x2dbabd(%rip), %rax    # 0x30d510
movl (%rax), %eax
leal -0xd(%rax), %ecx
cmpl $-0xc, %ecx
jae 0x31a67
movsd 0xd9ebb(%rip), %xmm0    # 0x10b920
jmp 0x31a99
leaq 0x2db562(%rip), %rcx    # 0x30cfd0
cmpl $0x7, %eax
movl $0xfffffffa, %edx       # imm = 0xFFFFFFFA
movl $0x6, %esi
cmovgel %edx, %esi
xorl %edx, %edx
xorpd %xmm0, %xmm0
ucomisd (%rcx), %xmm0
cmoval %esi, %edx
addl %eax, %edx
leaq 0x13886c(%rip), %rax    # 0x16a300
movsd (%rax,%rdx,8), %xmm0
leaq 0xfd947(%rip), %rsi     # 0x12f3e7
leaq -0x170(%rbp), %rdi
movb $0x1, %al
callq 0x9060
movl $0x2, %eax
movq $0x0, -0x50(%rbp)
cmpl $0x3, %eax
je 0x31c1d
cmpl $0x2, %eax
jne 0x31b0c
leaq -0x370(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x9340
leaq 0x1385bd(%rip), %r14    # 0x16a0a0
leaq -0x270(%rbp), %rbx
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x31b36
movq %rbx, %rsi
callq 0x9090
addq $0x18, %r14
testq %rax, %rax
je 0x31aea
leaq -0x8(%r14), %rcx
movb $0x1, %al
jmp 0x31b4f
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%r13)
movsd -0x40(%rbp), %xmm0
movsd %xmm0, (%r12)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, (%r15)
movl $0x4, %ebx
jmp 0x31e48
cmpq $0x0, 0x138552(%rip)    # 0x16a090
setne %al
leaq 0x138550(%rip), %rcx    # 0x16a098
leaq 0x138551(%rip), %r14    # 0x16a0a0
movq (%rcx), %rsi
movq (%r14), %rbx
testq %rsi, %rsi
setne %cl
andb %al, %cl
testq %rbx, %rbx
setne %al
andb %cl, %al
leaq -0x270(%rbp), %rdi
cmpb $0x1, %al
jne 0x31b89
callq 0x9340
leaq -0x170(%rbp), %rdi
movq %rbx, %rsi
callq 0x9340
jmp 0x31c1d
movl $0x4b, %esi
callq 0x9100
testq %rax, %rax
je 0x31bbb
leaq -0x270(%rbp), %rbx
movq %rbx, %rdi
callq 0x9470
movsd %xmm0, -0x50(%rbp)
movl $0x302e30, %eax         # imm = 0x302E30
movl %eax, (%rbx)
movl %eax, -0x170(%rbp)
jmp 0x31c1d
leaq 0x2db94e(%rip), %rax    # 0x30d510
movl (%rax), %eax
leal -0xd(%rax), %ecx
cmpl $-0xc, %ecx
jae 0x31bd6
movsd 0xd9d4c(%rip), %xmm0    # 0x10b920
jmp 0x31c08
leaq 0x2db3f3(%rip), %rcx    # 0x30cfd0
cmpl $0x7, %eax
movl $0xfffffffa, %edx       # imm = 0xFFFFFFFA
movl $0x6, %esi
cmovgel %edx, %esi
xorl %edx, %edx
xorpd %xmm0, %xmm0
ucomisd (%rcx), %xmm0
cmoval %esi, %edx
addl %eax, %edx
leaq 0x1386fd(%rip), %rax    # 0x16a300
movsd (%rax,%rdx,8), %xmm0
leaq 0xfd7d8(%rip), %rsi     # 0x12f3e7
leaq -0x370(%rbp), %rdi
movb $0x1, %al
callq 0x9060
leaq -0x270(%rbp), %rsi
xorl %edi, %edi
callq 0x3121e
movsd %xmm0, (%r13)
movl $0x1, %ebx
leaq -0x170(%rbp), %rsi
movl $0x1, %edi
callq 0x3121e
movsd %xmm0, (%r12)
leaq -0x370(%rbp), %rdi
callq 0x30e70
xorpd %xmm4, %xmm4
cmpeqsd %xmm0, %xmm4
movsd 0xd9cbe(%rip), %xmm1    # 0x10b928
andpd %xmm4, %xmm1
andnpd %xmm0, %xmm4
orpd %xmm1, %xmm4
movlpd %xmm4, (%r15)
movsd (%r13), %xmm2
movapd 0xd74f7(%rip), %xmm0    # 0x109180
andpd %xmm2, %xmm0
ucomisd 0xd9c7b(%rip), %xmm0    # 0x10b910
jbe 0x31cbc
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%r13)
movsd -0x40(%rbp), %xmm0
movsd %xmm0, (%r12)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, (%r15)
jmp 0x31e48
movsd (%r12), %xmm3
movapd 0xd74b6(%rip), %xmm0    # 0x109180
andpd %xmm3, %xmm0
ucomisd 0xd9b4a(%rip), %xmm0    # 0x10b820
jbe 0x31d02
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%r13)
movsd -0x40(%rbp), %xmm0
movsd %xmm0, (%r12)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, (%r15)
movl $0x2, %ebx
jmp 0x31e48
xorpd %xmm0, %xmm0
ucomisd %xmm4, %xmm0
ja 0x31de7
ucomisd 0xd7338(%rip), %xmm4    # 0x109050
ja 0x31de7
movsd 0xd7432(%rip), %xmm0    # 0x109158
divsd %xmm0, %xmm2
movsd %xmm2, (%r13)
divsd %xmm0, %xmm3
movsd %xmm3, (%r12)
leaq 0x30961f(%rip), %rax    # 0x33b360
movl $0xfffffffb, (%rax)     # imm = 0xFFFFFFFB
leaq 0x2db282(%rip), %r15    # 0x30cfd0
movsd (%r15), %xmm0
leaq 0x2db27e(%rip), %rbx    # 0x30cfd8
movsd (%rbx), %xmm1
leaq -0x50(%rbp), %r14
movsd (%r14), %xmm5
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0xe9807
movl %eax, -0x30(%rbp)
movsd (%r15), %xmm0
movsd (%rbx), %xmm1
movsd (%r13), %xmm2
movsd (%r12), %xmm3
callq 0xe9680
movapd %xmm0, %xmm2
movsd (%r14), %xmm5
movsd 0xd72cf(%rip), %xmm0    # 0x109070
ucomisd %xmm5, %xmm0
jbe 0x31e0e
movsd 0xd7289(%rip), %xmm0    # 0x109038
mulsd %xmm5, %xmm0
divsd 0xd9b75(%rip), %xmm5    # 0x10b930
mulsd 0xd9b75(%rip), %xmm5    # 0x10b938
movsd -0x48(%rbp), %xmm3
leaq 0x309589(%rip), %rax    # 0x33b358
movsd (%rax), %xmm4
leaq 0x30cbc6(%rip), %rdi    # 0x33e9a0
leaq 0xe31c1(%rip), %rsi     # 0x114fa2
movapd %xmm5, %xmm1
jmp 0x31e3c
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%r13)
movsd -0x40(%rbp), %xmm0
movsd %xmm0, (%r12)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, (%r15)
movl $0x3, %ebx
jmp 0x31e48
movapd %xmm5, %xmm1
divsd 0xd9b16(%rip), %xmm1    # 0x10b930
movsd -0x48(%rbp), %xmm3
leaq 0x309532(%rip), %rax    # 0x33b358
movsd (%rax), %xmm4
leaq 0x30cb6f(%rip), %rdi    # 0x33e9a0
leaq 0xe31c1(%rip), %rsi     # 0x114ff9
movapd %xmm5, %xmm0
movl -0x30(%rbp), %edx
movb $0x5, %al
callq 0x9060
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x348, %rsp            # imm = 0x348
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq