// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %r8, %r12
movq %rcx, %r13
movq %rdx, 0x60(%rsp)
movq %rsi, %r14
movq %rdi, 0x20(%rsp)
movl $0x1, %r15d
movl $0x1, %edi
movl $0x58, %esi
callq 0x2120
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rax)
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movq %r12, 0x18(%rsp)
movq %r12, 0x48(%rax)
movq %rax, 0x38(%rsp)
movq %rbx, 0x50(%rax)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %r14, 0x58(%rsp)
andl 0x14(%r14), %eax
movl $0x40, %esi
movl $0x1, %edx
movl %r15d, %ecx
shll %cl, %edx
incq %r15
decq %rsi
cmpl %eax, %edx
jb 0x16ecac
movq %rsi, 0x68(%rsp)
movq $-0x1, %rax
movq %r15, 0x50(%rsp)
movl %r15d, %ecx
shrq %cl, %rax
movq %rax, 0x48(%rsp)
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %r10
movq %rax, 0x48(%r10)
movq %rbx, 0x50(%r10)
cmpq $0x0, (%r10)
je 0x16eeae
movq %rbx, %rax
notq %rax
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x28(%rsp)
xorl %esi, %esi
movq %r13, 0x10(%rsp)
movq 0x10(%r10), %rax
movq (%rax,%r12), %r14
addq %r14, %r14
xorl %ecx, %ecx
subb 0x48(%r10), %cl
shrq %cl, %r14
movq 0x8(%rax,%r12), %r15
addq %r15, %r15
shrq %cl, %r15
movq (%r13), %rcx
movq 0x80(%r13), %rax
movl (%rcx,%r14,4), %edx
incq %rax
cmpq %rdx, %rax
jb 0x16ee9e
movl (%rcx,%r15,4), %ecx
cmpq %rcx, %rax
jb 0x16ee9e
cmpq %r15, %r14
je 0x16ee9e
movq %rsi, 0x8(%rsp)
movq 0x38(%rsp), %rdx
movq (%rdx), %rbp
movq 0x8(%rdx), %rax
cmpq %rax, %rbp
jne 0x16edb0
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdx)
movq 0x10(%rdx), %rdi
shlq $0x5, %rsi
callq 0x2370
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %r10
movq 0x38(%rsp), %rdx
movq %rax, 0x10(%rdx)
movq 0x10(%r10), %rcx
movq 0x10(%rdx), %rax
movq %rdx, %rsi
leaq 0x1(%rbp), %rdx
movq %rdx, (%rsi)
shlq $0x5, %rbp
movups (%rcx,%r12), %xmm0
movups 0x10(%rcx,%r12), %xmm1
movups %xmm1, 0x10(%rax,%rbp)
movups %xmm0, (%rax,%rbp)
movq 0x28(%rsp), %r11
movq %r11, %rcx
shlq $0x5, %rcx
movq 0x18(%rax,%rcx), %r8
movq %r8, %rsi
shrq $0x20, %rsi
movq (%rax,%rcx), %rdx
movq %rdx, %r9
andq %rbx, %r9
movq %r9, %rdi
subq %rsi, %rdi
decq %rsi
incq %rdi
testq %rdx, %rdx
movl $0x0, %ebp
cmovsq %rbp, %rsi
cmovnsq %r9, %rdi
addq %r9, %rsi
movl %r8d, %r8d
testq %rsi, %rsi
cmovleq %rbp, %rsi
testq %rdi, %rdi
cmovleq %rbp, %rdi
addq %rsi, %rdi
movq 0x8(%rax,%rcx), %rsi
shrq %rdi
movq 0x30(%rsp), %r9
andq %r9, %rdx
addq %rdi, %rdx
movq %rdx, (%rax,%rcx)
movq %rsi, %rdx
andq %rbx, %rdx
movq %rdx, %rdi
subq %r8, %rdi
decq %r8
incq %rdi
testq %rsi, %rsi
cmovsq %rbp, %r8
cmovnsq %rdx, %rdi
addq %rdx, %r8
testq %r8, %r8
cmovleq %rbp, %r8
testq %rdi, %rdi
cmovleq %rbp, %rdi
addq %r8, %rdi
shrq %rdi
andq %r9, %rsi
addq %rdi, %rsi
movq %rsi, 0x8(%rax,%rcx)
shlq $0x20, %r14
orq %r15, %r14
movq %r14, 0x10(%rax,%rcx)
incq %r11
movq %r11, 0x28(%rsp)
movq 0x8(%rsp), %rsi
incq %rsi
addq $0x20, %r12
cmpq (%r10), %rsi
jb 0x16ed16
movq 0x38(%rsp), %rax
movq (%rax), %r14
movq 0x10(%rax), %rdi
movq %r14, %rsi
shlq $0x5, %rsi
addq %rdi, %rsi
movq %rdi, 0x8(%rsp)
movq %rsi, 0x70(%rsp)
callq 0x16bfb8
testq %r14, %r14
movq %r14, 0x10(%rsp)
je 0x16f085
movq %r14, %rsi
movl $0x40, %eax
subq 0x18(%rsp), %rax
movq %rax, 0x40(%rsp)
movl $0x1, %r15d
xorl %r9d, %r9d
movabsq $0x100000000, %r13      # imm = 0x100000000
cmpq %rsi, %r15
je 0x16ef40
movq %r15, %rax
shlq $0x5, %rax
movq 0x8(%rsp), %rdx
movq (%rdx,%rax), %rax
addq %rax, %rax
movq 0x40(%rsp), %rcx
shrq %cl, %rax
movq %r9, %rdi
shlq $0x5, %rdi
movq (%rdx,%rdi), %rdx
addq %rdx, %rdx
shrq %cl, %rdx
cmpq %rdx, %rax
je 0x16f068
movq %r9, %rax
shlq $0x5, %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax), %rdx
addq %rdx, %rdx
movq 0x40(%rsp), %rcx
shrq %cl, %rdx
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%rdx,%rdx,4), %rcx
movl 0x10(%rax,%rcx,8), %edx
movq %rdx, 0x28(%rsp)
testq %rdx, %rdx
movq %r9, 0x20(%rsp)
je 0x16f046
leaq (%rax,%rcx,8), %rax
movq 0x18(%rax), %r8
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x30(%rsp)
xorl %edx, %edx
xorl %r10d, %r10d
movq %r9, %rdi
xorl %r11d, %r11d
cmpq %r15, %rdi
jae 0x16f02f
movq (%r8,%r11,8), %rax
shrq $0x21, %rax
movq 0x30(%rsp), %rcx
movl (%rcx,%rax,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%r10,%rcx), %r9
decq %r9
movq 0x68(%rsp), %rcx
shlq %cl, %rax
shrq %rax
movq %rdi, %rcx
shlq $0x5, %rcx
addq 0x8(%rsp), %rcx
movq (%rcx), %r14
movq %r14, %rsi
andq %rbx, %rsi
movq %r9, %rbp
subq %rsi, %rbp
jb 0x16f02f
subq %r10, %rsi
jb 0x16f023
movq (%r8,%r11,8), %r12
testq %r13, %r12
cmoveq %rsi, %rbp
shlq $0x1f, %r12
xorq %r14, %r12
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
andq %rsi, %r12
orq %rax, %r12
andq 0x48(%rsp), %rbp
orq %r12, %rbp
movq %rbp, (%rcx)
incq %rdx
incq %rdi
addq $0x20, %rcx
cmpq %rdi, %r15
jne 0x16efdd
movl (%r8,%r11,8), %eax
addq %rax, %r10
incq %r11
cmpq 0x28(%rsp), %r11
jne 0x16efa0
jmp 0x16f04b
movq %r9, %rdi
xorl %edx, %edx
cmpq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rcx
jne 0x16f076
movq %r15, %rax
subq %rcx, %rax
movq %r15, %r9
cmpq %rax, %rdx
jne 0x16f076
incq %r15
cmpq %rsi, %r15
jbe 0x16ef06
jmp 0x16f085
callq 0x2296c
movq 0x10(%rsp), %rsi
movq %r15, %r9
jmp 0x16f068
decq 0x50(%rsp)
movq 0x8(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x16c24b
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x16f259
movl $0x40, %eax
subq 0x18(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
movl $0x1, %r15d
xorl %r9d, %r9d
movabsq $0x100000000, %r13      # imm = 0x100000000
cmpq %rsi, %r15
je 0x16f113
movq %r15, %rax
shlq $0x5, %rax
movq 0x8(%rsp), %rdx
movq 0x8(%rdx,%rax), %rax
addq %rax, %rax
movq 0x18(%rsp), %rcx
shrq %cl, %rax
movq %r9, %rdi
shlq $0x5, %rdi
movq 0x8(%rdx,%rdi), %rdx
addq %rdx, %rdx
shrq %cl, %rdx
cmpq %rdx, %rax
je 0x16f23c
movq %r9, %rax
shlq $0x5, %rax
movq 0x8(%rsp), %rcx
movq 0x8(%rcx,%rax), %rdx
addq %rdx, %rdx
movq 0x18(%rsp), %rcx
shrq %cl, %rdx
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%rdx,%rdx,4), %rcx
movl 0x10(%rax,%rcx,8), %edx
movq %rdx, 0x28(%rsp)
testq %rdx, %rdx
movq %r9, 0x40(%rsp)
je 0x16f21a
leaq (%rax,%rcx,8), %rax
movq 0x18(%rax), %r8
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x30(%rsp)
xorl %edx, %edx
xorl %r10d, %r10d
movq %r9, %rdi
xorl %r11d, %r11d
cmpq %r15, %rdi
jae 0x16f203
movq (%r8,%r11,8), %rax
shrq $0x21, %rax
movq 0x30(%rsp), %rcx
movl (%rcx,%rax,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%r10,%rcx), %r9
decq %r9
movq 0x68(%rsp), %rcx
shlq %cl, %rax
shrq %rax
movq %rdi, %rcx
shlq $0x5, %rcx
addq 0x20(%rsp), %rcx
movq (%rcx), %rsi
movq %rsi, %r14
andq %rbx, %r14
movq %r9, %rbp
subq %r14, %rbp
jb 0x16f203
subq %r10, %r14
jb 0x16f1f7
movq (%r8,%r11,8), %r12
testq %r13, %r12
cmoveq %r14, %rbp
shlq $0x1f, %r12
xorq %rsi, %r12
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
andq %rsi, %r12
orq %rax, %r12
andq 0x48(%rsp), %rbp
orq %r12, %rbp
movq %rbp, (%rcx)
incq %rdx
incq %rdi
addq $0x20, %rcx
cmpq %rdi, %r15
jne 0x16f1b1
movl (%r8,%r11,8), %eax
addq %rax, %r10
incq %r11
cmpq 0x28(%rsp), %r11
jne 0x16f174
jmp 0x16f21f
movq %r9, %rdi
xorl %edx, %edx
cmpq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x40(%rsp), %rcx
jne 0x16f24a
movq %r15, %rax
subq %rcx, %rax
movq %r15, %r9
cmpq %rax, %rdx
jne 0x16f24a
incq %r15
cmpq %rsi, %r15
jbe 0x16f0d7
jmp 0x16f259
callq 0x22988
movq 0x10(%rsp), %rsi
movq %r15, %r9
jmp 0x16f23c
movq 0x50(%rsp), %rax
movq 0x38(%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