// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/mapper.cpp
// Function: isConcordant(alignment_t const*, alignment_t const*, paired_opt const&, int)
bool isConcordant(const alignment_t* mate1, const alignment_t* mate2, const paired_opt& options, int print){
    assert(mate1->rname.compare("*") != 0);
    assert(mate2->rname.compare("*") != 0);
    if(print >= 4) cerr << "check concordancy" << endl;
    bool firstLeft = true;
    //Check same chromosome
    if(mate1->rname != mate2->rname){
        if(print >= 4) cerr << "different chromosomes" << endl;
        return false;
    }
    //Check strand
    if(options.orientation == PAIR_FF){
        if(mate1->flag.test(4)!=mate2->flag.test(4)){
            if(print >= 4) cerr << "different orientation" << endl;
            return false;
        }
        else
            firstLeft = !mate1->flag.test(4);//if rc must be right
    }
    else if(options.orientation == PAIR_FR){
        if(mate1->flag.test(4)==mate2->flag.test(4)){
            if(print >= 4) cerr << "different orientation" << endl;
            return false;
        }
        else
            firstLeft = !mate1->flag.test(4);//if rc must be right
    }
    else{
        if(mate1->flag.test(4)==mate2->flag.test(4)){
            if(print >= 4) cerr << "different orientation" << endl;
            return false;
        }
        else
            firstLeft = mate1->flag.test(4);//if rc must be left
    }
    if(firstLeft && mate2->refE <= mate1->refB){
        if(print >= 4) cerr << "bad order of reads" << endl;
        return false;
    }
    else if(!firstLeft && mate1->refE <= mate2->refB){
        if(print >= 4) cerr << "bad order of reads" << endl;
        return false;
    }
    //check fragment insert size
    long begin = min(mate1->refB,mate2->refB);
    long end = max(mate1->refE,mate2->refE);
    long size = end - begin +1L;
    if(size < options.minInsert || size > options.maxInsert){
        if(print >= 2) cerr << "#INSERTERR " << size << endl;
        if(print >= 4) cerr << "bad insert: " << size << " is not in interval " << options.minInsert << ", " << options.maxInsert << endl;
        return false;
    }
    long begin1 = mate1->pos;
    long end1 = begin1 + (long)mate1->refLength-1L;
    long begin2 = mate2->pos;
    long end2 = begin2 + (long)mate2->refLength-1L;
    //check contain
    bool contain = (begin1 >= begin2 && end1 <= end2) || (begin2 >= begin1 && end2 <= end1);
    if(contain && !options.contain){
        if(print >= 4) cerr << "is contained" << endl;
        return false;
    }
    //check overlap
    bool overlap = contain || (begin2 >= begin1 && begin2 <= end1) || (end2 >= begin1 && end2 <= end1);
    if(!options.overlap && overlap){
        if(print >= 4) cerr << "is overlapping" << endl;
        return false;
    }
    //check dovetail
    if((firstLeft && (begin2 < begin1)) || (!firstLeft && (begin1 < begin2)) ){
        if(!overlap){
            if(print >= 4) cerr << "is dovetail" << endl;
            return false;
        }
        else if(!options.dovetail){
            if(print >= 4) cerr << "is dovetail" << endl;
            return false;
        }
    }
    if(print >= 2) cerr << "#CONCORDANT" << endl;
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
cmpl $0x4, %ecx
jl 0x1d7c3
movq 0x2c839(%rip), %rdi     # 0x49fe8
leaq 0x19a51(%rip), %rsi     # 0x37207
callq 0x72b0
movq %rax, %rdi
callq 0x7160
leaq 0x48(%r15), %rdi
leaq 0x48(%r14), %rsi
callq 0x22bb3
testb %al, %al
je 0x1d7f0
cmpl $0x4, %ebp
jl 0x1dad9
movq 0x2c804(%rip), %rdi     # 0x49fe8
leaq 0x19a2e(%rip), %rsi     # 0x37219
jmp 0x1d98d
movl %ebp, 0xc(%rsp)
movq %rbx, 0x10(%rsp)
movl 0x18(%rbx), %ebp
leaq 0x40(%r15), %r12
pushq $0x4
popq %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0xa11c
movl %eax, %ebx
leaq 0x40(%r14), %rdi
movq %r13, %rsi
callq 0xa11c
xorb %bl, %al
testl %ebp, %ebp
je 0x1d867
cmpl $0x2, %ebp
jne 0x1d92c
testb %al, %al
movq 0x10(%rsp), %rbx
movl 0xc(%rsp), %ebp
jne 0x1d951
pushq $0x4
popq %rsi
movq %r12, %rdi
callq 0xa11c
testb %al, %al
je 0x1d887
movq 0x8(%r15), %rsi
movq (%r14), %rdi
cmpq %rdi, %rsi
jle 0x1d95b
movq (%r15), %rdx
movq 0x8(%r14), %rcx
xorl %eax, %eax
jmp 0x1d8a0
testb %al, %al
je 0x1d974
pushq $0x4
popq %rsi
movq %r12, %rdi
callq 0xa11c
testb %al, %al
movq 0x10(%rsp), %rbx
movl 0xc(%rsp), %ebp
jne 0x1d84c
movq 0x8(%r14), %rcx
movq (%r15), %rdx
cmpq %rdx, %rcx
jle 0x1d95b
movq (%r14), %rdi
movq 0x8(%r15), %rsi
movb $0x1, %al
cmpq %rdx, %rdi
cmovlq %rdi, %rdx
cmpq %rcx, %rsi
cmovgq %rsi, %rcx
subq %rdx, %rcx
leaq 0x1(%rcx), %r12
movslq (%rbx), %rdx
cmpq %rdx, %r12
jl 0x1d9a1
movslq 0x4(%rbx), %rdx
cmpq %rdx, %rcx
jge 0x1d9a1
movq 0x18(%r15), %rcx
movslq 0x98(%r15), %rdi
addq %rcx, %rdi
movq 0x18(%r14), %rdx
movslq 0x98(%r14), %r8
addq %rdx, %r8
cmpq %rdx, %rcx
jl 0x1d8f4
cmpq %r8, %rdi
jle 0x1d906
cmpq %rcx, %rdx
jl 0x1da2f
cmpq %rdi, %r8
jg 0x1da2f
movb $0x1, %sil
cmpb $0x0, 0xb(%rbx)
jne 0x1da56
cmpl $0x4, %ebp
jl 0x1dad9
movq 0x2c6c5(%rip), %rdi     # 0x49fe8
leaq 0x1995c(%rip), %rsi     # 0x37286
jmp 0x1d98d
testb %al, %al
movq 0x10(%rsp), %rbx
movl 0xc(%rsp), %ebp
je 0x1d951
pushq $0x4
popq %rsi
movq %r12, %rdi
callq 0xa11c
testb %al, %al
je 0x1d84c
jmp 0x1d887
cmpl $0x4, %ebp
jge 0x1d97f
jmp 0x1dad9
cmpl $0x4, %ebp
jl 0x1dad9
movq 0x2c67d(%rip), %rdi     # 0x49fe8
leaq 0x198d3(%rip), %rsi     # 0x37245
jmp 0x1d98d
cmpl $0x4, 0xc(%rsp)
jl 0x1dad9
movq 0x2c662(%rip), %rdi     # 0x49fe8
leaq 0x198a2(%rip), %rsi     # 0x3722f
callq 0x72b0
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x7160
jmp 0x1dadb
cmpl $0x2, %ebp
jl 0x1dad9
movq 0x2c637(%rip), %rdi     # 0x49fe8
leaq 0x198a0(%rip), %rsi     # 0x37258
callq 0x72b0
movq %rax, %rdi
movq %r12, %rsi
callq 0x72a0
movq %rax, %rdi
callq 0x7160
cmpl $0x4, %ebp
jl 0x1dad9
movq 0x2c608(%rip), %rdi     # 0x49fe8
leaq 0x1987d(%rip), %rsi     # 0x37264
callq 0x72b0
movq %rax, %rdi
movq %r12, %rsi
callq 0x72a0
leaq 0x19873(%rip), %rsi     # 0x37271
movq %rax, %rdi
callq 0x72b0
movl (%rbx), %esi
movq %rax, %rdi
callq 0x75c0
leaq 0x18ef6(%rip), %rsi     # 0x3690d
movq %rax, %rdi
callq 0x72b0
movl 0x4(%rbx), %esi
movq %rax, %rdi
callq 0x75c0
jmp 0x1d992
cmpq %rcx, %rdx
setge %r9b
cmpq %rdi, %rdx
setl %r10b
movb $0x1, %sil
testb %r10b, %r9b
jne 0x1da56
cmpq %rdi, %r8
setle %dil
cmpq %rcx, %r8
setg %sil
andb %dil, %sil
cmpb $0x0, 0xc(%rbx)
jne 0x1da79
testb %sil, %sil
je 0x1da79
cmpl $0x4, %ebp
jl 0x1dad9
movq 0x2c57b(%rip), %rdi     # 0x49fe8
leaq 0x1981f(%rip), %rsi     # 0x37293
jmp 0x1d98d
xorl %edi, %edi
xorl %r8d, %r8d
cmpq %rdx, %rcx
setl %dil
setg %r8b
testb %al, %al
cmovnel %r8d, %edi
cmpb $0x1, %dil
jne 0x1daa0
testb %sil, %sil
je 0x1dac1
cmpb $0x0, 0xa(%rbx)
je 0x1dac1
movb $0x1, %bl
cmpl $0x2, %ebp
jl 0x1dadb
movq 0x2c53a(%rip), %rdi     # 0x49fe8
leaq 0x197f9(%rip), %rsi     # 0x372ae
callq 0x72b0
movb $0x1, %bl
jmp 0x1d994
cmpl $0x4, %ebp
jl 0x1dad9
movq 0x2c51b(%rip), %rdi     # 0x49fe8
leaq 0x197ce(%rip), %rsi     # 0x372a2
jmp 0x1d98d
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq