// File: /workspace/github_cpp_makefile_star2_O3/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, 0xd0(%rsp)
testq %rdx, %rdx
jle 0x1d4641
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %rax
movq %rdi, %r13
movq %r8, 0xc8(%rsp)
movq %rcx, %rdi
movq %rdx, %rsi
xorl %edx, %edx
movq %rax, 0x8(%rsp)
callq 0x3ecb0
movq 0x8(%rsp), %rsi
movq 0x28(%rbx), %rax
movq %rax, 0xa0(%rsp)
movq (%rbx), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%rbx), %rax
movq %rax, 0x98(%rsp)
movq 0x20(%rbx), %rax
movq %rax, 0x38(%rsp)
movq 0x10(%rbx), %rdi
movl $0x1, %eax
movq %rsi, %rbx
xorl %ebp, %ebp
xorl %r12d, %r12d
movq %rdi, 0x18(%rsp)
cmpl $0x0, 0x14(%rbx)
js 0x1d4031
testq %rax, %rax
setne %cl
testq %rbp, %rbp
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1d402a
movq %rbp, %r14
movslq -0x4(%rdi,%rbp,4), %rax
leaq (%rax,%rax,4), %rax
leaq (%rsi,%rax,8), %rbp
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x78(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x88(%rsp), %r8
leaq 0x80(%rsp), %r9
callq 0x1af0c9
movq %r13, %rdi
movq %rbp, %rsi
leaq 0x58(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x68(%rsp), %r8
leaq 0x60(%rsp), %r9
callq 0x1af0c9
movq 0x68(%rsp), %rsi
movq 0x88(%rsp), %rcx
cmpq %rcx, %rsi
jg 0x1d401b
movq 0x60(%rsp), %rdx
movq 0x80(%rsp), %rax
cmpq %rax, %rdx
jg 0x1d401b
movq 0x58(%rsp), %rdi
movq 0x78(%rsp), %r9
cmpq %r9, %rdi
jg 0x1d401b
movq 0x50(%rsp), %r8
movq 0x70(%rsp), %r10
cmpq %r10, %r8
jg 0x1d401b
xorq %rcx, %rsi
movq %rdx, %r11
xorq %rax, %r11
orq %rsi, %r11
xorq %r9, %rdi
orq %r11, %rdi
xorq %r10, %r8
orq %rdi, %r8
jne 0x1d4043
movq 0x8(%rsp), %rsi
movq %r14, %rbp
movq 0x18(%rsp), %rdi
xorl %eax, %eax
movl %r12d, (%rdi,%rbp,4)
incq %rbp
incq %r12
addq $0x28, %rbx
cmpq %r12, %r15
jne 0x1d3f4a
jmp 0x1d4072
subq %rcx, %rax
subq %rdx, %rcx
movl $0x0, %edx
cmovgeq %rdx, %rcx
addq %rax, %rcx
movl $0x1, %eax
cmpq $-0x80000000, %rcx      # imm = 0x80000000
movq 0x8(%rsp), %rsi
movq %r14, %rbp
movq 0x18(%rsp), %rdi
je 0x1d4028
jmp 0x1d402a
testq %rbp, %rbp
jle 0x1d4641
testq %rax, %rax
jne 0x1d4643
leaq (,%rbp,8), %rdx
movq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %r9
movq $-0x80000000, 0xb0(%rsp) # imm = 0x80000000
movq $-0x1, 0xa8(%rsp)
movq $-0x80000000, 0xb8(%rsp) # imm = 0x80000000
movq $-0x1, 0x48(%rsp)
xorl %edx, %edx
movq %rbp, 0xc0(%rsp)
movq %rdx, 0x28(%rsp)
movslq (%r10,%rdx,4), %rax
leaq (%rax,%rax,4), %rax
leaq (%r9,%rax,8), %r15
movslq 0x18(%r15), %rcx
movslq 0x14(%r15), %rax
subq %rax, %rcx
movq %rcx, 0x20(%rsp)
movq %rdx, %rax
subq 0xd0(%rsp), %rax
movl $0x0, %ebp
cmovgeq %rax, %rbp
leaq -0x1(%rdx), %r14
movl $0x1, %eax
cmpq %rbp, %rdx
jle 0x1d42e8
movq %rax, 0x10(%rsp)
movq $-0x1, 0x30(%rsp)
movq $0x0, 0x90(%rsp)
movq %r14, %r12
movslq (%r10,%r12,4), %rax
leaq (%rax,%rax,4), %rax
leaq (%r9,%rax,8), %rbx
movq %r13, %rdi
movq %r15, %rsi
leaq 0x78(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x88(%rsp), %r8
leaq 0x80(%rsp), %r9
callq 0x1af0c9
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x58(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x68(%rsp), %r8
leaq 0x60(%rsp), %r9
callq 0x1af0c9
movq 0x68(%rsp), %rsi
movq 0x88(%rsp), %rax
cmpq %rax, %rsi
jg 0x1d4206
movq 0x60(%rsp), %rdx
movq 0x80(%rsp), %rcx
cmpq %rcx, %rdx
jg 0x1d4206
movq 0x58(%rsp), %rdi
movq 0x78(%rsp), %r9
cmpq %r9, %rdi
jg 0x1d4206
movq 0x50(%rsp), %r8
movq 0x70(%rsp), %r10
cmpq %r10, %r8
jg 0x1d4206
xorq %rax, %rsi
movq %rdx, %r11
xorq %rcx, %r11
orq %rsi, %r11
xorq %r9, %rdi
orq %r11, %rdi
xorq %r10, %r8
orq %rdi, %r8
je 0x1d4206
subq %rax, %rcx
subq %rdx, %rax
movl $0x0, %edx
cmovgeq %rdx, %rax
addq %rcx, %rax
cmpq $-0x80000000, %rax      # imm = 0x80000000
jne 0x1d4226
movq 0x8(%rsp), %r9
movq 0x18(%rsp), %r10
leaq -0x1(%r12), %rax
cmpq %rbp, %r12
movq %rax, %r12
jg 0x1d4140
jmp 0x1d42f9
movq 0x98(%rsp), %rcx
addq (%rcx,%r12,8), %rax
movq 0x38(%rsp), %rcx
movslq (%rcx,%r12,4), %rcx
incq %rcx
movq 0x20(%rsp), %rdx
cmpq %rdx, %rax
movq 0x8(%rsp), %r9
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %rsi
jg 0x1d425e
jne 0x1d4280
cmpq %rcx, %rsi
jge 0x1d4280
xorl %edx, %edx
movq 0x90(%rsp), %rsi
testq %rsi, %rsi
setg %dl
subq %rdx, %rsi
movq %rsi, 0x90(%rsp)
movq %r12, 0x30(%rsp)
jmp 0x1d42ba
movq 0xa0(%rsp), %rax
movq 0x28(%rsp), %rcx
cmpq %rcx, (%rax,%r12,8)
jne 0x1d42b4
movq 0x90(%rsp), %rax
cmpq 0xc8(%rsp), %rax
jge 0x1d4639
incq %rax
movq %rax, 0x90(%rsp)
movq %rdx, %rax
movq %rsi, %rcx
movq 0x40(%rsp), %rdx
movslq (%rdx,%r12,4), %rdx
testq %rdx, %rdx
js 0x1d42d9
movq 0xa0(%rsp), %rsi
movq 0x28(%rsp), %rdi
movq %rdi, (%rsi,%rdx,8)
movq %rax, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
jmp 0x1d4210
movq %rax, 0x10(%rsp)
movq %r14, %rax
movq $-0x1, 0x30(%rsp)
movq 0x48(%rsp), %rbx
testq %rbx, %rbx
movq 0x110(%rsp), %r11
movq 0x98(%rsp), %r12
js 0x1d433e
movq 0x28(%rsp), %rcx
movslq (%r10,%rcx,4), %rcx
leaq (%rcx,%rcx,4), %rcx
movslq 0x18(%r9,%rcx,8), %rcx
movslq (%r10,%rbx,4), %rdx
leaq (%rdx,%rdx,4), %rdx
movslq 0x18(%r9,%rdx,8), %rdx
addq %r11, %rdx
cmpq %rcx, %rdx
jge 0x1d43c2
movq 0x28(%rsp), %rdx
cmpq %rbp, %rdx
jle 0x1d44d3
movslq (%r10,%rdx,4), %rcx
leaq (%rcx,%rcx,4), %rcx
movslq 0x18(%r9,%rcx,8), %rcx
movq $-0x1, %rbx
movq $-0x80000000, %rdx      # imm = 0x80000000
movq $-0x80000000, %rsi      # imm = 0x80000000
movslq (%r10,%r14,4), %rdi
leaq (%rdi,%rdi,4), %rdi
movslq 0x18(%r9,%rdi,8), %rdi
addq %r11, %rdi
cmpq %rcx, %rdi
jl 0x1d43c2
movq (%r12,%r14,8), %rdi
cmpq %rdi, %rsi
jge 0x1d439d
movq 0x38(%rsp), %rdx
movslq (%rdx,%r14,4), %rdx
movq %r14, %rbx
movq %rdi, %rsi
jmp 0x1d43b6
jne 0x1d43b6
movq 0x38(%rsp), %r8
movslq (%r8,%r14,4), %r8
cmpq %r8, %rdx
jge 0x1d43b6
movq %r14, %rbx
movq %rdi, %rsi
movq %r8, %rdx
leaq -0x1(%r14), %rdi
cmpq %rbp, %r14
movq %rdi, %r14
jg 0x1d436e
testq %rbx, %rbx
sets %cl
cmpq %rax, %rbx
setge %al
orb %cl, %al
movq 0xc0(%rsp), %rbp
jne 0x1d4505
movslq (%r10,%rbx,4), %rax
leaq (%rax,%rax,4), %rax
leaq (%r9,%rax,8), %r12
movq %r13, %rdi
movq %r15, %rsi
leaq 0x78(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x88(%rsp), %r8
leaq 0x80(%rsp), %r9
callq 0x1af0c9
movq %r13, %rdi
movq %r12, %rsi
leaq 0x58(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x68(%rsp), %r8
leaq 0x60(%rsp), %r9
callq 0x1af0c9
movq 0x68(%rsp), %rsi
movq 0x88(%rsp), %rax
cmpq %rax, %rsi
jg 0x1d448e
movq 0x60(%rsp), %rdx
movq 0x80(%rsp), %rcx
cmpq %rcx, %rdx
jg 0x1d448e
movq 0x58(%rsp), %rdi
movq 0x78(%rsp), %r9
cmpq %r9, %rdi
jg 0x1d448e
movq 0x50(%rsp), %r8
movq 0x70(%rsp), %r10
cmpq %r10, %r8
jg 0x1d448e
xorq %rax, %rsi
movq %rdx, %r11
xorq %rcx, %r11
orq %rsi, %r11
xorq %r9, %rdi
orq %r11, %rdi
xorq %r10, %r8
orq %rdi, %r8
jne 0x1d45a8
movq 0x8(%rsp), %r9
movq 0x18(%rsp), %r10
movq 0x110(%rsp), %r11
movq 0x98(%rsp), %r12
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %r8
movq %rsi, (%r12,%rdx,8)
movq 0x40(%rsp), %rax
movl %r8d, (%rax,%rdx,4)
movq 0x38(%rsp), %rax
movl %edi, (%rax,%rdx,4)
jmp 0x1d4537
movq 0x20(%rsp), %rsi
movq %rsi, (%r12,%rdx,8)
movq 0x40(%rsp), %rax
movq 0x30(%rsp), %rcx
movl %ecx, (%rax,%rdx,4)
movq 0x38(%rsp), %rax
movq 0x10(%rsp), %rdi
movl %edi, (%rax,%rdx,4)
movq %rdx, 0x48(%rsp)
movq 0xc0(%rsp), %rbp
jmp 0x1d4566
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq %rsi, (%r12,%rdx,8)
movq 0x40(%rsp), %rax
movq 0x30(%rsp), %rcx
movl %ecx, (%rax,%rdx,4)
movq 0x38(%rsp), %rax
movq 0x10(%rsp), %rdi
movl %edi, (%rax,%rdx,4)
movq %rdx, 0x48(%rsp)
testq %rbx, %rbx
js 0x1d4566
movslq (%r10,%rdx,4), %rax
leaq (%rax,%rax,4), %rax
movslq 0x18(%r9,%rax,8), %rax
movslq (%r10,%rbx,4), %rcx
leaq (%rcx,%rcx,4), %rcx
movslq 0x18(%r9,%rcx,8), %rcx
addq %r11, %rcx
cmpq %rax, %rcx
jl 0x1d4561
cmpq %rsi, (%r12,%rbx,8)
cmovlq %rdx, %rbx
movq %rbx, 0x48(%rsp)
movslq %edi, %rax
cmpq %rsi, 0xb8(%rsp)
jl 0x1d457f
jne 0x1d4597
cmpq %rax, 0xb0(%rsp)
jge 0x1d4597
movq %rsi, 0xb8(%rsp)
movq %rax, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
incq %rdx
cmpq %rbp, %rdx
jne 0x1d40dc
jmp 0x1d4658
subq %rax, %rcx
subq %rdx, %rax
movl $0x0, %edx
cmovgeq %rdx, %rax
addq %rcx, %rax
cmpq $-0x80000000, %rax      # imm = 0x80000000
movq 0x98(%rsp), %r12
jne 0x1d45e1
movq 0x8(%rsp), %r9
movq 0x18(%rsp), %r10
movq 0x110(%rsp), %r11
jmp 0x1d44a8
addq (%r12,%rbx,8), %rax
movq 0x38(%rsp), %rcx
movslq (%rcx,%rbx,4), %rcx
incq %rcx
movq 0x20(%rsp), %rsi
cmpq %rsi, %rax
movq 0x8(%rsp), %r9
movq 0x18(%rsp), %r10
movq 0x110(%rsp), %r11
movq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %r8
jg 0x1d462b
jne 0x1d44bc
cmpq %rcx, %rdi
jge 0x1d44bc
movq %rax, %rsi
movq %rcx, %rdi
movq %rbx, %r8
jmp 0x1d44bc
movq %r12, %rax
jmp 0x1d42f9
xorl %ebp, %ebp
movq %rbp, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa8(%rsp), %rdx
xorl %eax, %eax
testq %rdx, %rdx
js 0x1d4680
movq 0x40(%rsp), %rcx
movl $0xffffffff, (%r10,%rdx,4) # imm = 0xFFFFFFFF
movslq (%rcx,%rdx,4), %rdx
incq %rax
testq %rdx, %rdx
jns 0x1d466c
cmpq %rbp, %rax
jge 0x1d46ae
xorl %ecx, %ecx
movslq (%r10,%rcx,4), %rdx
testq %rdx, %rdx
js 0x1d46a6
leaq (%rdx,%rdx,4), %rdx
movq $-0x1, 0x14(%r9,%rdx,8)
movl $0xffffffff, 0x20(%r9,%rdx,8) # imm = 0xFFFFFFFF
incq %rcx
cmpq %rcx, %rbp
jne 0x1d4687
movq %rax, %rbp
jmp 0x1d4643