// File: /workspace/github_cpp_makefile_star2_O1/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 $0xf8, %rsp
movsd %xmm0, 0x28(%rsp)
movq %r9, (%rsp)
movq %r8, %r15
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
movq 0x140(%rsp), %rdi
movq 0x138(%rsp), %rbp
callq 0x3fd1c
movq %rbp, %rdi
callq 0x3db37
leaq 0x18(%r13), %r14
movl $0x0, 0x18(%r13)
xorl %eax, %eax
movq %rax, (%r13)
leaq 0x166cf5(%rip), %rcx    # 0x28a758
movl 0xa0(%rcx), %r11d
movl 0xa8(%rcx), %r10d
movl 0x1f8(%rcx), %ebp
movss 0x10af21(%rip), %xmm0   # 0x22e9a0
movq %rbx, %rdi
movq %r12, 0x8(%rsp)
movl %r12d, %esi
movl %r15d, %edx
movq (%rsp), %rcx
xorl %r8d, %r8d
movl 0x130(%rsp), %r9d
pushq %rax
pushq $0x1
pushq %r11
pushq 0x1a0(%rsp)
pushq %rbp
pushq $-0x1
pushq %rax
pushq 0x1b8(%rsp)
pushq 0x198(%rsp)
pushq %r10
movq 0x1b8(%rsp), %rax
pushq %rax
movq %r14, 0x58(%rsp)
pushq %r14
callq 0x11dbad
addq $0x60, %rsp
movl 0x1c(%r13), %esi
cmpl 0x10(%r13), %esi
jbe 0x123af4
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 0x123b74
movq 0x170(%rsp), %r15
xorl %ebx, %ebx
leaq 0x50(%rsp), %r14
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x20(%r13), %rax
movq (%rax,%r12), %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x113a7b
movq 0x28(%r13), %rcx
movq %rax, 0x8(%rcx,%rbx)
movl 0x50(%rsp), %eax
movl %eax, (%rcx,%rbx)
movq 0x20(%r13), %rax
movq (%rax,%r12), %rsi
movq 0x168(%rsp), %rdi
callq 0x1149e5
movq 0x28(%r13), %rcx
movl %eax, 0x4(%rcx,%rbx)
movslq 0x50(%rsp), %rax
addq %rax, (%r13)
incq %rbp
movl 0x18(%r13), %eax
addq $0x18, %r12
addq $0x10, %rbx
cmpq %rax, %rbp
jb 0x123b17
movq (%r13), %rax
cmpq 0x8(%r13), %rax
jbe 0x123bdd
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)
movq (%rsp), %rax
movl (%rax), %eax
movq %rax, (%rsp)
testq %rax, %rax
movq 0x140(%rsp), %r12
je 0x123cd0
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
movq 0x20(%r13), %rax
movq %rax, 0x38(%rsp)
movq 0x28(%r13), %rdx
xorl %edi, %edi
leaq 0x2(%rcx), %r8
addq 0x8(%rsp), %rcx
xorl %r9d, %r9d
movq %rdx, 0x30(%rsp)
movq %rdi, %rax
shlq $0x4, %rax
movslq (%rdx,%rax), %r10
testq %r10, %r10
jle 0x123cc3
addq %rdx, %rax
leaq (%rdi,%rdi,2), %rdx
movq 0x38(%rsp), %rsi
leaq (%rsi,%rdx,8), %r11
movq 0x8(%rax), %rbx
movq 0x30(%r13), %rax
movq %r10, %r14
negq %r14
leaq (%r9,%r9,2), %rdx
leaq (%rax,%rdx,8), %r15
addq $0x10, %r15
xorl %ebp, %ebp
movq (%rbx), %r12
movq 0x8(%r11), %rdx
xorq %r12, %rdx
shrq $0x20, %r12
movl %r12d, -0x4(%r15)
movl 0xc(%r11), %esi
testl %edx, %edx
jns 0x123c85
movzbl 0x10(%r11), %eax
subq %rsi, %rax
addq %rcx, %rax
movq %rax, %rsi
movl %esi, -0x8(%r15)
movl %r10d, (%r15)
movq (%rbx), %rax
shlq $0x21, %rax
addq %rdx, %rdx
andq %r8, %rdx
orq %rax, %rdx
orq %r12, %rdx
movq %rdx, -0x10(%r15)
addq $0x10, %rbx
decq %rbp
addq $0x18, %r15
cmpq %rbp, %r14
jne 0x123c5d
subq %rbp, %r9
movq 0x140(%rsp), %r12
movq 0x30(%rsp), %rdx
incq %rdi
cmpq (%rsp), %rdi
jne 0x123c1d
movq (%r13), %rax
movq 0x30(%r13), %rdi
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x11fe18
movq (%r13), %rcx
testq %rcx, %rcx
je 0x123d50
movl $0x1, %ebx
movl $0x18, %r14d
xorl %eax, %eax
cmpq %rcx, %rbx
je 0x123d17
movq 0x30(%r13), %rcx
movq (%rcx,%r14), %rdx
leaq (%rax,%rax,2), %rsi
cmpq (%rcx,%rsi,8), %rdx
jne 0x123d17
movq %rax, %rdx
jmp 0x123d3d
movq %rbx, %rcx
subq %rax, %rcx
movq %rbx, %rdx
cmpq $0x2, %rcx
jb 0x123d3d
movq 0x30(%r13), %rsi
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,8), %rdi
addq %r14, %rsi
callq 0x1200ba
movq %rbx, %rdx
incq %rbx
movq (%r13), %rcx
addq $0x18, %r14
movq %rdx, %rax
cmpq %rcx, %rbx
jbe 0x123cfb
movq 0x8(%r13), %rsi
cmpq 0x10(%r12), %rsi
jb 0x123d71
movq %rsi, 0x10(%r12)
movq (%r12), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%r12)
movq 0x178(%rsp), %r10
movq 0x160(%rsp), %rdx
movl 0x150(%rsp), %eax
movl 0x148(%rsp), %ebx
movq (%r13), %r11
testq %r11, %r11
je 0x123e1c
xorl %ecx, %ecx
movl $0x80000000, %esi       # imm = 0x80000000
movl $0x10, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq (%r12), %r11
movq 0x30(%r13), %r14
movq (%r14,%r8), %r14
shrq $0x21, %r14
movl (%r11,%rcx), %ebp
andl %esi, %ebp
orl %r14d, %ebp
movl %ebp, (%r11,%rcx)
movq 0x30(%r13), %r15
movq (%r15,%r8), %r15
shrq %r15
andl %esi, %r15d
orl %r14d, %r15d
movl %r15d, (%r11,%rcx)
movq 0x30(%r13), %r14
movl 0xc(%r14,%r8), %ebp
movl %ebp, 0x4(%r11,%rcx)
movl 0x8(%r14,%r8), %ebp
movl %ebp, 0x8(%r11,%rcx)
movl 0x10(%r14,%r8), %ebp
cmpl $0x1, %ebp
cmovnel %edi, %ebp
movl %ebp, 0xc(%r11,%rcx)
incq %r9
movq (%r13), %r11
addq $0x18, %r8
addq $0x10, %rcx
cmpq %r11, %r9
jb 0x123dae
movq %r11, 0x8(%r12)
xorl %r11d, %r11d
movq %r12, %rdi
movq 0x138(%rsp), %r15
movq %r15, %rsi
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %r8
xorl %r9d, %r9d
movsd 0x28(%rsp), %xmm0
pushq %r11
pushq %r10
pushq %rax
pushq %r11
callq 0x3f993
addq $0x20, %rsp
movq 0x10(%r15), %rdi
cmpl %ebx, %edi
jle 0x123fe9
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x10(%rsp)
movq (%r15), %rsi
callq 0x121388
movl 0x10(%r15), %edx
testq %rdx, %rdx
je 0x123eea
movq (%r15), %rax
movq 0x8(%rsp), %rcx
decl %ecx
imulq $0xa8, %rdx, %rdx
xorl %esi, %esi
movl 0x4(%rax,%rsi), %r8d
movl 0x8(%rax,%rsi), %r9d
testl %r8d, %r8d
je 0x123ede
movl $0x3, %edi
cmpl %ecx, %r9d
jb 0x123eb4
xorl %edi, %edi
testl %r8d, %r8d
setne %dil
movl 0x40(%rsp,%rdi,4), %r8d
incl %r8d
movl %r8d, 0x40(%rsp,%rdi,4)
cmpl %ebx, %r8d
jne 0x123ed0
movl 0x24(%rax,%rsi), %r8d
movl %r8d, 0x10(%rsp,%rdi,4)
addq $0xa8, %rsi
cmpq %rsi, %rdx
jne 0x123e92
jmp 0x123eea
movl $0x2, %edi
cmpl %ecx, %r9d
jne 0x123eab
jmp 0x123eb4
cmpl $0x0, 0x10(%rsp)
jg 0x123f0a
cmpl $0x0, 0x14(%rsp)
jg 0x123f0a
cmpl $0x0, 0x18(%rsp)
jg 0x123f0a
cmpl $0x0, 0x1c(%rsp)
jle 0x123fe9
cmpl $0x0, 0x10(%r15)
je 0x123fe2
movq 0x8(%rsp), %rax
leal -0x1(%rax), %r8d
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
movq (%r15), %rax
movl 0x4(%rax,%r12), %edx
movl 0x8(%rax,%r12), %esi
testl %edx, %edx
je 0x123fcf
movl $0x3, %ecx
cmpl %r8d, %esi
jb 0x123f4d
xorl %ecx, %ecx
testl %edx, %edx
setne %cl
movl 0x24(%rax,%r12), %edx
cmpl 0x10(%rsp,%rcx,4), %edx
jl 0x123fb6
movl %r13d, %ecx
cmpq %rcx, %r14
je 0x123fb3
leaq (%rax,%r12), %rbx
imulq $0xa8, %r13, %r15
addq %rax, %r15
movl $0xa8, %edx
leaq 0x50(%rsp), %rdi
movl %r8d, %ebp
movq %r15, %rsi
callq 0x2160
movl $0xa8, %edx
movq %r15, %rdi
movq 0x138(%rsp), %r15
movq %rbx, %rsi
callq 0x2160
movq (%r15), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0x50(%rsp), %rsi
callq 0x2160
movl %ebp, %r8d
incq %r13
incq %r14
movl 0x10(%r15), %eax
addq $0xa8, %r12
cmpq %rax, %r14
jb 0x123f27
jmp 0x123fe5
movl $0x2, %ecx
cmpl %r8d, %esi
jne 0x123f46
jmp 0x123f4d
xorl %r13d, %r13d
movq %r13, 0x10(%r15)
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq