// File: 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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
jg 0x2ad11f
movq $0x1, -0x8(%rbp)
jmp 0x2ad529
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq $-0x1, -0x60(%rbp)
movq $-0x1, -0x68(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rdx
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x2100
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x2ad18f
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x48(%rbp), %rcx
addq %rcx, %rax
movb $0x0, 0x2c(%rax)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2ad161
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x2ad277
movq -0x18(%rbp), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
imulq $0x1c, -0x48(%rbp), %rdx
addq %rdx, %rcx
movl 0x14(%rcx), %ecx
cmpq %rcx, %rax
jge 0x2ad231
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
imulq $0x1c, -0x50(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movb $0x2, 0x2c(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rax
imulq $0x1c, -0x50(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
imulq $0xa8, %rax, %rax
addq %rax, %rdi
movq -0x30(%rbp), %rsi
movq 0x10(%rbp), %rdx
movq 0x18(%rbp), %rcx
callq 0x2acd90
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2ad1b8
movq -0x18(%rbp), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0x14(%rax), %edx
movq -0x18(%rbp), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
subl 0x10(%rax), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
cltq
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2ad197
movq -0x58(%rbp), %rax
cmpq -0x20(%rbp), %rax
jne 0x2ad28b
movq $0x0, -0x58(%rbp)
jmp 0x2ad299
xorl %eax, %eax
subq -0x40(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rdx
subq $0x1, %rdx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
movl 0x10(%rcx), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movq %rax, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x2ad458
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x48(%rbp), %rcx
addq %rcx, %rax
cmpb $0x0, 0x2c(%rax)
jne 0x2ad330
movq -0x10(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x48(%rbp), %rax
addq %rax, %rdi
movq -0x30(%rbp), %rsi
movq 0x10(%rbp), %rdx
movq 0x18(%rbp), %rcx
callq 0x2acef0
movb %al, %cl
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x48(%rbp), %rdx
addq %rdx, %rax
movb %cl, 0x2c(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x48(%rbp), %rcx
addq %rcx, %rax
cmpb $0x0, 0x2c(%rax)
jne 0x2ad34d
jmp 0x2ad447
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x2ad3cb
movq -0x10(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x48(%rbp), %rax
addq %rax, %rsi
leaq -0x110(%rbp), %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x10(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x50(%rbp), %rax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x48(%rbp), %rax
addq %rax, %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x10(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x50(%rbp), %rax
addq %rax, %rdi
leaq -0x110(%rbp), %rsi
movl $0xa8, %edx
callq 0x2180
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x50(%rbp), %rcx
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
jne 0x2ad425
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x50(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq 0x18(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x2ad425
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x48(%rbp), %rax
jne 0x2ad423
movq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x2ad425
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x50(%rbp), %rcx
addq %rcx, %rax
movb $0x1, 0x2c(%rax)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x2ad2d0
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x2ad50c
movq -0x68(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x2ad500
movq -0x10(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x48(%rbp), %rax
addq %rax, %rsi
leaq -0x110(%rbp), %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x10(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x50(%rbp), %rax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x48(%rbp), %rax
addq %rax, %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x10(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x50(%rbp), %rax
addq %rax, %rdi
leaq -0x110(%rbp), %rsi
movl $0xa8, %edx
callq 0x2180
movq -0x10(%rbp), %rax
movq $0x1, 0x10(%rax)
movq -0x10(%rbp), %rax
cmpq $0x1, 0x10(%rax)
jne 0x2ad521
movq $0x1, -0x8(%rbp)
jmp 0x2ad529
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)