// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x70(%rsp)
movq %r8, 0x80(%rsp)
movq %rdi, 0x40(%rsp)
testq %rdx, %rdx
jle 0x187f1f
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %rax
xorl %r14d, %r14d
movq %rcx, %rdi
movq %rdx, %rsi
xorl %edx, %edx
movq %rax, 0x8(%rsp)
callq 0x1383e
movq 0x8(%rsp), %rsi
movq 0x28(%rbx), %rax
movq %rax, 0x28(%rsp)
movq (%rbx), %rax
movq %rax, 0x38(%rsp)
movq 0x8(%rbx), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rbx), %rax
movq %rax, (%rsp)
pushq $0x1
popq %rcx
movq %rsi, %rbx
xorl %r12d, %r12d
cmpq %r14, %r15
je 0x187be0
cmpl $0x0, 0x14(%rbx)
js 0x187bd7
testq %rcx, %rcx
setne %al
testq %r12, %r12
setg %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x187bcc
movq (%rsp), %rax
movslq -0x4(%rax,%r12,4), %rax
imulq $0x28, %rax, %rdx
addq %rsi, %rdx
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a3412
movq 0x8(%rsp), %rsi
xorl %ecx, %ecx
cmpq $-0x80000000, %rax      # imm = 0x80000000
setne %cl
movq (%rsp), %rax
movl %r14d, (%rax,%r12,4)
incq %r12
incq %r14
addq $0x28, %rbx
jmp 0x187b81
testq %r12, %r12
jle 0x187f1f
testq %rcx, %rcx
jne 0x187f22
leaq (,%r12,8), %rdx
movq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x8(%rsp), %rdi
movq $-0x80000000, 0x58(%rsp) # imm = 0x80000000
pushq $-0x1
popq %rdx
xorl %esi, %esi
movq %rdx, %rbx
movq $-0x80000000, 0x50(%rsp) # imm = 0x80000000
movq %r12, 0x48(%rsp)
cmpq %r12, %rsi
je 0x187f37
movq %rbx, 0x78(%rsp)
movq %rdx, 0x60(%rsp)
movq (%rsp), %rax
movslq (%rax,%rsi,4), %rax
imulq $0x28, %rax, %rax
leaq (%rdi,%rax), %rcx
movq %rcx, 0x68(%rsp)
movslq 0x18(%rdi,%rax), %rbp
movslq 0x14(%rdi,%rax), %rax
subq %rax, %rbp
movq %rsi, %r12
subq 0x70(%rsp), %r12
movl $0x0, %eax
cmovlq %rax, %r12
movq %rsi, 0x10(%rsp)
leaq -0x1(%rsi), %r14
movq %r14, %rbx
pushq $0x1
popq %r13
pushq $-0x1
popq %rax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
cmpq %r12, %rbx
jl 0x187d51
movq (%rsp), %rax
movslq (%rax,%rbx,4), %rax
imulq $0x28, %rax, %rdx
addq %rdi, %rdx
movq 0x40(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x1a3412
cmpq $-0x80000000, %rax      # imm = 0x80000000
jne 0x187cc7
movq %rbp, %rax
movq %r13, %rcx
movq 0x8(%rsp), %rdi
jmp 0x187d43
movq 0x20(%rsp), %rcx
addq (%rcx,%rbx,8), %rax
movq 0x18(%rsp), %rcx
movslq (%rcx,%rbx,4), %rcx
incq %rcx
cmpq %rbp, %rax
jg 0x187ce8
jne 0x187cff
cmpq %rcx, %r13
jge 0x187cff
xorl %edx, %edx
testq %r15, %r15
setg %dl
subq %rdx, %r15
movq %rbx, 0x30(%rsp)
movq 0x8(%rsp), %rdi
jmp 0x187d27
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
cmpq %rcx, (%rax,%rbx,8)
movq 0x8(%rsp), %rdi
jne 0x187d21
cmpq 0x80(%rsp), %r15
jge 0x187d51
incq %r15
movq %rbp, %rax
movq %r13, %rcx
movq 0x38(%rsp), %rdx
movslq (%rdx,%rbx,4), %rdx
testq %rdx, %rdx
js 0x187d43
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %r8
movq %r8, (%rsi,%rdx,8)
decq %rbx
movq %rax, %rbp
movq %rcx, %r13
jmp 0x187c8b
movq 0x78(%rsp), %r8
testq %r8, %r8
movq 0xc0(%rsp), %r15
js 0x187d8e
movq (%rsp), %rcx
movq 0x10(%rsp), %rax
movslq (%rcx,%rax,4), %rax
imulq $0x28, %rax, %rax
movslq 0x18(%rdi,%rax), %rax
movslq (%rcx,%r8,4), %rcx
imulq $0x28, %rcx, %rcx
movslq 0x18(%rdi,%rcx), %rcx
addq %r15, %rcx
cmpq %rax, %rcx
jge 0x187e0d
movq $-0x80000000, %rax      # imm = 0x80000000
pushq $-0x1
popq %r8
movq $-0x80000000, %rcx      # imm = 0x80000000
cmpq %r12, %r14
jl 0x187e0d
movq (%rsp), %rsi
movq 0x10(%rsp), %rdx
movslq (%rsi,%rdx,4), %rdx
imulq $0x28, %rdx, %rdx
movslq 0x18(%rdi,%rdx), %rdx
movslq (%rsi,%r14,4), %rsi
imulq $0x28, %rsi, %rsi
movslq 0x18(%rdi,%rsi), %rsi
addq %r15, %rsi
cmpq %rdx, %rsi
jl 0x187e0d
movq 0x20(%rsp), %rdx
movq (%rdx,%r14,8), %rdx
cmpq %rdx, %rax
jge 0x187def
movq 0x18(%rsp), %rax
movslq (%rax,%r14,4), %rcx
movq %r14, %r8
movq %rdx, %rax
jmp 0x187e08
jne 0x187e08
movq 0x18(%rsp), %rsi
movslq (%rsi,%r14,4), %rsi
cmpq %rsi, %rcx
jge 0x187e08
movq %r14, %r8
movq %rdx, %rax
movq %rsi, %rcx
decq %r14
jmp 0x187da0
testq %r8, %r8
sets %al
cmpq %rbx, %r8
setge %cl
orb %al, %cl
movq %r8, %rbx
jne 0x187e61
movq (%rsp), %rax
movslq (%rax,%rbx,4), %rax
imulq $0x28, %rax, %rdx
addq %rdi, %rdx
movq 0x40(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x1a3412
cmpq $-0x80000000, %rax      # imm = 0x80000000
movq 0x48(%rsp), %r12
movq 0x60(%rsp), %rdx
movq 0x10(%rsp), %rsi
movq 0x30(%rsp), %r8
jne 0x187e77
movq 0x8(%rsp), %rdi
jmp 0x187ea6
movq 0x48(%rsp), %r12
movq 0x60(%rsp), %rdx
movq 0x10(%rsp), %rsi
movq 0x30(%rsp), %r8
jmp 0x187ea6
movq 0x20(%rsp), %rcx
addq (%rcx,%rbx,8), %rax
movq 0x18(%rsp), %rcx
movslq (%rcx,%rbx,4), %rcx
incq %rcx
cmpq %rbp, %rax
movq 0x8(%rsp), %rdi
jg 0x187e9d
jne 0x187ea6
cmpq %rcx, %r13
jge 0x187ea6
movq %rax, %rbp
movq %rcx, %r13
movq %rbx, %r8
movq 0x20(%rsp), %rax
movq %rbp, (%rax,%rsi,8)
movq 0x38(%rsp), %rax
movl %r8d, (%rax,%rsi,4)
movq 0x18(%rsp), %rax
movl %r13d, (%rax,%rsi,4)
testq %rbx, %rbx
js 0x187ef7
movq (%rsp), %rcx
movslq (%rcx,%rsi,4), %rax
imulq $0x28, %rax, %rax
movslq 0x18(%rdi,%rax), %rax
movslq (%rcx,%rbx,4), %rcx
imulq $0x28, %rcx, %rcx
movslq 0x18(%rdi,%rcx), %rcx
addq %r15, %rcx
cmpq %rax, %rcx
jl 0x187efa
movq 0x20(%rsp), %rax
cmpq %rbp, (%rax,%rbx,8)
jge 0x187efa
movq %rsi, %rbx
cmpq %rbp, 0x58(%rsp)
jl 0x187f0a
jne 0x187f17
cmpq %r13, 0x50(%rsp)
jge 0x187f17
movq %rbp, 0x58(%rsp)
movq %r13, 0x50(%rsp)
movq %rsi, %rdx
incq %rsi
jmp 0x187c2a
xorl %r12d, %r12d
movq %r12, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
testq %rdx, %rdx
js 0x187f54
movq (%rsp), %rcx
orl $-0x1, (%rcx,%rdx,4)
movq 0x38(%rsp), %rcx
movslq (%rcx,%rdx,4), %rdx
incq %rax
jmp 0x187f39
cmpq %r12, %rax
jge 0x187f81
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x187f81
movq (%rsp), %rdx
movslq (%rdx,%rcx,4), %rdx
testq %rdx, %rdx
js 0x187f7c
imulq $0x28, %rdx, %rdx
orq $-0x1, 0x14(%rdi,%rdx)
orl $-0x1, 0x20(%rdi,%rdx)
incq %rcx
jmp 0x187f5b
movq %rax, %r12
jmp 0x187f22