// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heathgps.cpp
// Function: decode_rt17_msg(int, int)
void decode_rt17_msg(int msg, int status)
{
int page;
int pages;
int reply;
int i;
static int last_reply = (-2);
static int last_page = 0;

   // parse and decode the RT17 message

   start_msg_decode(1);

   have_critical_alarms = 17;
   if(status & 0x02) critical_alarms |= CRIT_PWR;
   else              critical_alarms &= (~CRIT_PWR);

   if(msg == 0x40) {  // GSOF data
      reply = rt17_buf[0];
      page = rt17_buf[1];
      pages = rt17_buf[2];
   }
   else if(msg == 0x57) {  // RAWDATA
      // rt17_buf[0] = message type subcode
      page = rt17_buf[1];
      reply = rt17_buf[2];

      pages = page & 0x0F;
      page  = (page >> 4) & 0x0F;
   }
   else {  // other messages
      for(i=0; i<tsip_wptr; i++) {  // copy RT17 buffer to tsip buffer
         if(i < MAX_TSIP) tsip_buf[i] = rt17_buf[i];
      }

      // !!!!parse message here
      if(msg == 0x55) parse_rt17_svdata(msg);
else if(debug_file) fprintf(debug_file, "Misc msg type %02X skipped.\n", msg);

      rt17_wptr = 0;
      last_reply = (-1);
      last_page = 0;
      return;
   }

if(0 && debug_file) {
   fprintf(debug_file, "copy page %d of %d   reply:%d  last_reply:%d  len:%d\n", page, pages, reply,last_reply, rt17_wptr);
}
   // merge multi-page packet data into one buffer (tsip_buf)
   if(page != (last_page + 1)) {  // page sequence error
if(debug_file) fprintf(debug_file, "page seq error: last:%d  this:%d\n", last_page, page);
      last_page = 0;
      return;
   }

   last_page = page;
   if(page == 1) {  // first page of data, copy all data
      last_reply = reply;
      for(i=0; i<tsip_wptr; i++) {
         if(rt17_wptr < MAX_TSIP) tsip_buf[rt17_wptr++] = rt17_buf[i];
      }
   }
   else if(reply != last_reply) {  // message sequence error
if(debug_file) fprintf(debug_file, "reply seq error: last:%d  this:%d\n", last_reply, reply);
   }
   else if(msg == 0x40) {  // copy only message data from following pages to tsip_buf
      for(i=3; i<tsip_wptr; i++) {  // skip subcode, reply, page, pages
         if(rt17_wptr < MAX_TSIP) tsip_buf[rt17_wptr++] = rt17_buf[i];
      }
   }
   else if(msg == 0x57) {  // copy only message data from following pages to tsip_buf
      for(i=4; i<tsip_wptr; i++) {  // skip subcode, page, reply, flags
         if(rt17_wptr < MAX_TSIP) tsip_buf[rt17_wptr++] = rt17_buf[i];
      }
   }
if(0 && debug_file) fprintf(debug_file, "Page:%02X  tsip_wptr:%d  rt17_wptr:%d\n", page, tsip_wptr, rt17_wptr);

   if(page != pages) {  // more pages follow for this message
      return;
   }
   last_page = 0;

   tsip_wptr = rt17_wptr;  // parse merged page buffer

   if(msg == 0x40) {  // GSOF
      parse_rt17_gsof(msg);
   }
   else if(msg == 0x57) {  // RAWDATA
      parse_rt17_rawdata(msg);
   }

   rt17_wptr = 0;
   last_reply = (-1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %esi, %r14d
movl %edi, %ebx
movl $0x1, %edi
callq 0x7f2b8
leaq 0x28c851(%rip), %rax    # 0x30d2a8
movl $0x11, (%rax)
leaq 0x28c76c(%rip), %rax    # 0x30d1d0
movl (%rax), %ecx
andl $-0x5, %ecx
andl $0x2, %r14d
leal (%rcx,%r14,2), %ecx
movl %ecx, (%rax)
cmpl $0x40, %ebx
jne 0x80a91
movzbl 0x2f0381(%rip), %r14d   # 0x370e01
leaq 0x2f0379(%rip), %rax    # 0x370e00
movzbl 0x2f0373(%rip), %r15d   # 0x370e02
jmp 0x80ab4
cmpl $0x57, %ebx
jne 0x80b5c
movzbl 0x2f035f(%rip), %r14d   # 0x370e01
movl %r14d, %r15d
andl $0xf, %r15d
shrl $0x4, %r14d
leaq 0x2f034e(%rip), %rax    # 0x370e02
movl 0x2f1356(%rip), %edx    # 0x371e10
leal 0x1(%rdx), %ecx
cmpl %ecx, %r14d
jne 0x80b2d
movzbl (%rax), %ecx
movl %r14d, 0x2f1344(%rip)   # 0x371e10
cmpl $0x1, %r14d
jne 0x80ba8
movl %ecx, 0xe9b4c(%rip)     # 0x16a628
leaq 0x2b776f(%rip), %rax    # 0x338252
movzwl (%rax), %eax
testq %rax, %rax
je 0x80c6a
movl 0x2f130b(%rip), %esi    # 0x371e00
xorl %ecx, %ecx
leaq 0x2f0302(%rip), %rdx    # 0x370e00
leaq 0x2b674b(%rip), %rdi    # 0x337250
cmpl $0xfff, %esi            # imm = 0xFFF
jg 0x80b20
movb (%rcx,%rdx), %r8b
movslq %esi, %r9
incl %esi
movl %esi, 0x2f12e4(%rip)    # 0x371e00
movb %r8b, (%rdi,%r9)
incq %rcx
cmpq %rcx, %rax
jne 0x80b05
jmp 0x80c6a
leaq 0x1bd85c(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x80b4d
leaq 0xa2296(%rip), %rsi     # 0x122dd9
movl %r14d, %ecx
xorl %eax, %eax
callq 0x9560
movl $0x0, 0x2f12b9(%rip)    # 0x371e10
jmp 0x80d61
leaq 0x2b76ef(%rip), %rax    # 0x338252
movzwl (%rax), %eax
testq %rax, %rax
je 0x80b94
xorl %ecx, %ecx
leaq 0x2f028c(%rip), %rdx    # 0x370e00
leaq 0x2b66d5(%rip), %rsi    # 0x337250
cmpq $0xfff, %rcx            # imm = 0xFFF
ja 0x80b8c
movb (%rcx,%rdx), %dil
movb %dil, (%rsi,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x80b7b
cmpl $0x55, %ebx
jne 0x80c11
movl $0x55, %edi
callq 0x80822
jmp 0x80c30
movl 0xe9a7a(%rip), %edx     # 0x16a628
cmpl %ecx, %edx
jne 0x80c4d
cmpl $0x40, %ebx
jne 0x80cb5
leaq 0x2b768c(%rip), %rcx    # 0x338252
movzwl (%rcx), %eax
cmpq $0x4, %rax
jb 0x80d0f
movl $0x3, %ecx
movl 0x2f1222(%rip), %edi    # 0x371e00
leaq 0x2f021b(%rip), %rdx    # 0x370e00
leaq 0x2b6664(%rip), %rsi    # 0x337250
cmpl $0xfff, %edi            # imm = 0xFFF
jg 0x80c07
movb (%rcx,%rdx), %r8b
movslq %edi, %r9
incl %edi
movl %edi, 0x2f11fd(%rip)    # 0x371e00
movb %r8b, (%rsi,%r9)
incq %rcx
cmpq %rcx, %rax
jne 0x80bec
jmp 0x80c6a
leaq 0x1bd778(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x80c30
leaq 0xa2195(%rip), %rsi     # 0x122dbc
movl %ebx, %edx
xorl %eax, %eax
callq 0x9560
xorl %eax, %eax
movl %eax, 0x2f11c8(%rip)    # 0x371e00
movl $0xffffffff, 0xe99e6(%rip) # imm = 0xFFFFFFFF
movl %eax, 0x2f11c8(%rip)    # 0x371e10
jmp 0x80d61
leaq 0x1bd73c(%rip), %rax    # 0x23e390
movq (%rax), %rdi
testq %rdi, %rdi
je 0x80c6a
leaq 0xa2198(%rip), %rsi     # 0x122dfb
xorl %eax, %eax
callq 0x9560
cmpl %r15d, %r14d
jne 0x80d61
movl $0x0, 0x2f1193(%rip)    # 0x371e10
movzwl 0x2f117c(%rip), %eax    # 0x371e00
leaq 0x2b75c7(%rip), %rcx    # 0x338252
movw %ax, (%rcx)
cmpl $0x40, %ebx
jne 0x80c9d
callq 0x80946
jmp 0x80d4d
cmpl $0x57, %ebx
jne 0x80d4d
movl $0x57, %edi
callq 0x8086a
jmp 0x80d4d
cmpl $0x57, %ebx
jne 0x80d2d
leaq 0x2b7591(%rip), %rcx    # 0x338252
movzwl (%rcx), %eax
cmpq $0x5, %rax
jb 0x80d6c
movl $0x4, %ecx
movl 0x2f1127(%rip), %edi    # 0x371e00
leaq 0x2f0120(%rip), %rdx    # 0x370e00
leaq 0x2b6569(%rip), %rsi    # 0x337250
cmpl $0xfff, %edi            # imm = 0xFFF
jg 0x80d02
movb (%rcx,%rdx), %r8b
movslq %edi, %r9
incl %edi
movl %edi, 0x2f1102(%rip)    # 0x371e00
movb %r8b, (%rsi,%r9)
incq %rcx
cmpq %rcx, %rax
jne 0x80ce7
jmp 0x80c6a
cmpl %r15d, %r14d
jne 0x80d61
movl $0x0, 0x2f10f2(%rip)    # 0x371e10
movzwl 0x2f10db(%rip), %eax    # 0x371e00
movw %ax, (%rcx)
jmp 0x80c93
cmpl %r15d, %r14d
jne 0x80d61
movl $0x0, 0x2f10d4(%rip)    # 0x371e10
movzwl 0x2f10bd(%rip), %eax    # 0x371e00
leaq 0x2b7508(%rip), %rcx    # 0x338252
movw %ax, (%rcx)
movl $0x0, 0x2f10a9(%rip)    # 0x371e00
movl $0xffffffff, 0xe98c7(%rip) # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
cmpl %r15d, %r14d
jne 0x80d61
movl $0x0, 0x2f1095(%rip)    # 0x371e10
movzwl 0x2f107e(%rip), %eax    # 0x371e00
movw %ax, (%rcx)
jmp 0x80ca6