// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movsd %xmm1, -0x20(%rbp)
movsd %xmm2, -0x28(%rbp)
movsd %xmm3, -0x30(%rbp)
movsd %xmm4, -0x38(%rbp)
movsd %xmm5, -0x40(%rbp)
movsd %xmm6, -0x48(%rbp)
movsd %xmm7, -0x50(%rbp)
movq -0x10(%rbp), %rax
andq $0x1e, %rax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jne 0xd6a1
movl $0x0, -0x4(%rbp)
jmp 0xd7bb
cmpl $0x1e, -0x54(%rbp)
jne 0xd6b3
movl $0x1, -0x4(%rbp)
jmp 0xd7bb
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
movsd -0x28(%rbp), %xmm2
movsd -0x30(%rbp), %xmm3
movsd -0x38(%rbp), %xmm4
movsd -0x40(%rbp), %xmm5
movsd -0x48(%rbp), %xmm6
movsd -0x50(%rbp), %xmm7
callq 0xd4a0
movsd %xmm0, -0x60(%rbp)
movsd -0x60(%rbp), %xmm0
callq 0xd5a0
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %ecx
addl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x68(%rbp)
movq -0x10(%rbp), %rax
andq $0x6, %rax
cmpq $0x6, %rax
jne 0xd733
movl -0x68(%rbp), %eax
andl $0x6, %eax
cmpl $0x0, %eax
je 0xd727
movl $0x1, -0x4(%rbp)
jmp 0xd7bb
movl $0x0, -0x4(%rbp)
jmp 0xd7bb
movq -0x10(%rbp), %rax
andq $0x18, %rax
cmpq $0x18, %rax
jne 0xd75e
movl -0x68(%rbp), %eax
andl $0x18, %eax
cmpl $0x0, %eax
je 0xd755
movl $0x1, -0x4(%rbp)
jmp 0xd7bb
movl $0x0, -0x4(%rbp)
jmp 0xd7bb
movq -0x10(%rbp), %rax
andq $0xc, %rax
cmpq $0xc, %rax
jne 0xd789
movl -0x68(%rbp), %eax
andl $0xc, %eax
cmpl $0x0, %eax
je 0xd780
movl $0x1, -0x4(%rbp)
jmp 0xd7bb
movl $0x0, -0x4(%rbp)
jmp 0xd7bb
movq -0x10(%rbp), %rax
andq $0x12, %rax
cmpq $0x12, %rax
jne 0xd7b4
movl -0x68(%rbp), %eax
andl $0x12, %eax
cmpl $0x0, %eax
je 0xd7ab
movl $0x1, -0x4(%rbp)
jmp 0xd7bb
movl $0x0, -0x4(%rbp)
jmp 0xd7bb
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq