// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/mapper.cpp
// Function: nextChain(std::vector<match_t, std::allocator<match_t>> const&, std::__cxx11::list<unsigned long, std::allocator<unsigned long>>&, unsigned long, double, std::vector<bool, std::allocator<bool>>&, unsigned long, int&)
int nextChain(const vector<match_t>& matches, list<size_t>& chain, size_t anchorPos, double errorPercent, vector<bool>& used, size_t qLength, int & coverage){
    int curEditDist = 0;
    coverage = 0;
    coverage += (int)matches[anchorPos].len;
    chain.clear();
    chain.push_back(anchorPos);
    //boundaries
    const match_t & firstSeed = matches[anchorPos];
    long refstrLB = firstSeed.ref;
    long queryLB = firstSeed.query;
    long refstrRB = refstrLB + firstSeed.len -1L;
    long queryRB = queryLB + firstSeed.len-1L;
    
    //extend chain to the right
    int memsIndex = anchorPos+1;
    while (memsIndex < (int) matches.size()) {
            const match_t & match = matches[memsIndex];
            //if maxoverlap read, reference, skew
            long qDist = match.query - (queryRB + 1);
            long refDist = match.ref - (refstrRB + 1);
            long qDistLong = match.query+match.len - queryLB;
            long refDistLong = match.ref+match.len - refstrLB;
            if(qDist>=MAX_OVERLAP && refDist>=MAX_OVERLAP && abs(qDist-refDist)<=(long)(errorPercent*max(qDistLong,refDistLong))){
                chain.push_back(memsIndex);
                curEditDist += max(1L,abs(qDist-refDist));
                refstrLB = match.ref;
                refstrRB = match.ref + match.len - 1L;
                queryLB = match.query;
                queryRB = match.query + match.len - 1L;
                used[memsIndex] = true;
                coverage += min(qDist,refDist) < 0 ? (int)(matches[memsIndex].len+min(qDist,refDist)) : (int)matches[memsIndex].len;
            }
        memsIndex++;
    }
    curEditDist += (int)(queryRB + 1) < (int) qLength ? 1 : 0;
    //extend chain to the left
    memsIndex = anchorPos-1;
    refstrLB = firstSeed.ref;
    queryLB = firstSeed.query;
    refstrRB = refstrLB + firstSeed.len -1L;
    queryRB = queryLB + firstSeed.len-1L;
    while (memsIndex >= 0) {
        const match_t & match = matches[memsIndex];
        long qDist = queryLB - (match.query+match.len);
        long refDist = refstrLB - (match.ref+match.len);
        long qDistLong = queryRB - (match.query)+1;
        long refDistLong = refstrRB - (match.ref)+1;
        if(qDist>=MAX_OVERLAP && refDist>=MAX_OVERLAP && abs(qDist-refDist)<=(long)(errorPercent*max(qDistLong,refDistLong))){//errorPercent*(qLength/4)
            chain.push_front(memsIndex);
            curEditDist += max(1L,abs(qDist-refDist));
            refstrLB = match.ref;
            refstrRB = match.ref + match.len -1L;
            queryLB = match.query;
            queryRB = match.query + match.len-1L;
            used[memsIndex] = true;
            coverage += min(qDist,refDist) < 0 ? (int)(matches[memsIndex].len+min(qDist,refDist)) : (int)matches[memsIndex].len;
        }
        memsIndex--;
    }
    curEditDist += queryLB > 0 ? 1 : 0;
    return curEditDist;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, 0x10(%rsp)
movq %rcx, 0x40(%rsp)
movsd %xmm0, 0x50(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x60(%rsp), %r14
movq %rdx, (%r14)
andl $0x0, (%r9)
movq (%rdi), %rax
shlq $0x4, %rdx
movl 0xc(%rax,%rdx), %eax
movq %r9, 0x48(%rsp)
movl %eax, (%r9)
movq %rsi, %rdi
callq 0x22102
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2211a
movq (%r14), %r12
movq %r15, 0x28(%rsp)
movq (%r15), %r10
movq %r12, %rax
shlq $0x4, %rax
leaq (%r10,%rax), %r11
movq (%r10,%rax), %rcx
movslq 0x8(%r10,%rax), %rdx
movslq 0xc(%r10,%rax), %rax
movq %rcx, 0x20(%rsp)
leaq (%rax,%rcx), %rbp
decq %rbp
movq %rdx, 0x18(%rsp)
leaq (%rax,%rdx), %rcx
decq %rcx
xorl %r9d, %r9d
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
subq %r10, %rax
shrq $0x4, %rax
movq %rcx, %r8
notq %rcx
notq %rbp
incl %r12d
movslq %r12d, %r12
movq %r12, %r14
shlq $0x4, %r14
orq $0xc, %r14
movsd 0x50(%rsp), %xmm1
cmpl %eax, %r12d
jge 0x1b5a2
movslq -0x4(%r10,%r14), %rdx
leaq (%rdx,%rcx), %r15
cmpq $-0xa, %r15
jl 0x1b4fe
movq -0xc(%r10,%r14), %rsi
leaq (%rsi,%rbp), %r13
cmpq $-0xa, %r13
jl 0x1b4fe
movslq (%r10,%r14), %rdi
subq 0x20(%rsp), %rsi
addq %rdi, %rsi
addq %rdi, %rdx
subq 0x18(%rsp), %rdx
movq %r15, %rdi
subq %r13, %rdi
movq %rdi, %rbx
negq %rbx
cmovsq %rdi, %rbx
cmpq %rsi, %rdx
cmovleq %rsi, %rdx
cvtsi2sd %rdx, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdx
cmpq %rdx, %rbx
jle 0x1b507
incq %r12
addq $0x10, %r14
jmp 0x1b499
movq %r12, 0x30(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movl %r9d, 0xc(%rsp)
movq %r10, %rbp
movq %r11, 0x58(%rsp)
callq 0x22126
cmpq $0x1, %rbx
adcq $0x0, %rbx
addl %ebx, 0xc(%rsp)
movq -0xc(%rbp,%r14), %rcx
movslq (%rbp,%r14), %rax
movq %rbp, %rdx
movq %rcx, 0x20(%rsp)
leaq -0x1(%rcx,%rax), %rbp
movslq -0x4(%rdx,%r14), %rcx
movq %rcx, 0x18(%rsp)
leaq (%rcx,%rax), %rbx
decq %rbx
movq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x22132
movq 0x58(%rsp), %r11
movl 0xc(%rsp), %r9d
orq %rdx, (%rax)
cmpq %r15, %r13
cmovlq %r13, %r15
movq 0x28(%rsp), %rax
movq (%rax), %r10
movq %r15, %rax
sarq $0x3f, %rax
andl %r15d, %eax
addl (%r10,%r14), %eax
movq 0x48(%rsp), %rcx
addl %eax, (%rcx)
movq %rbx, %rcx
jmp 0x1b469
incl %r8d
xorl %eax, %eax
cmpl 0x10(%rsp), %r8d
setl %al
addl %eax, %r9d
movl 0x60(%rsp), %ebp
movq (%r11), %rsi
movslq 0x8(%r11), %rdi
movslq 0xc(%r11), %r8
leaq (%r8,%rsi), %r11
decq %r11
addq %rdi, %r8
incq %r11
decq %rbp
testl %ebp, %ebp
js 0x1b6f7
movl %ebp, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movq %r14, %r13
shlq $0x4, %r13
movslq 0x8(%r10,%r13), %rax
movslq 0xc(%r10,%r13), %rcx
movq %rdi, %r12
subq %rcx, %r12
subq %rax, %r12
cmpq $-0xa, %r12
jl 0x1b5ce
movq (%r10,%r13), %rdx
addq %rdx, %rcx
movq %rsi, %r15
subq %rcx, %r15
cmpq $-0xa, %r15
jl 0x1b5ce
movq %r11, %rcx
subq %rdx, %rcx
movq %r8, %rdx
subq %rax, %rdx
movq %r12, %rax
subq %r15, %rax
movq %rax, %rbx
negq %rbx
cmovsq %rax, %rbx
cmpq %rcx, %rdx
cmovleq %rcx, %rdx
cvtsi2sd %rdx, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
cmpq %rax, %rbx
jg 0x1b5ce
addq %r13, %r10
movq %r10, 0x10(%rsp)
movq %r14, 0x30(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movl %r9d, 0xc(%rsp)
callq 0x2216c
cmpq $0x1, %rbx
adcq $0x0, %rbx
addl %ebx, 0xc(%rsp)
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movq %rax, 0x20(%rsp)
movslq 0xc(%rcx), %rbx
leaq -0x1(%rax,%rbx), %rax
movq %rax, 0x18(%rsp)
movslq 0x8(%rcx), %rax
movq %rax, 0x10(%rsp)
addq %rax, %rbx
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x22132
movq 0x18(%rsp), %r11
movq %rbx, %r8
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %r9d
movsd 0x50(%rsp), %xmm1
orq %rdx, (%rax)
cmpq %r12, %r15
cmovlq %r15, %r12
movq 0x28(%rsp), %rax
movq (%rax), %r10
movq %r12, %rax
sarq $0x3f, %rax
andl %r12d, %eax
addl 0xc(%r10,%r13), %eax
movq 0x48(%rsp), %rcx
addl %eax, (%rcx)
jmp 0x1b5cb
xorl %eax, %eax
testq %rdi, %rdi
setg %al
addl %eax, %r9d
movl %r9d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq