// File: DoubleString[P]RtConverter/src/Rtklib/rt17.cpp
// Function: bamboo::GetWeek(bamboo::raw_t*, double)
static int GetWeek(raw_t *Raw, double Tow)
{
    rt17_t *rt17 = (rt17_t*) Raw->rcv_data;
    int Week = 0;

    if (rt17->Flags & M_WEEK_OPTION)
    {
        if ((Tow && rt17->Tow) && (Tow < rt17->Tow))
        {
            tracet(2, "RT17: GPS WEEK rolled over from %d to %d.\n", rt17->Week, rt17->Week + 1);
            rt17->Week++;
        }

        if (Tow != 0.0)
            rt17->Tow = Tow;
    }
    else if (!(rt17->Flags & M_WEEK_SCAN))
    {
        char *opt = strstr(Raw->opt, "-WEEK=");

        rt17->Flags |= M_WEEK_SCAN;

        if (opt)
        {
            if (!sscanf(opt+6, "%d", &Week) || (Week <= 0))
                tracet(0, "RT17: Invalid -WEEK=n receiver option value.\n");
            else
            {
                rt17->Week = Week;
                rt17->Flags |= M_WEEK_OPTION;
                tracet(2, "RT17: Initial GPS WEEK explicitly set to %d by user.\n", Week, Week);
            }
        }
    }

    Week = rt17->Week;

    if (!Week && !(rt17->Flags & (M_WEEK_OPTION|M_WEEK_EPH)))
    {
        if ((Raw->time.time == 0) && (Raw->time.sec == 0.0))
            Raw->time = timeget();
        
        time2gpst(Raw->time, &Week);

        if (Tow != 0.0)
            Raw->time = gpst2time(Week, Tow);

        rt17->Week = Week;
        rt17->Flags |= M_WEEK_TIME; 
        tracet(2, "RT17: Initial GPS WEEK number unknown; WEEK number %d assumed for now.\n", Week);       
    }
 
    return Week;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x82ba0(%rax), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x69bdc
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x69b5c
jp 0x69b5c
jmp 0x69bb7
movq -0x18(%rbp), %rax
movsd 0x10(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x69b72
jp 0x69b72
jmp 0x69bb7
movsd -0x10(%rbp), %xmm1
movq -0x18(%rbp), %rax
movsd 0x10(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x69bb7
movq -0x18(%rbp), %rax
movl 0x34(%rax), %edx
movq -0x18(%rbp), %rax
movl 0x34(%rax), %ecx
addl $0x1, %ecx
movl $0x2, %edi
leaq 0x55fbe(%rip), %rsi     # 0xbfb61
movb $0x0, %al
callq 0x95890
movq -0x18(%rbp), %rax
movl 0x34(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x34(%rax)
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x69bc9
jp 0x69bc9
jmp 0x69bd7
movsd -0x10(%rbp), %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x10(%rax)
jmp 0x69c8b
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x69c89
movq -0x8(%rbp), %rdi
addq $0x82a90, %rdi          # imm = 0x82A90
leaq 0x56306(%rip), %rsi     # 0xbff07
callq 0x91b0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%rax)
cmpq $0x0, -0x28(%rbp)
je 0x69c87
movq -0x28(%rbp), %rdi
addq $0x6, %rdi
leaq 0x5f2cd(%rip), %rsi     # 0xc8efa
leaq -0x1c(%rbp), %rdx
movb $0x0, %al
callq 0x9750
cmpl $0x0, %eax
je 0x69c43
cmpl $0x0, -0x1c(%rbp)
jg 0x69c55
xorl %edi, %edi
leaq 0x562c2(%rip), %rsi     # 0xbff0e
movb $0x0, %al
callq 0x95890
jmp 0x69c85
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x18(%rax)
movl -0x1c(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl $0x2, %edi
leaq 0x562be(%rip), %rsi     # 0xbff3c
movb $0x0, %al
callq 0x95890
jmp 0x69c87
jmp 0x69c89
jmp 0x69c8b
movq -0x18(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x69d7e
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x3, %eax
cmpl $0x0, %eax
jne 0x69d7e
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x69cf1
movq -0x8(%rbp), %rax
movsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x69cf1
jp 0x69cf1
callq 0x8e920
movq %rax, -0x38(%rbp)
movsd %xmm0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x48(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rdi
movsd -0x40(%rbp), %xmm0
leaq -0x1c(%rbp), %rsi
callq 0x8e540
movsd -0x10(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x69d28
jp 0x69d28
jmp 0x69d51
movl -0x1c(%rbp), %edi
movsd -0x10(%rbp), %xmm0
callq 0x8e4a0
movq %rax, -0x58(%rbp)
movsd %xmm0, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
orl $0x4, %ecx
movl %ecx, 0x18(%rax)
movl -0x1c(%rbp), %edx
movl $0x2, %edi
leaq 0x561fb(%rip), %rsi     # 0xbff72
movb $0x0, %al
callq 0x95890
movl -0x1c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)