// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: extract_nccov(ma_hit_t*, uc_block_t*, asg_t const*, ul_ov_t*, unsigned int, long, long)
uint32_t extract_nccov(ma_hit_t *in, uc_block_t *rovlp, const asg_t *rg, ul_ov_t *res, uint32_t adjust_rev, int64_t min_ovlp, int64_t max_hang)
{
	uint64_t tn; int64_t os, oe, s_shift, e_shift, tt, qs, qe, ts, te; 
	tn = Get_tn((*in)); if(rg->seq[tn].del) return 0;	
	os = MAX(rovlp->ts, Get_qs((*in)));
	oe = MIN(rovlp->te, Get_qe((*in)));
	if(oe <= os) return 0;

	///[os, oe) -> rovlp->t*
	s_shift = get_offset_adjust(os-rovlp->ts, rovlp->te-rovlp->ts, rovlp->qe-rovlp->qs);
    e_shift = get_offset_adjust(rovlp->te-oe, rovlp->te-rovlp->ts, rovlp->qe-rovlp->qs);
	if(rovlp->rev) {
		tt = s_shift; s_shift = e_shift; e_shift = tt;
	}
	qs = rovlp->qs + s_shift; qe = ((int64_t)rovlp->qe)-e_shift; 
	if(qs >= qe) return 0;

	///[os, oe) -> in->q*
	s_shift = get_offset_adjust(os-Get_qs((*in)), Get_qe((*in))-Get_qs((*in)), Get_te((*in))-Get_ts((*in)));
	e_shift = get_offset_adjust(Get_qe((*in))-oe, Get_qe((*in))-Get_qs((*in)), Get_te((*in))-Get_ts((*in)));
	if(in->rev) {
		tt = s_shift; s_shift = e_shift; e_shift = tt;
	}
	ts = Get_ts((*in)) + s_shift; te = ((int64_t)Get_te((*in)))-e_shift;
	if(ts >= te) return 0;

	memset(res, 0, sizeof(*res));
	res->qn = 0; res->qs = qs; res->qe = qe; 
	res->tn = tn; res->ts = ts; res->te = te; 
	res->el = rovlp->el; res->rev = ((rovlp->rev == in->rev)?0:1);
	if(adjust_rev && res->rev) {///for linear chaining
		res->ts = rg->seq[tn].len - te;
		res->te = rg->seq[tn].len - ts;
	}

	// fprintf(stderr, "\n[M::%s::id->%u::%c] q::[%u, %u), t::[%u, %u)\n", 
	// 			__func__, rovlp->hid, "+-"[rovlp->rev], rovlp->qs, rovlp->qe, rovlp->ts, rovlp->te);
	// fprintf(stderr, "+[M::%s::] qn::%u, q::[%u, %u), %c, tn::%u, t::[%u, %u)\n", 
	// 			__func__, Get_qn((*in)), Get_qs((*in)), in->qe, "+-"[in->rev], in->tn, in->ts, in->te);
	// fprintf(stderr, "-[M::%s::] qn::%u, q::[%u, %u), %c, tn::%u, t::[%u, %u)\n", 
	// 			__func__, res->qn, res->qs, res->qe, "+-"[res->rev], res->tn, res->ts, res->te);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl 0xc(%rdi), %r9d
movq 0x20(%rdx), %r10
xorl %eax, %eax
cmpl $0x0, (%r10,%r9,8)
js 0x18d9a9
movl 0xc(%rsi), %r10d
movl 0x10(%rsi), %r15d
movl (%rdi), %r11d
movl 0x8(%rdi), %ebx
cmpl %r11d, %r10d
movl %r11d, %r14d
cmoval %r10d, %r14d
cmpl %ebx, %r15d
movl %ebx, %ebp
cmovbl %r15d, %ebp
cmpl %r14d, %ebp
jbe 0x18d9a9
movl %r8d, -0xc(%rsp)
movq %r9, -0x8(%rsp)
movl %r14d, %r13d
subl %r10d, %r13d
movl %r15d, %r9d
subl %r10d, %r9d
movl 0x4(%rsi), %r12d
movl 0x8(%rsi), %r8d
movl %r8d, %r10d
subl %r12d, %r10d
cvtsi2sd %r13, %xmm0
cvtsi2sd %r9, %xmm1
cvtsi2sd %r10, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r9
movl %r9d, %r9d
subl %ebp, %r15d
cvtsi2sd %r15, %xmm0
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r10
movl %r10d, %r15d
testb $0x20, 0x20(%rsi)
movq %r9, %r10
cmoveq %r15, %r10
cmoveq %r9, %r15
addq %r12, %r15
subq %r10, %r8
cmpq %r8, %r15
jge 0x18d9a9
subl %r11d, %r14d
movl %ebx, %r9d
subl %r11d, %r9d
movl 0x10(%rdi), %r12d
movl 0x14(%rdi), %r11d
movl %r11d, %r10d
subl %r12d, %r10d
cvtsi2sd %r14, %xmm0
cvtsi2sd %r9, %xmm1
cvtsi2sd %r10, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r9
movl %r9d, %r9d
subl %ebp, %ebx
cvtsi2sd %rbx, %xmm0
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %r10
movl %r10d, %ebx
testb $-0x80, 0x1b(%rdi)
movq %r9, %r10
cmoveq %rbx, %r10
cmoveq %r9, %rbx
addq %r12, %rbx
subq %r10, %r11
cmpq %r11, %rbx
jge 0x18d9a9
xorpd %xmm0, %xmm0
movupd %xmm0, (%rcx)
movupd %xmm0, 0xc(%rcx)
movl %r15d, 0x4(%rcx)
movl %r8d, 0x8(%rcx)
movq -0x8(%rsp), %r9
movl %r9d, 0xc(%rcx)
movl %ebx, 0x10(%rcx)
movl %r11d, 0x14(%rcx)
movl 0x20(%rsi), %eax
shll $0x17, %eax
andl $0x40000000, %eax       # imm = 0x40000000
movl %eax, 0x18(%rcx)
movl 0x20(%rsi), %esi
shrl $0x5, %esi
andl $0x1, %esi
movl 0x18(%rdi), %edi
shrl $0x1f, %edi
xorl %r8d, %r8d
cmpl %esi, %edi
setne %r8b
shll $0x1f, %r8d
orl %eax, %r8d
movl %r8d, 0x18(%rcx)
movl $0x1, %eax
cmpl $0x0, -0xc(%rsp)
je 0x18d9a9
cmpl %esi, %edi
je 0x18d9a9
movq 0x20(%rdx), %r8
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl (%r8,%r9,8), %edi
andl %esi, %edi
subl %r11d, %edi
movl %edi, 0x10(%rcx)
andl (%r8,%r9,8), %esi
subl %ebx, %esi
movl %esi, 0x14(%rcx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq