// File: /workspace/github_cpp_makefile_star2_O2/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)
movq %rsi, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
testq %rdx, %rdx
jle 0x170cb2
movq %r8, %r14
movq %rdi, %rbx
movq 0x120(%rsp), %r12
movq 0x10(%rdi), %r15
shlq $0x3, %r9
movq %r8, %rdi
movl $0xff, %esi
movq %r9, %rdx
callq 0x20e0
xorl %ecx, %ecx
testq %r15, %r15
movq %r15, 0x8(%rsp)
cmovgq %r15, %rcx
pushq $0x2c
popq %rax
movq %rcx, 0x38(%rsp)
subq $0x1, %rcx
jb 0x170a72
movq (%rbx), %rdx
movb $0x0, (%rdx,%rax)
addq $0xa8, %rax
jmp 0x170a5d
xorl %eax, %eax
xorl %edx, %edx
cmpq 0x18(%rsp), %rax
je 0x170afb
movq %rdx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
imulq $0x1c, %rax, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %rbp
movl 0x10(%rcx,%rax), %r13d
imulq $0x1c, %r13, %r15
addq (%rsp), %r15
movl 0x14(%rbp), %eax
cmpq %rax, %r13
jae 0x170adb
movq (%rbx), %rax
movl (%r15), %ecx
imulq $0xa8, %rcx, %rcx
leaq (%rax,%rcx), %rdi
movb $0x2, 0x2c(%rax,%rcx)
movq %r14, %rsi
movq %r12, %rdx
movq 0x128(%rsp), %rcx
callq 0x170896
incq %r13
addq $0x1c, %r15
jmp 0x170aa1
subl 0x10(%rbp), %eax
xorl %ecx, %ecx
cmpl $0x1, %eax
sete %cl
movq 0x20(%rsp), %rdx
addq %rcx, %rdx
movq 0x28(%rsp), %rax
incq %rax
jmp 0x170a76
xorl %r13d, %r13d
movq 0x18(%rsp), %rcx
cmpq %rcx, %rdx
movq 0x8(%rsp), %rax
notq %rax
cmoveq %r13, %rax
movq %rax, 0x8(%rsp)
imulq $0x1c, %rcx, %rax
movq 0x10(%rsp), %rcx
movl -0xc(%rcx,%rax), %eax
imulq $0x1c, %rax, %rax
movq (%rsp), %rcx
movl (%rcx,%rax), %eax
movq %rax, 0x30(%rsp)
movq 0x128(%rsp), %rax
decq %rax
movq %rax, 0x10(%rsp)
pushq $-0x1
popq %rax
movq %rax, (%rsp)
xorl %edx, %edx
xorl %r15d, %r15d
cmpq %rdx, 0x38(%rsp)
je 0x170c45
movq (%rbx), %rbp
cmpb $0x0, 0x2c(%rbp,%r13)
jne 0x170b95
addq %r13, %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq %rdx, %rbp
movq %r12, %rdx
movq 0x128(%rsp), %rcx
callq 0x17091b
movq %rbp, %rdx
movq (%rbx), %rbp
movb %al, 0x2c(%rbp,%r13)
testl %eax, %eax
je 0x170c36
cmpq %r15, %rdx
je 0x170bfc
movq %rbp, %rsi
addq %r13, %rsi
movq %rsi, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
callq 0x2160
movq %r13, 0x28(%rsp)
imulq $0xa8, %r15, %r13
addq %r13, %rbp
movl $0xa8, %edx
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
addq (%rbx), %r13
movl $0xa8, %edx
movq %r13, %rdi
movq 0x28(%rsp), %r13
leaq 0x40(%rsp), %rsi
callq 0x2160
movq 0x20(%rsp), %rdx
movq (%rbx), %rbp
imulq $0xa8, %r15, %rax
cmpl $0x0, 0x4(%rbp,%rax)
jne 0x170c2e
leaq (%rax,%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, 0x10(%rsp)
jne 0x170c2e
incq 0x8(%rsp)
cmpq %rdx, 0x30(%rsp)
movq (%rsp), %rcx
cmoveq %r15, %rcx
movq %rcx, (%rsp)
movb $0x1, 0x2c(%rbp,%rax)
incq %r15
incq %rdx
addq $0xa8, %r13
jmp 0x170b50
movq %r15, 0x10(%rbx)
cmpq %r15, 0x8(%rsp)
jne 0x170ca7
movq (%rsp), %rax
testq %rax, %rax
je 0x170c9b
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 (%rbx), %rdi
movl $0xa8, %edx
movq %r12, %rsi
callq 0x2160
movq $0x1, 0x10(%rbx)
pushq $0x1
popq %r15
xorl %eax, %eax
cmpq $0x1, %r15
sete %al
jmp 0x170cb5
pushq $0x1
popq %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq