// File: /workspace/github_cpp_makefile_star2_O2/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, %r15d
movl %r8d, %r14d
movq %rdx, %rbx
movq %rsi, 0x10(%rsp)
movq %rdi, %r13
movq 0xf8(%rsp), %rax
movq 0xe8(%rsp), %r12
andq $0x0, (%rax)
movl %r9d, %edi
callq 0x15cdd5
movl %eax, %ebp
movl 0x24(%r12), %edi
callq 0x15cdd5
addl %eax, %ebp
movl %r14d, 0x8(%rsp)
xorl %r14d, %ebp
movl %ebp, %edi
callq 0x15cdd5
movq %rax, 0x28(%rsp)
movq 0xa8(%rsp), %rbp
andl $0x0, (%rbp)
subq $0x60, %rsp
movl 0x140(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0x118(%rsp), %r12
movq 0x110(%rsp), %rax
movq %rax, 0x40(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rsp)
movl 0x138(%rsp), %eax
movl %eax, 0x10(%rsp)
andq $0x0, 0x58(%rsp)
movq %r13, 0x8(%rsp)
orl $-0x1, 0x38(%rsp)
movq %rbp, (%rsp)
orl $-0x1, 0x30(%rsp)
movl $0x1, 0x50(%rsp)
andq $0x0, 0x28(%rsp)
movss 0x8cc28(%rip), %xmm0    # 0x1ecaa0
movq 0x100(%rsp), %rdi
movl %r15d, %esi
movl 0x120(%rsp), %edx
movl 0x128(%rsp), %ecx
xorl %r8d, %r8d
movl 0x130(%rsp), %r9d
callq 0xdf468
addq $0x60, %rsp
movq (%r12), %rdi
subq $0x8, %rsp
leaq 0x28(%rsp), %rax
leaq 0x20(%rsp), %rsi
leaq 0x24(%rsp), %r10
leaq 0x40(%rsp), %r14
pushq $0x1
popq %rdx
movq %r13, %rcx
movq 0x18(%rsp), %r8
movq %rbx, 0x18(%rsp)
movq %rbx, %r9
pushq %rax
pushq %rsi
pushq %r10
pushq %r14
pushq %rbp
callq 0x15ab72
addq $0x30, %rsp
movq (%r14), %r10
testq %r10, %r10
movq %r15, 0x30(%rsp)
je 0x15ffa8
leal (%r15,%r15), %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), %r14
movl %edi, %esi
pushq (%r12)
pushq %rbx
pushq %r14
pushq %rax
pushq %r10
pushq %r11
callq 0x15b287
addq $0x30, %rsp
movq %rax, %r13
movl (%r14), %r15d
xorl %r14d, %r14d
testl %r15d, %r15d
je 0x15ffd2
movq (%r12), %rdi
movq (%rsp), %rcx
movl %r15d, %edx
movq %r13, %r8
callq 0x15ba59
movq %rax, %rbx
movl 0x18(%rsp), %r12d
movq 0x20(%rsp), %rbp
testl %r15d, %r15d
cmovlel %r14d, %r15d
imulq $0x28, %r15, %r15
cmpq %r14, %r15
je 0x15ff9e
movl 0x4(%rbx,%r14), %edi
addl %edi, %edi
sarl %edi
movslq (%rbx,%r14), %rsi
shlq $0x4, %rsi
addq %r13, %rsi
movl %r12d, %edx
movq %rbp, %rcx
callq 0x15bc04
addq $0x28, %r14
jmp 0x15ff74
movq 0xb8(%rsp), %r12
jmp 0x15ffd4
movq 0xe8(%rsp), %rbp
testq %rax, %rax
je 0x15ffc1
movq (%r12), %rdi
movq %rax, %rsi
callq 0x1a38cf
andl $0x0, 0xc(%rsp)
andq $0x0, (%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
jmp 0x15ffdc
xorl %ebx, %ebx
movq 0xe8(%rsp), %rbp
movq (%r12), %rdi
movq 0x20(%rsp), %rsi
callq 0x1a38cf
movq (%r12), %rdi
movq %rsp, %rax
movq (%rax), %rsi
movq %rax, %r14
callq 0x1a38cf
movl 0x8(%rbp), %eax
movl 0xc(%rbp), %ecx
movq (%r12), %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 %r14, 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), %r15
movq %r15, %rsi
movq %rbx, %r8
movq 0x80(%rsp), %r14
movl %r14d, %r9d
callq 0x15d9c6
addq $0x50, %rsp
movq (%r12), %rsi
movq 0x18(%r15), %rdx
movq %rsp, %rcx
movq (%rcx), %r8
movl 0x40(%rbp), %r10d
movl 0x44(%rbp), %r11d
xorl %edi, %edi
movl %eax, %ecx
movq %rbx, %r9
pushq %r11
pushq %r10
pushq 0x38(%rsp)
pushq %r13
callq 0x15eccf
addq $0x20, %rsp
movq 0xf8(%rsp), %r15
movq %rax, (%r15)
movl 0x1c(%rsp), %ecx
movl %ecx, 0x14(%rax)
movl 0x8(%rsp), %ecx
movq %rcx, 0x30(%rax)
movslq %r14d, %rcx
movq %rcx, 0x38(%rax)
movq (%r12), %rdi
movq %r13, %rsi
callq 0x1a38cf
movq (%r12), %rdi
movq %rbx, %rsi
callq 0x1a38cf
movq (%r12), %rdi
movq %rsp, %rax
movq (%rax), %rsi
callq 0x1a38cf
movq (%r12), %rdi
movss 0x54(%rbp), %xmm0
movq (%r15), %rax
movl 0x8(%rax), %esi
movq 0x18(%rax), %rdx
xorl %r8d, %r8d
callq 0x15f52b
movss 0x58(%rbp), %xmm0
movl 0xc8(%rsp), %edi
addl %edi, %edi
movl 0x4c(%rbp), %esi
movq (%r15), %rax
movl 0x8(%rax), %edx
movq 0x18(%rax), %rcx
callq 0x15f7fe
movq (%r12), %rdi
movq (%r15), %rsi
callq 0x15f96e
movq (%r15), %rsi
movq 0xa8(%rsp), %rax
movl (%rax), %ecx
movl 0x44(%rbp), %r8d
movl %r14d, %edx
callq 0x15fbb5
movq (%r12), %rdi
testq %rdi, %rdi
je 0x160174
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x1a3b6e
movq 0x10(%rbx), %rax
cmpq 0x18(%rbx), %rax
jne 0x160183
cmpq $0x10000001, 0x60(%rsp) # imm = 0x10000001
jb 0x160174
movq (%r12), %rdi
callq 0x1a3894
callq 0x1a388b
movq %rax, (%r12)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xf2e56(%rip), %rax     # 0x252fe0
movq (%rax), %rdi
leaq 0x91f42(%rip), %rsi     # 0x1f20d6
leaq 0x91f56(%rip), %rdx     # 0x1f20f1
movl 0x8(%rsp), %ecx
xorl %eax, %eax
callq 0x22b0
callq 0x20f0