// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_u_trans_hit(u_trans_hit_idx*, u_trans_hit_t*)
uint32_t get_u_trans_hit(u_trans_hit_idx *t, u_trans_hit_t *hit)
{
    uint32_t uid, ori, l, v, nv, is_update, r_beg, r_end, ovlp;
    uint32_t *a = t->a, a_n = t->an, k, c_uId, idx, offPre;
    ma_utg_t *u = NULL;
    asg_arc_t *av  = NULL;
    hit->qSpre = hit->qEpre = hit->qScur = hit->qEcur = hit->qn = (uint32_t)-1;
    hit->tSpre = hit->tEpre = hit->tScur = hit->tEcur = hit->tn = (uint32_t)-1;

    while (t->u_i < a_n) ///(u_i = 0; u_i < a_n; u_i++)
    {
        uid = a[t->u_i] >> 1;
        ori = a[t->u_i] & 1;
        u = &(t->ug->u.a[uid]);
        if(u->n == 0) continue;
        
        while(t->r_i < u->n) ///for (r_i = 0; r_i < u->n; r_i++)
        {
            l = 0;
            v = (ori == 1?((uint64_t)((u->a[u->n-t->r_i-1])^(uint64_t)(0x100000000)))>>32:((uint64_t)(u->a[t->r_i]))>>32);

            if(t->p_v != (uint32_t)-1)
            {
                av = asg_arc_a(t->read_sg, t->p_v);
                nv = asg_arc_n(t->read_sg, t->p_v);
                
                for (k = 0; k < nv; k++)
                {
                    if(av[k].del) continue;
                    if(av[k].v == v) 
                    {
                        l = asg_arc_len(av[k]);
                        break;
                    }
                }
                if(k == nv) fprintf(stderr, "ERROR-nv\n");
            }

            ///[t->cBeg, t->cEnd)
            r_beg = t->len; r_end = t->len + 
                        (t->p_v != (uint32_t)-1? t->read_sg->seq[t->p_v>>1].len : 0);
            ovlp = ((MIN(t->cEnd, r_end) > MAX(t->cBeg, r_beg))? (MIN(t->cEnd, r_end) - MAX(t->cBeg, r_beg)) : 0);
            c_uId = get_origin_uid(v, t->t_ch, &offPre, &idx);


            if(ovlp == 0)
            {
                if(r_beg >= t->cEnd)
                {
                    if(t->p_uId != (uint32_t)-1)
                    {
                        hit->qn = t->p_uId;
                        hit->qScur = t->s_pos_cur; hit->qEcur = t->len + t->read_sg->seq[t->p_v>>1].len;


                        ovlp = ((MIN(t->cEnd, hit->qEcur) > MAX(t->cBeg, hit->qScur))? 
                                                (MIN(t->cEnd, hit->qEcur) - MAX(t->cBeg, hit->qScur)) : 0);
                        if(ovlp == 0) return 0;

                        
                        ///[t->s_pre_v, t->p_v]
                        uint32_t a_pos, b_pos;
                        get_origin_uid(t->s_pre_v, t->t_ch, &a_pos, NULL);
                        get_origin_uid(t->p_v, t->t_ch, &b_pos, NULL);
                        hit->qSpre = MIN(a_pos, b_pos);
                        hit->qEpre = MAX((a_pos + t->read_sg->seq[t->s_pre_v>>1].len), (b_pos+t->read_sg->seq[t->p_v>>1].len));
                        
                        ///[t->cBeg, t->cEnd)
                        if(hit->qScur < t->cBeg)
                        {
                            if((hit->qn&1) == 0)
                            {
                                ///hit->qSpre += (t->cBeg - hit->qScur);
                                hit->qSpre += get_offset_adjust(t->cBeg - hit->qScur, 
                                                    hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
                            }
                            else
                            {
                                ///hit->qEpre -= (t->cBeg - hit->qScur);
                                hit->qEpre -= get_offset_adjust(t->cBeg - hit->qScur, 
                                                    hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
                            }
                            hit->qScur = t->cBeg;
                        }

                        if(hit->qEcur > t->cEnd)
                        {
                            if((hit->qn&1) == 0)
                            {
                                ///hit->qEpre -= (hit->qEcur - t->cEnd);
                                hit->qEpre -= get_offset_adjust(hit->qEcur - t->cEnd,
                                                    hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
                            }
                            else
                            {
                                // hit->qSpre += (hit->qEcur - t->cEnd);
                                hit->qSpre += get_offset_adjust(hit->qEcur - t->cEnd,
                                                    hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
                            }
                            hit->qEcur = t->cEnd;
                        }
                        t->p_uId = (uint32_t)-1;
                        return 1;
                    }
                    return 0;
                }
                else
                {
                    t->p_uId = c_uId; t->s_pos_cur = t->len + l; t->s_pre_v = v;
                    t->p_v = v; t->len += l; t->p_idx = idx;
                    t->r_i++;
                    continue;
                }
            }
            

            is_update = 0;
            if(t->p_uId != c_uId)
            {
                is_update = 1;
                
            }
            else///p_uId == c_uId
            {
                if((t->p_uId&1) == 0) ///forward
                {
                    if(idx != (t->p_idx+1))
                    {
                        is_update = 1;
                    }
                }
                else //backward
                {
                    if((idx + 1) != t->p_idx)
                    {
                        is_update = 1;
                    }
                }
            } 
            
            if(is_update)
            {
                if(t->p_uId != (uint32_t)-1)
                {                    
                    hit->qn = t->p_uId;
                    hit->qScur = t->s_pos_cur; hit->qEcur = t->len + t->read_sg->seq[t->p_v>>1].len;
                    ///[t->s_pre_v, t->p_v]
                    uint32_t a_pos, b_pos;
                    get_origin_uid(t->s_pre_v, t->t_ch, &a_pos, NULL);
                    get_origin_uid(t->p_v, t->t_ch, &b_pos, NULL);
                    hit->qSpre = MIN(a_pos, b_pos);
                    hit->qEpre = MAX((a_pos + t->read_sg->seq[t->s_pre_v>>1].len), (b_pos+t->read_sg->seq[t->p_v>>1].len));
                    
                    ///[t->cBeg, t->cEnd)
                    if(hit->qScur < t->cBeg)
                    {
                        if((hit->qn&1) == 0)
                        {
                            ///hit->qSpre += (t->cBeg - hit->qScur);
                            hit->qSpre += get_offset_adjust(t->cBeg - hit->qScur, 
                                                hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
                        }
                        else
                        {
                            ///hit->qEpre -= (t->cBeg - hit->qScur);
                            hit->qEpre -= get_offset_adjust(t->cBeg - hit->qScur, 
                                                hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
                        }
                        hit->qScur = t->cBeg;
                    }

                    if(hit->qEcur > t->cEnd)
                    {
                        if((hit->qn&1) == 0)
                        {
                            ///hit->qEpre -= (hit->qEcur - t->cEnd);
                            hit->qEpre -= get_offset_adjust(hit->qEcur - t->cEnd,
                                                hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
                        }
                        else
                        {
                            // hit->qSpre += (hit->qEcur - t->cEnd);
                            hit->qSpre += get_offset_adjust(hit->qEcur - t->cEnd,
                                                hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
                        }
                        hit->qEcur = t->cEnd;
                    }


                    t->p_uId = c_uId; t->s_pos_cur = t->len + l; t->s_pre_v = v;
                    t->p_v = v; t->len += l; t->p_idx = idx;
                    t->r_i++;
                    return 1;
                }
                t->p_uId = c_uId; t->s_pos_cur = t->len + l; t->s_pre_v = v;
            }

            t->p_v = v; t->len += l; t->p_idx = idx;
            t->r_i++;
        }
        t->r_i = 0;
        t->u_i++;
    }
    
    if(t->p_uId != (uint32_t)-1)
    {
        hit->qn = t->p_uId;
        hit->qScur = t->s_pos_cur; hit->qEcur = t->len + t->read_sg->seq[t->p_v>>1].len;


        ovlp = ((MIN(t->cEnd, hit->qEcur) > MAX(t->cBeg, hit->qScur))? 
                                (MIN(t->cEnd, hit->qEcur) - MAX(t->cBeg, hit->qScur)) : 0);
        if(ovlp == 0) return 0;

        
        ///[t->s_pre_v, t->p_v]
        uint32_t a_pos, b_pos;
        get_origin_uid(t->s_pre_v, t->t_ch, &a_pos, NULL);
        get_origin_uid(t->p_v, t->t_ch, &b_pos, NULL);
        hit->qSpre = MIN(a_pos, b_pos);
        hit->qEpre = MAX((a_pos + t->read_sg->seq[t->s_pre_v>>1].len), (b_pos+t->read_sg->seq[t->p_v>>1].len));
        
        ///[t->cBeg, t->cEnd)
        if(hit->qScur < t->cBeg)
        {
            if((hit->qn&1) == 0)
            {
                ///hit->qSpre += (t->cBeg - hit->qScur);
                hit->qSpre += get_offset_adjust(t->cBeg - hit->qScur, 
                                    hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
            }
            else
            {
                ///hit->qEpre -= (t->cBeg - hit->qScur);
                hit->qEpre -= get_offset_adjust(t->cBeg - hit->qScur, 
                                    hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
            }
            hit->qScur = t->cBeg;
        }

        if(hit->qEcur > t->cEnd)
        {
            if((hit->qn&1) == 0)
            {
                ///hit->qEpre -= (hit->qEcur - t->cEnd);
                hit->qEpre -= get_offset_adjust(hit->qEcur - t->cEnd,
                                    hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
            }
            else
            {
                // hit->qSpre += (hit->qEcur - t->cEnd);
                hit->qSpre += get_offset_adjust(hit->qEcur - t->cEnd,
                                    hit->qEcur-hit->qScur, hit->qEpre-hit->qSpre);
            }
            hit->qEcur = t->cEnd;
        }

        t->p_uId = (uint32_t)-1;
        return 1;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r8
movq 0x30(%rdi), %rdi
movl 0x38(%r8), %r9d
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x10(%rsi)
movdqu %xmm0, (%rsi)
movq %rsi, 0x18(%rsp)
orq $-0x1, 0x20(%rsi)
movl (%r8), %eax
movq %rdi, 0x20(%rsp)
movl %r9d, 0x30(%rsp)
movl %eax, %ecx
cmpl %r9d, %ecx
jae 0x81f43
movl (%rdi,%rcx,4), %esi
movl %esi, %eax
shrl %eax
movq 0x40(%r8), %rdx
movq 0x10(%rdx), %r13
imulq $0x28, %rax, %rdx
movl 0x10(%r13,%rdx), %eax
testl %eax, %eax
je 0x81d34
addq %rdx, %r13
movl 0x4(%r8), %ecx
movl %esi, 0x2c(%rsp)
cmpl %eax, %ecx
jae 0x81f31
movq 0x18(%r13), %rdx
testb $0x1, %sil
jne 0x81d7f
movl %ecx, %eax
movl 0x4(%rdx,%rax,8), %r15d
jmp 0x81d8c
notl %ecx
addl %ecx, %eax
movl 0x4(%rdx,%rax,8), %r15d
xorq $0x1, %r15
movl 0x18(%r8), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x81e0d
movq 0x48(%r8), %rcx
movq 0x8(%rcx), %rsi
movq 0x28(%rcx), %rcx
movq (%rcx,%rax,8), %rcx
movq %rcx, %rdi
shrq $0x20, %rdi
movl %ecx, %edx
imulq $0x18, %rdi, %rdi
addq %rdi, %rsi
addq $0xc, %rsi
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x81eff
cmpl $0x0, (%rsi)
js 0x81dd4
cmpl %r15d, -0x4(%rsi)
je 0x81ddd
incq %rdi
addq $0x18, %rsi
jmp 0x81dc0
movl -0xc(%rsi), %edx
movl %edx, 0x8(%rsp)
cmpl %edi, %ecx
je 0x81f07
movl 0x8(%r8), %ebp
cmpl $-0x1, %eax
je 0x81e19
movq 0x48(%r8), %rcx
movq 0x20(%rcx), %rcx
shrl %eax
movl (%rcx,%rax,8), %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %r14d
jmp 0x81e1c
movl 0x8(%r8), %ebp
movl $0x0, 0x8(%rsp)
xorl %r14d, %r14d
addl %ebp, %r14d
movl 0x24(%r8), %ebx
movl 0x28(%r8), %eax
cmpl %r14d, %eax
cmovbl %eax, %r14d
cmpl %ebp, %ebx
cmovbel %ebp, %ebx
movq 0x50(%r8), %rsi
movl %r15d, %edi
leaq 0x34(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %r8, %r12
callq 0xc31da
cmpl %ebx, %r14d
jbe 0x81e78
movl 0x20(%r12), %ecx
cmpl %eax, %ecx
movq %r12, %r8
movl 0x8(%rsp), %ebp
jne 0x81eb3
movl 0xc(%rsp), %esi
testb $0x1, %al
jne 0x81eaa
movl 0x1c(%r8), %ecx
incl %ecx
cmpl %ecx, %esi
je 0x81ed2
jmp 0x8210f
movl 0x28(%r12), %ecx
cmpl %ecx, %ebp
movq %r12, %r8
jae 0x82054
movl %eax, 0x20(%r8)
movl 0x8(%rsp), %eax
addl 0x8(%r8), %eax
movl %eax, 0xc(%r8)
movl %r15d, 0x10(%r8)
movl %r15d, 0x18(%r8)
movl %eax, 0x8(%r8)
movl 0xc(%rsp), %esi
jmp 0x81eda
leal 0x1(%rsi), %edx
cmpl 0x1c(%r8), %edx
je 0x81ed2
cmpl $-0x1, %ecx
jne 0x82111
movl %eax, 0x20(%r8)
movl 0x8(%r8), %eax
addl %ebp, %eax
movl %eax, 0xc(%r8)
movl %r15d, 0x10(%r8)
movl 0xc(%rsp), %esi
movl %r15d, 0x18(%r8)
addl %ebp, 0x8(%r8)
movl %esi, 0x1c(%r8)
movl 0x4(%r8), %ecx
incl %ecx
movl %ecx, 0x4(%r8)
movl 0x10(%r13), %eax
movq 0x20(%rsp), %rdi
movl 0x30(%rsp), %r9d
movl 0x2c(%rsp), %esi
jmp 0x81d64
movl $0x0, 0x8(%rsp)
movq 0x1d10d2(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x1669f3(%rip), %rdi    # 0x1e890b
pushq $0x9
popq %rsi
pushq $0x1
popq %rdx
movq %r8, %rbx
callq 0x2410
movq %rbx, %r8
movl 0x18(%rbx), %eax
jmp 0x81dec
andl $0x0, 0x4(%r8)
movl (%r8), %eax
incl %eax
movl %eax, (%r8)
jmp 0x81d32
movl 0x20(%r8), %ecx
xorl %eax, %eax
cmpl $-0x1, %ecx
je 0x82288
movq 0x18(%rsp), %r14
movl %ecx, 0x10(%r14)
movl 0xc(%r8), %ecx
movl %ecx, 0x8(%r14)
movq 0x48(%r8), %rdx
movq 0x20(%rdx), %rdx
movl 0x18(%r8), %esi
shrl %esi
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rdx,%rsi,8), %edx
andl %ebp, %edx
addl 0x8(%r8), %edx
movl %edx, 0xc(%r14)
movl 0x24(%r8), %esi
movl 0x28(%r8), %edi
cmpl %edx, %edi
cmovbl %edi, %edx
cmpl %ecx, %esi
cmoval %esi, %ecx
cmpl %ecx, %edx
jbe 0x82288
movl 0x10(%r8), %edi
movq 0x50(%r8), %rsi
leaq 0x10(%rsp), %r15
movq %r15, %rdx
xorl %ecx, %ecx
movq %r8, %rbx
callq 0xc31da
movl 0x18(%rbx), %edi
movq 0x50(%rbx), %rsi
leaq 0x14(%rsp), %r12
movq %r12, %rdx
xorl %ecx, %ecx
callq 0xc31da
movq %rbx, %r9
movl (%r15), %ecx
movl (%r12), %edx
cmpl %edx, %ecx
movl %edx, %eax
cmovbl %ecx, %eax
movl %eax, (%r14)
movq 0x48(%rbx), %rsi
movq 0x20(%rsi), %rsi
movl 0x10(%rbx), %edi
movl 0x18(%rbx), %r8d
shrl %edi
movl (%rsi,%rdi,8), %edi
andl %ebp, %edi
addl %ecx, %edi
shrl %r8d
andl (%rsi,%r8,8), %ebp
addl %edx, %ebp
cmpl %ebp, %edi
cmoval %edi, %ebp
movl %ebp, 0x4(%r14)
movl 0x8(%r14), %esi
movl 0x24(%rbx), %edx
movl %edx, %edi
subl %esi, %edi
jbe 0x820af
movl 0xc(%r14), %ecx
movl %ecx, %r8d
subl %esi, %r8d
movl %ebp, %esi
subl %eax, %esi
cvtsi2sd %rdi, %xmm0
cvtsi2sd %r8, %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %rsi, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rsi
testb $0x1, 0x10(%r14)
jne 0x820b7
addl %esi, %eax
movl %eax, (%r14)
jmp 0x820bd
movl 0x20(%r8), %edx
xorl %eax, %eax
cmpl $-0x1, %edx
je 0x82288
movq 0x18(%rsp), %r14
movl %edx, 0x10(%r14)
movl 0xc(%r8), %edx
movl %edx, 0x8(%r14)
movq 0x48(%r8), %rsi
movq 0x20(%rsi), %rsi
movl 0x18(%r8), %edi
shrl %edi
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl (%rsi,%rdi,8), %esi
andl %ebp, %esi
addl 0x8(%r8), %esi
movl %esi, 0xc(%r14)
cmpl %esi, %ecx
cmovbl %ecx, %esi
movl 0x24(%r8), %ecx
cmpl %edx, %ecx
cmoval %ecx, %edx
cmpl %edx, %esi
ja 0x81f9d
jmp 0x82288
movl 0xc(%r14), %ecx
movl %esi, %edx
jmp 0x820c1
subl %esi, %ebp
movl %ebp, 0x4(%r14)
movl %edx, 0x8(%r14)
movl 0x28(%r9), %esi
movl %ecx, %edi
subl %esi, %edi
jbe 0x82105
subl %edx, %ecx
movl %ebp, %edx
subl %eax, %edx
cvtsi2sd %rdi, %xmm0
cvtsi2sd %rcx, %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %rdx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
testb $0x1, 0x10(%r14)
jne 0x820fc
subl %ecx, %ebp
movl %ebp, 0x4(%r14)
jmp 0x82101
addl %ecx, %eax
movl %eax, (%r14)
movl %esi, 0xc(%r14)
orl $-0x1, 0x20(%r9)
jmp 0x82285
movl %eax, %ecx
movl %eax, 0x20(%rsp)
movq 0x18(%rsp), %r14
movl %ecx, 0x10(%r14)
movl 0xc(%r8), %eax
movl %eax, 0x8(%r14)
movq 0x48(%r8), %rax
movq 0x20(%rax), %rax
movl 0x18(%r8), %ecx
shrl %ecx
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl (%rax,%rcx,8), %eax
andl %r12d, %eax
addl 0x8(%r8), %eax
movl %eax, 0xc(%r14)
movl 0x10(%r8), %edi
movq 0x50(%r8), %rsi
leaq 0x10(%rsp), %rdx
xorl %ecx, %ecx
movq %r8, %rbx
callq 0xc31da
movl 0x18(%rbx), %edi
movq 0x50(%rbx), %rsi
leaq 0x14(%rsp), %r13
movq %r13, %rdx
xorl %ecx, %ecx
callq 0xc31da
movq %rbx, %r9
leaq 0x10(%rsp), %rax
movl (%rax), %ecx
movl (%r13), %edx
movq %r14, %r10
cmpl %edx, %ecx
movl %edx, %eax
cmovbl %ecx, %eax
movl %eax, (%r14)
movq 0x48(%rbx), %rsi
movq 0x20(%rsi), %rsi
movl 0x10(%rbx), %edi
movl 0x18(%rbx), %r8d
shrl %edi
movl (%rsi,%rdi,8), %edi
andl %r12d, %edi
addl %ecx, %edi
shrl %r8d
andl (%rsi,%r8,8), %r12d
addl %edx, %r12d
cmpl %r12d, %edi
cmoval %edi, %r12d
movl %r12d, 0x4(%r14)
movl 0x8(%r14), %esi
movl 0x24(%rbx), %edx
movl %edx, %edi
subl %esi, %edi
jbe 0x82204
movl 0xc(%r10), %ecx
movl %ecx, %r8d
subl %esi, %r8d
movl %r12d, %esi
subl %eax, %esi
cvtsi2sd %rdi, %xmm0
cvtsi2sd %r8, %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %rsi, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rsi
testb $0x1, 0x10(%r10)
jne 0x8220c
addl %esi, %eax
movl %eax, (%r10)
jmp 0x82213
movl 0xc(%r10), %ecx
movl %esi, %edx
jmp 0x82217
subl %esi, %r12d
movl %r12d, 0x4(%r10)
movl %edx, 0x8(%r10)
movl 0x28(%r9), %esi
movl %ecx, %edi
subl %esi, %edi
jbe 0x8225d
subl %edx, %ecx
movl %r12d, %edx
subl %eax, %edx
cvtsi2sd %rdi, %xmm0
cvtsi2sd %rcx, %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %rdx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
testb $0x1, 0x10(%r10)
jne 0x82254
subl %ecx, %r12d
movl %r12d, 0x4(%r10)
jmp 0x82259
addl %ecx, %eax
movl %eax, (%r10)
movl %esi, 0xc(%r10)
movl 0x20(%rsp), %eax
movl %eax, 0x20(%r9)
addl 0x8(%r9), %ebp
movl %ebp, 0xc(%r9)
movl %r15d, 0x10(%r9)
movl %r15d, 0x18(%r9)
movl %ebp, 0x8(%r9)
movl 0xc(%rsp), %eax
movl %eax, 0x1c(%r9)
incl 0x4(%r9)
pushq $0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq