// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movq %r9, 0xc8(%rsp)
movq %r8, 0xc0(%rsp)
movq %rdi, 0x90(%rsp)
testq %rdx, %rdx
jle 0x1ca725
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %rax
movq %rcx, %rdi
movq %rdx, %rsi
xorl %edx, %edx
movq %rax, (%rsp)
callq 0x3f828
movq (%rsp), %rsi
movq 0x28(%rbx), %rax
movq %rax, 0x80(%rsp)
movq (%rbx), %rax
movq %rax, 0x88(%rsp)
movq 0x8(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rbx), %r11
movl $0x1, %eax
movq %rsi, %rbx
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %r11, 0x8(%rsp)
movq 0x90(%rsp), %r14
cmpl $0x0, 0x14(%rbx)
js 0x1ca138
testq %rax, %rax
setne %cl
testq %r13, %r13
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1ca131
movslq -0x4(%r11,%r13,4), %rax
leaq (%rax,%rax,4), %rax
leaq (%rsi,%rax,8), %rbp
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x58(%rsp), %r9
callq 0x1a5e4b
movq %r14, %rdi
movq %rbp, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x38(%rsp), %r9
callq 0x1a5e4b
movq 0x40(%rsp), %rax
movq 0x60(%rsp), %rcx
cmpq %rcx, %rax
jle 0x1ca0f9
xorl %eax, %eax
movq (%rsp), %rsi
movq 0x8(%rsp), %r11
jmp 0x1ca131
movq 0x38(%rsp), %rsi
movq 0x58(%rsp), %rdx
cmpq %rdx, %rsi
movq 0x8(%rsp), %r11
jg 0x1ca12b
movq 0x30(%rsp), %rdi
movq 0x50(%rsp), %r9
cmpq %r9, %rdi
jg 0x1ca12b
movq 0x28(%rsp), %r8
movq 0x48(%rsp), %r10
cmpq %r10, %r8
jle 0x1ca14a
xorl %eax, %eax
movq (%rsp), %rsi
movl %r12d, (%r11,%r13,4)
incq %r13
incq %r12
addq $0x28, %rbx
cmpq %r12, %r15
jne 0x1ca071
jmp 0x1ca1a8
movq %r13, %rbp
movq %rax, %r11
xorq %rcx, %r11
movq %rsi, %r13
xorq %rdx, %r13
orq %r11, %r13
xorq %r9, %rdi
orq %r13, %rdi
xorq %r10, %r8
orq %rdi, %r8
jne 0x1ca16e
xorl %eax, %eax
jmp 0x1ca19a
cmpq %rdx, %rsi
cmovgeq %rdx, %rsi
cmpq %rcx, %rax
cmovleq %rcx, %rax
subq %rsi, %rax
movl $0x0, %esi
cmovgeq %rsi, %rax
subq %rcx, %rdx
addq %rax, %rdx
xorl %eax, %eax
cmpq $-0x80000000, %rdx      # imm = 0x80000000
setne %al
movq (%rsp), %rsi
movq 0x8(%rsp), %r11
movq %rbp, %r13
jmp 0x1ca131
testq %r13, %r13
jle 0x1ca725
testq %rax, %rax
jne 0x1ca728
leaq (,%r13,8), %rdx
movq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x8(%rsp), %r11
movq (%rsp), %r9
movq $-0x80000000, 0xa0(%rsp) # imm = 0x80000000
movq $-0x1, 0x98(%rsp)
movq $-0x80000000, 0xa8(%rsp) # imm = 0x80000000
movq $-0x1, %r14
xorl %r15d, %r15d
movq %r13, 0xb8(%rsp)
movq %r14, 0xd0(%rsp)
movslq (%r11,%r15,4), %rax
leaq (%rax,%rax,4), %rax
leaq (%r9,%rax,8), %rax
movslq 0x18(%rax), %r14
movq %rax, 0xb0(%rsp)
movslq 0x14(%rax), %rax
subq %rax, %r14
movq %r15, %rax
subq 0xc8(%rsp), %rax
movl $0x0, %ecx
cmovgeq %rax, %rcx
leaq -0x1(%r15), %rbp
movl $0x1, %r13d
movq %r15, 0x70(%rsp)
movq %rcx, 0x20(%rsp)
cmpq %rcx, %r15
jle 0x1ca448
movl $0x1, %r12d
movq $-0x1, 0x68(%rsp)
movq $0x0, 0x78(%rsp)
movq %rbp, %r15
movslq (%r11,%r15,4), %rax
leaq (%rax,%rax,4), %rax
leaq (%r9,%rax,8), %rbx
movq 0x90(%rsp), %r13
movq %r13, %rdi
movq 0xb0(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x58(%rsp), %r9
callq 0x1a5e4b
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x38(%rsp), %r9
callq 0x1a5e4b
movq 0x40(%rsp), %rax
movq 0x60(%rsp), %rcx
movq $-0x80000000, %rbx      # imm = 0x80000000
cmpq %rcx, %rax
jle 0x1ca2fb
movq (%rsp), %r9
movq 0x8(%rsp), %r11
jmp 0x1ca335
movq 0x38(%rsp), %rsi
movq 0x58(%rsp), %rdx
cmpq %rdx, %rsi
movq 0x8(%rsp), %r11
jg 0x1ca331
movq 0x30(%rsp), %rdi
movq 0x50(%rsp), %r9
cmpq %r9, %rdi
jg 0x1ca331
movq 0x28(%rsp), %r8
movq 0x48(%rsp), %r10
cmpq %r10, %r8
jle 0x1ca3f6
movq (%rsp), %r9
cmpq $-0x80000000, %rbx      # imm = 0x80000000
jne 0x1ca349
movq %r14, %rbx
movq %r12, %r13
jmp 0x1ca3dc
movq 0x18(%rsp), %rax
addq (%rax,%r15,8), %rbx
movq 0x10(%rsp), %rax
movslq (%rax,%r15,4), %r13
incq %r13
cmpq %r14, %rbx
jg 0x1ca36a
jne 0x1ca386
cmpq %r13, %r12
jge 0x1ca386
xorl %eax, %eax
movq 0x78(%rsp), %rcx
testq %rcx, %rcx
setg %al
subq %rax, %rcx
movq %rcx, 0x78(%rsp)
movq %r15, 0x68(%rsp)
jmp 0x1ca3ba
movq 0x80(%rsp), %rax
movq 0x70(%rsp), %rcx
cmpq %rcx, (%rax,%r15,8)
jne 0x1ca3b4
movq 0x78(%rsp), %rax
cmpq 0xc0(%rsp), %rax
jge 0x1ca717
incq %rax
movq %rax, 0x78(%rsp)
movq %r14, %rbx
movq %r12, %r13
movq 0x88(%rsp), %rax
movslq (%rax,%r15,4), %rax
testq %rax, %rax
js 0x1ca3dc
movq 0x80(%rsp), %rcx
movq 0x70(%rsp), %rdx
movq %rdx, (%rcx,%rax,8)
leaq -0x1(%r15), %rax
movq %r13, %r12
movq %rbx, %r14
cmpq 0x20(%rsp), %r15
movq %rax, %r15
jg 0x1ca283
jmp 0x1ca457
movq %rax, %r11
xorq %rcx, %r11
movq %rsi, %r13
xorq %rdx, %r13
orq %r11, %r13
movq 0x8(%rsp), %r11
xorq %r9, %rdi
orq %r13, %rdi
xorq %r10, %r8
orq %rdi, %r8
movq (%rsp), %r9
je 0x1ca335
cmpq %rdx, %rsi
cmovgeq %rdx, %rsi
cmpq %rcx, %rax
cmovleq %rcx, %rax
subq %rsi, %rax
movl $0x0, %esi
cmovgeq %rsi, %rax
subq %rcx, %rdx
addq %rax, %rdx
movq %rdx, %rbx
jmp 0x1ca335
movq %rbp, %rax
movq %r14, %rbx
movq $-0x1, 0x68(%rsp)
movq 0xd0(%rsp), %r14
testq %r14, %r14
movq 0x110(%rsp), %r12
movq 0x70(%rsp), %r15
js 0x1ca493
movslq (%r11,%r15,4), %rcx
leaq (%rcx,%rcx,4), %rcx
movslq 0x18(%r9,%rcx,8), %rcx
movslq (%r11,%r14,4), %rdx
leaq (%rdx,%rdx,4), %rdx
movslq 0x18(%r9,%rdx,8), %rdx
addq %r12, %rdx
cmpq %rcx, %rdx
jge 0x1ca50e
movq $-0x1, %r14
cmpq 0x20(%rsp), %r15
jle 0x1ca50e
movslq (%r11,%r15,4), %rcx
leaq (%rcx,%rcx,4), %rcx
movslq 0x18(%r9,%rcx,8), %rcx
movq $-0x80000000, %rdx      # imm = 0x80000000
movq $-0x80000000, %rsi      # imm = 0x80000000
movslq (%r11,%rbp,4), %rdi
leaq (%rdi,%rdi,4), %rdi
movslq 0x18(%r9,%rdi,8), %rdi
addq %r12, %rdi
cmpq %rcx, %rdi
jl 0x1ca50e
movq 0x18(%rsp), %rdi
movq (%rdi,%rbp,8), %rdi
cmpq %rdi, %rsi
jge 0x1ca4f0
movq 0x10(%rsp), %rdx
movslq (%rdx,%rbp,4), %rdx
movq %rbp, %r14
movq %rdi, %rsi
jmp 0x1ca500
jne 0x1ca500
movq 0x10(%rsp), %r8
movslq (%r8,%rbp,4), %r8
cmpq %r8, %rdx
jl 0x1ca4df
leaq -0x1(%rbp), %rdi
cmpq 0x20(%rsp), %rbp
movq %rdi, %rbp
jg 0x1ca4bc
testq %r14, %r14
sets %cl
cmpq %rax, %r14
setge %al
orb %cl, %al
jne 0x1ca5aa
movq %r14, %rbp
movslq (%r11,%r14,4), %rax
leaq (%rax,%rax,4), %rax
leaq (%r9,%rax,8), %rax
movq %rax, 0x20(%rsp)
movq 0x90(%rsp), %r14
movq %r14, %rdi
movq 0xb0(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x58(%rsp), %r9
callq 0x1a5e4b
movq %r14, %rdi
movq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x38(%rsp), %r9
callq 0x1a5e4b
movq 0x40(%rsp), %rcx
movq 0x60(%rsp), %rdx
movq $-0x80000000, %rax      # imm = 0x80000000
cmpq %rdx, %rcx
jle 0x1ca5b4
movq (%rsp), %r9
movq 0x8(%rsp), %r11
movq %rbp, %r14
jmp 0x1ca649
movq 0x68(%rsp), %rdx
jmp 0x1ca680
movq 0x38(%rsp), %rdi
movq 0x58(%rsp), %rsi
cmpq %rsi, %rdi
movq (%rsp), %r9
movq 0x8(%rsp), %r11
movq %rbp, %r14
jg 0x1ca649
movq 0x30(%rsp), %r8
movq 0x50(%rsp), %r10
cmpq %r10, %r8
jg 0x1ca649
movq 0x28(%rsp), %r9
movq 0x48(%rsp), %r11
cmpq %r11, %r9
jle 0x1ca5f8
movq (%rsp), %r9
movq 0x8(%rsp), %r11
jmp 0x1ca649
movq %rcx, %r14
xorq %rdx, %r14
movq %rdi, %r15
xorq %rsi, %r15
orq %r14, %r15
movq %rbp, %r14
xorq %r10, %r8
orq %r15, %r8
movq 0x70(%rsp), %r15
xorq %r11, %r9
orq %r8, %r9
movq (%rsp), %r9
movq 0x8(%rsp), %r11
je 0x1ca649
cmpq %rsi, %rdi
cmovgeq %rsi, %rdi
cmpq %rdx, %rcx
cmovleq %rdx, %rcx
subq %rdi, %rcx
movl $0x0, %eax
cmovgeq %rax, %rcx
subq %rdx, %rsi
addq %rcx, %rsi
movq %rsi, %rax
cmpq $-0x80000000, %rax      # imm = 0x80000000
movq 0x68(%rsp), %rdx
je 0x1ca680
movq 0x18(%rsp), %rcx
addq (%rcx,%r14,8), %rax
movq 0x10(%rsp), %rcx
movslq (%rcx,%r14,4), %rcx
incq %rcx
cmpq %rbx, %rax
jg 0x1ca677
jne 0x1ca680
cmpq %rcx, %r13
jge 0x1ca680
movq %rax, %rbx
movq %rcx, %r13
movq %r14, %rdx
movq 0x18(%rsp), %rax
movq %rbx, (%rax,%r15,8)
movq 0x88(%rsp), %rax
movl %edx, (%rax,%r15,4)
movq 0x10(%rsp), %rax
movl %r13d, (%rax,%r15,4)
testq %r14, %r14
js 0x1ca6d0
movslq (%r11,%r15,4), %rax
leaq (%rax,%rax,4), %rax
movslq 0x18(%r9,%rax,8), %rax
movslq (%r11,%r14,4), %rcx
leaq (%rcx,%rcx,4), %rcx
movslq 0x18(%r9,%rcx,8), %rcx
addq %r12, %rcx
cmpq %rax, %rcx
jl 0x1ca6d3
movq 0x18(%rsp), %rax
cmpq %rbx, (%rax,%r14,8)
jge 0x1ca6d3
movq %r15, %r14
cmpq %rbx, 0xa8(%rsp)
jl 0x1ca6e9
jne 0x1ca701
cmpq %r13, 0xa0(%rsp)
jge 0x1ca701
movq %rbx, 0xa8(%rsp)
movq %r13, 0xa0(%rsp)
movq %r15, 0x98(%rsp)
incq %r15
movq 0xb8(%rsp), %r13
cmpq %r13, %r15
jne 0x1ca210
jmp 0x1ca73d
movq %r15, %rax
movq %r14, %rbx
movq %r12, %r13
jmp 0x1ca457
xorl %r13d, %r13d
movq %r13, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x98(%rsp), %rdx
xorl %eax, %eax
testq %rdx, %rdx
js 0x1ca768
movq 0x88(%rsp), %rcx
movl $0xffffffff, (%r11,%rdx,4) # imm = 0xFFFFFFFF
movslq (%rcx,%rdx,4), %rdx
incq %rax
testq %rdx, %rdx
jns 0x1ca754
cmpq %r13, %rax
jge 0x1ca7a3
cmpq $0x2, %r13
movl $0x1, %ecx
cmovgeq %r13, %rcx
xorl %edx, %edx
movslq (%r11,%rdx,4), %rsi
testq %rsi, %rsi
js 0x1ca79b
leaq (%rsi,%rsi,4), %rsi
movq $-0x1, 0x14(%r9,%rsi,8)
movl $0xffffffff, 0x20(%r9,%rsi,8) # imm = 0xFFFFFFFF
incq %rdx
cmpq %rdx, %rcx
jne 0x1ca77c
movq %rax, %r13
jmp 0x1ca728