// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %r8, %r12
movq %rcx, 0x28(%rsp)
movsd %xmm0, 0x38(%rsp)
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
movl $0x0, (%r9)
movq (%rdi), %rax
movq %rdx, %r13
shlq $0x4, %r13
movl 0xc(%rax,%r13), %eax
movq %r9, 0x30(%rsp)
movl %eax, (%r9)
movq %rsi, %rdi
callq 0x2b998
movl $0x18, %edi
callq 0x72e0
movq %r15, 0x10(%rax)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x7070
movq %rbx, 0x18(%rsp)
incq 0x10(%rbx)
movq (%r14), %r11
movq %r14, 0x20(%rsp)
movq 0x8(%r14), %rdi
movq (%r11,%r13), %rax
movslq 0x8(%r11,%r13), %rdx
movslq 0xc(%r11,%r13), %rsi
leaq -0x1(%rsi,%rax), %rcx
addq %rdx, %rsi
decq %rsi
leal 0x1(%r15), %r8d
movq %rdi, %r9
subq %r11, %r9
shrq $0x4, %r9
xorl %r14d, %r14d
cmpl %r9d, %r8d
jge 0x225f1
movq %r12, 0x48(%rsp)
movq %r15, 0x50(%rsp)
addq %r11, %r13
movq %r13, 0x40(%rsp)
movslq %r8d, %r12
movq %r12, %rbp
shlq $0x4, %rbp
orq $0xc, %rbp
xorl %r14d, %r14d
movslq -0x4(%r11,%rbp), %r8
movq %rsi, %r13
notq %r13
addq %r8, %r13
cmpq $-0xa, %r13
jl 0x225ab
movq -0xc(%r11,%rbp), %r9
movq %rcx, %rbx
notq %rbx
addq %r9, %rbx
cmpq $-0xa, %rbx
jl 0x225ab
movslq (%r11,%rbp), %r10
subq %rax, %r9
addq %r10, %r9
addq %r10, %r8
subq %rdx, %r8
movq %r13, %r10
subq %rbx, %r10
movq %r10, %r15
negq %r15
cmovsq %r10, %r15
cmpq %r9, %r8
cmovleq %r9, %r8
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
mulsd 0x38(%rsp), %xmm0
cvttsd2si %xmm0, %r8
cmpq %r8, %r15
jg 0x225ab
movl $0x18, %edi
movq %r11, 0x10(%rsp)
callq 0x72e0
movq %r12, 0x10(%rax)
movq %rax, %rdi
movl %r14d, 0xc(%rsp)
movq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0x7070
incq 0x10(%r14)
movl 0xc(%rsp), %r14d
cmpq $0x1, %r15
adcq $0x0, %r15
addl %r15d, %r14d
movq 0x10(%rsp), %rdx
movq -0xc(%rdx,%rbp), %rax
leal 0x3f(%r12), %ecx
testl %r12d, %r12d
cmovnsl %r12d, %ecx
sarl $0x6, %ecx
movslq %ecx, %rsi
shlq $0x3, %rsi
movq 0x28(%rsp), %rcx
addq (%rcx), %rsi
movslq (%rdx,%rbp), %rdi
movq %rdx, %r10
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
leaq 0x3f(%rdx), %rcx
andq %r12, %rcx
xorl %r8d, %r8d
cmpq %rdx, %rcx
setbe %r8b
movl $0x1, %r9d
movl %r12d, %ecx
shlq %cl, %r9
movslq -0x4(%r10,%rbp), %rdx
orq %r9, -0x8(%rsi,%r8,8)
leaq (%rax,%rdi), %rcx
decq %rcx
leaq (%rdx,%rdi), %rsi
decq %rsi
cmpq %r13, %rbx
cmovlq %rbx, %r13
movq %r13, %rdi
sarq $0x3f, %rdi
andl %r13d, %edi
movq 0x20(%rsp), %r9
movq (%r9), %r11
addl (%r11,%rbp), %edi
movq 0x30(%rsp), %r8
addl %edi, (%r8)
movq 0x8(%r9), %rdi
incq %r12
movq %rdi, %r8
subq %r11, %r8
shrq $0x4, %r8
movslq %r8d, %r8
addq $0x10, %rbp
cmpq %r8, %r12
jl 0x22465
movq 0x40(%rsp), %rcx
movq (%rcx), %rax
movslq 0x8(%rcx), %rdx
movslq 0xc(%rcx), %rdi
leaq (%rdi,%rax), %rcx
decq %rcx
addq %rdx, %rdi
decq %rdi
movq 0x50(%rsp), %r15
movq 0x48(%rsp), %r12
jmp 0x225f4
movq %rsi, %rdi
incl %esi
xorl %r10d, %r10d
cmpl %r12d, %esi
setl %r10b
addl %r14d, %r10d
decl %r15d
js 0x2274f
movl %r15d, %r13d
movq %r13, %rbx
shlq $0x4, %rbx
orq $0xc, %rbx
movslq -0x4(%r11,%rbx), %rsi
movslq (%r11,%rbx), %r8
movq %rdx, %r15
subq %r8, %r15
subq %rsi, %r15
cmpq $-0xa, %r15
jl 0x2273b
movq -0xc(%r11,%rbx), %r9
addq %r9, %r8
movq %rax, %r12
subq %r8, %r12
cmpq $-0xa, %r12
jl 0x2273b
movq %rcx, %r8
subq %r9, %r8
incq %r8
movq %rdi, %r9
subq %rsi, %r9
incq %r9
movq %r15, %rsi
subq %r12, %rsi
movq %rsi, %rbp
negq %rbp
cmovsq %rsi, %rbp
cmpq %r8, %r9
cmovleq %r8, %r9
xorps %xmm0, %xmm0
cvtsi2sd %r9, %xmm0
mulsd 0x38(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
cmpq %rsi, %rbp
jg 0x2273b
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movl $0x18, %edi
movq %r11, %r14
movl %r10d, 0xc(%rsp)
callq 0x72e0
movq %r13, 0x10(%rax)
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x7070
movl 0xc(%rsp), %r10d
movq 0x18(%rsp), %rax
incq 0x10(%rax)
cmpq $0x1, %rbp
adcq $0x0, %rbp
movq -0xc(%r14,%rbx), %rax
movslq (%r14,%rbx), %rsi
movslq -0x4(%r14,%rbx), %rdx
movq 0x28(%rsp), %rcx
movq (%rcx), %rdi
movl %r13d, %r8d
movl $0x1, %r9d
movl %r13d, %ecx
shlq %cl, %r9
shrl $0x6, %r8d
orq %r9, (%rdi,%r8,8)
addl %ebp, %r10d
cmpq %r15, %r12
cmovlq %r12, %r15
movq %r15, %rcx
sarq $0x3f, %rcx
andl %r15d, %ecx
movq 0x20(%rsp), %rdi
movq (%rdi), %r11
addl (%r11,%rbx), %ecx
movq 0x30(%rsp), %rdi
addl %ecx, (%rdi)
leaq (%rax,%rsi), %rcx
decq %rcx
leaq (%rdx,%rsi), %rdi
decq %rdi
leaq -0x1(%r13), %rsi
addq $-0x10, %rbx
testq %r13, %r13
movq %rsi, %r13
jg 0x2261a
xorl %eax, %eax
testq %rdx, %rdx
setg %al
addl %r10d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq