// File: 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:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movq %rdx, 0x140(%rsp)
movsd %xmm0, 0x138(%rsp)
movq %rcx, 0x130(%rsp)
movq %r8, 0x128(%rsp)
movq %r9, 0x120(%rsp)
movl $0x0, 0x11c(%rsp)
movq 0x120(%rsp), %rax
movl $0x0, (%rax)
movq 0x150(%rsp), %rdi
movq 0x140(%rsp), %rsi
callq 0x4c8f0
movl 0xc(%rax), %ecx
movq 0x120(%rsp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq 0x148(%rsp), %rdi
callq 0x4c910
movq 0x148(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x4c940
movq 0x150(%rsp), %rdi
movq 0x140(%rsp), %rsi
callq 0x4c8f0
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rax
movslq 0x8(%rax), %rax
movq %rax, 0x100(%rsp)
movq 0x108(%rsp), %rax
movq 0x110(%rsp), %rcx
movslq 0xc(%rcx), %rcx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rax
movq 0x110(%rsp), %rcx
movslq 0xc(%rcx), %rcx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, 0xf0(%rsp)
movq 0x140(%rsp), %rax
addq $0x1, %rax
movl %eax, 0xec(%rsp)
movl 0xec(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x150(%rsp), %rdi
callq 0x23440
movq %rax, %rcx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x3f1e9
movq 0x150(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0x4c8f0
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
movslq 0x8(%rax), %rax
movq 0xf0(%rsp), %rcx
addq $0x1, %rcx
subq %rcx, %rax
movq %rax, 0xd8(%rsp)
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movq 0xf8(%rsp), %rcx
addq $0x1, %rcx
subq %rcx, %rax
movq %rax, 0xd0(%rsp)
movq 0xe0(%rsp), %rax
movl 0x8(%rax), %eax
movq 0xe0(%rsp), %rcx
addl 0xc(%rcx), %eax
cltq
subq 0x100(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movq 0xe0(%rsp), %rcx
movslq 0xc(%rcx), %rcx
addq %rcx, %rax
subq 0x108(%rsp), %rax
movq %rax, 0xc0(%rsp)
cmpq $-0xa, 0xd8(%rsp)
jl 0x3f1d3
cmpq $-0xa, 0xd0(%rsp)
jl 0x3f1d3
movq 0xd8(%rsp), %rdi
subq 0xd0(%rsp), %rdi
callq 0x4c980
movq %rax, 0x38(%rsp)
movsd 0x138(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1ec80
movsd 0x30(%rsp), %xmm0
movq %rax, %rcx
movq 0x38(%rsp), %rax
cvtsi2sdq (%rcx), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
cmpq %rcx, %rax
jg 0x3f1d3
movq 0x148(%rsp), %rdi
movslq 0xec(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0xb8(%rsp), %rsi
callq 0x4c9a0
movq $0x1, 0xb0(%rsp)
movq 0xd8(%rsp), %rdi
subq 0xd0(%rsp), %rdi
callq 0x4c980
movq %rax, 0xa8(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x1ec80
movq (%rax), %rcx
movslq 0x11c(%rsp), %rax
addq %rcx, %rax
movl %eax, 0x11c(%rsp)
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x108(%rsp)
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movq 0xe0(%rsp), %rcx
movslq 0xc(%rcx), %rcx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, 0xf8(%rsp)
movq 0xe0(%rsp), %rax
movslq 0x8(%rax), %rax
movq %rax, 0x100(%rsp)
movq 0xe0(%rsp), %rax
movl 0x8(%rax), %eax
movq 0xe0(%rsp), %rcx
addl 0xc(%rcx), %eax
cltq
subq $0x1, %rax
movq %rax, 0xf0(%rsp)
movq 0x130(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0x4c9e0
movq %rax, 0x98(%rsp)
movq %rdx, 0xa0(%rsp)
leaq 0x98(%rsp), %rdi
movl $0x1, %esi
callq 0x4ca40
leaq 0xd8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x1efc0
cmpq $0x0, (%rax)
jge 0x3f1a7
movq 0x150(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0x4c8f0
movslq 0xc(%rax), %rax
movq %rax, 0x20(%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x1efc0
movq %rax, %rcx
movq 0x20(%rsp), %rax
addq (%rcx), %rax
movl %eax, 0x2c(%rsp)
jmp 0x3f1c3
movq 0x150(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0x4c8f0
movl 0xc(%rax), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %ecx
movq 0x120(%rsp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x3eecc
movq 0xf0(%rsp), %rax
addq $0x1, %rax
movl %eax, %edx
movq 0x128(%rsp), %rax
movl %eax, %esi
xorl %eax, %eax
movl $0x1, %ecx
cmpl %esi, %edx
cmovll %ecx, %eax
addl 0x11c(%rsp), %eax
movl %eax, 0x11c(%rsp)
movq 0x140(%rsp), %rax
subq $0x1, %rax
movl %eax, 0xec(%rsp)
movq 0x110(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rax
movslq 0x8(%rax), %rax
movq %rax, 0x100(%rsp)
movq 0x108(%rsp), %rax
movq 0x110(%rsp), %rcx
movslq 0xc(%rcx), %rcx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rax
movq 0x110(%rsp), %rcx
movslq 0xc(%rcx), %rcx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, 0xf0(%rsp)
cmpl $0x0, 0xec(%rsp)
jl 0x3f57a
movq 0x150(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0x4c8f0
movq %rax, 0x90(%rsp)
movq 0x100(%rsp), %rax
movq 0x90(%rsp), %rcx
movl 0x8(%rcx), %ecx
movq 0x90(%rsp), %rdx
addl 0xc(%rdx), %ecx
movslq %ecx, %rcx
subq %rcx, %rax
movq %rax, 0x88(%rsp)
movq 0x108(%rsp), %rax
movq 0x90(%rsp), %rcx
movq (%rcx), %rcx
movq 0x90(%rsp), %rdx
movslq 0xc(%rdx), %rdx
addq %rdx, %rcx
subq %rcx, %rax
movq %rax, 0x80(%rsp)
movq 0xf0(%rsp), %rax
movq 0x90(%rsp), %rcx
movslq 0x8(%rcx), %rcx
subq %rcx, %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
movq 0xf8(%rsp), %rax
movq 0x90(%rsp), %rcx
subq (%rcx), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
cmpq $-0xa, 0x88(%rsp)
jl 0x3f564
cmpq $-0xa, 0x80(%rsp)
jl 0x3f564
movq 0x88(%rsp), %rdi
subq 0x80(%rsp), %rdi
callq 0x4c980
movq %rax, 0x18(%rsp)
movsd 0x138(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x1ec80
movsd 0x10(%rsp), %xmm0
movq %rax, %rcx
movq 0x18(%rsp), %rax
cvtsi2sdq (%rcx), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
cmpq %rcx, %rax
jg 0x3f564
movq 0x148(%rsp), %rdi
movslq 0xec(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x68(%rsp), %rsi
callq 0x4ca90
movq $0x1, 0x60(%rsp)
movq 0x88(%rsp), %rdi
subq 0x80(%rsp), %rdi
callq 0x4c980
movq %rax, 0x58(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x1ec80
movq (%rax), %rcx
movslq 0x11c(%rsp), %rax
addq %rcx, %rax
movl %eax, 0x11c(%rsp)
movq 0x90(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x108(%rsp)
movq 0x90(%rsp), %rax
movq (%rax), %rax
movq 0x90(%rsp), %rcx
movslq 0xc(%rcx), %rcx
addq %rcx, %rax
subq $0x1, %rax
movq %rax, 0xf8(%rsp)
movq 0x90(%rsp), %rax
movslq 0x8(%rax), %rax
movq %rax, 0x100(%rsp)
movq 0x90(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x90(%rsp), %rcx
addl 0xc(%rcx), %eax
cltq
subq $0x1, %rax
movq %rax, 0xf0(%rsp)
movq 0x130(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0x4c9e0
movq %rax, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
leaq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0x4ca40
leaq 0x88(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x1efc0
cmpq $0x0, (%rax)
jge 0x3f538
movq 0x150(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0x4c8f0
movslq 0xc(%rax), %rax
movq %rax, (%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x1efc0
movq %rax, %rcx
movq (%rsp), %rax
addq (%rcx), %rax
movl %eax, 0xc(%rsp)
jmp 0x3f554
movq 0x150(%rsp), %rdi
movslq 0xec(%rsp), %rsi
callq 0x4c8f0
movl 0xc(%rax), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %ecx
movq 0x120(%rsp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl 0xec(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x3f29b
movq 0x100(%rsp), %rdx
xorl %eax, %eax
movl $0x1, %ecx
cmpq $0x0, %rdx
cmovgl %ecx, %eax
addl 0x11c(%rsp), %eax
movl %eax, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
addq $0x158, %rsp            # imm = 0x158
retq
nopl (%rax)