// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/tovlp.cpp
// Function: get_utg_trans_hit(utg_trans_hit_idx*, utg_thit_t*)
uint32_t get_utg_trans_hit(utg_trans_hit_idx *t, utg_thit_t *hit)
{
    uint32_t r_beg, r_end, ovlp;
    uint32_t k, l, qn, v, w, nv;
    uint32_t *a = t->a, a_n = t->an;
    asg_arc_t *av  = NULL;
    hit->qs = hit->qe = hit->qn = hit->qus = hit->que = (uint32_t)-1;
    hit->ts = hit->te = hit->tn = hit->tus = hit->tue = (uint32_t)-1;
    
    while (t->ui < a_n)
    {
        qn = a[t->ui];
        l = t->ug->g->seq[qn>>1].len;
        if(t->ui + 1 < a_n)
        {
            v = a[t->ui]; w = a[t->ui+1];
            av = asg_arc_a(t->ug->g, v);
            nv = asg_arc_n(t->ug->g, v);
            for (k = 0; k < nv; k++)
            {
                if(av[k].del) continue;
                if(av[k].v == w) 
                {
                    l = asg_arc_len(av[k]);
                    break;
                }
            }
            if(k >= nv) fprintf(stderr, "ERROR-mc-1\n");
        }

        r_beg = t->len; r_end = t->len + t->ug->g->seq[qn>>1].len;
        t->len += l; t->ui++;

        ovlp = ((MIN(t->cEnd, r_end) > MAX(t->cBeg, r_beg))? (MIN(t->cEnd, r_end) - MAX(t->cBeg, r_beg)) : 0);
        if(ovlp == 0 && r_beg >= t->cEnd) return 0;
        if(ovlp == 0) continue;

        hit->qn = qn; hit->qs = r_beg; hit->qe = r_end; hit->qus = 0; hit->que = t->ug->g->seq[qn>>1].len;
        if(hit->qs < t->cBeg)
        {
            hit->qus += (t->cBeg - hit->qs);
            hit->qs = t->cBeg;
        } 
        
        if(hit->qe > t->cEnd)
        {
            hit->que -= (hit->qe - t->cEnd);
            hit->qe = t->cEnd;
        } 
        
        return 1;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r15
movq 0x10(%rdi), %r13
movl 0x18(%rdi), %ebp
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x10(%rsi)
movdqu %xmm0, (%rsi)
movq %rsi, (%rsp)
movq $-0x1, 0x20(%rsi)
movl (%rdi), %r11d
xorl %eax, %eax
cmpl %ebp, %r11d
jae 0x197814
movl %r11d, %ecx
movl (%r13,%rcx,4), %ebx
movq 0x20(%r15), %rcx
movq 0x18(%rcx), %rdx
movq 0x20(%rdx), %rcx
movl %ebx, %r14d
shrl %r14d
movl (%rcx,%r14,8), %r12d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %r12d
incl %r11d
cmpl %ebp, %r11d
jae 0x19777f
movq 0x28(%rdx), %rsi
movq (%rsi,%rbx,8), %rdi
testl %edi, %edi
je 0x19773e
movl %r11d, %esi
movl (%r13,%rsi,4), %esi
movq 0x8(%rdx), %rdx
movq %rdi, %r8
shrq $0x20, %r8
movl %edi, %r10d
leaq (%r8,%r8,2), %rdi
leaq (%rdx,%rdi,8), %r9
addq $0xc, %r9
shlq $0x3, %r10
leaq (%r10,%r10,2), %r10
xorl %r8d, %r8d
cmpl $0x0, (%r9,%r8)
js 0x197735
cmpl %esi, -0x4(%r9,%r8)
je 0x197784
addq $0x18, %r8
cmpq %r8, %r10
jne 0x197727
movq 0xfd89b(%rip), %rax     # 0x294fe0
movq (%rax), %rcx
movl $0xb, %esi
movl $0x1, %edx
leaq 0xa8211(%rip), %rdi     # 0x23f96a
callq 0x2410
xorl %eax, %eax
movq 0x20(%r15), %rcx
movq 0x18(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl (%r15), %r11d
movl (%rcx,%r14,8), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
incl %r11d
jmp 0x19778f
movl %r12d, %edx
jmp 0x19778f
leaq (%rdx,%rdi,8), %rsi
movl %r12d, %edx
movl (%r8,%rsi), %r12d
movl 0x4(%r15), %r8d
movl 0x8(%r15), %edi
addl %r8d, %edx
addl %r8d, %r12d
movl %r12d, 0x4(%r15)
movl %r11d, (%r15)
movl 0xc(%r15), %esi
cmpl %edx, %esi
movl %edx, %r9d
cmovbl %esi, %r9d
cmpl %r8d, %edi
movl %r8d, %r10d
cmoval %edi, %r10d
cmpl %r10d, %r9d
ja 0x1977cb
cmpl %esi, %r8d
jb 0x1976b3
jmp 0x197814
movq (%rsp), %r9
movl %ebx, 0x8(%r9)
movl %r8d, (%r9)
movl %edx, 0x4(%r9)
movl $0x0, 0xc(%r9)
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andl (%rcx,%r14,8), %r10d
movl %r10d, 0x10(%r9)
movl %edi, %ecx
subl %r8d, %ecx
jbe 0x1977fe
movl %ecx, 0xc(%r9)
movl %edi, (%r9)
movl $0x1, %eax
movl %esi, %ecx
subl %edx, %ecx
jae 0x197814
addl %r10d, %ecx
movl %ecx, 0x10(%r9)
movl %esi, 0x4(%r9)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq