// File: DoubleString[P]RtConverter/src/Rtklib/rtcm.cpp
// Function: bamboo::input_rtcm2(bamboo::rtcm_t*, unsigned char)
extern int input_rtcm2(rtcm_t *rtcm, unsigned char data)
{
    unsigned char preamb;
    int i;
    
    trace(5,"input_rtcm2: data=%02x\n",data);
    
    if ((data&0xC0)!=0x40) return 0; /* ignore if upper 2bit != 01 */
    
    for (i=0;i<6;i++,data>>=1) { /* decode 6-of-8 form */
        rtcm->word=(rtcm->word<<1)+(data&1);
        
        /* synchronize frame */
        if (rtcm->nbyte==0) {
            preamb=(unsigned char)(rtcm->word>>22);
            if (rtcm->word&0x40000000) preamb^=0xFF; /* decode preamble */
            if (preamb!=RTCM2PREAMB) continue;
            
            /* check parity */
            if (!decode_word(rtcm->word,rtcm->buff)) continue;
            rtcm->nbyte=3; rtcm->nbit=0;
            continue;
        }
        if (++rtcm->nbit<30) continue; else rtcm->nbit=0;
        
        /* check parity */
        if (!decode_word(rtcm->word,rtcm->buff+rtcm->nbyte)) {
            trace(2,"rtcm2 partity error: i=%d word=%08x\n",i,rtcm->word);
            rtcm->nbyte=0; rtcm->word&=0x3;
            continue;
        }
        rtcm->nbyte+=3;
        if (rtcm->nbyte==6) rtcm->len=(rtcm->buff[5]>>3)*3+6;
        if (rtcm->nbyte<rtcm->len) continue;
        rtcm->nbyte=0; rtcm->word&=0x3;
        
        /* decode rtcm2 message */
        return decode_rtcm2(rtcm);
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movzbl -0x11(%rbp), %edx
movl $0x5, %edi
leaq 0x55407(%rip), %rsi     # 0xc0449
movb $0x0, %al
callq 0x8b980
movzbl -0x11(%rbp), %eax
andl $0xc0, %eax
cmpl $0x40, %eax
je 0x6b063
movl $0x0, -0x4(%rbp)
jmp 0x6b268
movl $0x0, -0x18(%rbp)
cmpl $0x6, -0x18(%rbp)
jge 0x6b261
movq -0x10(%rbp), %rax
movl 0x9f574(%rax), %ecx
shll %ecx
movzbl -0x11(%rbp), %eax
andl $0x1, %eax
addl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x9f574(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x9f0b8(%rax)
jne 0x6b127
movq -0x10(%rbp), %rax
movl 0x9f574(%rax), %eax
shrl $0x16, %eax
movb %al, -0x12(%rbp)
movq -0x10(%rbp), %rax
movl 0x9f574(%rax), %eax
andl $0x40000000, %eax       # imm = 0x40000000
cmpl $0x0, %eax
je 0x6b0d4
movzbl -0x12(%rbp), %eax
xorl $0xff, %eax
movb %al, -0x12(%rbp)
movzbl -0x12(%rbp), %eax
cmpl $0x66, %eax
je 0x6b0e2
jmp 0x6b24a
movq -0x10(%rbp), %rax
movl 0x9f574(%rax), %edi
movq -0x10(%rbp), %rsi
addq $0x9f0c4, %rsi          # imm = 0x9F0C4
callq 0x8c3e0
cmpl $0x0, %eax
jne 0x6b106
jmp 0x6b24a
movq -0x10(%rbp), %rax
movl $0x3, 0x9f0b8(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x9f0bc(%rax)
jmp 0x6b24a
movq -0x10(%rbp), %rcx
movl 0x9f0bc(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x9f0bc(%rcx)
cmpl $0x1e, %eax
jge 0x6b144
jmp 0x6b24a
movq -0x10(%rbp), %rax
movl $0x0, 0x9f0bc(%rax)
movq -0x10(%rbp), %rax
movl 0x9f574(%rax), %edi
movq -0x10(%rbp), %rsi
addq $0x9f0c4, %rsi          # imm = 0x9F0C4
movq -0x10(%rbp), %rax
movslq 0x9f0b8(%rax), %rax
addq %rax, %rsi
callq 0x8c3e0
cmpl $0x0, %eax
jne 0x6b1c5
movl -0x18(%rbp), %edx
movq -0x10(%rbp), %rax
movl 0x9f574(%rax), %ecx
movl $0x2, %edi
leaq 0x552c9(%rip), %rsi     # 0xc0461
movb $0x0, %al
callq 0x8b980
movq -0x10(%rbp), %rax
movl $0x0, 0x9f0b8(%rax)
movq -0x10(%rbp), %rax
movl 0x9f574(%rax), %ecx
andl $0x3, %ecx
movl %ecx, 0x9f574(%rax)
jmp 0x6b24a
movq -0x10(%rbp), %rax
movl 0x9f0b8(%rax), %ecx
addl $0x3, %ecx
movl %ecx, 0x9f0b8(%rax)
movq -0x10(%rbp), %rax
cmpl $0x6, 0x9f0b8(%rax)
jne 0x6b203
movq -0x10(%rbp), %rax
movzbl 0x9f0c9(%rax), %eax
sarl $0x3, %eax
imull $0x3, %eax, %ecx
addl $0x6, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x9f0c0(%rax)
movq -0x10(%rbp), %rax
movl 0x9f0b8(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x9f0c0(%rcx), %eax
jge 0x6b21b
jmp 0x6b24a
movq -0x10(%rbp), %rax
movl $0x0, 0x9f0b8(%rax)
movq -0x10(%rbp), %rax
movl 0x9f574(%rax), %ecx
andl $0x3, %ecx
movl %ecx, 0x9f574(%rax)
movq -0x10(%rbp), %rdi
callq 0x6b7e0
movl %eax, -0x4(%rbp)
jmp 0x6b268
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movzbl -0x11(%rbp), %eax
sarl %eax
movb %al, -0x11(%rbp)
jmp 0x6b06a
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)