// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movl %r9d, %r15d
movq %rdx, %r12
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
movq 0x108(%rsp), %rdx
movq 0xf8(%rsp), %r9
movl 0xd8(%rsp), %ecx
movq 0xc8(%rsp), %r14
movq 0xb8(%rsp), %rbp
xorl %eax, %eax
movq %rax, (%rdx)
movl %r15d, %edx
shll $0xf, %edx
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
xorl %r11d, %edx
addl %r15d, %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 0xb0(%rsp), %rdi
addl %edx, %r9d
movl %r8d, 0x1c(%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, %ebx
shll $0xb, %ebx
xorl %r11d, %ebx
addl %esi, %ebx
movl %ebx, %edx
shrl $0x10, %edx
movq %rdx, 0x28(%rsp)
movl $0x0, (%rbp)
movl 0xd0(%rsp), %edx
movl 0xe0(%rsp), %r9d
movl 0xe8(%rsp), %esi
movq 0xc0(%rsp), %r8
movl 0xf0(%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 0x8fbbd(%rip), %xmm0    # 0x22e9a0
movl %r15d, %esi
xorl %r8d, %r8d
callq 0x11dbad
addq $0x60, %rsp
movq (%r14), %rdi
subq $0x8, %rsp
leaq 0x40(%rsp), %rax
leaq 0x38(%rsp), %rsi
leaq 0x3c(%rsp), %r10
leaq 0x50(%rsp), %r14
movl $0x1, %edx
movq %r13, %rcx
movq 0x28(%rsp), %r8
movq %r12, 0x48(%rsp)
movq %r12, %r9
pushq %rax
pushq %rsi
pushq %r10
pushq %r14
pushq %rbp
callq 0x1992c8
addq $0x30, %rsp
movq (%r14), %r10
testq %r10, %r10
je 0x19ee8f
leal (%r15,%r15), %edi
movq 0xf8(%rsp), %rbp
movl 0x8(%rbp), %edx
movl 0x28(%rbp), %ecx
movl 0x2c(%rbp), %r8d
movl 0x30(%rbp), %r9d
movl 0x34(%rbp), %r11d
movss 0x50(%rbp), %xmm0
leaq 0x10(%rsp), %r14
leaq 0xc(%rsp), %r12
movl %edi, %esi
movq 0xc8(%rsp), %r13
pushq (%r13)
pushq %r14
pushq %r12
pushq %rax
pushq %r10
pushq %r11
callq 0x199a53
addq $0x30, %rsp
movq %rax, %r12
movq %r13, %r14
jmp 0x19eec3
movq 0xf8(%rsp), %rbp
testq %rax, %rax
movq 0xc8(%rsp), %r14
je 0x19eeaf
movq (%r14), %rdi
movq %rax, %rsi
callq 0x1e3632
movl $0x0, 0xc(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r12d, %r12d
movq %r15, 0x20(%rsp)
movq 0x28(%rsp), %rax
xorl %ebx, %eax
movq %rax, 0x28(%rsp)
movl 0xc(%rsp), %edx
testl %edx, %edx
je 0x19ef35
movq %r14, %r13
movq (%r14), %rdi
movq 0x10(%rsp), %rcx
movq %r12, %r8
callq 0x19a3b0
cmpl $0x0, 0xc(%rsp)
jle 0x19ef3d
movq %rax, %r15
movq %rax, %rbx
addq $0x4, %rbx
xorl %r14d, %r14d
movl (%rbx), %edi
addl %edi, %edi
sarl %edi
movslq -0x4(%rbx), %rsi
shlq $0x4, %rsi
addq %r12, %rsi
movl 0x30(%rsp), %edx
movq 0x38(%rsp), %rcx
callq 0x19a54e
incq %r14
movslq 0xc(%rsp), %rax
addq $0x28, %rbx
cmpq %rax, %r14
jl 0x19ef03
jmp 0x19ef40
movq %r14, %r13
xorl %r15d, %r15d
jmp 0x19ef40
movq %rax, %r15
movq %r13, %r14
movq (%r13), %rdi
movq 0x38(%rsp), %rsi
callq 0x1e3632
movq (%r13), %rdi
leaq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %rax, %rbx
callq 0x1e3632
movl 0x8(%rbp), %eax
movl 0xc(%rbp), %ecx
movq (%r13), %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 0x90(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %r8
movq 0x70(%rsp), %r13
movl %r13d, %r9d
callq 0x19c53a
addq $0x50, %rsp
movq (%r14), %rsi
movq 0x18(%rbx), %rdx
leaq 0x10(%rsp), %rcx
movq (%rcx), %r8
movl 0x40(%rbp), %r10d
movl 0x44(%rbp), %r11d
xorl %edi, %edi
movl %eax, %ecx
movq %r15, %r9
pushq %r11
pushq %r10
pushq 0x38(%rsp)
pushq %r12
callq 0x19d9e0
addq $0x20, %rsp
movq 0x108(%rsp), %rbx
movq %rax, (%rbx)
movl 0x34(%rsp), %ecx
movl %ecx, 0x14(%rax)
movl 0x1c(%rsp), %ecx
movq %rcx, 0x30(%rax)
movslq %r13d, %rcx
movq %rcx, 0x38(%rax)
movq (%r14), %rdi
movq %r12, %rsi
callq 0x1e3632
movq (%r14), %rdi
movq %r15, %rsi
movq %r14, %r12
callq 0x1e3632
movq (%r14), %rdi
leaq 0x10(%rsp), %rax
movq (%rax), %rsi
callq 0x1e3632
movq (%r14), %rdi
movss 0x54(%rbp), %xmm0
movq (%rbx), %rax
movl 0x8(%rax), %esi
movq 0x18(%rax), %rdx
xorl %r8d, %r8d
callq 0x19e2f6
movss 0x58(%rbp), %xmm0
movl 0xd8(%rsp), %edi
addl %edi, %edi
movl 0x4c(%rbp), %esi
movq (%rbx), %rax
movl 0x8(%rax), %edx
movq 0x18(%rax), %rcx
callq 0x19e5fd
movq (%r14), %rdi
movq (%rbx), %rsi
callq 0x19e78e
movq (%rbx), %rsi
movq 0xb8(%rsp), %rax
movl (%rax), %ecx
movl 0x44(%rbp), %r8d
movl %r13d, %edx
callq 0x19ea5d
movq (%r14), %rdi
testq %rdi, %rdi
je 0x19f0dd
leaq 0x50(%rsp), %r14
movq %r14, %rsi
callq 0x1e38a2
movq 0x10(%r14), %rax
cmpq 0x18(%r14), %rax
jne 0x19f0ec
cmpq $0x10000001, 0x70(%rsp) # imm = 0x10000001
jb 0x19f0dd
movq (%r12), %rdi
callq 0x1e35f4
callq 0x1e35d3
movq %rax, (%r12)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x1c(%rsp), %edi
callq 0x29e98