// File: 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
subq $0x3b0, %rsp            # imm = 0x3B0
movq %rdi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x45e22
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x46519
movq -0x10(%rbp), %rdi
callq 0x9890
leaq 0x3546f6(%rip), %rax    # 0x39a528
movsd (%rax), %xmm0
movsd %xmm0, -0x18(%rbp)
leaq 0x3546ee(%rip), %rax    # 0x39a530
movsd (%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
leaq 0x3546e6(%rip), %rax    # 0x39a538
movsd (%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x350(%rbp)
cmpq $0x0, -0x350(%rbp)
je 0x45ea2
movq -0x350(%rbp), %rdi
movl $0x2c, %esi
callq 0x9100
movq %rax, -0x350(%rbp)
cmpq $0x0, -0x350(%rbp)
je 0x45e9e
movq -0x350(%rbp), %rax
movb $0x20, (%rax)
jmp 0x45ea0
jmp 0x45ea2
jmp 0x45e66
movq -0x10(%rbp), %rdi
leaq 0x12af18(%rip), %rsi    # 0x170dc5
callq 0x9090
cmpq $0x0, %rax
je 0x45fd3
leaq 0x3262ed(%rip), %rax    # 0x36c1b0
movsd (%rax), %xmm0
movsd %xmm0, -0x378(%rbp)
leaq 0x3262e2(%rip), %rax    # 0x36c1b8
movsd (%rax), %xmm0
movsd %xmm0, -0x380(%rbp)
leaq 0x3262d7(%rip), %rax    # 0x36c1c0
movsd (%rax), %xmm0
movsd %xmm0, -0x388(%rbp)
callq 0x2d870
movl %eax, -0x344(%rbp)
movsd -0x378(%rbp), %xmm0
leaq 0x3262a1(%rip), %rax    # 0x36c1b0
movsd %xmm0, (%rax)
movsd -0x380(%rbp), %xmm0
leaq 0x326296(%rip), %rax    # 0x36c1b8
movsd %xmm0, (%rax)
movsd -0x388(%rbp), %xmm0
leaq 0x32628b(%rip), %rax    # 0x36c1c0
movsd %xmm0, (%rax)
cmpl $0x0, -0x344(%rbp)
je 0x45f47
jmp 0x46174
leaq -0x140(%rbp), %rdi
movsd 0x129122(%rip), %xmm0   # 0x16f078
leaq 0x354573(%rip), %rax    # 0x39a4d0
mulsd (%rax), %xmm0
movsd 0x12b65f(%rip), %xmm1   # 0x1715c8
divsd %xmm1, %xmm0
leaq 0x14f505(%rip), %rsi    # 0x195479
movb $0x1, %al
callq 0x9060
leaq -0x240(%rbp), %rdi
movsd 0x1290ee(%rip), %xmm0   # 0x16f078
leaq 0x354547(%rip), %rax    # 0x39a4d8
mulsd (%rax), %xmm0
movsd 0x12b62b(%rip), %xmm1   # 0x1715c8
divsd %xmm1, %xmm0
leaq 0x14f4d1(%rip), %rsi    # 0x195479
movb $0x1, %al
callq 0x9060
leaq -0x340(%rbp), %rdi
leaq 0x14f4bc(%rip), %rsi    # 0x195479
xorps %xmm0, %xmm0
movb $0x1, %al
callq 0x9060
movl $0x3, -0x344(%rbp)
jmp 0x46042
movq -0x10(%rbp), %rdi
leaq -0x140(%rbp), %rdx
leaq -0x240(%rbp), %rcx
leaq -0x340(%rbp), %r8
leaq 0x1337c6(%rip), %rsi    # 0x1797b9
movb $0x0, %al
callq 0x9370
movl %eax, -0x344(%rbp)
cmpl $0x1, -0x344(%rbp)
jne 0x46040
leaq -0x240(%rbp), %rax
movq %rax, -0x3a8(%rbp)
callq 0x45d70
movq -0x3a8(%rbp), %rdi
leaq 0x14bfa9(%rip), %rsi    # 0x191fd3
movb $0x1, %al
callq 0x9060
movl -0x344(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x344(%rbp)
jmp 0x46042
xorps %xmm0, %xmm0
movsd %xmm0, -0x360(%rbp)
cmpl $0x2, -0x344(%rbp)
jne 0x46169
leaq -0x340(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x9350
leaq -0x140(%rbp), %rsi
leaq -0x3a0(%rbp), %rdi
callq 0x45c70
movq -0x3a0(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x398(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x390(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x460db
cmpq $0x0, -0x38(%rbp)
je 0x460db
cmpq $0x0, -0x30(%rbp)
je 0x460db
leaq -0x140(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x9350
leaq -0x240(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x9350
jmp 0x46167
leaq -0x140(%rbp), %rdi
movl $0x4b, %esi
callq 0x9100
cmpq $0x0, %rax
je 0x4612e
leaq -0x140(%rbp), %rdi
callq 0x9480
movsd %xmm0, -0x360(%rbp)
leaq -0x140(%rbp), %rdi
leaq 0x133a56(%rip), %rsi    # 0x179b6a
callq 0x9350
leaq -0x240(%rbp), %rdi
leaq 0x133a43(%rip), %rsi    # 0x179b6a
callq 0x9350
jmp 0x46165
leaq -0x340(%rbp), %rax
movq %rax, -0x3b0(%rbp)
callq 0x45d70
movq -0x3b0(%rbp), %rdi
leaq 0x14be84(%rip), %rsi    # 0x191fd3
movb $0x1, %al
callq 0x9060
movl -0x344(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x344(%rbp)
jmp 0x46167
jmp 0x461b2
cmpl $0x3, -0x344(%rbp)
je 0x461b0
jmp 0x46174
movsd -0x18(%rbp), %xmm0
leaq 0x3543a8(%rip), %rax    # 0x39a528
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
leaq 0x3543a0(%rip), %rax    # 0x39a530
movsd %xmm0, (%rax)
movsd -0x28(%rbp), %xmm0
leaq 0x354398(%rip), %rax    # 0x39a538
movsd %xmm0, (%rax)
movl $0x4, -0x4(%rbp)
jmp 0x46519
jmp 0x461b2
leaq -0x140(%rbp), %rsi
xorl %edi, %edi
callq 0x453c0
leaq 0x354361(%rip), %rax    # 0x39a528
movsd %xmm0, (%rax)
leaq -0x240(%rbp), %rsi
movl $0x1, %edi
callq 0x453c0
leaq 0x35434d(%rip), %rax    # 0x39a530
movsd %xmm0, (%rax)
leaq -0x340(%rbp), %rdi
callq 0x44cc0
leaq 0x35433e(%rip), %rax    # 0x39a538
movsd %xmm0, (%rax)
leaq 0x354333(%rip), %rax    # 0x39a538
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x46227
jp 0x46227
leaq 0x35431d(%rip), %rax    # 0x39a538
movsd 0x128f6d(%rip), %xmm0   # 0x16f190
movsd %xmm0, (%rax)
movsd 0x128e51(%rip), %xmm0   # 0x16f080
leaq 0x3542f2(%rip), %rax    # 0x39a528
ucomisd (%rax), %xmm0
ja 0x46255
leaq 0x3542e5(%rip), %rax    # 0x39a528
movsd (%rax), %xmm0
movsd 0x128e39(%rip), %xmm1   # 0x16f088
ucomisd %xmm1, %xmm0
jbe 0x46291
movsd -0x18(%rbp), %xmm0
leaq 0x3542c7(%rip), %rax    # 0x39a528
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
leaq 0x3542bf(%rip), %rax    # 0x39a530
movsd %xmm0, (%rax)
movsd -0x28(%rbp), %xmm0
leaq 0x3542b7(%rip), %rax    # 0x39a538
movsd %xmm0, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x46519
movsd 0x128dd7(%rip), %xmm0   # 0x16f070
leaq 0x354290(%rip), %rax    # 0x39a530
ucomisd (%rax), %xmm0
ja 0x462bf
leaq 0x354283(%rip), %rax    # 0x39a530
movsd (%rax), %xmm0
movsd 0x128dbf(%rip), %xmm1   # 0x16f078
ucomisd %xmm1, %xmm0
jbe 0x462fb
movsd -0x18(%rbp), %xmm0
leaq 0x35425d(%rip), %rax    # 0x39a528
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
leaq 0x354255(%rip), %rax    # 0x39a530
movsd %xmm0, (%rax)
movsd -0x28(%rbp), %xmm0
leaq 0x35424d(%rip), %rax    # 0x39a538
movsd %xmm0, (%rax)
movl $0x2, -0x4(%rbp)
jmp 0x46519
xorps %xmm0, %xmm0
leaq 0x354233(%rip), %rax    # 0x39a538
ucomisd (%rax), %xmm0
ja 0x46324
leaq 0x354226(%rip), %rax    # 0x39a538
movsd (%rax), %xmm0
movsd 0x128d22(%rip), %xmm1   # 0x16f040
ucomisd %xmm1, %xmm0
jbe 0x46360
movsd -0x18(%rbp), %xmm0
leaq 0x3541f8(%rip), %rax    # 0x39a528
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
leaq 0x3541f0(%rip), %rax    # 0x39a530
movsd %xmm0, (%rax)
movsd -0x28(%rbp), %xmm0
leaq 0x3541e8(%rip), %rax    # 0x39a538
movsd %xmm0, (%rax)
movl $0x3, -0x4(%rbp)
jmp 0x46519
jmp 0x46362
jmp 0x46364
leaq 0x3541bd(%rip), %rax    # 0x39a528
movsd (%rax), %xmm0
movsd 0x128e09(%rip), %xmm1   # 0x16f180
divsd %xmm1, %xmm0
leaq 0x3541a6(%rip), %rax    # 0x39a528
movsd %xmm0, (%rax)
leaq 0x3541a3(%rip), %rax    # 0x39a530
movsd (%rax), %xmm0
movsd 0x128de7(%rip), %xmm1   # 0x16f180
divsd %xmm1, %xmm0
leaq 0x35418c(%rip), %rax    # 0x39a530
movsd %xmm0, (%rax)
leaq 0x354191(%rip), %rax    # 0x39a540
movl $0xfffffffb, (%rax)     # imm = 0xFFFFFFFB
leaq 0x325df4(%rip), %rax    # 0x36c1b0
movsd (%rax), %xmm0
leaq 0x325df1(%rip), %rax    # 0x36c1b8
movsd (%rax), %xmm1
leaq 0x354156(%rip), %rax    # 0x39a528
movsd (%rax), %xmm2
leaq 0x354153(%rip), %rax    # 0x39a530
movsd (%rax), %xmm3
leaq 0x354150(%rip), %rax    # 0x39a538
movsd (%rax), %xmm4
movsd -0x360(%rbp), %xmm5
leaq -0x360(%rbp), %rdi
leaq -0x370(%rbp), %rsi
callq 0x13fbb0
movl %eax, -0x354(%rbp)
leaq 0x325d9c(%rip), %rax    # 0x36c1b0
movsd (%rax), %xmm0
leaq 0x325d99(%rip), %rax    # 0x36c1b8
movsd (%rax), %xmm1
leaq 0x3540fe(%rip), %rax    # 0x39a528
movsd (%rax), %xmm2
leaq 0x3540fb(%rip), %rax    # 0x39a530
movsd (%rax), %xmm3
callq 0x13f980
movsd %xmm0, -0x368(%rbp)
movsd 0x128c1a(%rip), %xmm0   # 0x16f068
ucomisd -0x360(%rbp), %xmm0
jbe 0x464c0
movsd 0x128bd0(%rip), %xmm0   # 0x16f030
mulsd -0x360(%rbp), %xmm0
movsd -0x360(%rbp), %xmm1
movsd 0x12b228(%rip), %xmm2   # 0x1716a0
divsd %xmm2, %xmm1
movsd 0x12b224(%rip), %xmm2   # 0x1716a8
mulsd %xmm2, %xmm1
movsd -0x368(%rbp), %xmm2
movsd -0x370(%rbp), %xmm3
leaq 0x354099(%rip), %rax    # 0x39a538
movsd (%rax), %xmm4
movl -0x354(%rbp), %edx
leaq 0x3576d0(%rip), %rdi    # 0x39db80
leaq 0x13351b(%rip), %rsi    # 0x1799d2
movb $0x5, %al
callq 0x9060
jmp 0x46512
movsd -0x360(%rbp), %xmm0
movsd -0x360(%rbp), %xmm1
movsd 0x12b1c8(%rip), %xmm2   # 0x1716a0
divsd %xmm2, %xmm1
movsd -0x368(%rbp), %xmm2
movsd -0x370(%rbp), %xmm3
leaq 0x354045(%rip), %rax    # 0x39a538
movsd (%rax), %xmm4
movl -0x354(%rbp), %edx
leaq 0x35767c(%rip), %rdi    # 0x39db80
leaq 0x13351e(%rip), %rsi    # 0x179a29
movb $0x5, %al
callq 0x9060
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x3b0, %rsp            # imm = 0x3B0
popq %rbp
retq
nopw %cs:(%rax,%rax)