// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/./Overlaps.h
// Function: ma_hit2arc(ma_hit_t const*, int, int, int, float, int, asg_arc_t*)
static inline int ma_hit2arc(const ma_hit_t *h, int ql, int tl, int max_hang, float int_frac, int min_ovlp, asg_arc_t *p)
{
	int32_t tl5, tl3, ext5, ext3, qs = (int32_t)h->qns;
	uint32_t u, v, l; // u: query end; v: target end; l: length from u to v

	///if query and target are in different strand
	if (h->rev) tl5 = tl - h->te, tl3 = h->ts; // tl5: 5'-end overhang (on the query strand); tl3: similar
	else tl5 = h->ts, tl3 = tl - h->te;

	///ext5 and ext3 is the hang on left side and right side, respectively
	ext5 = qs < tl5? qs : tl5;
	ext3 = ql - (int)h->qe < tl3? ql - (int)h->qe : tl3;


	/**
	if (ext5 > max_hang || ext3 > max_hang || h->qe - qs < (h->qe - qs + ext5 + ext3) * int_frac)
		return MA_HT_INT;
	**/
	///ext3 and ext5 should be always 0
	if (ext5 > max_hang || ext3 > max_hang 
	|| h->qe - qs < (h->qe - qs + ext5 + ext3) * int_frac
	|| h->te - h->ts < (h->te - h->ts + ext5 + ext3) * int_frac)
	{
		return MA_HT_INT;
	}
		
	/**
	********************************query-to-target overlap****************************
	case 1: u = 0, rev = 0                           in the view of target: direction is 1 
	query: CCCCCCCCTAATTAAAAT                        target: TAATTAAAATGGGGGG (use ex-target as query)
	               ||||||||||         <--->                  ||||||||||
	       target: TAATTAAAATGGGGGG           query: CCCCCCCCTAATTAAAAT (use ex-query as target)

	case 2: u = 0, rev = 1                           in the view of target: direction is 0
	query: CCCCCCCCTAATTAAAAT					     target: CCCCCCATTTTAATTA  (use ex-target as query)
                   ||||||||||        <--->                         ||||||||||
	       target: TAATTAAAATGGGGGG                         query: ATTTTAATTAGGGGGGGG  (use ex-query as target)
	********************************query-to-target overlap****************************

	********************************target-to-query overlap****************************
	case 3: u = 1, rev = 0                           in the view of target: direction is 0
			 query: AAATAATATCCCCCCGCG                target: GGGCCGGCAAATAATAT (use ex-target as query)
					|||||||||          <--->                          |||||||||
	target: GGGCCGGCAAATAATAT                                  query: AAATAATATCCCCCCGCG (use ex-query as target)

	case 4: u = 1, rev = 1                          in the view of target: direction is 1
	         query: AAATAATATCCCCCCGCG                        target: ATATTATTTGCCGGCCC (use ex-target as query)
                    |||||||||             <--->                       |||||||||
	target: GGGCCGGCAAATAATAT                          query: CGCGGGGGATATTATTT (use ex-query as target)
	********************************target-to-query overlap****************************
	**/

	if (qs <= tl5 && ql - (int)h->qe <= tl3) return MA_HT_QCONT; // query contained in target
	else if (qs >= tl5 && ql - (int)h->qe >= tl3) return MA_HT_TCONT; // target contained in query 
	else if (qs > tl5) u = 0, v = !!h->rev, l = qs - tl5; ///u = 0 means query-to-target overlap, l is the length of node in string graph (not the overlap length)
	else u = 1, v = !h->rev, l = (ql - h->qe) - tl3; ///u = 1 means target-to-query overlaps, l is the length of node in string graph (not the overlap length)
	if ((int)h->qe - qs + ext5 + ext3 < min_ovlp || (int)h->te - (int)h->ts + ext5 + ext3 < min_ovlp) return MA_HT_SHORT_OVLP; // short overlap
	///u = 0 / 1 means query-to-target / target-to-query overlaps, 
	///l is the length of node in string graph (not the overlap length between two reads)
	u |= h->qns>>32<<1, v |= h->tn<<1;
	/**
	p->ul: |____________31__________|__________1___________|______________32_____________|
	                    qn            direction of overlap       length of this node (not overlap length)
						                (in the view of query)
	p->v : |___________31___________|__________1___________|
				        tn             reverse direction of overlap
						              (in the view of target)
	p->ol: overlap length
	**/
	p->ul = (uint64_t)u<<32 | l, p->v = v, p->ol = ql - l, p->del = 0;
	///l is the length of node in string graph (not the overlap length)

	p->strong = h->ml;
	p->el = h->el;
	p->no_l_indel = h->no_l_indel;
	return l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %r9, -0x8(%rsp)
movq (%rdi), %r10
movq 0x18(%rdi), %r9
testl %r9d, %r9d
js 0x908b
movl 0x10(%rdi), %r13d
movl 0x14(%rdi), %r15d
subl %r15d, %edx
movl %edx, %ebx
movl %r13d, %edx
jmp 0x9099
movl 0x10(%rdi), %r13d
movl 0x14(%rdi), %r15d
subl %r15d, %edx
movl %r13d, %ebx
cmpl %r10d, %edx
movl %r10d, %r11d
cmovll %edx, %r11d
movl 0x8(%rdi), %ebp
movl %esi, %r12d
subl %ebp, %r12d
cmpl %ebx, %r12d
movl %ebx, %r14d
cmovll %r12d, %r14d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %r11d
jg 0x91da
cmpl %ecx, %r14d
jg 0x91da
movl %ebp, %ecx
subl %r10d, %ecx
cvtsi2ss %rcx, %xmm1
addl %r11d, %ecx
addl %r14d, %ecx
cvtsi2ss %rcx, %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0x91da
subl %r13d, %r15d
cvtsi2ss %r15, %xmm1
addl %r11d, %r14d
addl %r15d, %r14d
cvtsi2ss %r14, %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0x91da
cmpl %edx, %r10d
jg 0x9123
cmpl %ebx, %r12d
jg 0x9123
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x91da
cmpl %edx, %r10d
jl 0x9137
cmpl %ebx, %r12d
jl 0x9137
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
jmp 0x91da
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
cmpl %r8d, %ecx
jl 0x91da
cmpl %r8d, %r14d
jl 0x91da
testl %r9d, %r9d
setns %cl
addl %ebp, %ebx
movl %esi, %r8d
subl %ebx, %r8d
xorl %r9d, %r9d
movl %r10d, %eax
subl %edx, %eax
setle %r9b
setg %dl
cmovlel %r8d, %eax
xorb %cl, %dl
movzbl %dl, %ecx
shrq $0x1f, %r10
andl $-0x2, %r10d
orl %r10d, %r9d
movl 0xc(%rdi), %edx
leal (%rcx,%rdx,2), %ecx
shlq $0x20, %r9
leaq (%rax,%r9), %rdx
movq -0x8(%rsp), %r8
movq %rdx, (%r8)
movl %ecx, 0x8(%r8)
subl %eax, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl %esi, 0xc(%r8)
movzwl 0x1a(%rdi), %ecx
andl $0x4000, %ecx           # imm = 0x4000
movl $0xffffbfff, %edx       # imm = 0xFFFFBFFF
andl 0x10(%r8), %edx
orl %ecx, %edx
movw %dx, 0x10(%r8)
movb 0x20(%rdi), %cl
movb %cl, 0x12(%r8)
movzbl 0x21(%rdi), %ecx
shll $0xf, %ecx
andl $0x7fff, %edx           # imm = 0x7FFF
orl %ecx, %edx
movw %dx, 0x10(%r8)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq