// File: /workspace/github_cpp_makefile_star2_O2/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 $0x158, %rsp            # imm = 0x158
movsd %xmm0, 0x88(%rsp)
movq %r9, 0x68(%rsp)
movq %r8, %r15
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, 0x80(%rsp)
movq %rdi, %r13
movq 0x1c8(%rsp), %rbx
movq 0x1a0(%rsp), %rdi
movq 0x198(%rsp), %r12
callq 0x13d24
movq %r12, %rdi
callq 0x11935
andl $0x0, 0x18(%r13)
leaq 0x18(%r13), %r12
andq $0x0, (%r13)
leaq 0x16ee39(%rip), %rax    # 0x253758
movl 0xa0(%rax), %ecx
movl 0xa8(%rax), %edx
movl 0x1f8(%rax), %eax
movl %ecx, 0x48(%rsp)
movq 0x1e8(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, 0x38(%rsp)
movq 0x1e0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x18(%rsp)
movl %edx, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
andq $0x0, 0x58(%rsp)
movq %r12, (%rsp)
orl $-0x1, 0x30(%rsp)
movl $0x1, 0x50(%rsp)
andq $0x0, 0x28(%rsp)
movd 0x108112(%rip), %xmm0   # 0x1ecaa0
movq %r14, %rdi
movq %rbp, 0x60(%rsp)
movl %ebp, %esi
movl %r15d, %edx
movq 0x68(%rsp), %rcx
xorl %r8d, %r8d
movl 0x190(%rsp), %r9d
callq 0xdf468
movl 0x1c(%r13), %esi
cmpl 0x10(%r13), %esi
jbe 0xe49cf
movl %esi, 0x10(%r13)
movq 0x28(%r13), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x28(%r13)
andq $0x0, (%r13)
xorl %ebx, %ebx
leaq 0xb0(%rsp), %r14
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %r15d, %r15d
movl (%r12), %eax
cmpq %rax, %r15
jae 0xe4a53
movq 0x20(%r13), %rax
movq (%rax,%rbx), %rsi
movq 0x1d0(%rsp), %rdi
movq %r14, %rdx
callq 0xd4e67
movq 0x28(%r13), %rcx
movq %rax, 0x8(%rcx,%rbp)
movl 0xb0(%rsp), %eax
movl %eax, (%rcx,%rbp)
movq 0x20(%r13), %rax
movq (%rax,%rbx), %rsi
movq 0x1c8(%rsp), %rdi
callq 0xd5bc2
movq 0x28(%r13), %rcx
movl %eax, 0x4(%rcx,%rbp)
movslq 0xb0(%rsp), %rcx
addq (%r13), %rcx
movq %rcx, (%r13)
incq %r15
addq $0x10, %rbp
addq $0x18, %rbx
jmp 0xe49e5
cmpq 0x8(%r13), %rcx
jbe 0xe4ab3
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x20, %rcx
orq %rax, %rcx
incq %rcx
movq %rcx, 0x8(%r13)
movq 0x30(%r13), %rdi
imulq $0x18, %rcx, %rsi
callq 0x2370
movq %rax, 0x30(%r13)
movl 0x18(%r13), %eax
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
xorl %edi, %edi
leaq 0x2(%rcx), %rsi
addq 0x60(%rsp), %rcx
xorl %edx, %edx
movq %rax, 0x90(%rsp)
cmpq %rax, %rdx
je 0xe4b96
imulq $0x18, %rdx, %r8
addq 0x20(%r13), %r8
movq 0x28(%r13), %rax
movq %rdx, 0x98(%rsp)
shlq $0x4, %rdx
leaq (%rax,%rdx), %r9
movl (%rax,%rdx), %r10d
testl %r10d, %r10d
movl $0x0, %r11d
cmovgl %r10d, %r11d
imulq $0x18, %rdi, %rbx
addq %r11, %rdi
movq %rdi, 0x68(%rsp)
shlq $0x4, %r11
xorl %r14d, %r14d
cmpq %r14, %r11
je 0xe4b79
movq 0x8(%r9), %rdx
movq 0x30(%r13), %r15
movq (%rdx,%r14), %r12
movq 0x8(%r8), %rbp
xorq %r12, %rbp
shrq $0x20, %r12
movl %r12d, 0xc(%r15,%rbx)
movl 0xc(%r8), %edi
testl %ebp, %ebp
jns 0xe4b4d
movzbl 0x10(%r8), %eax
subq %rdi, %rax
addq %rcx, %rax
movq %rax, %rdi
movl %edi, 0x8(%r15,%rbx)
movl %r10d, 0x10(%r15,%rbx)
movq (%rdx,%r14), %rax
shlq $0x21, %rax
addq %rbp, %rbp
andq %rsi, %rbp
orq %rax, %rbp
orq %r12, %rbp
movq %rbp, (%r15,%rbx)
addq $0x10, %r14
addq $0x18, %rbx
jmp 0xe4b16
movq 0x98(%rsp), %rdx
incq %rdx
movq 0x90(%rsp), %rax
movq 0x68(%rsp), %rdi
jmp 0xe4acd
movq 0x30(%r13), %rdi
imulq $0x18, (%r13), %rsi
addq %rdi, %rsi
callq 0xe1068
pushq $0x1
popq %rbx
pushq $0x18
popq %r14
xorl %eax, %eax
cmpq (%r13), %rbx
ja 0xe4c00
je 0xe4bcf
movq 0x30(%r13), %rcx
movq (%rcx,%r14), %rdx
imulq $0x18, %rax, %rsi
cmpq (%rcx,%rsi), %rdx
jne 0xe4bcf
movq %rax, %rcx
jmp 0xe4bf4
movq %rbx, %rdx
subq %rax, %rdx
movq %rbx, %rcx
cmpq $0x2, %rdx
jb 0xe4bf4
movq 0x30(%r13), %rsi
imulq $0x18, %rax, %rdi
addq %rsi, %rdi
addq %r14, %rsi
callq 0xe1300
movq %rbx, %rcx
incq %rbx
addq $0x18, %r14
movq %rcx, %rax
jmp 0xe4bb0
movq 0x8(%r13), %rsi
movq 0x1a0(%rsp), %r14
cmpq 0x10(%r14), %rsi
jb 0xe4c25
movq %rsi, 0x10(%r14)
movq (%r14), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%r14)
xorl %eax, %eax
movl $0x80000000, %ecx       # imm = 0x80000000
pushq $0x10
popq %rdx
xorl %esi, %esi
xorl %edi, %edi
movq 0x198(%rsp), %rbx
movq (%r13), %r8
cmpq %r8, %rdi
jae 0xe4cac
movq (%r14), %r8
movq 0x30(%r13), %r9
movq (%r9,%rsi), %r9
shrq $0x21, %r9
movl (%r8,%rax), %r10d
andl %ecx, %r10d
orl %r9d, %r10d
movl %r10d, (%r8,%rax)
movq 0x30(%r13), %r10
movq (%r10,%rsi), %r10
shrq %r10
andl %ecx, %r10d
orl %r9d, %r10d
movl %r10d, (%r8,%rax)
movq 0x30(%r13), %r9
movq 0x8(%r9,%rsi), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x4(%r8,%rax)
movl 0x10(%r9,%rsi), %r9d
cmpl $0x1, %r9d
cmovnel %edx, %r9d
movl %r9d, 0xc(%r8,%rax)
incq %rdi
addq $0x18, %rsi
addq $0x10, %rax
jmp 0xe4c3b
movq %r8, 0x8(%r14)
andq $0x0, 0x18(%rsp)
movq 0x1d8(%rsp), %rax
movq %rax, 0x10(%rsp)
andq $0x0, (%rsp)
movl 0x1b0(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq 0x1c0(%rsp), %rdx
movq 0x80(%rsp), %rcx
movq 0x60(%rsp), %r8
xorl %r9d, %r9d
movsd 0x88(%rsp), %xmm0
callq 0x139a9
movq 0x10(%rbx), %rdi
cmpl 0x1a8(%rsp), %edi
jle 0xe4e4d
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x70(%rsp)
movq (%rbx), %rsi
callq 0xe248f
movl 0x10(%rbx), %r13d
imulq $0xa8, %r13, %rbx
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0xe4d81
movq 0x198(%rsp), %rax
movq (%rax), %r15
leaq (%r15,%r14), %rdi
movq 0x60(%rsp), %rsi
callq 0xe3aee
movl %eax, %eax
movl 0xa0(%rsp,%rax,4), %ecx
incl %ecx
movl %ecx, 0xa0(%rsp,%rax,4)
cmpl 0x1a8(%rsp), %ecx
jne 0xe4d78
movl 0x24(%r15,%r14), %ecx
movl %ecx, 0x70(%rsp,%rax,4)
addq $0xa8, %r14
jmp 0xe4d36
movdqa 0x70(%rsp), %xmm0
pxor %xmm1, %xmm1
pcmpgtd %xmm1, %xmm0
movmskps %xmm0, %eax
testl %eax, %eax
je 0xe4e4d
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %ebx, %ebx
movl %r13d, %eax
cmpq %rax, %rbx
jae 0xe4e41
movq 0x198(%rsp), %rax
movq (%rax), %r12
leaq (%r12,%rbp), %r14
movq %r14, %rdi
movq 0x60(%rsp), %rsi
callq 0xe3aee
movl 0x24(%r12,%rbp), %ecx
movl %eax, %eax
cmpl 0x70(%rsp,%rax,4), %ecx
jl 0xe4e32
movl %r15d, %eax
cmpq %rax, %rbx
je 0xe4e2f
imulq $0xa8, %r15, %rax
addq %rax, %r12
movl $0xa8, %edx
leaq 0xb0(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x2160
movq 0x198(%rsp), %r14
movq (%r14), %rdi
addq %rbp, %rdi
movl $0xa8, %edx
movq %r13, %rsi
callq 0x2160
movl 0x10(%r14), %r13d
incq %r15
incq %rbx
addq $0xa8, %rbp
jmp 0xe4da1
movq 0x198(%rsp), %rax
movq %r15, 0x10(%rax)
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq