// File: /workspace/github_cpp_makefile_star2_O2/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 $0x358, %rsp            # imm = 0x358
testq %rdi, %rdi
je 0x2ef4e
movq %rdi, %rbx
callq 0x988f
leaq 0x2f64ef(%rip), %r13    # 0x325348
movsd (%r13), %xmm0
movsd %xmm0, -0x40(%rbp)
leaq 0x2f64e5(%rip), %r12    # 0x325350
movsd (%r12), %xmm0
movsd %xmm0, -0x48(%rbp)
leaq 0x2f64db(%rip), %r15    # 0x325358
movsd (%r15), %xmm0
movsd %xmm0, -0x38(%rbp)
pushq $0x2c
popq %r14
movq %rbx, %rax
movq %rax, %rdi
movl %r14d, %esi
callq 0x9100
testq %rax, %rax
je 0x2eea3
movb $0x20, (%rax)
jmp 0x2ee8e
leaq 0xc5fa9(%rip), %rsi     # 0xf4e53
movq %rbx, %rdi
callq 0x9090
testq %rax, %rax
je 0x2ef65
leaq 0x2c810e(%rip), %rbx    # 0x2f6fd0
movsd (%rbx), %xmm0
movsd %xmm0, -0x30(%rbp)
leaq 0x2c8106(%rip), %r14    # 0x2f6fd8
movsd (%r14), %xmm0
movsd %xmm0, -0x60(%rbp)
movq %r12, %r13
movq %r15, %r12
leaq 0x2c80f7(%rip), %r15    # 0x2f6fe0
movsd (%r15), %xmm0
movsd %xmm0, -0x58(%rbp)
callq 0x1eecd
movsd -0x30(%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 0x2f6426(%rip), %r13    # 0x325348
testl %eax, %eax
je 0x2efb8
movsd -0x40(%rbp), %xmm0
movsd %xmm0, (%r13)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, (%r12)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%r15)
pushq $0x4
jmp 0x2ef50
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xcff31(%rip), %rsi     # 0xfee9d
leaq -0x280(%rbp), %rdx
leaq -0x180(%rbp), %rcx
leaq -0x380(%rbp), %r8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9350
cmpl $0x1, %eax
jne 0x2f03b
callq 0x2edda
leaq 0xea457(%rip), %rsi     # 0x1193f7
leaq -0x180(%rbp), %rdi
movb $0x1, %al
callq 0x9060
andq $0x0, -0x50(%rbp)
jmp 0x2f052
leaq 0x2f6331(%rip), %rax    # 0x3252f0
movsd (%rax), %xmm0
mulsd 0xc68dd(%rip), %xmm0    # 0xf58a8
divsd 0xc68cd(%rip), %xmm0    # 0xf58a0
leaq 0xed88d(%rip), %rbx     # 0x11c867
leaq -0x280(%rbp), %rdi
movq %rbx, %rsi
movb $0x1, %al
callq 0x9060
leaq 0x2f6306(%rip), %rax    # 0x3252f8
movsd 0xc68ae(%rip), %xmm0    # 0xf58a8
mulsd (%rax), %xmm0
divsd 0xc689a(%rip), %xmm0    # 0xf58a0
leaq -0x180(%rbp), %rdi
movq %rbx, %rsi
movb $0x1, %al
callq 0x9060
leaq -0x380(%rbp), %rdi
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq %rbx, %rsi
movb $0x1, %al
callq 0x9060
andq $0x0, -0x50(%rbp)
jmp 0x2f110
andq $0x0, -0x50(%rbp)
cmpl $0x3, %eax
je 0x2f107
cmpl $0x2, %eax
jne 0x2ef2a
leaq -0x380(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x9330
leaq -0x78(%rbp), %r14
leaq -0x280(%rbp), %rsi
movq %r14, %rdi
callq 0x2ed73
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rbx
cmpq $0x0, (%r14)
setne %al
testq %rsi, %rsi
setne %cl
andb %al, %cl
testq %rbx, %rbx
setne %al
andb %cl, %al
leaq -0x280(%rbp), %rdi
cmpb $0x1, %al
jne 0x2f0b8
callq 0x9330
leaq -0x180(%rbp), %rdi
movq %rbx, %rsi
callq 0x9330
jmp 0x2f107
pushq $0x4b
popq %rsi
callq 0x9100
testq %rax, %rax
je 0x2f0ed
leaq -0x280(%rbp), %rbx
movq %rbx, %rdi
callq 0x9470
movsd %xmm0, -0x30(%rbp)
movsd %xmm0, -0x50(%rbp)
movl $0x302e30, %eax         # imm = 0x302E30
movl %eax, (%rbx)
movl %eax, -0x180(%rbp)
jmp 0x2f110
callq 0x2edda
leaq 0xea2fe(%rip), %rsi     # 0x1193f7
leaq -0x380(%rbp), %rdi
movb $0x1, %al
callq 0x9060
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
leaq -0x280(%rbp), %rsi
xorl %edi, %edi
callq 0x2e7c9
movsd %xmm0, (%r13)
pushq $0x1
popq %rbx
leaq -0x180(%rbp), %rsi
movl %ebx, %edi
callq 0x2e7c9
movsd %xmm0, (%r12)
leaq -0x380(%rbp), %rdi
callq 0x2e470
xorpd %xmm4, %xmm4
cmpeqsd %xmm0, %xmm4
movsd 0xc6850(%rip), %xmm1    # 0xf59a8
andpd %xmm4, %xmm1
andnpd %xmm0, %xmm4
orpd %xmm1, %xmm4
movlpd %xmm4, (%r15)
movsd (%r13), %xmm2
movapd 0xc4019(%rip), %xmm0    # 0xf3190
andpd %xmm2, %xmm0
ucomisd 0xc680d(%rip), %xmm0    # 0xf5990
jbe 0x2f1aa
movsd -0x40(%rbp), %xmm0
movsd %xmm0, (%r13)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, (%r12)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%r15)
jmp 0x2ef51
movsd (%r12), %xmm3
movapd 0xc3fd8(%rip), %xmm0    # 0xf3190
andpd %xmm3, %xmm0
ucomisd 0xc66e4(%rip), %xmm0    # 0xf58a8
jbe 0x2f1ed
movsd -0x40(%rbp), %xmm0
movsd %xmm0, (%r13)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, (%r12)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%r15)
pushq $0x2
jmp 0x2ef50
xorpd %xmm0, %xmm0
ucomisd %xmm4, %xmm0
ja 0x2f2d2
ucomisd 0xc3e4d(%rip), %xmm4    # 0xf3050
ja 0x2f2d2
movsd 0xc3f4f(%rip), %xmm0    # 0xf3160
divsd %xmm0, %xmm2
movsd %xmm2, (%r13)
divsd %xmm0, %xmm3
movsd %xmm3, (%r12)
leaq 0x2f6134(%rip), %rax    # 0x325360
movl $0xfffffffb, (%rax)     # imm = 0xFFFFFFFB
leaq 0x2c7d97(%rip), %r15    # 0x2f6fd0
movsd (%r15), %xmm0
leaq 0x2c7d93(%rip), %rbx    # 0x2f6fd8
movsd (%rbx), %xmm1
leaq -0x50(%rbp), %r14
leaq -0x78(%rbp), %rsi
movsd -0x30(%rbp), %xmm5
movq %r14, %rdi
callq 0xd5c84
movl %eax, -0x38(%rbp)
movsd (%r15), %xmm0
movsd (%rbx), %xmm1
movsd (%r13), %xmm2
movsd (%r12), %xmm3
callq 0xd5b00
movapd %xmm0, %xmm2
movsd (%r14), %xmm5
movsd 0xc3de4(%rip), %xmm0    # 0xf3070
ucomisd %xmm5, %xmm0
jbe 0x2f2f9
movsd 0xc3d9e(%rip), %xmm0    # 0xf3038
mulsd %xmm5, %xmm0
divsd 0xc670a(%rip), %xmm5    # 0xf59b0
mulsd 0xc670a(%rip), %xmm5    # 0xf59b8
movsd -0x78(%rbp), %xmm3
leaq 0x2f609e(%rip), %rax    # 0x325358
movsd (%rax), %xmm4
leaq 0x2f96db(%rip), %rdi    # 0x3289a0
leaq 0xcfdea(%rip), %rsi     # 0xff0b6
movapd %xmm5, %xmm1
jmp 0x2f327
movsd -0x40(%rbp), %xmm0
movsd %xmm0, (%r13)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, (%r12)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%r15)
pushq $0x3
jmp 0x2ef50
movapd %xmm5, %xmm1
divsd 0xc66ab(%rip), %xmm1    # 0xf59b0
movsd -0x78(%rbp), %xmm3
leaq 0x2f6047(%rip), %rax    # 0x325358
movsd (%rax), %xmm4
leaq 0x2f9684(%rip), %rdi    # 0x3289a0
leaq 0xcfdea(%rip), %rsi     # 0xff10d
movapd %xmm5, %xmm0
movl -0x38(%rbp), %edx
movb $0x5, %al
callq 0x9060
xorl %ebx, %ebx
jmp 0x2ef51