// File: /workspace/github_c_makefile_star2_O3/esheldon[P]xshear/src/sdss-survey.c
// Function: test_quad_sincos_sdss
int test_quad_sincos_sdss(int64 maskflags,
                          double sinlam1, double coslam1,
                          double sineta1, double coseta1,
                          double sinlam2, double coslam2,
                          double sineta2, double coseta2) {

	static const int OK=1, BAD=0;
    // none of the quadrants are ok
    int allcheck = maskflags & SDSS_QUADALL_OK;
    if (allcheck == 0) {
        return BAD;
    }
    // all quadrants are OK
    if ( allcheck == SDSS_QUADALL_OK) {
        return OK;
    }

    // first determine which quandrant the source is in

    double theta = posangle_survey_sincos(sinlam1, coslam1,
                                          sineta1, coseta1,
                                          sinlam2, coslam2,
                                          sineta2, coseta2);
    // note quadrant is in [0,3] to be consistent with stomp
    int quadrant = survey_quad(theta);

    // this we can and with the bits
    int quadbit = 1 << (quadrant+1);

    // work with the first good adjacent pair we find.  We already checked the
    // case where all quadrants are OK, so it is now arbitrary which one we
    // check against first, but we should be consistent for a given lens.
    // Might figure out which gives better systematics?
    //
    // It would *not* be right to try another pair if the first good one
    // fails; we need to always check on for a given lens to make sure
    // we only draw pairs from that one.
    
    if ((maskflags & SDSS_QUAD12_OK) == SDSS_QUAD12_OK) {
        if ((quadbit & SDSS_QUAD12_OK) != 0) {
            return OK;
        } else {
            return BAD;
        }
    }
    if ((maskflags & SDSS_QUAD34_OK) == SDSS_QUAD34_OK) {
        if ((quadbit & SDSS_QUAD34_OK) != 0) {
            return OK;
        } else {
            return BAD;
        }
    }
    if ((maskflags & SDSS_QUAD23_OK) == SDSS_QUAD23_OK) {
        if ((quadbit & SDSS_QUAD23_OK) != 0) {
            return OK;
        } else {
            return BAD;
        }
    }
    if ((maskflags & SDSS_QUAD41_OK) == SDSS_QUAD41_OK) {
        if ((quadbit & SDSS_QUAD41_OK) != 0) {
            return OK;
        } else {
            return BAD;
        }
    }


    // if we get here we didn't match a good pair of quadrants
    return BAD;

}

// Assembly:
pushq %rbx
movq %rdi, %rbx
movl %ebx, %eax
andl $0x1e, %eax
je 0x8fda
cmpl $0x1e, %eax
jne 0x8eda
movb $0x1, %al
jmp 0x8fdc
movapd %xmm0, %xmm8
mulsd %xmm6, %xmm2
movapd %xmm7, %xmm0
mulsd %xmm3, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm6, %xmm7
mulsd %xmm3, %xmm6
subsd %xmm7, %xmm6
mulsd %xmm4, %xmm1
divsd %xmm5, %xmm1
mulsd %xmm0, %xmm8
subsd %xmm1, %xmm8
movapd %xmm6, %xmm0
movapd %xmm8, %xmm1
callq 0x2210
addsd 0x11d(%rip), %xmm0      # 0x9040
movsd 0x105(%rip), %xmm1      # 0x9030
subsd %xmm0, %xmm1
mulsd 0x1151(%rip), %xmm1     # 0xa088
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x8f51
xorl %ecx, %ecx
movsd 0x155(%rip), %xmm0      # 0x90a0
ucomisd %xmm1, %xmm0
ja 0x8f96
ucomisd 0x147(%rip), %xmm1      # 0x90a0
jb 0x8f6e
movl $0x1, %ecx
movsd 0x1130(%rip), %xmm0     # 0xa098
ucomisd %xmm1, %xmm0
ja 0x8f96
movsd 0x112a(%rip), %xmm0     # 0xa0a0
movsd 0x111a(%rip), %xmm2     # 0xa098
cmplepd %xmm1, %xmm2
cmpltpd %xmm0, %xmm1
andpd %xmm2, %xmm1
movd %xmm1, %ecx
andl $0x1, %ecx
xorl $0x3, %ecx
movl $0x2, %eax
shll %cl, %eax
movl %ebx, %edx
notl %edx
testb $0x6, %dl
jne 0x8fae
cmpl $0x2, %ecx
setb %al
jmp 0x8fdc
movl %ebx, %ecx
notl %ecx
testb $0x18, %cl
jne 0x8fbf
cmpl $0x8, %eax
setae %al
jmp 0x8fdc
movl %ebx, %ecx
notl %ecx
testb $0xc, %cl
jne 0x8fcc
testb $0xc, %al
jmp 0x8fd5
notl %ebx
testb $0x12, %bl
jne 0x8fda
testb $0x12, %al
setne %al
jmp 0x8fdc
xorl %eax, %eax
movzbl %al, %eax
popq %rbx
retq