// File: /workspace/github_c_makefile_star2_O1/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 0x8e75
cmpl $0x1e, %eax
jne 0x8d75
movb $0x1, %al
jmp 0x8e77
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 0x282(%rip), %xmm0      # 0x9040
movsd 0x26a(%rip), %xmm1      # 0x9030
subsd %xmm0, %xmm1
mulsd 0x12a6(%rip), %xmm1     # 0xa078
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x8dec
xorl %ecx, %ecx
movsd 0x2ba(%rip), %xmm0      # 0x90a0
ucomisd %xmm1, %xmm0
ja 0x8e31
ucomisd 0x2ac(%rip), %xmm1      # 0x90a0
jb 0x8e09
movl $0x1, %ecx
movsd 0x1285(%rip), %xmm0     # 0xa088
ucomisd %xmm1, %xmm0
ja 0x8e31
movsd 0x127f(%rip), %xmm0     # 0xa090
movsd 0x126f(%rip), %xmm2     # 0xa088
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 0x8e49
cmpl $0x2, %ecx
setb %al
jmp 0x8e77
movl %ebx, %ecx
notl %ecx
testb $0x18, %cl
jne 0x8e5a
cmpl $0x8, %eax
setae %al
jmp 0x8e77
movl %ebx, %ecx
notl %ecx
testb $0xc, %cl
jne 0x8e67
testb $0xc, %al
jmp 0x8e70
notl %ebx
testb $0x12, %bl
jne 0x8e75
testb $0x12, %al
setne %al
jmp 0x8e77
xorl %eax, %eax
movzbl %al, %eax
popq %rbx
retq