// File: /workspace/github_cpp_makefile_star2_O3/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 $0xf8, %rsp
movq %rcx, 0x28(%rsp)
testq %rdx, %rdx
jle 0x1bb0f7
movq %r8, %r14
movq %rsi, %rbp
movq %rdi, %rbx
movq %rdx, (%rsp)
movq 0x10(%rdi), %r15
shlq $0x3, %r9
movq %r8, %rdi
movl $0xff, %esi
movq %r9, %rdx
callq 0x20e0
movq %r15, 0x30(%rsp)
testq %r15, %r15
jle 0x1baead
movq 0x30(%rsp), %rcx
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
decq %rcx
movq %rcx, %xmm0
movl $0xa04, %ecx            # imm = 0xA04
addq (%rbx), %rcx
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa %xmm0, 0x10(%rsp)
movdqa 0x7b373(%rip), %xmm1    # 0x235ce0
movdqa 0x7b37b(%rip), %xmm2    # 0x235cf0
movdqa 0x7b383(%rip), %xmm3    # 0x235d00
movdqa 0x7b38b(%rip), %xmm4    # 0x235d10
movdqa 0x76913(%rip), %xmm5    # 0x2312a0
movdqa 0x7691b(%rip), %xmm6    # 0x2312b0
movdqa 0x76923(%rip), %xmm7    # 0x2312c0
movdqa 0x7692a(%rip), %xmm8    # 0x2312d0
movdqa 0x76931(%rip), %xmm9    # 0x2312e0
pcmpeqd %xmm10, %xmm10
movdqa 0x10(%rsp), %xmm12
pxor %xmm9, %xmm12
movdqa %xmm8, %xmm13
pxor %xmm9, %xmm13
movdqa %xmm13, %xmm15
pcmpgtd %xmm12, %xmm15
pshufd $0xa0, %xmm15, %xmm14   # xmm14 = xmm15[0,0,2,2]
pshuflw $0xe8, %xmm14, %xmm0    # xmm0 = xmm14[0,2,2,3,4,5,6,7]
pcmpeqd %xmm12, %xmm13
pshufd $0xf5, %xmm13, %xmm13   # xmm13 = xmm13[1,1,3,3]
pshuflw $0xe8, %xmm13, %xmm11   # xmm11 = xmm13[0,2,2,3,4,5,6,7]
pand %xmm0, %xmm11
pshufd $0xf5, %xmm15, %xmm15   # xmm15 = xmm15[1,1,3,3]
pshuflw $0xe8, %xmm15, %xmm0    # xmm0 = xmm15[0,2,2,3,4,5,6,7]
por %xmm11, %xmm0
pxor %xmm10, %xmm0
packssdw %xmm0, %xmm0
movd %xmm0, %edx
testb $0x1, %dl
je 0x1baa20
movb $0x0, -0x9d8(%rcx)
pand %xmm14, %xmm13
por %xmm15, %xmm13
packssdw %xmm13, %xmm13
pxor %xmm10, %xmm13
packssdw %xmm13, %xmm13
packsswb %xmm13, %xmm13
movd %xmm13, %edx
shrl $0x8, %edx
testb $0x1, %dl
je 0x1baa52
movb $0x0, -0x930(%rcx)
movdqa %xmm7, %xmm0
pxor %xmm9, %xmm0
movdqa %xmm0, %xmm11
pcmpgtd %xmm12, %xmm11
pshufd $0xa0, %xmm11, %xmm13   # xmm13 = xmm11[0,0,2,2]
pcmpeqd %xmm12, %xmm0
pshufd $0xf5, %xmm0, %xmm14    # xmm14 = xmm0[1,1,3,3]
movdqa %xmm14, %xmm0
pand %xmm13, %xmm0
pshufd $0xf5, %xmm11, %xmm15   # xmm15 = xmm11[1,1,3,3]
por %xmm15, %xmm0
packssdw %xmm0, %xmm0
pxor %xmm10, %xmm0
packssdw %xmm0, %xmm0
packsswb %xmm0, %xmm0
movd %xmm0, %edx
shrl $0x10, %edx
testb $0x1, %dl
je 0x1baaaf
movb $0x0, -0x888(%rcx)
pshufhw $0x84, %xmm13, %xmm0    # xmm0 = xmm13[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm14, %xmm11   # xmm11 = xmm14[0,1,2,3,4,5,4,6]
pand %xmm0, %xmm11
pshufhw $0x84, %xmm15, %xmm0    # xmm0 = xmm15[0,1,2,3,4,5,4,6]
por %xmm11, %xmm0
pxor %xmm10, %xmm0
packssdw %xmm0, %xmm0
packsswb %xmm0, %xmm0
movd %xmm0, %edx
shrl $0x18, %edx
testb $0x1, %dl
je 0x1baaeb
movb $0x0, -0x7e0(%rcx)
movdqa %xmm6, %xmm0
pxor %xmm9, %xmm0
movdqa %xmm0, %xmm11
pcmpgtd %xmm12, %xmm11
pshufd $0xa0, %xmm11, %xmm14   # xmm14 = xmm11[0,0,2,2]
pshuflw $0xe8, %xmm14, %xmm15   # xmm15 = xmm14[0,2,2,3,4,5,6,7]
pcmpeqd %xmm12, %xmm0
pshufd $0xf5, %xmm0, %xmm13    # xmm13 = xmm0[1,1,3,3]
pshuflw $0xe8, %xmm13, %xmm0    # xmm0 = xmm13[0,2,2,3,4,5,6,7]
pand %xmm15, %xmm0
pshufd $0xf5, %xmm11, %xmm15   # xmm15 = xmm11[1,1,3,3]
pshuflw $0xe8, %xmm15, %xmm11   # xmm11 = xmm15[0,2,2,3,4,5,6,7]
por %xmm0, %xmm11
pxor %xmm10, %xmm11
packssdw %xmm11, %xmm0
packsswb %xmm0, %xmm0
pextrw $0x2, %xmm0, %edx
testb $0x1, %dl
je 0x1bab50
movb $0x0, -0x738(%rcx)
pand %xmm14, %xmm13
por %xmm15, %xmm13
packssdw %xmm13, %xmm13
pxor %xmm10, %xmm13
packssdw %xmm13, %xmm0
packsswb %xmm0, %xmm0
pextrw $0x2, %xmm0, %edx
shrl $0x8, %edx
testb $0x1, %dl
je 0x1bab81
movb $0x0, -0x690(%rcx)
movdqa %xmm5, %xmm0
pxor %xmm9, %xmm0
movdqa %xmm0, %xmm11
pcmpgtd %xmm12, %xmm11
pshufd $0xa0, %xmm11, %xmm13   # xmm13 = xmm11[0,0,2,2]
pcmpeqd %xmm12, %xmm0
pshufd $0xf5, %xmm0, %xmm14    # xmm14 = xmm0[1,1,3,3]
movdqa %xmm14, %xmm0
pand %xmm13, %xmm0
pshufd $0xf5, %xmm11, %xmm15   # xmm15 = xmm11[1,1,3,3]
por %xmm15, %xmm0
packssdw %xmm0, %xmm0
pxor %xmm10, %xmm0
packssdw %xmm0, %xmm0
packsswb %xmm0, %xmm0
pextrw $0x3, %xmm0, %edx
testb $0x1, %dl
je 0x1babdc
movb $0x0, -0x5e8(%rcx)
pshufhw $0x84, %xmm13, %xmm0    # xmm0 = xmm13[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm14, %xmm11   # xmm11 = xmm14[0,1,2,3,4,5,4,6]
pand %xmm0, %xmm11
pshufhw $0x84, %xmm15, %xmm0    # xmm0 = xmm15[0,1,2,3,4,5,4,6]
por %xmm11, %xmm0
pxor %xmm10, %xmm0
packssdw %xmm0, %xmm0
packsswb %xmm0, %xmm0
pextrw $0x3, %xmm0, %edx
shrl $0x8, %edx
testb $0x1, %dl
je 0x1bac19
movb $0x0, -0x540(%rcx)
movdqa %xmm4, %xmm0
pxor %xmm9, %xmm0
movdqa %xmm0, %xmm11
pcmpgtd %xmm12, %xmm11
pshufd $0xa0, %xmm11, %xmm14   # xmm14 = xmm11[0,0,2,2]
pshuflw $0xe8, %xmm14, %xmm15   # xmm15 = xmm14[0,2,2,3,4,5,6,7]
pcmpeqd %xmm12, %xmm0
pshufd $0xf5, %xmm0, %xmm13    # xmm13 = xmm0[1,1,3,3]
pshuflw $0xe8, %xmm13, %xmm0    # xmm0 = xmm13[0,2,2,3,4,5,6,7]
pand %xmm15, %xmm0
pshufd $0xf5, %xmm11, %xmm15   # xmm15 = xmm11[1,1,3,3]
pshuflw $0xe8, %xmm15, %xmm11   # xmm11 = xmm15[0,2,2,3,4,5,6,7]
por %xmm0, %xmm11
pxor %xmm10, %xmm11
packssdw %xmm11, %xmm11
packsswb %xmm11, %xmm0
pextrw $0x4, %xmm0, %edx
testb $0x1, %dl
je 0x1bac7f
movb $0x0, -0x498(%rcx)
pand %xmm14, %xmm13
por %xmm15, %xmm13
packssdw %xmm13, %xmm13
pxor %xmm10, %xmm13
packssdw %xmm13, %xmm13
packsswb %xmm13, %xmm0
pextrw $0x4, %xmm0, %edx
shrl $0x8, %edx
testb $0x1, %dl
je 0x1bacb1
movb $0x0, -0x3f0(%rcx)
movdqa %xmm3, %xmm0
pxor %xmm9, %xmm0
movdqa %xmm0, %xmm11
pcmpgtd %xmm12, %xmm11
pshufd $0xa0, %xmm11, %xmm13   # xmm13 = xmm11[0,0,2,2]
pcmpeqd %xmm12, %xmm0
pshufd $0xf5, %xmm0, %xmm14    # xmm14 = xmm0[1,1,3,3]
movdqa %xmm14, %xmm0
pand %xmm13, %xmm0
pshufd $0xf5, %xmm11, %xmm15   # xmm15 = xmm11[1,1,3,3]
por %xmm15, %xmm0
packssdw %xmm0, %xmm0
pxor %xmm10, %xmm0
packssdw %xmm0, %xmm0
packsswb %xmm0, %xmm0
pextrw $0x5, %xmm0, %edx
testb $0x1, %dl
je 0x1bad0c
movb $0x0, -0x348(%rcx)
pshufhw $0x84, %xmm13, %xmm0    # xmm0 = xmm13[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm14, %xmm11   # xmm11 = xmm14[0,1,2,3,4,5,4,6]
pand %xmm0, %xmm11
pshufhw $0x84, %xmm15, %xmm0    # xmm0 = xmm15[0,1,2,3,4,5,4,6]
por %xmm11, %xmm0
pxor %xmm10, %xmm0
packssdw %xmm0, %xmm0
packsswb %xmm0, %xmm0
pextrw $0x5, %xmm0, %edx
shrl $0x8, %edx
testb $0x1, %dl
je 0x1bad49
movb $0x0, -0x2a0(%rcx)
movdqa %xmm2, %xmm0
pxor %xmm9, %xmm0
movdqa %xmm0, %xmm11
pcmpgtd %xmm12, %xmm11
pshufd $0xa0, %xmm11, %xmm14   # xmm14 = xmm11[0,0,2,2]
pshuflw $0xe8, %xmm14, %xmm15   # xmm15 = xmm14[0,2,2,3,4,5,6,7]
pcmpeqd %xmm12, %xmm0
pshufd $0xf5, %xmm0, %xmm13    # xmm13 = xmm0[1,1,3,3]
pshuflw $0xe8, %xmm13, %xmm0    # xmm0 = xmm13[0,2,2,3,4,5,6,7]
pand %xmm15, %xmm0
pshufd $0xf5, %xmm11, %xmm15   # xmm15 = xmm11[1,1,3,3]
pshuflw $0xe8, %xmm15, %xmm11   # xmm11 = xmm15[0,2,2,3,4,5,6,7]
por %xmm0, %xmm11
pxor %xmm10, %xmm11
packssdw %xmm11, %xmm0
packsswb %xmm0, %xmm0
pextrw $0x6, %xmm0, %edx
testb $0x1, %dl
je 0x1badae
movb $0x0, -0x1f8(%rcx)
pand %xmm14, %xmm13
por %xmm15, %xmm13
packssdw %xmm13, %xmm13
pxor %xmm10, %xmm13
packssdw %xmm13, %xmm0
packsswb %xmm0, %xmm0
pextrw $0x6, %xmm0, %edx
shrl $0x8, %edx
testb $0x1, %dl
je 0x1baddf
movb $0x0, -0x150(%rcx)
movdqa %xmm1, %xmm0
pxor %xmm9, %xmm0
movdqa %xmm0, %xmm11
pcmpgtd %xmm12, %xmm11
pshufd $0xa0, %xmm11, %xmm13   # xmm13 = xmm11[0,0,2,2]
pcmpeqd %xmm12, %xmm0
pshufd $0xf5, %xmm0, %xmm12    # xmm12 = xmm0[1,1,3,3]
movdqa %xmm12, %xmm0
pand %xmm13, %xmm0
pshufd $0xf5, %xmm11, %xmm14   # xmm14 = xmm11[1,1,3,3]
por %xmm14, %xmm0
packssdw %xmm0, %xmm0
pxor %xmm10, %xmm0
packssdw %xmm0, %xmm0
packsswb %xmm0, %xmm0
pextrw $0x7, %xmm0, %edx
testb $0x1, %dl
je 0x1bae3a
movb $0x0, -0xa8(%rcx)
pshufhw $0x84, %xmm13, %xmm0    # xmm0 = xmm13[0,1,2,3,4,5,4,6]
pshufhw $0x84, %xmm12, %xmm11   # xmm11 = xmm12[0,1,2,3,4,5,4,6]
pand %xmm0, %xmm11
pshufhw $0x84, %xmm14, %xmm0    # xmm0 = xmm14[0,1,2,3,4,5,4,6]
por %xmm11, %xmm0
pxor %xmm10, %xmm0
packssdw %xmm0, %xmm0
packsswb %xmm0, %xmm0
pextrw $0x7, %xmm0, %edx
shrl $0x8, %edx
testb $0x1, %dl
je 0x1bae73
movb $0x0, (%rcx)
movdqa 0x7aea5(%rip), %xmm0    # 0x235d20
paddq %xmm0, %xmm8
paddq %xmm0, %xmm7
paddq %xmm0, %xmm6
paddq %xmm0, %xmm5
paddq %xmm0, %xmm4
paddq %xmm0, %xmm3
paddq %xmm0, %xmm2
paddq %xmm0, %xmm1
addq $0xa80, %rcx            # imm = 0xA80
addq $-0x10, %rax
jne 0x1ba9b4
movq %r14, 0x40(%rsp)
movq %rbx, 0x10(%rsp)
xorl %edi, %edi
xorl %esi, %esi
movq (%rsp), %rdx
movq %rbp, 0x48(%rsp)
movq 0x10(%rsp), %r15
movq 0x40(%rsp), %rbx
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %r12
addq %rsi, %r12
movl 0x10(%rbp,%r12), %r13d
movl 0x14(%rbp,%r12), %eax
cmpl %eax, %r13d
jae 0x1baf6d
movq %rsi, 0x38(%rsp)
movq %rdi, 0x8(%rsp)
addq %rbp, %r12
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rbp
movq 0x28(%rsp), %rax
addq %r13, %rax
addq %rax, %rbp
movq 0x130(%rsp), %r14
movq (%r15), %rax
movl (%rbp), %ecx
imulq $0xa8, %rcx, %rcx
leaq (%rax,%rcx), %rdi
movb $0x2, 0x2c(%rax,%rcx)
movq %rbx, %rsi
movq %r14, %rdx
movq 0x138(%rsp), %rcx
callq 0x1ba7ad
incq %r13
movl 0x14(%r12), %eax
addq $0x1c, %rbp
cmpq %rax, %r13
jb 0x1baf1b
movl 0x10(%r12), %r13d
movq 0x48(%rsp), %rbp
movq (%rsp), %rdx
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rsi
subl %r13d, %eax
xorl %ecx, %ecx
cmpl $0x1, %eax
sete %cl
addq %rcx, %rdi
incq %rsi
cmpq %rdx, %rsi
jne 0x1baece
xorl %esi, %esi
cmpq %rdx, %rdi
movq 0x30(%rsp), %rcx
movq %rcx, %rax
notq %rax
cmovneq %rax, %rsi
testq %rcx, %rcx
jle 0x1bb101
movq %rsi, (%rsp)
leaq (%rdx,%rdx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdx, %rax
movl -0xc(%rbp,%rax), %eax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
movq 0x28(%rsp), %rax
movl (%rax,%rcx), %eax
movq %rax, 0x28(%rsp)
movq 0x138(%rsp), %r15
leaq -0x1(%r15), %rax
movq %rax, 0x38(%rsp)
movq 0x10(%rsp), %r12
movq (%r12), %r13
movq $-0x1, 0x8(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpb $0x0, 0x2c(%r13,%rbx)
jne 0x1bb032
addq %rbx, %r13
movq %r13, %rdi
movq 0x40(%rsp), %rsi
movq 0x130(%rsp), %rdx
movq 0x138(%rsp), %rcx
callq 0x1ba81d
movq (%r12), %r13
movb %al, 0x2c(%r13,%rbx)
testl %eax, %eax
je 0x1bb0c7
cmpq %rbp, %r14
je 0x1bb08a
leaq (%rbx,%r13), %rsi
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
movq %rsi, %r15
callq 0x2160
imulq $0xa8, %rbp, %r12
addq %r12, %r13
movl $0xa8, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x2160
movq 0x10(%rsp), %rax
addq (%rax), %r12
movl $0xa8, %edx
movq %r12, %rdi
movq 0x10(%rsp), %r12
leaq 0x50(%rsp), %rsi
callq 0x2160
movq (%r12), %r13
imulq $0xa8, %rbp, %rax
cmpl $0x0, 0x4(%r13,%rax)
jne 0x1bb0be
leaq (%rax,%r13), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, 0x38(%rsp)
jne 0x1bb0be
incq (%rsp)
cmpq %r14, 0x28(%rsp)
movq 0x8(%rsp), %rcx
cmoveq %rbp, %rcx
movq %rcx, 0x8(%rsp)
movb $0x1, 0x2c(%r13,%rax)
incq %rbp
incq %r14
addq $0xa8, %rbx
cmpq %r14, 0x30(%rsp)
jne 0x1baff9
movq %rbp, 0x10(%r12)
cmpq %rbp, (%rsp)
jne 0x1bb17a
movq 0x8(%rsp), %r15
testq %r15, %r15
jne 0x1bb11f
jmp 0x1bb16c
movl $0x1, %eax
jmp 0x1bb183
movq 0x10(%rsp), %r12
movq $0x0, 0x10(%r12)
testq %rsi, %rsi
je 0x1bb118
xorl %ebp, %ebp
jmp 0x1bb17a
movq $-0x1, %r15
movq (%r12), %r14
imulq $0xa8, %r15, %r15
addq %r14, %r15
leaq 0x50(%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 0x10(%rsp), %rax
movq (%rax), %rdi
movl $0xa8, %edx
movq %r12, %rsi
movq 0x10(%rsp), %r12
callq 0x2160
movq $0x1, 0x10(%r12)
movl $0x1, %ebp
xorl %eax, %eax
cmpq $0x1, %rbp
sete %al
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq