// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/anchor.cpp
// Function: ha_get_inter_candidates(ha_abufl_s*, unsigned long, char*, unsigned long, unsigned long, unsigned long, unsigned long, overlap_region_alloc*, Candidates_list*, double, int, int, kvec_t_u8_warp*, kvec_t_u64_warp*, void*, ha_pt_s*, overlap_region*, kvec_t_u64_warp*, st_mt_t*)
void ha_get_inter_candidates(ha_abufl_t *ab, uint64_t id, char* r, uint64_t rlen, uint64_t rw, uint64_t rk, uint64_t is_hpc,
        overlap_region_alloc *ol, Candidates_list *cl, double bw_thres, int max_n_chain, int keep_whole_chain,
						    kvec_t_u8_warp* k_flag, kvec_t_u64_warp* chain_idx, void *ha_flt_tab, ha_pt_t *ha_idx, 
                            overlap_region* f_cigar, kvec_t_u64_warp* dbg_ct, st_mt_t *sp)
{
	uint64_t i, k, l;
	// prepare
    clear_Candidates_list(cl);
    clear_overlap_region_alloc(ol);
	ab->mz.n = 0, ab->n_a = 0;

	// get the list of anchors
    mz2_ha_sketch(r, rlen, rw, rk, 0, is_hpc, &ab->mz, ha_flt_tab, asm_opt.mz_sample_dist, k_flag, dbg_ct,   
    NULL, -1, asm_opt.dp_min_len, -1, sp, asm_opt.mz_rewin, 1, NULL);
	
	// minimizer of queried read
	if (ab->mz.m > ab->old_mz_m) {
		ab->old_mz_m = ab->mz.m;
		REALLOC(ab->seed, ab->old_mz_m);
	}
	for (i = 0, ab->n_a = 0; i < ab->mz.n; ++i) {
		int n;
		ab->seed[i].a = ha_ptl_get(ha_idx, ab->mz.a[i].x, &n);
		ab->seed[i].n = n;
		ab->seed[i].cnt = ha_ft_cnt(ha_flt_tab, ab->mz.a[i].x);
		ab->n_a += n;
	}
	if (ab->n_a > ab->m_a) {
		ab->m_a = ab->n_a;
		kroundup64(ab->m_a);
		REALLOC(ab->a, ab->m_a);
	}
	for (i = 0, k = 0; i < ab->mz.n; ++i) {
		int j;
		///z is one of the minimizer
		ha_mzl_t *z = &ab->mz.a[i];
		seedl_t *s = &ab->seed[i];
		for (j = 0; j < s->n; ++j) {
			const ha_idxposl_t *y = &s->a[j];
			anchor1_t *an = &ab->a[k++];
			uint8_t rev = z->rev == y->rev? 0 : 1;
			an->other_off = y->pos;
			an->self_off = rev? rlen - 1 - (z->pos + 1 - z->span) : z->pos;
			an->cnt = s->n;
			an->srt = (uint64_t)y->rid<<33 | (uint64_t)rev<<32 | an->other_off;
		}
	}

	// sort anchors
	radix_sort_ha_an1(ab->a, ab->a + ab->n_a);
	for (k = 1, l = 0; k <= ab->n_a; ++k) {
		if (k == ab->n_a || ab->a[k].srt != ab->a[l].srt) {
			if (k - l > 1)
				radix_sort_ha_an2(ab->a + l, ab->a + k);
			l = k;
		}
	}


	// copy over to _cl_
	if (ab->m_a >= (uint64_t)cl->size) {
		cl->size = ab->m_a;
		REALLOC(cl->list, cl->size);
	}
	for (k = 0; k < ab->n_a; ++k) {
		k_mer_hit *p = &cl->list[k];
		p->readID = ab->a[k].srt >> 33;
		p->strand = ab->a[k].srt >> 32 & 1;
		p->offset = ab->a[k].other_off;
		p->self_offset = ab->a[k].self_off;
		p->cnt = (ab->a[k].cnt == 1? 1 : 16);
	}
	cl->length = ab->n_a;

	calculate_overlap_region_by_chaining(cl, ol, chain_idx, id, rlen, /**&R_INF**/NULL, NULL, bw_thres, keep_whole_chain, f_cigar, NULL);

	#if 0
	if (ol->length > 0) {
		fprintf(stderr, "B\t%ld\t%ld\t%d\n", (long)rid, (long)ol->length, rlen);
		for (int i = 0; i < (int)ol->length; ++i) {
			overlap_region *r = &ol->list[i];
			fprintf(stderr, "C\t%d\t%d\t%d\t%c\t%d\t%ld\t%d\t%d\t%c\t%d\t%d\n", (int)r->x_id, (int)r->x_pos_s, (int)r->x_pos_e, "+-"[r->x_pos_strand],
					(int)r->y_id, (long)Get_READ_LENGTH(R_INF, r->y_id), (int)r->y_pos_s, (int)r->y_pos_e, "+-"[r->y_pos_strand], (int)r->shared_seed, ha_ov_type(r, rlen));
		}
	}
	#endif
	
	if ((int)ol->length > max_n_chain) {
		int32_t w, n[4], s[4];
		n[0] = n[1] = n[2] = n[3] = 0, s[0] = s[1] = s[2] = s[3] = 0;
		ks_introsort_or_ss(ol->length, ol->list);
		for (i = 0; i < (uint32_t)ol->length; ++i) {
			const overlap_region *r = &ol->list[i];
			w = ha_ov_type(r, rlen);
			++n[w];
			if ((int)n[w] == max_n_chain) s[w] = r->shared_seed;
		}
		if (s[0] > 0 || s[1] > 0 || s[2] > 0 || s[3] > 0) {
			// n[0] = n[1] = n[2] = n[3] = 0;
			for (i = 0, k = 0; i < (uint32_t)ol->length; ++i) {
				overlap_region *r = &ol->list[i];
				w = ha_ov_type(r, rlen);
				// ++n[w];
				// if (((int)n[w] <= max_n_chain) || (r->shared_seed >= s[w] && s[w] >= (asm_opt.k_mer_length<<1))) {
				if (r->shared_seed >= s[w]) {
					if ((uint32_t)k != i) {
						overlap_region t;
						t = ol->list[k];
						ol->list[k] = ol->list[i];
						ol->list[i] = t;
					}
					++k;
				}
			}
			ol->length = k;
		}
	}

	///ks_introsort_or_xs(overlap_list->length, overlap_list->list);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movsd %xmm0, 0x38(%rsp)
movq %r9, 0x10(%rsp)
movq %r8, %r15
movq %rcx, %r14
movq %rdx, %rbx
movq %rsi, 0x30(%rsp)
movq %rdi, %r13
movq 0x150(%rsp), %rdi
movq 0x148(%rsp), %r12
callq 0x3f1bd
movq %r12, %rdi
callq 0x3d018
leaq 0x18(%r13), %rax
movl $0x0, 0x18(%r13)
xorl %r10d, %r10d
movq %r10, (%r13)
leaq 0x16bdef(%rip), %rcx    # 0x295758
movl 0xa0(%rcx), %ebp
movl 0xa8(%rcx), %r11d
movl 0x1f8(%rcx), %r12d
movd 0x11122b(%rip), %xmm0   # 0x23abb0
movq %rbx, %rdi
movq 0x178(%rsp), %rbx
movq %r14, 0x8(%rsp)
movl %r14d, %esi
movl %r15d, %edx
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
movl 0x140(%rsp), %r9d
pushq %r10
pushq $0x1
pushq %rbp
pushq 0x1b0(%rsp)
pushq %r12
pushq $-0x1
pushq %r10
pushq 0x1c8(%rsp)
pushq 0x1a8(%rsp)
pushq %r11
pushq %rbx
pushq %rax
callq 0x123ac6
addq $0x60, %rsp
movl 0x1c(%r13), %esi
cmpl 0x10(%r13), %esi
jbe 0x1299f7
movl %esi, 0x10(%r13)
movq 0x28(%r13), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%r13)
movq $0x0, (%r13)
cmpl $0x0, 0x18(%r13)
je 0x129bde
movq %rbx, %r15
xorl %ebx, %ebx
leaq 0x60(%rsp), %r14
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x20(%r13), %rax
movq (%rax,%r12), %rsi
movq 0x180(%rsp), %rdi
movq %r14, %rdx
callq 0x119548
movq 0x28(%r13), %rcx
movq %rax, 0x8(%rcx,%rbx)
movl 0x60(%rsp), %eax
movl %eax, (%rcx,%rbx)
movq 0x20(%r13), %rax
movq (%rax,%r12), %rsi
movq %r15, %rdi
callq 0x11a473
movq 0x28(%r13), %rcx
movl %eax, 0x4(%rcx,%rbx)
movslq 0x60(%rsp), %rax
addq (%r13), %rax
movq %rax, (%r13)
incq %rbp
movl 0x18(%r13), %ecx
addq $0x18, %r12
addq $0x10, %rbx
cmpq %rcx, %rbp
jb 0x129a19
cmpq 0x8(%r13), %rax
jbe 0x129ae3
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x20, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r13)
movq 0x30(%r13), %rdi
leaq 0x8(,%rax,8), %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq %rax, 0x30(%r13)
movl 0x18(%r13), %ecx
leaq 0x8(%r13), %rax
movq %rax, 0x18(%rsp)
testl %ecx, %ecx
movq 0x150(%rsp), %r12
je 0x129bef
movl %ecx, %eax
movq %rax, 0x10(%rsp)
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
movq 0x20(%r13), %rax
movq %rax, 0x48(%rsp)
movq 0x28(%r13), %rdx
xorl %edi, %edi
leaq 0x2(%rcx), %r8
addq 0x8(%rsp), %rcx
xorl %r9d, %r9d
movq %rdx, 0x40(%rsp)
movq %rdi, %rax
shlq $0x4, %rax
movslq (%rdx,%rax), %r10
testq %r10, %r10
jle 0x129bce
addq %rdx, %rax
leaq (%rdi,%rdi,2), %rdx
movq 0x48(%rsp), %rsi
leaq (%rsi,%rdx,8), %r11
movq 0x8(%rax), %r14
movq 0x30(%r13), %rax
movq %r10, %r15
negq %r15
leaq (%r9,%r9,2), %rdx
leaq (%rax,%rdx,8), %rbp
addq $0x10, %rbp
xorl %ebx, %ebx
movq (%r14), %r12
movq 0x8(%r11), %rdx
xorq %r12, %rdx
shrq $0x20, %r12
movl %r12d, -0x4(%rbp)
movl 0xc(%r11), %esi
testl %edx, %edx
jns 0x129b90
movzbl 0x10(%r11), %eax
subq %rsi, %rax
addq %rcx, %rax
movq %rax, %rsi
movl %esi, -0x8(%rbp)
movl %r10d, (%rbp)
movq (%r14), %rax
shlq $0x21, %rax
addq %rdx, %rdx
andq %r8, %rdx
orq %rax, %rdx
orq %r12, %rdx
movq %rdx, -0x10(%rbp)
addq $0x10, %r14
decq %rbx
addq $0x18, %rbp
cmpq %rbx, %r15
jne 0x129b68
subq %rbx, %r9
movq 0x150(%rsp), %r12
movq 0x40(%rsp), %rdx
incq %rdi
cmpq 0x10(%rsp), %rdi
jne 0x129b28
jmp 0x129bef
leaq 0x8(%r13), %rax
movq %rax, 0x18(%rsp)
movq 0x150(%rsp), %r12
movq (%r13), %rax
movq 0x30(%r13), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x125ce1
movq (%r13), %rcx
testq %rcx, %rcx
je 0x129c72
movl $0x1, %r14d
movl $0x18, %r15d
xorl %eax, %eax
cmpq %rcx, %r14
je 0x129c37
movq 0x30(%r13), %rdx
movq (%rdx,%r15), %rsi
leaq (%rax,%rax,2), %rdi
cmpq (%rdx,%rdi,8), %rsi
jne 0x129c37
movq %rax, %rdx
jmp 0x129c61
movq %r14, %rsi
subq %rax, %rsi
movq %r14, %rdx
cmpq $0x2, %rsi
jb 0x129c61
movq 0x30(%r13), %rsi
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,8), %rdi
addq %r15, %rsi
callq 0x125f7f
movq (%r13), %rcx
movq %r14, %rdx
incq %r14
addq $0x18, %r15
movq %rdx, %rax
cmpq %rcx, %r14
jbe 0x129c1b
jmp 0x129c74
xorl %ecx, %ecx
movq 0x18(%rsp), %rax
movq (%rax), %rsi
cmpq 0x10(%r12), %rsi
jb 0x129c9d
movq %rsi, 0x10(%r12)
movq (%r12), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%r12)
movq (%r13), %rcx
movq 0x188(%rsp), %r10
movq 0x170(%rsp), %rdx
movl 0x160(%rsp), %eax
movl 0x158(%rsp), %ebx
testq %rcx, %rcx
je 0x129d45
movq 0x30(%r13), %r11
xorl %ecx, %ecx
movl $0x80000000, %esi       # imm = 0x80000000
movl $0x10, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq (%r12), %r14
movq (%r11,%r8), %r11
shrq $0x21, %r11
movl (%r14,%rcx), %ebp
andl %esi, %ebp
orl %r11d, %ebp
movl %ebp, (%r14,%rcx)
movq 0x30(%r13), %r15
movq (%r15,%r8), %r15
shrq %r15
andl %esi, %r15d
orl %r11d, %r15d
movl %r15d, (%r14,%rcx)
movq 0x30(%r13), %r11
movq 0x8(%r11,%r8), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x4(%r14,%rcx)
movl 0x10(%r11,%r8), %ebp
cmpl $0x1, %ebp
cmovnel %edi, %ebp
movl %ebp, 0xc(%r14,%rcx)
incq %r9
movq (%r13), %r14
addq $0x18, %r8
addq $0x10, %rcx
cmpq %r14, %r9
jb 0x129cda
jmp 0x129d48
xorl %r14d, %r14d
movq %r14, 0x8(%r12)
xorl %r11d, %r11d
movq %r12, %rdi
movq 0x148(%rsp), %r15
movq %r15, %rsi
movq 0x30(%rsp), %rcx
movq 0x8(%rsp), %r8
xorl %r9d, %r9d
movsd 0x38(%rsp), %xmm0
pushq %r11
pushq %r10
pushq %rax
pushq %r11
callq 0x3ee1b
addq $0x20, %rsp
movq 0x10(%r15), %rdi
cmpl %ebx, %edi
jle 0x129ef6
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x20(%rsp)
movq (%r15), %rsi
callq 0x127252
movl 0x10(%r15), %eax
testq %rax, %rax
je 0x129ef6
movq (%r15), %rcx
movq 0x8(%rsp), %rdx
leal -0x1(%rdx), %ebp
imulq $0xa8, %rax, %rdx
xorl %esi, %esi
movl 0x4(%rcx,%rsi), %r8d
movl 0x8(%rcx,%rsi), %r9d
testl %r8d, %r8d
je 0x129e0f
movl $0x3, %edi
cmpl %ebp, %r9d
jb 0x129de5
xorl %edi, %edi
testl %r8d, %r8d
setne %dil
movl 0x50(%rsp,%rdi,4), %r8d
incl %r8d
movl %r8d, 0x50(%rsp,%rdi,4)
cmpl %ebx, %r8d
jne 0x129e01
movl 0x24(%rcx,%rsi), %r8d
movl %r8d, 0x20(%rsp,%rdi,4)
addq $0xa8, %rsi
cmpq %rsi, %rdx
jne 0x129dc3
jmp 0x129e1b
movl $0x2, %edi
cmpl %ebp, %r9d
jne 0x129ddc
jmp 0x129de5
movdqa 0x20(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %ecx
testl %ecx, %ecx
je 0x129ef6
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
movq (%r15), %rcx
movl 0x4(%rcx,%r12), %esi
movl 0x8(%rcx,%r12), %edi
testl %esi, %esi
je 0x129ee0
movl $0x3, %edx
cmpl %ebp, %edi
jb 0x129e62
xorl %edx, %edx
testl %esi, %esi
setne %dl
movl 0x24(%rcx,%r12), %esi
cmpl 0x20(%rsp,%rdx,4), %esi
jl 0x129ec9
movl %r13d, %edx
cmpq %rdx, %r14
je 0x129ec6
leaq (%rcx,%r12), %rbx
imulq $0xa8, %r13, %r15
addq %rcx, %r15
movl $0xa8, %edx
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r15, %rdi
movq 0x148(%rsp), %r15
movq %rbx, %rsi
callq 0x2160
movq (%r15), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0x60(%rsp), %rsi
callq 0x2160
movl 0x10(%r15), %eax
incq %r13
incq %r14
movl %eax, %ecx
addq $0xa8, %r12
cmpq %rcx, %r14
jb 0x129e3d
jmp 0x129ef2
movl $0x2, %edx
cmpl %ebp, %edi
jne 0x129e5b
jmp 0x129e62
movq %r13, 0x10(%r15)
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq