// File: 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:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movl %ecx, 0x54(%rsp)
cmpl $0x4, 0x54(%rsp)
jl 0x432d0
movq 0x53d3b(%rip), %rdi     # 0x96ff0
leaq 0x3adeb(%rip), %rsi     # 0x7e0a7
callq 0x8360
movq %rax, %rdi
movq 0x53ce5(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x1, 0x53(%rsp)
movq 0x68(%rsp), %rdi
addq $0x48, %rdi
movq 0x60(%rsp), %rsi
addq $0x48, %rsi
callq 0x4e540
testb $0x1, %al
jne 0x432f2
jmp 0x43325
cmpl $0x4, 0x54(%rsp)
jl 0x4331b
movq 0x53cf0(%rip), %rdi     # 0x96ff0
leaq 0x3adb2(%rip), %rsi     # 0x7e0b9
callq 0x8360
movq %rax, %rdi
movq 0x53c9a(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
movq 0x58(%rsp), %rax
cmpl $0x2, 0x18(%rax)
jne 0x433c6
movq 0x68(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x10(%rsp)
movq 0x60(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
movb %al, %cl
movl 0x10(%rsp), %eax
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x433a6
cmpl $0x4, 0x54(%rsp)
jl 0x4339c
movq 0x53c6f(%rip), %rdi     # 0x96ff0
leaq 0x3ad47(%rip), %rsi     # 0x7e0cf
callq 0x8360
movq %rax, %rdi
movq 0x53c19(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
movq 0x68(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
xorb $-0x1, %al
andb $0x1, %al
movb %al, 0x53(%rsp)
jmp 0x434f6
movq 0x58(%rsp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x43467
movq 0x68(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0xc(%rsp)
movq 0x60(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
movb %al, %cl
movl 0xc(%rsp), %eax
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x43447
cmpl $0x4, 0x54(%rsp)
jl 0x4343d
movq 0x53bce(%rip), %rdi     # 0x96ff0
leaq 0x3aca6(%rip), %rsi     # 0x7e0cf
callq 0x8360
movq %rax, %rdi
movq 0x53b78(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
movq 0x68(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
xorb $-0x1, %al
andb $0x1, %al
movb %al, 0x53(%rsp)
jmp 0x434f4
movq 0x68(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x8(%rsp)
movq 0x60(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
movb %al, %cl
movl 0x8(%rsp), %eax
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x434d9
cmpl $0x4, 0x54(%rsp)
jl 0x434cf
movq 0x53b3c(%rip), %rdi     # 0x96ff0
leaq 0x3ac14(%rip), %rsi     # 0x7e0cf
callq 0x8360
movq %rax, %rdi
movq 0x53ae6(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
movq 0x68(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
andb $0x1, %al
movb %al, 0x53(%rsp)
jmp 0x434f4
jmp 0x434f6
testb $0x1, 0x53(%rsp)
je 0x43543
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x68(%rsp), %rcx
cmpq (%rcx), %rax
jg 0x43543
cmpl $0x4, 0x54(%rsp)
jl 0x43539
movq 0x53ad2(%rip), %rdi     # 0x96ff0
leaq 0x3abc0(%rip), %rsi     # 0x7e0e5
callq 0x8360
movq %rax, %rdi
movq 0x53a7c(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
testb $0x1, 0x53(%rsp)
jne 0x43590
movq 0x68(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x60(%rsp), %rcx
cmpq (%rcx), %rax
jg 0x43590
cmpl $0x4, 0x54(%rsp)
jl 0x43586
movq 0x53a85(%rip), %rdi     # 0x96ff0
leaq 0x3ab73(%rip), %rsi     # 0x7e0e5
callq 0x8360
movq %rax, %rdi
movq 0x53a2f(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
jmp 0x43592
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x1efc0
movq (%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x68(%rsp), %rdi
addq $0x8, %rdi
movq 0x60(%rsp), %rsi
addq $0x8, %rsi
callq 0x1ec80
movq (%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
subq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq 0x58(%rsp), %rcx
movslq (%rcx), %rcx
cmpq %rcx, %rax
jl 0x43604
movq 0x38(%rsp), %rax
movq 0x58(%rsp), %rcx
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
jle 0x436b7
cmpl $0x2, 0x54(%rsp)
jl 0x4363a
movq 0x539de(%rip), %rdi     # 0x96ff0
leaq 0x3aadf(%rip), %rsi     # 0x7e0f8
callq 0x8360
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x8790
movq %rax, %rdi
movq 0x5397b(%rip), %rsi     # 0x96fb0
callq 0x83b0
cmpl $0x4, 0x54(%rsp)
jl 0x436ad
movq 0x539a8(%rip), %rdi     # 0x96ff0
leaq 0x3aab5(%rip), %rsi     # 0x7e104
callq 0x8360
movq %rax, %rdi
movq 0x38(%rsp), %rsi
callq 0x8790
movq %rax, %rdi
leaq 0x3aaa6(%rip), %rsi     # 0x7e111
callq 0x8360
movq %rax, %rdi
movq 0x58(%rsp), %rax
movl (%rax), %esi
callq 0x8710
movq %rax, %rdi
leaq 0x3a254(%rip), %rsi     # 0x7d8dd
callq 0x8360
movq %rax, %rdi
movq 0x58(%rsp), %rax
movl 0x4(%rax), %esi
callq 0x8710
movq %rax, %rdi
movq 0x53908(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
movq 0x68(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq 0x68(%rsp), %rcx
movslq 0x98(%rcx), %rcx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, 0x28(%rsp)
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq 0x60(%rsp), %rcx
movslq 0x98(%rcx), %rcx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
cmpq 0x20(%rsp), %rax
jl 0x4372b
movq 0x28(%rsp), %rcx
movb $0x1, %al
cmpq 0x18(%rsp), %rcx
movb %al, 0x7(%rsp)
jle 0x43756
movq 0x20(%rsp), %rcx
xorl %eax, %eax
cmpq 0x30(%rsp), %rcx
movb %al, 0x6(%rsp)
jl 0x4374e
movq 0x18(%rsp), %rax
cmpq 0x28(%rsp), %rax
setle %al
movb %al, 0x6(%rsp)
movb 0x6(%rsp), %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
andb $0x1, %al
movb %al, 0x17(%rsp)
testb $0x1, 0x17(%rsp)
je 0x437a5
movq 0x58(%rsp), %rax
testb $0x1, 0xb(%rax)
jne 0x437a5
cmpl $0x4, 0x54(%rsp)
jl 0x4379b
movq 0x53870(%rip), %rdi     # 0x96ff0
leaq 0x3a99f(%rip), %rsi     # 0x7e126
callq 0x8360
movq %rax, %rdi
movq 0x5381a(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
movb $0x1, %al
testb $0x1, 0x17(%rsp)
movb %al, 0x5(%rsp)
jne 0x437fb
movq 0x20(%rsp), %rax
cmpq 0x30(%rsp), %rax
jl 0x437d0
movq 0x20(%rsp), %rcx
movb $0x1, %al
cmpq 0x28(%rsp), %rcx
movb %al, 0x5(%rsp)
jle 0x437fb
movq 0x18(%rsp), %rcx
xorl %eax, %eax
cmpq 0x30(%rsp), %rcx
movb %al, 0x4(%rsp)
jl 0x437f3
movq 0x18(%rsp), %rax
cmpq 0x28(%rsp), %rax
setle %al
movb %al, 0x4(%rsp)
movb 0x4(%rsp), %al
movb %al, 0x5(%rsp)
movb 0x5(%rsp), %al
andb $0x1, %al
movb %al, 0x16(%rsp)
movq 0x58(%rsp), %rax
testb $0x1, 0xc(%rax)
jne 0x4384a
testb $0x1, 0x16(%rsp)
je 0x4384a
cmpl $0x4, 0x54(%rsp)
jl 0x43840
movq 0x537cb(%rip), %rdi     # 0x96ff0
leaq 0x3a907(%rip), %rsi     # 0x7e133
callq 0x8360
movq %rax, %rdi
movq 0x53775(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
testb $0x1, 0x53(%rsp)
je 0x4385d
movq 0x20(%rsp), %rax
cmpq 0x30(%rsp), %rax
jl 0x43874
testb $0x1, 0x53(%rsp)
jne 0x438ea
movq 0x30(%rsp), %rax
cmpq 0x20(%rsp), %rax
jge 0x438ea
testb $0x1, 0x16(%rsp)
jne 0x438ab
cmpl $0x4, 0x54(%rsp)
jl 0x438a4
movq 0x53767(%rip), %rdi     # 0x96ff0
leaq 0x3a8b2(%rip), %rsi     # 0x7e142
callq 0x8360
movq %rax, %rdi
movq 0x53711(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
movq 0x58(%rsp), %rax
testb $0x1, 0xa(%rax)
jne 0x438e6
cmpl $0x4, 0x54(%rsp)
jl 0x438df
movq 0x5372c(%rip), %rdi     # 0x96ff0
leaq 0x3a877(%rip), %rsi     # 0x7e142
callq 0x8360
movq %rax, %rdi
movq 0x536d6(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x0, 0x77(%rsp)
jmp 0x43918
jmp 0x438e8
jmp 0x438ea
cmpl $0x2, 0x54(%rsp)
jl 0x43913
movq 0x536f8(%rip), %rdi     # 0x96ff0
leaq 0x3a84f(%rip), %rsi     # 0x7e14e
callq 0x8360
movq %rax, %rdi
movq 0x536a2(%rip), %rsi     # 0x96fb0
callq 0x83b0
movb $0x1, 0x77(%rsp)
movb 0x77(%rsp), %al
andb $0x1, %al
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)