// File: chhylp123[P]hifiasm/inter.cpp
// Function: flat_rovlp_chain(ma_ug_t*, mg_lchain_t*, long, Chain_Data*, long, long, long)
int64_t flat_rovlp_chain(ma_ug_t *ug, mg_lchain_t *x, int64_t x_n, Chain_Data* dp, int64_t max_skip, int64_t max_iter, int64_t max_dis)
{
	if(x_n <= 0) return 0;
	int32_t *p, *c_n, *id; int64_t *f, *t, i, j, a_n, st, max_ii, cl; mg_lchain_t *li, *lj;
	int64_t mm_sc, mm_n, mm_idx, n_skip, end_j, sc, sn, max, max_n, tot_sc = INT32_MIN, tot_n = INT32_MIN, tot_i = -1; 
	resize_Chain_Data(dp, x_n, NULL);
	t = dp->tmp; p = dp->score; f = dp->pre; c_n = dp->occ; id = dp->indels;
	for (i = a_n = 0, cl = 1; i < x_n; i++) {
		if(x[i].qs >= 0) {
			if(cl && a_n > 0) {
				li = &(x[i]); lj = &(x[id[a_n-1]]);
				sc = flat_rovlp_chain_sc(ug, li, lj);
				if(sc == INT32_MIN) cl = 0;
			}
			id[a_n++] = i;
		}
	}
	if(a_n <= 0) return 0;
	if(cl) return a_n;

	memset(t, 0, (a_n*sizeof((*t)))); 
	for (i = st = 0, max_ii = -1; i < a_n; ++i) {
		li = &(x[id[i]]);
		mm_sc = li->qe - li->qs; mm_n = 1; mm_idx = -1; n_skip = 0; end_j = -1; 
		st = (i<max_iter)?(0):(i-max_iter);
		for (j = i-1; j >= st; --j) {
			lj = &(x[id[j]]);
			sc = flat_rovlp_chain_sc(ug, li, lj);
			if(sc == INT32_MIN) continue;
			sc += f[j]; sn = c_n[j] + 1;
			if((sc > mm_sc) || ((sc == mm_sc) && (sn > mm_n))) {
				mm_sc = sc, mm_idx = j; mm_n = sn;
				if (n_skip > 0) --n_skip;
			} else if (t[j] == i) {
				if (++n_skip > max_skip)
					break;
			}
			if (p[j] >= 0) t[p[j]] = i;
		}

		end_j = j;
        if (max_ii < 0 || ((x[id[i]].qe) > (x[id[max_ii]].qe+max_dis))) {//too long
            max = INT32_MIN; max_n = INT32_MIN; max_ii = -1;
            for (j = i - 1; (j >= st) && (x[id[i]].qe<=(max_dis+x[id[j]].qe)); --j) {
                if ((max < f[j]) || ((max == f[j]) && (max_n < c_n[j]))) {
                    max = f[j]; max_n = c_n[j]; max_ii = j;
                }
            }
        }
        
        if (max_ii >= 0 && max_ii < end_j) {///just have a try with a[i]<->a[max_ii]
            lj = &(x[id[max_ii]]); 
			sc = flat_rovlp_chain_sc(ug, li, lj);
			if(sc != INT32_MIN) {
				sc += f[max_ii]; sn = c_n[max_ii] + 1;
				if((sc > mm_sc) || ((sc == mm_sc) && (sn > mm_n))) {
					mm_sc = sc; mm_idx = max_ii; mm_n = sn;
				}
			}
        }

        f[i] = mm_sc; p[i] = mm_idx; c_n[i] = mm_n; 
        if ((max_ii < 0) || ((x[id[i]].qe<=max_dis+x[id[max_ii]].qe) && (f[max_ii]<f[i]))) {
            max_ii = i;
        }

        if((tot_sc < f[i]) || ((tot_sc == f[i]) && (tot_n < c_n[i]))) {
            tot_sc = f[i]; tot_n = c_n[i]; tot_i = i;
        }
	}

	cl = 0; i = tot_i; 
    while (i >= 0) {
		id[i] = -1; i = p[i]; cl++;
        // t[cl++] = i; i = p[i];
    }

	if(cl < a_n) {
		for (i = 0; i < a_n; ++i) {
			if(id[i] < 0) continue;
			li = &(x[id[i]]);
			li->dist_pre = li->qs = li->qe = -1;
		}
	}
	return cl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
jg 0x2d9eab
movq $0x0, -0x8(%rbp)
jmp 0x2da713
movq $-0x80000000, -0xf0(%rbp) # imm = 0x80000000
movq $-0x80000000, -0xf8(%rbp) # imm = 0x80000000
movq $-0x1, -0x100(%rbp)
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x226f0
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x1, -0x90(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x2da004
movq -0x18(%rbp), %rax
imulq $0x28, -0x68(%rbp), %rcx
addq %rcx, %rax
cmpl $0x0, 0x14(%rax)
jl 0x2d9ff1
cmpq $0x0, -0x90(%rbp)
je 0x2d9fd5
cmpq $0x0, -0x78(%rbp)
jle 0x2d9fd5
movq -0x18(%rbp), %rax
imulq $0x28, -0x68(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x78(%rbp), %rdx
subq $0x1, %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rdx
callq 0x30d530
movq %rax, -0xd0(%rbp)
cmpq $-0x80000000, -0xd0(%rbp) # imm = 0x80000000
jne 0x2d9fd3
movq $0x0, -0x90(%rbp)
jmp 0x2d9fd5
movq -0x68(%rbp), %rax
movl %eax, %edx
movq -0x50(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x78(%rbp)
movl %edx, (%rax,%rcx,4)
jmp 0x2d9ff3
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x2d9f33
cmpq $0x0, -0x78(%rbp)
jg 0x2da018
movq $0x0, -0x8(%rbp)
jmp 0x2da713
cmpq $0x0, -0x90(%rbp)
je 0x2da02f
movq -0x78(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2da713
movq -0x60(%rbp), %rdi
movq -0x78(%rbp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2100
movq $0x0, -0x80(%rbp)
movq $0x0, -0x68(%rbp)
movq $-0x1, -0x88(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0x2da631
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x98(%rbp), %rcx
subl 0x14(%rcx), %eax
cltq
movq %rax, -0xa8(%rbp)
movq $0x1, -0xb0(%rbp)
movq $-0x1, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $-0x1, -0xc8(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x2da0e7
xorl %eax, %eax
movq %rax, -0x108(%rbp)
jmp 0x2da0f6
movq -0x68(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x68(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x80(%rbp), %rax
jl 0x2da278
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x70(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rdx
callq 0x30d530
movq %rax, -0xd0(%rbp)
cmpq $-0x80000000, -0xd0(%rbp) # imm = 0x80000000
jne 0x2da169
jmp 0x2da267
movq -0x58(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addq -0xd0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x48(%rbp), %rax
movq -0x70(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl $0x1, %eax
cltq
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jg 0x2da1ca
movq -0xd0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jne 0x2da20f
movq -0xd8(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jle 0x2da20f
movq -0xd0(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xc0(%rbp)
jle 0x2da20d
movq -0xc0(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x2da23f
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x68(%rbp), %rax
jne 0x2da23d
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
cmpq -0x30(%rbp), %rax
jle 0x2da23b
jmp 0x2da278
jmp 0x2da23d
jmp 0x2da23f
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0x2da265
movq -0x68(%rbp), %rdx
movq -0x60(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x70(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x2da267
movq -0x70(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x2da10d
movq -0x70(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0x88(%rbp)
jl 0x2da2d3
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movslq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
movq -0x50(%rbp), %rdx
movq -0x88(%rbp), %rsi
movslq (%rdx,%rsi,4), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movslq 0x18(%rcx), %rcx
addq 0x10(%rbp), %rcx
cmpq %rcx, %rax
jle 0x2da3f2
movq $-0x80000000, -0xe0(%rbp) # imm = 0x80000000
movq $-0x80000000, -0xe8(%rbp) # imm = 0x80000000
movq $-0x1, -0x88(%rbp)
movq -0x68(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rcx
xorl %eax, %eax
cmpq -0x80(%rbp), %rcx
movb %al, -0x109(%rbp)
jl 0x2da35b
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movslq 0x18(%rax), %rax
movq 0x10(%rbp), %rcx
movq -0x18(%rbp), %rdx
movq -0x50(%rbp), %rsi
movq -0x70(%rbp), %rdi
movslq (%rsi,%rdi,4), %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
movslq 0x18(%rdx), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
setle %al
movb %al, -0x109(%rbp)
movb -0x109(%rbp), %al
testb $0x1, %al
jne 0x2da36a
jmp 0x2da3f0
movq -0xe0(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x70(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jl 0x2da3ac
movq -0xe0(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x70(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jne 0x2da3dd
movq -0xe8(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x70(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpq %rcx, %rax
jge 0x2da3dd
movq -0x58(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xe0(%rbp)
movq -0x48(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0xe8(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x2da3df
movq -0x70(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x2da300
jmp 0x2da3f2
cmpq $0x0, -0x88(%rbp)
jl 0x2da4f9
movq -0x88(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jge 0x2da4f9
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x88(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rdx
callq 0x30d530
movq %rax, -0xd0(%rbp)
cmpq $-0x80000000, -0xd0(%rbp) # imm = 0x80000000
je 0x2da4f7
movq -0x58(%rbp), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addq -0xd0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x48(%rbp), %rax
movq -0x88(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl $0x1, %eax
cltq
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jg 0x2da4cb
movq -0xd0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jne 0x2da4f5
movq -0xd8(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jle 0x2da4f5
movq -0xd0(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x2da4f7
jmp 0x2da4f9
movq -0xa8(%rbp), %rdx
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xb8(%rbp), %rax
movl %eax, %edx
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0xb0(%rbp), %rax
movl %eax, %edx
movq -0x48(%rbp), %rax
movq -0x68(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
cmpq $0x0, -0x88(%rbp)
jl 0x2da5a0
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movslq 0x18(%rax), %rax
movq 0x10(%rbp), %rcx
movq -0x18(%rbp), %rdx
movq -0x50(%rbp), %rsi
movq -0x88(%rbp), %rdi
movslq (%rsi,%rdi,4), %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
movslq 0x18(%rdx), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jg 0x2da5ab
movq -0x58(%rbp), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x58(%rbp), %rcx
movq -0x68(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jge 0x2da5ab
movq -0x68(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0xf0(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x68(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jl 0x2da5ed
movq -0xf0(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x68(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jne 0x2da61e
movq -0xf8(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x68(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpq %rcx, %rax
jge 0x2da61e
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xf0(%rbp)
movq -0x48(%rbp), %rax
movq -0x68(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0xf8(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x100(%rbp)
jmp 0x2da620
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x2da05d
movq $0x0, -0x90(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jl 0x2da681
movq -0x50(%rbp), %rax
movq -0x68(%rbp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0x68(%rbp)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x2da647
movq -0x90(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0x2da708
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0x2da706
movq -0x50(%rbp), %rax
movq -0x68(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jge 0x2da6b0
jmp 0x2da6f8
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl $0xffffffff, 0x18(%rax) # imm = 0xFFFFFFFF
movq -0x98(%rbp), %rax
movl $0xffffffff, 0x14(%rax) # imm = 0xFFFFFFFF
movq -0x98(%rbp), %rax
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x2da696
jmp 0x2da708
movq -0x90(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq