// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/horder.cpp
// Function: get_r_hits_for_trio(kvec_pe_hit*, asg_t*, ma_ug_t*, bubble_type*, unsigned long, unsigned long)
kvec_pe_hit *get_r_hits_for_trio(kvec_pe_hit *u_hits, asg_t* r_g, ma_ug_t* ug, bubble_type* bub, uint64_t uID_bits, uint64_t pos_mode)
{
    kvec_pe_hit *r_hits = NULL;
    CALLOC(r_hits, 1);
    uint64_t k, l, i, r_i, offset, rid, rev, rBeg, rEnd, ubits, p_mode, upos, rpos, update, ubeg, uend, suid, euid;
    ma_utg_t *u = NULL;
    memset(r_hits, 0, sizeof(*r_hits));
    r_hits->uID_bits = uID_bits;
    r_hits->pos_mode = pos_mode;
    //reset for reads
    for (ubits=1; (uint64_t)(1<<ubits)<(uint64_t)r_g->n_seq; ubits++);
    p_mode = ((uint64_t)-1) >> (ubits + 1);

    u_hits->uID_bits = uID_bits; u_hits->pos_mode = pos_mode;
    for (i = r_i = 0; i < u_hits->a.n; i++)
    {
        suid = get_hit_suid(*u_hits, i);
        euid = get_hit_euid(*u_hits, i);
        if(IF_HOM(suid, *bub)) continue;
        if(IF_HOM(euid, *bub)) continue;
        if(suid == euid) continue;
        kv_push(pe_hit, r_hits->a, u_hits->a.a[i]);

        resolve_hit(r_hits->a.a[r_i].s, r_hits->a.a[r_i].len>>32, r_hits->uID_bits, 
                                                                r_hits->pos_mode, NULL, &ubeg, &uend);
        upos = (ubeg+uend-1)>>1;
        r_hits->a.a[r_i].s -= get_hit_spos(*r_hits, r_i);
        r_hits->a.a[r_i].s += upos;


        resolve_hit(r_hits->a.a[r_i].e, (uint32_t)r_hits->a.a[r_i].len, r_hits->uID_bits, 
                                                                r_hits->pos_mode, NULL, &ubeg, &uend);
        upos = (ubeg+uend-1)>>1;
        r_hits->a.a[r_i].e -= get_hit_epos(*r_hits, r_i);///pos at unitig
        r_hits->a.a[r_i].e += upos;
        
        r_hits->a.a[r_i].id = (suid<<32)|euid;
        r_i++;
    }

    radix_sort_pe_hit_idx_hn1(r_hits->a.a, r_hits->a.a + r_hits->a.n);
    for (k = 1, l = 0; k <= r_hits->a.n; ++k) 
    {   
        if (k == r_hits->a.n || get_hit_suid(*r_hits, k) != get_hit_suid(*r_hits, l))//same suid
        {
            ///already sort by spos
            u = &(ug->u.a[get_hit_suid(*r_hits, l)]);
            update = 0;
            for (i = offset = 0, r_i = l; i < u->n; i++)
            {
                rid = u->a[i]>>33; 
                rBeg = offset;
                rEnd = rBeg + r_g->seq[rid].len - 1;
                for (; r_i < k; r_i++)
                {
                    upos = get_hit_spos(*r_hits, r_i);///pos at unitig
                    
                    if(upos > rEnd) break;
                    if(upos >= rBeg && upos <= rEnd)
                    {
                        rpos = (((u->a[i]>>32)&1)? rEnd - upos : upos - rBeg);///pos at read
                        rev = ((u->a[i]>>32)&1) ^ (r_hits->a.a[r_i].s>>63);
                        r_hits->a.a[r_i].s = (rev<<63) | ((rid << (64-ubits))>>1) | (rpos & p_mode);
                        
                        update++;
                    }
                }
                offset += (uint32_t)u->a[i];
            }

            if(r_i != k || update != k - l) fprintf(stderr, "ERROR-r_i\n");
            l = k;
        }
    }

    radix_sort_pe_hit_idx_hn2(r_hits->a.a, r_hits->a.a + r_hits->a.n);
    for (k = 1, l = 0; k <= r_hits->a.n; ++k) 
    {
        if (k == r_hits->a.n || get_hit_euid(*r_hits, k) != get_hit_euid(*r_hits, l))//same euid
        {
            ///already sort by epos
            u = &(ug->u.a[get_hit_euid(*r_hits, l)]);
            update = 0;
            for (i = offset = 0, r_i = l; i < u->n; i++)
            {
                rid = u->a[i]>>33; 
                rBeg = offset;
                rEnd = rBeg + r_g->seq[rid].len - 1;
                for (; r_i < k; r_i++)
                {
                    upos = get_hit_epos(*r_hits, r_i);///pos at unitig

                    if(upos > rEnd) break;
                    if(upos >= rBeg && upos <= rEnd)
                    {
                        rpos = (((u->a[i]>>32)&1)? rEnd - upos : upos - rBeg);///pos at read
                        rev = ((u->a[i]>>32)&1) ^ (r_hits->a.a[r_i].e>>63);
                        r_hits->a.a[r_i].e = (rev<<63) | ((rid << (64-ubits))>>1) | (rpos & p_mode);
                        
                        update++;
                    }
                }
                offset += (uint32_t)u->a[i];
            }

            if(r_i != k || update != k - l) fprintf(stderr, "ERROR-r_i\n");
            l = k;
        }
    }


    r_hits->uID_bits = ubits;
    r_hits->pos_mode = p_mode;
    return r_hits;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %r15
movq %r8, %r12
movq %rcx, %rbp
movq %rdx, 0x58(%rsp)
movq %rsi, %r14
movq %rdi, 0x10(%rsp)
movl $0x1, %ebx
movl $0x1, %edi
movl $0x58, %esi
callq 0x2120
movq %r12, 0x28(%rsp)
movq %r12, 0x48(%rax)
movq %rax, 0x40(%rsp)
movq %r15, 0x50(%rax)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %r14, 0x50(%rsp)
andl 0x14(%r14), %eax
movl $0x40, %edi
movl $0x1, %edx
movl %ebx, %ecx
shll %cl, %edx
incq %rbx
decq %rdi
cmpl %eax, %edx
jb 0x1757a4
movq $-0x1, %rax
movl %ebx, %ecx
shrq %cl, %rax
movq %rax, 0x48(%rsp)
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rax, 0x48(%rcx)
movq %r15, 0x50(%rcx)
movq (%rcx), %rsi
decq %rbx
movq %rbx, 0x70(%rsp)
testq %rsi, %rsi
je 0x175d71
movq %rdi, 0x60(%rsp)
movq %r15, %rax
notq %rax
movq %rax, 0x38(%rsp)
xorl %r13d, %r13d
xorl %eax, %eax
xorl %edi, %edi
movq $0x0, 0x18(%rsp)
xorl %r11d, %r11d
movq $0x0, 0x30(%rsp)
xorl %r12d, %r12d
movq 0x40(%rsp), %rdx
movq %rbp, %r14
movq 0x10(%rsp), %r9
movq 0x10(%r9), %r8
movq (%r8,%r13), %rbx
addq %rbx, %rbx
xorl %ecx, %ecx
subb 0x48(%r9), %cl
shrq %cl, %rbx
movq 0x8(%r8,%r13), %rbp
addq %rbp, %rbp
shrq %cl, %rbp
movq (%r14), %r9
movq 0x80(%r14), %rcx
movl (%r9,%rbx,4), %r10d
incq %rcx
cmpq %r10, %rcx
jb 0x1759c7
movl (%r9,%rbp,4), %r9d
cmpq %r9, %rcx
jb 0x1759c7
cmpq %rbp, %rbx
je 0x1759c7
movq 0x18(%rsp), %rcx
cmpq %rcx, %r11
jne 0x1758cb
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x20(%rsp)
movq %rsi, 0x8(%rdx)
shlq $0x5, %rsi
movq %r11, 0x8(%rsp)
callq 0x2370
movq 0x8(%rsp), %r11
movq 0x40(%rsp), %rdx
movq %rax, 0x10(%rdx)
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %r8
movq 0x20(%rsp), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x1(%r11), %rcx
movq %rcx, 0x20(%rsp)
movq %rcx, (%rdx)
shlq $0x5, %r11
movups (%r8,%r13), %xmm0
movups 0x10(%r8,%r13), %xmm1
movups %xmm1, 0x10(%rax,%r11)
movups %xmm0, (%rax,%r11)
movq 0x30(%rsp), %rsi
shlq $0x5, %rsi
movq 0x18(%rax,%rsi), %r8
movq %r8, %r10
shrq $0x20, %r10
movq (%rax,%rsi), %rdx
movq 0x8(%rax,%rsi), %rdi
movq %rdx, %r9
andq %r15, %r9
movq %r9, %r11
subq %r10, %r11
decq %r10
incq %r11
testq %rdx, %rdx
movl $0x0, %ecx
cmovsq %rcx, %r10
cmovnsq %r9, %r11
addq %r9, %r10
testq %r10, %r10
jg 0x17593c
xorl %r10d, %r10d
testq %r11, %r11
jg 0x175944
xorl %r11d, %r11d
addq %r11, %r10
shrq %r10
andq 0x38(%rsp), %rdx
addq %r10, %rdx
movq %rdx, (%rax,%rsi)
movq %rdi, %rdx
andq %r15, %rdx
movl %r8d, %r9d
movq %rdx, %r8
subq %r9, %r8
decq %r9
incq %r8
testq %rdi, %rdi
movl $0x0, %ecx
cmovsq %rcx, %r9
cmovnsq %rdx, %r8
addq %rdx, %r9
testq %r9, %r9
jg 0x175986
xorl %r9d, %r9d
testq %r8, %r8
movq 0x40(%rsp), %rdx
jg 0x175993
xorl %r8d, %r8d
addq %r9, %r8
shrq %r8
andq 0x38(%rsp), %rdi
addq %r8, %rdi
movq %rdi, 0x8(%rax,%rsi)
shlq $0x20, %rbx
orq %rbp, %rbx
movq %rbx, 0x10(%rax,%rsi)
incq 0x30(%rsp)
movq 0x10(%rsp), %rsi
movq (%rsi), %rsi
movq %rax, %rdi
movq 0x20(%rsp), %r11
incq %r12
addq $0x20, %r13
cmpq %rsi, %r12
jb 0x175825
movq 0x10(%rdx), %r12
movq %r11, %rbx
shlq $0x5, %rbx
addq %r12, %rbx
movq %r12, %rdi
movq %rbx, %rsi
movq %r11, 0x8(%rsp)
callq 0x172a07
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
je 0x175d7f
movq %rbx, 0x68(%rsp)
movl $0x40, %eax
subq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movl $0x1, %ebx
xorl %r8d, %r8d
movq %r12, 0x10(%rsp)
movabsq $0x100000000, %r14      # imm = 0x100000000
cmpq %rsi, %rbx
jne 0x175a4b
movq %r8, %rax
shlq $0x5, %rax
movq (%r12,%rax), %rax
addq %rax, %rax
movq 0x18(%rsp), %rcx
shrq %cl, %rax
jmp 0x175a7b
movq %rbx, %rax
shlq $0x5, %rax
movq (%r12,%rax), %rdx
addq %rdx, %rdx
movq 0x18(%rsp), %rcx
shrq %cl, %rdx
movq %r8, %rax
shlq $0x5, %rax
movq (%r12,%rax), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %rdx
je 0x175b8e
movq 0x58(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
movl 0x10(%rcx,%rax,8), %esi
testq %rsi, %rsi
movq %r8, 0x20(%rsp)
je 0x175b6a
leaq (%rcx,%rax,8), %rax
movq 0x18(%rax), %rdi
movq 0x50(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x30(%rsp)
xorl %edx, %edx
xorl %r10d, %r10d
xorl %r11d, %r11d
movq %rsi, 0x38(%rsp)
movq (%rdi,%r11,8), %rbp
cmpq %rbx, %r8
jae 0x175b57
shrq $0x21, %rbp
movq 0x30(%rsp), %rax
movl (%rax,%rbp,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%r10,%rax), %r9
decq %r9
movq 0x60(%rsp), %rcx
shlq %cl, %rbp
shrq %rbp
movq %r8, %rcx
shlq $0x5, %rcx
addq 0x10(%rsp), %rcx
movq (%rcx), %r13
movq %r13, %r12
andq %r15, %r12
movq %r9, %rax
subq %r12, %rax
jb 0x175b4e
subq %r10, %r12
jb 0x175b3f
movq (%rdi,%r11,8), %rsi
testq %r14, %rsi
cmoveq %r12, %rax
shlq $0x1f, %rsi
xorq %r13, %rsi
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
andq %r12, %rsi
orq %rbp, %rsi
andq 0x48(%rsp), %rax
orq %rsi, %rax
movq %rax, (%rcx)
incq %rdx
incq %r8
addq $0x20, %rcx
cmpq %r8, %rbx
jne 0x175af9
movq %rbx, %r8
movl (%rdi,%r11,8), %ebp
movq 0x38(%rsp), %rsi
movl %ebp, %eax
addq %rax, %r10
incq %r11
cmpq %rsi, %r11
jne 0x175abd
jmp 0x175b6c
xorl %edx, %edx
cmpq %rbx, %r8
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %r12
movq 0x20(%rsp), %rcx
jne 0x175b9c
movq %rbx, %rax
subq %rcx, %rax
movq %rbx, %r8
cmpq %rax, %rdx
jne 0x175b9c
incq %rbx
cmpq %rsi, %rbx
jbe 0x175a2e
jmp 0x175bab
callq 0x21c70
movq 0x8(%rsp), %rsi
movq %rbx, %r8
jmp 0x175b8e
movq %r12, %rdi
movq 0x68(%rsp), %rsi
callq 0x172c90
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
je 0x175d8a
movl $0x40, %eax
subq 0x28(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x8(%r12), %rax
movq %rax, 0x20(%rsp)
movl $0x1, %ebx
xorl %r8d, %r8d
movabsq $0x100000000, %r14      # imm = 0x100000000
cmpq %rsi, %rbx
jne 0x175c0f
movq %r8, %rax
shlq $0x5, %rax
movq 0x8(%r12,%rax), %rax
addq %rax, %rax
movq 0x28(%rsp), %rcx
shrq %cl, %rax
jmp 0x175c41
movq %rbx, %rax
shlq $0x5, %rax
movq 0x8(%r12,%rax), %rdx
addq %rdx, %rdx
movq 0x28(%rsp), %rcx
shrq %cl, %rdx
movq %r8, %rax
shlq $0x5, %rax
movq 0x8(%r12,%rax), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %rdx
je 0x175d54
movq 0x58(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
movl 0x10(%rcx,%rax,8), %esi
testq %rsi, %rsi
movq %r8, 0x18(%rsp)
je 0x175d30
leaq (%rcx,%rax,8), %rax
movq 0x18(%rax), %rdi
movq 0x50(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x30(%rsp)
xorl %edx, %edx
xorl %r10d, %r10d
xorl %r11d, %r11d
movq %rsi, 0x38(%rsp)
movq (%rdi,%r11,8), %rbp
cmpq %rbx, %r8
jae 0x175d1d
shrq $0x21, %rbp
movq 0x30(%rsp), %rax
movl (%rax,%rbp,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%r10,%rax), %r9
decq %r9
movq 0x60(%rsp), %rcx
shlq %cl, %rbp
shrq %rbp
movq %r8, %rcx
shlq $0x5, %rcx
addq 0x20(%rsp), %rcx
movq (%rcx), %r12
movq %r12, %r13
andq %r15, %r13
movq %r9, %rax
subq %r13, %rax
jb 0x175d14
subq %r10, %r13
jb 0x175d05
movq (%rdi,%r11,8), %rsi
testq %r14, %rsi
cmoveq %r13, %rax
shlq $0x1f, %rsi
xorq %r12, %rsi
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
andq %r12, %rsi
orq %rbp, %rsi
andq 0x48(%rsp), %rax
orq %rsi, %rax
movq %rax, (%rcx)
incq %rdx
incq %r8
addq $0x20, %rcx
cmpq %r8, %rbx
jne 0x175cbf
movq %rbx, %r8
movl (%rdi,%r11,8), %ebp
movq 0x38(%rsp), %rsi
movl %ebp, %eax
addq %rax, %r10
incq %r11
cmpq %rsi, %r11
jne 0x175c83
jmp 0x175d32
xorl %edx, %edx
cmpq %rbx, %r8
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %rcx
jne 0x175d62
movq %rbx, %rax
subq %rcx, %rax
movq %rbx, %r8
cmpq %rax, %rdx
jne 0x175d62
incq %rbx
cmpq %rsi, %rbx
jbe 0x175bf1
jmp 0x175d8a
callq 0x21c8c
movq 0x8(%rsp), %rsi
movq %rbx, %r8
jmp 0x175d54
xorl %ebx, %ebx
xorl %edi, %edi
xorl %esi, %esi
callq 0x172a07
xorl %r12d, %r12d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x172c90
movq 0x70(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rax, 0x48(%rcx)
movq 0x48(%rsp), %rax
movq %rax, 0x50(%rcx)
movq %rcx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq