// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: mg_map_frag(void const*, ha_pt_s const*, ma_ug_t const*, asg_t const*, unsigned int, int, char const*, ha_mzl_v*, st_mt_t*, mg_tbuf_s*, int, int, int, int, int, mg_idxopt_t const*, ug_opt_t const*, mg_gchains_t**)
void mg_map_frag(const void *ha_flt_tab, const ha_pt_t *ha_idx, const ma_ug_t *ug, const asg_t *rg, const uint32_t qid, const int qlen, const char *qseq, ha_mzl_v *mz, 
st_mt_t *sp, mg_tbuf_t *b, int32_t w, int32_t k, int32_t hpc, int32_t mz_sd, int32_t mz_rewin, const mg_idxopt_t *opt, const ug_opt_t *uopt, mg_gchains_t **gcs)
{
    mg128_t *a = NULL;
    int64_t n_a;
    int32_t *mini_pos;
    int i, rep_len, n_mini_pos, n_lc, max_chain_gap_qry, max_chain_gap_ref, n_gc;
	uint32_t hash;
    uint64_t *u;
	mg_lchain_t *lc;
	km_stat_t kmst;
	(*gcs) = NULL;

	hash  = qid;
    hash ^= __ac_Wang_hash(qlen) + __ac_Wang_hash(opt->seed);
    hash  = __ac_Wang_hash(hash);

    mz->n = 0;
    mz2_ha_sketch(qseq, qlen, w, k, 0, hpc, mz, ha_flt_tab, mz_sd, NULL, NULL, NULL, -1, -1, -1, sp, mz_rewin, 1, NULL);
	///a[]->y: weight(8)seg_id(8)flag(8)span(8)pos(32);--->query
    ///a[]->x: rid(31)rev(1)rpos(33);--->reference
    a = collect_seed_hits(b->km, opt, 1/**opt->hap_n**/, ha_flt_tab, ha_idx, ug, mz, &n_a, &rep_len, &n_mini_pos, &mini_pos);
	/**
	// might be recover
	if (opt->max_gap_pre > 0 && opt->max_gap_pre * 2 < opt->max_gap) n_a = flt_anchors(n_a, a, opt->max_gap_pre);
    max_chain_gap_qry = max_chain_gap_ref = opt->max_gap;
	**/
	max_chain_gap_qry = max_chain_gap_ref = qlen*2;
    if (n_a == 0) {//no matched minimizer
        if(a) kfree(b->km, a);
        a = 0, n_lc = 0, u = 0;
    } else {
		a = mg_lchain_dp(max_chain_gap_ref, max_chain_gap_qry, opt->bw, opt->max_lc_skip, opt->max_lc_iter, 
		opt->min_lc_cnt, opt->min_lc_score, opt->chn_pen_gap, n_a, a, &n_lc, &u, b->km);
    }
	
	if (n_lc) {///n_lc is how many linear chain we found
		lc = mg_lchain_gen(b->km, qlen, n_lc, u, a, ug);//lc->the status of each chain; u->idx of each chain;
		for (i = 0; i < n_lc; ++i)///update a[] since ref_id|rev has already been saved to lc[].v
			mg_update_anchors(lc[i].cnt, &a[lc[i].off], n_mini_pos, mini_pos);///update a[].x
	} else lc = 0;
	kfree(b->km, mini_pos); kfree(b->km, u);
	// fprintf(stderr, "++0++qid: %u, qlen: %d, n_a: %ld, n_lc: %d\n", qid, qlen, n_a, n_lc);
	/**
	 * up to here, a[] has been changed
	 * a[].x: idx_in_minimizer_arr(32)r_pos(32)
	 * a[].y: weight(8)query_id(8)flag(8)span(8)q_pos(32)
	**/
	// for (i = 0; i < n_lc; i++) {
	// 	mg_lchain_t *ri = &lc[i];
	// 	fprintf(stderr, "+0)))))))))))))))))))))))))))+\tA\tutg%.6d%c\t%c\tqs:%u\tqe:%u\tql:%d\tts:%u\tte:%u\ttl:%u\n", 
    //             (ri->v>>1)+1, "lc"[ug->u.a[ri->v>>1].circ], "+-"[ri->v&1], ri->qs, ri->qe, qlen, ri->rs, ri->re, ug->u.a[ri->v>>1].len);
	// }
	max_chain_gap_qry = max_chain_gap_ref = opt->max_gap;
	n_gc = mg_gchain1_dp(b->km, ug, rg, &n_lc, lc, qlen, max_chain_gap_ref, max_chain_gap_qry, opt->bw, opt->max_gc_skip, opt->ref_bonus,
						opt->chn_pen_gap, opt->mask_level, opt->max_gc_seq_ext, uopt, a, &u);
	// for (i = 0; i < n_lc; i++) {
	// 	mg_lchain_t *ri = &lc[i];
	// 	fprintf(stderr, "-0-\tA\tutg%.6d%c\t%c\tqs:%u\tqe:%u\tql:%d\tts:%u\tte:%u\ttl:%u\n", 
    //             (ri->v>>1)+1, "lc"[ug->u.a[ri->v>>1].circ], "+-"[ri->v&1], ri->qs, ri->qe, qlen, ri->rs, ri->re, ug->u.a[ri->v>>1].len);
	// }
	(*gcs) = mg_gchain_gen(0, b->km, ug->g, n_gc, u, lc, a, hash, opt->min_gc_cnt, opt->min_gc_score);
	(*gcs)->rep_len = rep_len; (*gcs)->qid = qid; (*gcs)->qlen = qlen;
	kfree(b->km, a);
	kfree(b->km, lc);
	kfree(b->km, u);

	mg_gchain_set_parent(b->km, opt->mask_level, (*gcs)->n_gc, (*gcs)->gc, opt->sub_diff, 0);
	mg_gchain_flt_sub(opt->pri_ratio, k * 2, opt->best_n, (*gcs)->n_gc, (*gcs)->gc);
	mg_gchain_drop_flt(b->km, (*gcs));
	mg_gchain_set_mapq(b->km, (*gcs), qlen, mz->n, opt->min_gc_score);

	if (b->km) {
		km_stat(b->km, &kmst);
		if (kmst.n_blocks != kmst.n_cores) {
			fprintf(stderr, "[E::%s] memory leak at %u\n", __func__, qid);
			abort();
		}
		if (kmst.largest > 1U<<28) {
			km_destroy(b->km);
			b->km = km_init();
		}
	}
	// fprintf(stderr, "++6++qid: %u, (*gcs)->n_gc: %d\n", qid, (*gcs)->n_gc);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, %r12d
movq %rdx, %r15
movq %rsi, 0x10(%rsp)
movq %rdi, %r13
movq 0xf8(%rsp), %rdx
movq 0xe8(%rsp), %r9
movl 0xc8(%rsp), %ecx
movq 0xb8(%rsp), %r14
movq 0xa8(%rsp), %rbp
xorl %eax, %eax
movq %rax, (%rdx)
movl %r12d, %edx
shll $0xf, %edx
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
xorl %r11d, %edx
addl %r12d, %edx
movl %edx, %esi
shrl $0xa, %esi
xorl %edx, %esi
leal (%rsi,%rsi,8), %edx
movl %edx, %esi
shrl $0x6, %esi
xorl %edx, %esi
movl %esi, %edi
shll $0xb, %edi
xorl %r11d, %edi
addl %esi, %edi
movl %edi, %edx
shrl $0x10, %edx
xorl %edi, %edx
movl 0x24(%r9), %esi
movl %esi, %edi
shll $0xf, %edi
xorl %r11d, %edi
addl %esi, %edi
movl %edi, %esi
shrl $0xa, %esi
xorl %edi, %esi
leal (%rsi,%rsi,8), %esi
movl %esi, %edi
shrl $0x6, %edi
xorl %esi, %edi
movl %edi, %esi
shll $0xb, %esi
xorl %r11d, %esi
addl %edi, %esi
movl %esi, %r9d
shrl $0x10, %r9d
xorl %esi, %r9d
movq 0xa0(%rsp), %rdi
addl %edx, %r9d
movl %r8d, 0x8(%rsp)
xorl %r8d, %r9d
movl %r9d, %edx
shll $0xf, %edx
xorl %r11d, %edx
addl %r9d, %edx
movl %edx, %esi
shrl $0xa, %esi
xorl %edx, %esi
leal (%rsi,%rsi,8), %edx
movl %edx, %esi
shrl $0x6, %esi
xorl %edx, %esi
movl %esi, %edx
shll $0xb, %edx
xorl %r11d, %edx
addl %esi, %edx
movl %edx, %esi
shrl $0x10, %esi
xorl %edx, %esi
movq %rsi, 0x28(%rsp)
movl 0xc0(%rsp), %edx
movl $0x0, (%rbp)
movl 0xd0(%rsp), %r9d
movl 0xd8(%rsp), %esi
movq 0xb0(%rsp), %r8
movl 0xe0(%rsp), %r10d
subq $0x60, %rsp
movq %rax, 0x58(%rsp)
movl %r10d, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movl %r11d, 0x38(%rsp)
movl %r11d, 0x30(%rsp)
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rsp)
movl %esi, 0x10(%rsp)
movq %r13, 0x8(%rsp)
movq %rbp, (%rsp)
movl $0x1, 0x50(%rsp)
movss 0x930df(%rip), %xmm0    # 0x23abb0
movl %r12d, %esi
xorl %r8d, %r8d
callq 0x123ac6
addq $0x60, %rsp
movq (%r14), %rdi
subq $0x8, %rsp
leaq 0x28(%rsp), %rax
leaq 0x20(%rsp), %rsi
leaq 0x24(%rsp), %r10
leaq 0x40(%rsp), %rbx
movl $0x1, %edx
movq %r13, %rcx
movq 0x18(%rsp), %r8
movq %r15, 0x18(%rsp)
movq %r15, %r9
pushq %rax
pushq %rsi
pushq %r10
pushq %rbx
pushq %rbp
callq 0x1a1f9d
addq $0x30, %rsp
movq (%rbx), %r10
testq %r10, %r10
movq %r12, 0x30(%rsp)
je 0x1a7bdf
leal (%r12,%r12), %edi
movq 0xe8(%rsp), %rsi
movl 0x8(%rsi), %edx
movl 0x28(%rsi), %ecx
movl 0x2c(%rsi), %r8d
movl 0x30(%rsi), %r9d
movl 0x34(%rsi), %r11d
movss 0x50(%rsi), %xmm0
movq %rsp, %rbx
leaq 0xc(%rsp), %r15
movl %edi, %esi
pushq (%r14)
pushq %rbx
pushq %r15
pushq %rax
pushq %r10
pushq %r11
callq 0x1a2737
addq $0x30, %rsp
movq %rax, %r12
movl (%r15), %ebp
testq %rbp, %rbp
je 0x1a7c02
movq (%r14), %rdi
movq (%rsp), %rcx
movl %ebp, %edx
movq %r12, %r8
callq 0x1a302f
movq %rax, %r13
testl %ebp, %ebp
jle 0x1a7c05
movl 0x18(%rsp), %ebx
movq 0x20(%rsp), %r15
shlq $0x3, %rbp
leaq (%rbp,%rbp,4), %rbp
xorl %r14d, %r14d
movl 0x4(%r13,%r14), %edi
addl %edi, %edi
sarl %edi
movslq (%r13,%r14), %rsi
shlq $0x4, %rsi
addq %r12, %rsi
movl %ebx, %edx
movq %r15, %rcx
callq 0x1a31e9
addq $0x28, %r14
cmpq %r14, %rbp
jne 0x1a7bad
movq 0xb8(%rsp), %r14
jmp 0x1a7c05
testq %rax, %rax
je 0x1a7bef
movq (%r14), %rdi
movq %rax, %rsi
callq 0x1ee04f
movl $0x0, 0xc(%rsp)
movq $0x0, (%rsp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq (%r14), %rdi
movq 0x20(%rsp), %rsi
callq 0x1ee04f
movq (%r14), %rdi
movq %rsp, %rax
movq (%rax), %rsi
movq %rax, %rbx
callq 0x1ee04f
movq 0xe8(%rsp), %rbp
movl 0x8(%rbp), %eax
movl 0xc(%rbp), %ecx
movq (%r14), %rdi
movl 0x38(%rbp), %edx
movl 0x3c(%rbp), %esi
movss 0x50(%rbp), %xmm0
movss 0x54(%rbp), %xmm1
movl 0x20(%rbp), %r8d
subq $0x50, %rsp
movq %rbx, 0x40(%rsp)
movl %r8d, 0x28(%rsp)
movl %esi, 0x20(%rsp)
movl %edx, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movl %ecx, 0x8(%rsp)
movl %ecx, (%rsp)
leaq 0x5c(%rsp), %rcx
movq 0x60(%rsp), %rbx
movq %rbx, %rsi
movq %r13, %r8
movq 0x80(%rsp), %r15
movl %r15d, %r9d
callq 0x1a521b
addq $0x50, %rsp
movq (%r14), %rsi
movq 0x18(%rbx), %rdx
movq %rsp, %rcx
movq (%rcx), %r8
movl 0x40(%rbp), %r10d
movl 0x44(%rbp), %r11d
xorl %edi, %edi
movl %eax, %ecx
movq %r13, %r9
pushq %r11
pushq %r10
pushq 0x38(%rsp)
pushq %r12
callq 0x1a6671
addq $0x20, %rsp
movq 0xf8(%rsp), %rbx
movq %rax, (%rbx)
movl 0x1c(%rsp), %ecx
movl %ecx, 0x14(%rax)
movl 0x8(%rsp), %ecx
movq %rcx, 0x30(%rax)
movslq %r15d, %rcx
movq %rcx, 0x38(%rax)
movq (%r14), %rdi
movq %r12, %rsi
callq 0x1ee04f
movq (%r14), %rdi
movq %r13, %rsi
callq 0x1ee04f
movq (%r14), %rdi
movq %rsp, %rax
movq (%rax), %rsi
callq 0x1ee04f
movq (%r14), %rdi
movss 0x54(%rbp), %xmm0
movq (%rbx), %rax
movl 0x8(%rax), %esi
movq 0x18(%rax), %rdx
xorl %r8d, %r8d
callq 0x1a6fa6
movss 0x58(%rbp), %xmm0
movl 0xc8(%rsp), %edi
addl %edi, %edi
movl 0x4c(%rbp), %esi
movq (%rbx), %rax
movl 0x8(%rax), %edx
movq 0x18(%rax), %rcx
callq 0x1a7309
movq (%r14), %rdi
movq (%rbx), %rsi
callq 0x1a7499
movq (%rbx), %rsi
movq 0xa8(%rsp), %rax
movl (%rax), %ecx
movl 0x44(%rbp), %r8d
movl %r15d, %edx
callq 0x1a7743
movq (%r14), %rdi
testq %rdi, %rdi
je 0x1a7d99
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x1ee2bf
movq 0x10(%rbx), %rax
cmpq 0x18(%rbx), %rax
jne 0x1a7da8
cmpq $0x10000001, 0x60(%rsp) # imm = 0x10000001
jb 0x1a7d99
movq (%r14), %rdi
callq 0x1ee011
callq 0x1edff7
movq %rax, (%r14)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x8(%rsp), %edi
callq 0x29316