// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: filter_sec(overlap_region_alloc*, ul_ov_t*, long, ul_ov_t*, unsigned long*, unsigned long, unsigned long, unsigned long)
int64_t filter_sec(overlap_region_alloc *ol, ul_ov_t *idx, int64_t idx_n, ul_ov_t *a, uint64_t *w_idx, uint64_t nw, uint64_t wl, uint64_t ql)
{
	if(idx_n <= 0) return 1;
	int64_t on = ol->length, k, z, on_contain = 0, max_i = -1, max_k = -1; overlap_region t;
	memset(w_idx, -1, nw*sizeof((*w_idx)));
	for (k = 0; k < on; k++) ol->list[k].is_match = 0;
	for (k = 0; k < idx_n; k++) {///potiential best chains 
		// fprintf(stderr, "[M::%s::pri_chain[%ld]] q_coord::[%u, %u), occ::%u\n", 
		// 								__func__, k, idx[k].qs, idx[k].qe, idx[k].te-idx[k].ts);
		for (z = idx[k].ts; z < idx[k].te; z++) {
			ol->list[a[z].qn].is_match = 2;
			set_w_e(&(ol->list[a[z].qn]), w_idx, wl, ql);
			// fprintf(stderr, "[M::%s::utg%.6dl]\n", __func__, (int32_t)a[z].tn+1);
		}
		on_contain += (((idx[k].te-idx[k].ts)==1)?1:0); 
	}
	if(on_contain == idx_n) {///each primary chain only has one alignment 
		on_contain = 0;
	} else {
		on_contain = -on-1;///in this case, on_contain == z is always wrong
	}
	max_i = a[idx[idx_n-1].ts].qn;
	for (k = z = 0; k < on; k++) {
		if(!ol->list[k].is_match) ol->list[k].is_match = ck_w_err(&(ol->list[k]), w_idx, wl, ql);
		if(!ol->list[k].is_match) continue;
		if(z != k) {
			t = ol->list[k];
			ol->list[k] = ol->list[z];
			ol->list[z] = t;
		}
		if(ol->list[z].x_pos_s == 0 && ol->list[z].x_pos_e == ql - 1) {
			on_contain++;
			if(max_i == k) max_k = z;
		}
		// if(ol->list[z].is_match == 1) alt_occ++;
		// else ol->list[z].is_match = 1;
		ol->list[z].is_match = 1;
		z++;
	}
	ol->length = z;
	// fprintf(stderr, "+[M::%s] oln::%ld\n", __func__, ol->length);
	if(on_contain == z) {///do not contribute to phase
		k = max_k; z = 0;
		if(z != k) {
			t = ol->list[k];
			ol->list[k] = ol->list[z];
			ol->list[z] = t;
		}
		ol->length = 1;
	}
	// fprintf(stderr, "-[M::%s] oln::%ld\n", __func__, ol->length);
	// if(alt_occ == 0 || ol->length == 1) return 1;//if all alignments are primary or there is only one alignment
	// for (k = ol->length; k < on; k++) ol->list[k].is_match = 2;//recover trans alignments
	// ol->length = on;
	if(ol->length == 1) return 1;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rcx, (%rsp)
testq %rdx, %rdx
jle 0x1b18f6
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
movq 0x10(%rdi), %r14
shlq $0x3, %r9
movq %r8, 0x38(%rsp)
movq %r8, %rdi
movl $0xff, %esi
movq %r9, %rdx
callq 0x20e0
testq %r14, %r14
jle 0x1b16b1
movq (%rbx), %rax
addq $0x2c, %rax
movq %r14, %rcx
movb $0x0, (%rax)
addq $0xa8, %rax
decq %rcx
jne 0x1b16a3
movq %r14, 0x30(%rsp)
movq %rbx, 0x8(%rsp)
movq 0x128(%rsp), %rcx
movq 0x120(%rsp), %rdx
cmpq $0x2, %r15
movl $0x1, %edi
movq %r15, 0x18(%rsp)
cmovgeq %r15, %rdi
xorl %r9d, %r9d
xorl %r15d, %r15d
movq %r13, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x8(%rsp), %r14
leaq (%r15,%r15,8), %rax
leaq (%rax,%rax,2), %rax
addq %r15, %rax
leaq (%rax,%r13), %rbp
movl 0x10(%r13,%rax), %r12d
movl 0x14(%r13,%rax), %eax
cmpl %eax, %r12d
jae 0x1b1777
movq %r9, 0x28(%rsp)
leaq (%r12,%r12,8), %rax
leaq (%rax,%rax,2), %rbx
movq (%rsp), %rax
addq %r12, %rax
addq %rax, %rbx
movq 0x38(%rsp), %r13
movq (%r14), %rax
movl (%rbx), %esi
imulq $0xa8, %rsi, %rsi
leaq (%rax,%rsi), %rdi
movb $0x2, 0x2c(%rax,%rsi)
movq %r13, %rsi
callq 0x1b1514
movq 0x120(%rsp), %rdx
movq 0x128(%rsp), %rcx
incq %r12
movl 0x14(%rbp), %eax
addq $0x1c, %rbx
cmpq %rax, %r12
jb 0x1b172c
movq 0x20(%rsp), %r13
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %r9
subl 0x10(%rbp), %eax
xorl %esi, %esi
cmpl $0x1, %eax
sete %sil
addq %rsi, %r9
incq %r15
cmpq %rdi, %r15
jne 0x1b16f2
xorl %r14d, %r14d
movq 0x18(%rsp), %rsi
cmpq %rsi, %r9
movq 0x30(%rsp), %r12
movq %r12, %rax
notq %rax
cmoveq %r14, %rax
movq %rax, 0x10(%rsp)
testq %r12, %r12
jle 0x1b18fd
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rsi, %rax
movl -0xc(%r13,%rax), %eax
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
movq (%rsp), %rax
movl (%rax,%rsi), %eax
movq %rax, 0x18(%rsp)
leaq -0x1(%rcx), %rax
movq %rax, 0x20(%rsp)
movq $-0x1, (%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x8(%rsp), %rbx
movq (%rbx), %rdi
cmpb $0x0, 0x2c(%rdi,%r13)
jne 0x1b1830
addq %r13, %rdi
movq 0x38(%rsp), %rsi
callq 0x1b1582
movq 0x120(%rsp), %rdx
movq 0x128(%rsp), %rcx
movq (%rbx), %rsi
movb %al, 0x2c(%rsi,%r13)
movq (%rbx), %rbp
cmpb $0x0, 0x2c(%rbp,%r13)
je 0x1b18e1
cmpq %r14, %r15
je 0x1b18a4
movq %rbp, %rsi
addq %r13, %rsi
movq %rsi, 0x28(%rsp)
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
callq 0x2160
imulq $0xa8, %r14, %r12
addq %r12, %rbp
movl $0xa8, %edx
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
addq (%rbx), %r12
movl $0xa8, %edx
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
movq 0x30(%rsp), %r12
callq 0x2160
movq 0x120(%rsp), %rdx
movq 0x128(%rsp), %rcx
movq (%rbx), %rax
imulq $0xa8, %r14, %rsi
cmpl $0x0, 0x4(%rax,%rsi)
jne 0x1b18d9
leaq (%rax,%rsi), %rdi
movl 0x8(%rdi), %edi
cmpq %rdi, 0x20(%rsp)
jne 0x1b18d9
incq 0x10(%rsp)
cmpq %r15, 0x18(%rsp)
movq (%rsp), %rdi
cmoveq %r14, %rdi
movq %rdi, (%rsp)
movb $0x1, 0x2c(%rax,%rsi)
incq %r14
incq %r15
addq $0xa8, %r13
cmpq %r15, %r12
jne 0x1b1800
jmp 0x1b190a
movl $0x1, %eax
jmp 0x1b197c
movq $-0x1, (%rsp)
movq 0x8(%rsp), %rbx
movq %r14, 0x10(%rbx)
cmpq %r14, 0x10(%rsp)
jne 0x1b1972
movq (%rsp), %rax
testq %rax, %rax
je 0x1b196a
movq (%rbx), %r14
imulq $0xa8, %rax, %r15
addq %r14, %r15
leaq 0x40(%rsp), %r12
movl $0xa8, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2160
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movl $0xa8, %edx
movq %r12, %rsi
movq 0x8(%rsp), %rbx
callq 0x2160
movq $0x1, 0x10(%rbx)
xorl %eax, %eax
cmpq $0x1, 0x10(%rbx)
sete %al
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq