// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %r8, 0x40(%rsp)
movq %rcx, 0x20(%rsp)
movsd %xmm0, 0x30(%rsp)
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
movl $0x0, (%r9)
movq (%rdi), %rax
movq %rdx, %rbx
shlq $0x4, %rbx
movl 0xc(%rax,%rbx), %eax
movq %r9, 0x28(%rsp)
movl %eax, (%r9)
movq %rsi, %rdi
callq 0x2bacc
movl $0x18, %edi
callq 0x72e0
movq %r12, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x7070
movq %r15, %r10
movq %r14, 0x8(%rsp)
incq 0x10(%r14)
movq (%r15), %r13
movq 0x8(%r15), %rdx
leaq (%rbx,%r13), %r8
movslq 0x8(%r13,%rbx), %rax
movslq 0xc(%r13,%rbx), %rsi
leaq (%rsi,%rax), %rcx
decq %rcx
movq %r12, 0x10(%rsp)
leal 0x1(%r12), %edi
subq %r13, %rdx
shrq $0x4, %rdx
xorl %r11d, %r11d
cmpl %edx, %edi
movq %r15, 0x18(%rsp)
movq %r8, 0x38(%rsp)
jge 0x228cb
movq (%r8), %rdx
addq %rdx, %rsi
decq %rsi
movslq %edi, %rbx
movq %rbx, %rbp
shlq $0x4, %rbp
orq $0xc, %rbp
xorl %r11d, %r11d
movslq -0x4(%r13,%rbp), %rdi
movq %rcx, %r14
notq %r14
addq %rdi, %r14
cmpq $-0xa, %r14
jl 0x228aa
movq -0xc(%r13,%rbp), %r8
movq %rsi, %r15
notq %r15
addq %r8, %r15
cmpq $-0xa, %r15
jl 0x228aa
movslq (%r13,%rbp), %r9
subq %rdx, %r8
addq %r9, %r8
addq %r9, %rdi
subq %rax, %rdi
movq %r14, %r9
subq %r15, %r9
movq %r9, %r12
negq %r12
cmovsq %r9, %r12
cmpq %r8, %rdi
cmovleq %r8, %rdi
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
mulsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %rdi
cmpq %rdi, %r12
jg 0x228aa
movl $0x18, %edi
movl %r11d, 0x4(%rsp)
callq 0x72e0
movq %rbx, 0x10(%rax)
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x7070
movl 0x4(%rsp), %r11d
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rax
incq 0x10(%rax)
cmpq $0x1, %r12
adcq $0x0, %r12
addl %r12d, %r11d
movq -0xc(%r13,%rbp), %rdx
movslq (%r13,%rbp), %rdi
leaq 0x3f(%rbx), %rsi
testq %rbx, %rbx
cmovnsq %rbx, %rsi
sarq $0x6, %rsi
shlq $0x3, %rsi
movq 0x20(%rsp), %rax
addq (%rax), %rsi
movslq -0x4(%r13,%rbp), %rax
movabsq $-0x8000000000000000, %r9 # imm = 0x8000000000000000
leaq 0x3f(%r9), %rcx
andq %rbx, %rcx
xorl %r8d, %r8d
cmpq %r9, %rcx
setbe %r8b
movl $0x1, %r9d
movl %ebx, %ecx
shlq %cl, %r9
orq %r9, -0x8(%rsi,%r8,8)
cmpq %r14, %r15
cmovlq %r15, %r14
movq %r14, %rcx
sarq $0x3f, %rcx
andl %r14d, %ecx
movq (%r10), %rsi
addl (%rsi,%rbp), %ecx
movq 0x28(%rsp), %rsi
addl %ecx, (%rsi)
leaq (%rdx,%rdi), %rsi
decq %rsi
leaq (%rax,%rdi), %rcx
decq %rcx
incq %rbx
movq (%r10), %r13
movq 0x8(%r10), %rdi
subq %r13, %rdi
shrq $0x4, %rdi
movslq %edi, %rdi
addq $0x10, %rbp
cmpq %rdi, %rbx
jl 0x22777
incl %ecx
xorl %eax, %eax
cmpl 0x40(%rsp), %ecx
setl %al
addl %r11d, %eax
movl %eax, %r11d
movq 0x38(%rsp), %rax
movslq 0x8(%rax), %rdx
movq 0x10(%rsp), %rdi
decl %edi
js 0x22a49
movslq 0xc(%rax), %rsi
leaq (%rsi,%rdx), %rcx
decq %rcx
movq (%rax), %rax
addq %rax, %rsi
decq %rsi
movl %edi, %r13d
movq %r13, %r14
shlq $0x4, %r14
orq $0xc, %r14
movq (%r10), %r12
movslq -0x4(%r12,%r14), %rdi
movslq (%r12,%r14), %r8
movq %rdx, %r15
subq %r8, %r15
subq %rdi, %r15
cmpq $-0xa, %r15
jl 0x22a35
movq -0xc(%r12,%r14), %r9
addq %r9, %r8
movq %rax, %rbx
subq %r8, %rbx
cmpq $-0xa, %rbx
jl 0x22a35
movq %rsi, %r8
subq %r9, %r8
incq %r8
movq %rcx, %r9
subq %rdi, %r9
incq %r9
movq %r15, %rdi
subq %rbx, %rdi
movq %rdi, %rbp
negq %rbp
cmovsq %rdi, %rbp
cmpq %r8, %r9
cmovleq %r8, %r9
xorps %xmm0, %xmm0
cvtsi2sd %r9, %xmm0
mulsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %rdi
cmpq %rdi, %rbp
jg 0x22a35
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movl $0x18, %edi
movl %r11d, 0x4(%rsp)
callq 0x72e0
movq %r13, 0x10(%rax)
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x7070
movl 0x4(%rsp), %r11d
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rax
incq 0x10(%rax)
cmpq $0x1, %rbp
adcq $0x0, %rbp
movq -0xc(%r12,%r14), %rax
movslq (%r12,%r14), %rdi
movslq -0x4(%r12,%r14), %rdx
movq 0x20(%rsp), %rcx
movq (%rcx), %rsi
movq %r13, %r8
movl $0x1, %r9d
movl %r13d, %ecx
shlq %cl, %r9
shrq $0x6, %r8
orq %r9, (%rsi,%r8,8)
addl %ebp, %r11d
cmpq %r15, %rbx
cmovlq %rbx, %r15
movq %r15, %rcx
sarq $0x3f, %rcx
andl %r15d, %ecx
movq (%r10), %rsi
addl (%rsi,%r14), %ecx
movq 0x28(%rsp), %rsi
addl %ecx, (%rsi)
leaq (%rax,%rdi), %rsi
decq %rsi
leaq (%rdx,%rdi), %rcx
decq %rcx
leaq -0x1(%r13), %rdi
addq $-0x10, %r14
testq %r13, %r13
movq %rdi, %r13
jg 0x22914
xorl %eax, %eax
testq %rdx, %rdx
setg %al
addl %r11d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq