// File: DoubleString[P]RtConverter/src/Rtklib/rt17.cpp
// Function: bamboo::DecodeType17(bamboo::raw_t*, unsigned int)
static int DecodeType17(raw_t *Raw, unsigned int rif)
{
    rt17_t *rt17 = (rt17_t*) Raw->rcv_data;
    unsigned char *p = rt17->MessageBuffer;
    double ClockOffset, tow;
    int Flags1, Flags2, FlagStatus, i, n, nsat, prn, Week;
    gtime_t Time;
    obsd_t *obs;

    tow = R8(p) * 0.001; p += 8;         /* Receive time within the current GPS week. */
    ClockOffset = R8(p) * 0.001; p += 8; /* Clock offset value. 0.0 = not known */ 

#if 0
    tow += ClockOffset;
#endif
 
    /* The observation data does not have the current GPS week number. Punt! */
    Week = GetWeek(Raw, tow);
    Time = gpst2time(Week, tow);

    nsat = U1(p); p++; /* Number of SV data blocks in the record */

    for (i = n = 0; (i < nsat) && (i < MAXOBS); i++)
    {
        obs = &Raw->obs.data[n];
        memset(obs, 0, sizeof(obsd_t));
        obs->time = Time;

        if (rif & M_CONCISE)
        {
            /* Satellite number (1-32). */
            prn = U1(p);
            p++;

            /* These indicate what data is loaded, is valid, etc */
            Flags1 = U1(p);
            p++; 
            Flags2 = U1(p);
            p++;

            /* These are not needed by RTKLIB */
            p++;    /* I1 Satellite Elevation Angle (degrees) */
            p += 2; /* I2 Satellite Azimuth (degrees) */

            if (Flags1 & M_BIT6) /* L1 data valid */
            {
                /* Measure of L1 signal strength (dB * 4) */
                obs->SNR[0] = U1(p);
                p++;
                
                /* Full L1 C/A code or P-code pseudorange (meters) */
                obs->P[0] = R8(p);
                p += 8;
        
                /*  L1 Continuous Phase (cycles) */
                if (Flags1 & M_BIT4) /* L1 phase valid */
                    obs->L[0] = -R8(p);
                p += 8;

                /* L1 Doppler (Hz) */
                obs->D[0] = R4(p);
                p += 4; 
            }

            if (Flags1 & M_BIT0)  /* L2 data loaded */
            {
                /* Measure of L2 signal strength (dB * 4) */
                obs->SNR[1] = U1(p);
                p++;
                
                /* L2 Continuous Phase (cycles) */
                if (Flags1 & M_BIT5)
                    obs->L[1] = -R8(p);
                p += 8; 

                /* L2 P-Code or L2 Encrypted Code */              
                if (Flags1 & M_BIT5) /* L2 range valid */
                    obs->P[1] = obs->P[0] + R4(p);
                p += 4;
            }
        
            /*
            | We can't use the IODE flags in this context.
            | We already have slip flags and don't need slip counters.
            */
            if (rif & M_ENHANCED)
            {
                p++; /* U1 IODE, Issue of Data Ephemeris */
                p++; /* U1 L1 cycle slip roll-over counter */ 
                p++; /* U1 L2 cycle slip roll-over counter */ 
            }           
        }
        else /* Expanded Format */
        {
            /* Satellite number (1-32) */
            prn = U1(p);
            p++;

            /* These indicate what data is loaded, is valid, etc */
            Flags1 = U1(p);
            p++;
            Flags2 = U1(p);
            p++;

            /* Indicates whether FLAGS1 bit 6 and FLAGS2 are valid */
            FlagStatus = U1(p);
            p++;

            /* These are not needed by RTKLIB */
            p += 2; /* I2 Satellite Elevation Angle (degrees) */
            p += 2; /* I2 Satellite Azimuth (degrees) */

            /*
            | FLAG STATUS bit 0 set   = Bit 6 of FLAGS1 and bit 0-7 of FLAGS2 are valid.
            | FLAG STATUS bit 0 clear = Bit 6 of FLAGS1 and bit 0-7 of FLAGS2 are UNDEFINED.
            |
            | According to reference #1 above, this bit should ALWAYS be set
            | for RAWDATA. If this bit is not set, then we're lost and cannot
            | process this message any further.
            */
            if (!(FlagStatus & M_BIT0)) /* Flags invalid */
                return 0;
          
            if (Flags1 & M_BIT6) /* L1 data valid */
            {           
                /* Measure of satellite signal strength (dB) */
                obs->SNR[0] = R8(p) * 4.0;
                p += 8;

                /* Full L1 C/A code or P-code pseudorange (meters) */
                obs->P[0] = R8(p);
                p += 8;

                /* L1 Continuous Phase (cycles) */
                if (Flags1 & M_BIT4) /* L1 phase valid */
                    obs->L[0] = -R8(p);
                p += 8;

                /* L1 Doppler (Hz) */
                obs->D[0] = R8(p);
                p += 8;

                /* Reserved 8 bytes */
                p += 8;
            }

            if (Flags1 & M_BIT0) /* L2 data loaded */
            {
                /* Measure of L2 signal strength (dB) */
                obs->SNR[1] = R8(p) * 4.0;
                p += 8;

                /* L2 Continuous Phase (cycles) */                
                if (Flags1 & M_BIT5) /* L2 phase valid */
                    obs->L[1] = -R8(p);
                p += 8;

                /* L2 P-Code or L2 Encrypted Code */              
                if (Flags1 & M_BIT5) /* L2 pseudorange valid */
                    obs->P[1] = obs->P[0] + R8(p);
                p += 8;
            }   
                
            if (rif & M_ENHANCED)
            {
                /*
                | We can't use the IODE flags in this context.
                | We already have slip flags and don't need slip counters.
                */
                p++; /* U1 IODE, Issue of Data Ephemeris */
                p++; /* U1 L1 cycle slip roll-over counter */ 
                p++; /* U1 L2 cycle slip roll-over counter */ 
                p++; /* U1 Reserved byte */

                /* L2 Doppler (Hz) */
                obs->D[1] = R8(p);
                p += 8;
            }
        }

        obs->code[0] = (obs->P[0] == 0.0) ? CODE_NONE : (Flags2 & M_BIT0) ? CODE_L1P : CODE_L1C;
        obs->code[1] = (obs->P[1] == 0.0) ? CODE_NONE : (Flags2 & M_BIT2) ? CODE_L2W : (Flags2 & M_BIT1) ? CODE_L2P : CODE_L2C;

        if (Flags1 & M_BIT1)
            obs->LLI[0] |= 1;  /* L1 cycle slip */

        if (Flags1 & M_BIT2)
            obs->LLI[1] |= 1;  /* L2 cycle slip */
        if ((Flags2 & M_BIT2) && (obs->P[1] != 0.0))
            obs->LLI[1] |= 4; /* Tracking encrypted code */

        if (!(obs->sat = satno(SYS_GPS, prn)))
        {
            tracet(2, "RT17: Satellite number error, PRN=%d.\n", prn);
            continue;
        }

#if 0
        /* Apply clock offset to observables */
        if (ClockOffset != 0.0)
        {
            obs->P[0] += ClockOffset * (CLIGHT/FREQ1);
            obs->P[1] += ClockOffset * (CLIGHT/FREQ2);
            obs->L[0] += ClockOffset * FREQ1;
            obs->L[1] += ClockOffset * FREQ2;
        }
#endif
         n++;
    }

    Raw->time = Time;
    Raw->obs.n = n;
    
    if (n > 0)
    {
        tracet(2, "RT17: Observations output:\n");
        traceobs(2, Raw->obs.data, Raw->obs.n);
    }

    return (n > 0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x82ba0(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x69120
movsd 0x54a1b(%rip), %xmm1    # 0xbdd48
mulsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x69120
movsd 0x549f5(%rip), %xmm1    # 0xbdd48
mulsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movsd -0x38(%rbp), %xmm0
callq 0x69b10
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %edi
movsd -0x38(%rbp), %xmm0
callq 0x8e4a0
movq %rax, -0x80(%rbp)
movsd %xmm0, -0x78(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %ecx
xorl %eax, %eax
cmpl -0x50(%rbp), %ecx
movb %al, -0x81(%rbp)
jge 0x693e0
cmpl $0x40, -0x48(%rbp)
setl %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
testb $0x1, %al
jne 0x693ef
jmp 0x699f5
movq -0x10(%rbp), %rax
movq 0x1d28(%rax), %rax
movslq -0x4c(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x60, %edx
callq 0x93d0
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, (%rax)
movq -0x60(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x69622
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movl -0x3c(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x69539
movq -0x28(%rbp), %rax
movb (%rax), %cl
movq -0x70(%rbp), %rax
movb %cl, 0x12(%rax)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x69120
movq -0x70(%rbp), %rax
movsd %xmm0, 0x38(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movl -0x3c(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x6950f
movq -0x28(%rbp), %rdi
callq 0x69120
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x70(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x69d90
movq -0x70(%rbp), %rax
movss %xmm0, 0x50(%rax)
movq -0x28(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rbp)
movl -0x3c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x695ee
movq -0x28(%rbp), %rax
movb (%rax), %cl
movq -0x70(%rbp), %rax
movb %cl, 0x13(%rax)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movl -0x3c(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x69595
movq -0x28(%rbp), %rdi
callq 0x69120
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x70(%rbp), %rax
movsd %xmm0, 0x28(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movl -0x3c(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x695e2
movq -0x70(%rbp), %rax
movsd 0x38(%rax), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x28(%rbp), %rdi
callq 0x69d90
movaps %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movq -0x70(%rbp), %rax
movsd %xmm0, 0x40(%rax)
movq -0x28(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x28(%rbp)
movl -0x14(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x6961d
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x69897
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movl -0x44(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x696a9
movl $0x0, -0x4(%rbp)
jmp 0x69a5c
movl -0x3c(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x69772
movq -0x28(%rbp), %rdi
callq 0x69120
movsd 0x53727(%rip), %xmm1    # 0xbcdf0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq -0x70(%rbp), %rax
movb %cl, 0x12(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x69120
movq -0x70(%rbp), %rax
movsd %xmm0, 0x38(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movl -0x3c(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x69738
movq -0x28(%rbp), %rdi
callq 0x69120
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x70(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x69120
cvtsd2ss %xmm0, %xmm0
movq -0x70(%rbp), %rax
movss %xmm0, 0x50(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movl -0x3c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x69838
movq -0x28(%rbp), %rdi
callq 0x69120
movsd 0x5365e(%rip), %xmm1    # 0xbcdf0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq -0x70(%rbp), %rax
movb %cl, 0x13(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movl -0x3c(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x697e3
movq -0x28(%rbp), %rdi
callq 0x69120
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x70(%rbp), %rax
movsd %xmm0, 0x28(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movl -0x3c(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x6982c
movq -0x70(%rbp), %rax
movsd 0x38(%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
movq -0x28(%rbp), %rdi
callq 0x69120
movaps %xmm0, %xmm1
movsd -0x98(%rbp), %xmm0
addsd %xmm1, %xmm0
movq -0x70(%rbp), %rax
movsd %xmm0, 0x40(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movl -0x14(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x69895
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x69120
cvtsd2ss %xmm0, %xmm0
movq -0x70(%rbp), %rax
movss %xmm0, 0x54(%rax)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
jmp 0x69897
movq -0x70(%rbp), %rax
movsd 0x38(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x698b5
jp 0x698b5
xorl %eax, %eax
movl %eax, -0x9c(%rbp)
jmp 0x698d1
movl -0x40(%rbp), %edx
andl $0x1, %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movb %al, %cl
movq -0x70(%rbp), %rax
movb %cl, 0x18(%rax)
movq -0x70(%rbp), %rax
movsd 0x40(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x698fe
jp 0x698fe
xorl %eax, %eax
movl %eax, -0xa0(%rbp)
jmp 0x6993e
movl -0x40(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x69916
movl $0x14, %eax
movl %eax, -0xa4(%rbp)
jmp 0x69932
movl -0x40(%rbp), %edx
andl $0x2, %edx
movl $0xe, %eax
movl $0x13, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
movb %al, %cl
movq -0x70(%rbp), %rax
movb %cl, 0x19(%rax)
movl -0x3c(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x69966
movq -0x70(%rbp), %rax
movzbl 0x15(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x15(%rax)
movl -0x3c(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x6997f
movq -0x70(%rbp), %rax
movzbl 0x16(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x16(%rax)
movl -0x40(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x699ae
movq -0x70(%rbp), %rax
movsd 0x40(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x699a0
jp 0x699a0
jmp 0x699ae
movq -0x70(%rbp), %rax
movzbl 0x16(%rax), %ecx
orl $0x4, %ecx
movb %cl, 0x16(%rax)
movl -0x54(%rbp), %esi
movl $0x1, %edi
callq 0x8b1b0
movq -0x70(%rbp), %rcx
movb %al, 0x10(%rcx)
cmpb $0x0, %al
jne 0x699de
movl -0x54(%rbp), %edx
movl $0x2, %edi
leaq 0x564ef(%rip), %rsi     # 0xbfec4
movb $0x0, %al
callq 0x95890
jmp 0x699e7
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x693c3
movq -0x10(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, (%rax)
movq -0x60(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl -0x4c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1d20(%rax)
cmpl $0x0, -0x4c(%rbp)
jle 0x69a4d
movl $0x2, %edi
leaq 0x564c4(%rip), %rsi     # 0xbfeeb
movb $0x0, %al
callq 0x95890
movq -0x10(%rbp), %rax
movq 0x1d28(%rax), %rsi
movq -0x10(%rbp), %rax
movl 0x1d20(%rax), %edx
movl $0x2, %edi
callq 0x958a0
cmpl $0x0, -0x4c(%rbp)
setg %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)