// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq %rcx, 0x10(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, %r14
movq %rdi, 0x20(%rsp)
pushq $0x1
popq %r12
pushq $0x58
popq %rsi
movq %r12, %rdi
callq 0x2120
movq %rax, %r9
movq %r15, 0x48(%rax)
movq %rbx, 0x50(%rax)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %r14, 0x68(%rsp)
andl 0x14(%r14), %eax
pushq $0x40
popq %rdi
pushq $0x1
popq %rdx
movl %edx, %esi
movl %r12d, %ecx
shll %cl, %esi
incq %r12
decq %rdi
cmpl %eax, %esi
jb 0x131cc8
movq %rdi, 0x60(%rsp)
movq %r15, 0x50(%rsp)
movq 0x20(%rsp), %r14
movq %r15, 0x48(%r14)
movq %rbx, 0x50(%r14)
movq %rbx, %rax
notq %rax
movq %rax, 0x30(%rsp)
movq %r12, 0x48(%rsp)
leaq -0x1(%r12), %rax
movq %rax, 0x70(%rsp)
xorl %ebp, %ebp
xorl %eax, %eax
movq $0x0, 0x28(%rsp)
xorl %r10d, %r10d
movq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
xorl %edx, %edx
movq %r9, 0x40(%rsp)
movq 0x10(%rsp), %r11
cmpq (%r14), %r12
movq %r10, 0x18(%rsp)
jae 0x131ee5
movq 0x10(%r14), %rsi
movq (%rsi,%rbp), %r15
addq %r15, %r15
movb 0x48(%r14), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %r15
movq 0x8(%rsi,%rbp), %r13
addq %r13, %r13
shrq %cl, %r13
movq (%r11), %rdi
movq 0x80(%r11), %rcx
movl (%rdi,%r15,4), %r8d
incq %rcx
cmpq %r8, %rcx
jae 0x131d80
movq %r10, %rcx
jmp 0x131ed6
movl (%rdi,%r13,4), %edi
cmpq %rdi, %rcx
jb 0x131d78
cmpq %r13, %r15
je 0x131d78
movq 0x28(%rsp), %rcx
cmpq %rcx, %r10
movq %rdx, 0x38(%rsp)
jne 0x131de1
leaq (%rcx,%rcx), %r14
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %r14
movq %r14, 0x8(%r9)
movq %r14, %rsi
shlq $0x5, %rsi
movq 0x8(%rsp), %rdi
callq 0x2370
movq 0x38(%rsp), %rdx
movq 0x18(%rsp), %r10
movq 0x40(%rsp), %r9
movq %rax, 0x10(%r9)
movq 0x20(%rsp), %rcx
movq 0x10(%rcx), %rsi
movq %r14, 0x28(%rsp)
leaq 0x1(%r10), %rcx
movq %rcx, (%r9)
shlq $0x5, %r10
movups (%rsi,%rbp), %xmm0
movups 0x10(%rsi,%rbp), %xmm1
movups %xmm1, 0x10(%rax,%r10)
movups %xmm0, (%rax,%r10)
movq %rdx, %rsi
shlq $0x5, %rsi
movq 0x18(%rax,%rsi), %r8
movq %r8, %r10
shrq $0x20, %r10
movq (%rax,%rsi), %r9
movq 0x8(%rax,%rsi), %rdi
movq %r9, %r14
andq %rbx, %r14
movq %r14, %r11
subq %r10, %r11
decq %r10
incq %r11
testq %r9, %r9
movl $0x0, %edx
cmovsq %rdx, %r10
cmovnsq %r14, %r11
addq %r14, %r10
testq %r10, %r10
jg 0x131e49
xorl %r10d, %r10d
testq %r11, %r11
movq 0x20(%rsp), %r14
jg 0x131e56
xorl %r11d, %r11d
addq %r11, %r10
shrq %r10
andq 0x30(%rsp), %r9
addq %r10, %r9
movq %r9, (%rax,%rsi)
movq %rdi, %r10
andq %rbx, %r10
movl %r8d, %r9d
movq %r10, %r8
subq %r9, %r8
decq %r9
incq %r8
testq %rdi, %rdi
movl $0x0, %edx
cmovsq %rdx, %r9
cmovnsq %r10, %r8
addq %r10, %r9
testq %r9, %r9
jg 0x131e98
xorl %r9d, %r9d
testq %r8, %r8
movq 0x10(%rsp), %r11
movq 0x38(%rsp), %rdx
jg 0x131eaa
xorl %r8d, %r8d
addq %r9, %r8
shrq %r8
andq 0x30(%rsp), %rdi
addq %r8, %rdi
movq %rdi, 0x8(%rax,%rsi)
shlq $0x20, %r15
orq %r13, %r15
movq %r15, 0x10(%rax,%rsi)
incq %rdx
movq %rax, 0x8(%rsp)
movq 0x40(%rsp), %r9
incq %r12
addq $0x20, %rbp
movq %rcx, %r10
jmp 0x131d32
pushq $-0x1
popq %rax
movq 0x48(%rsp), %rcx
shrq %cl, %rax
movq %rax, 0x38(%rsp)
movq %r10, %rsi
shlq $0x5, %rsi
movq 0x8(%rsp), %rdi
addq %rdi, %rsi
movq %rsi, 0x48(%rsp)
callq 0x12eda6
movq 0x18(%rsp), %rsi
pushq $0x40
popq %rax
subq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
pushq $0x1
popq %r15
xorl %r8d, %r8d
movabsq $0x100000000, %r12      # imm = 0x100000000
cmpq %rsi, %r15
ja 0x1320b4
jne 0x131f59
movq %r8, %rax
shlq $0x5, %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax), %rax
addq %rax, %rax
movq 0x10(%rsp), %rcx
shrq %cl, %rax
jmp 0x131f8e
movq %r15, %rax
shlq $0x5, %rax
movq 0x8(%rsp), %rdi
movq (%rdi,%rax), %rdx
addq %rdx, %rdx
movq 0x10(%rsp), %rcx
shrq %cl, %rdx
movq %r8, %rax
shlq $0x5, %rax
movq (%rdi,%rax), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %rdx
je 0x132086
movq 0x58(%rsp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rax, %rax
leaq (%rcx,%rax), %rdx
movq %rdx, 0x20(%rsp)
movl 0x10(%rcx,%rax), %eax
movq %rax, 0x30(%rsp)
xorl %r9d, %r9d
movq %r8, 0x28(%rsp)
xorl %r10d, %r10d
xorl %edx, %edx
cmpq 0x30(%rsp), %r9
je 0x132069
movq 0x20(%rsp), %rax
movq 0x18(%rax), %r11
movq (%r11,%r9,8), %r13
shrq $0x21, %r13
movq 0x68(%rsp), %rax
movq 0x20(%rax), %rax
movl (%rax,%r13,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%r10,%rax), %rsi
decq %rsi
movq 0x60(%rsp), %rcx
shlq %cl, %r13
shrq %r13
movq %r8, %rcx
shlq $0x5, %rcx
addq 0x8(%rsp), %rcx
cmpq %r15, %r8
jae 0x13205a
movq (%rcx), %r14
movq %r14, %rdi
andq %rbx, %rdi
movq %rsi, %rax
subq %rdi, %rax
jb 0x13205a
subq %r10, %rdi
jb 0x132051
movq (%r11,%r9,8), %rbp
testq %r12, %rbp
cmoveq %rdi, %rax
shlq $0x1f, %rbp
xorq %r14, %rbp
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
andq %rdi, %rbp
orq %r13, %rbp
andq 0x38(%rsp), %rax
orq %rbp, %rax
movq %rax, (%rcx)
incq %rdx
incq %r8
addq $0x20, %rcx
jmp 0x132006
movl (%r11,%r9,8), %eax
addq %rax, %r10
incq %r9
jmp 0x131fba
cmpq %r15, %r8
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %rcx
jne 0x13208e
movq %r15, %rax
subq %rcx, %rax
movq %r15, %r8
cmpq %rax, %rdx
jne 0x13208e
incq %r15
jmp 0x131f31
movq 0x120f4b(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xbd621(%rip), %rdi     # 0x1ef6c0
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x18(%rsp), %rsi
movq %r15, %r8
jmp 0x132086
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq 0x48(%rsp), %rsi
callq 0x12f027
movq 0x18(%rsp), %rdi
leaq 0x8(%r14), %rax
movq %rax, 0x28(%rsp)
pushq $0x1
popq %r13
xorl %r10d, %r10d
movq 0x40(%rsp), %rax
movabsq $0x100000000, %r12      # imm = 0x100000000
cmpq %rdi, %r13
ja 0x13227d
jne 0x132113
movq %r10, %rax
shlq $0x5, %rax
movq 0x8(%rsp), %rcx
movq 0x8(%rcx,%rax), %rsi
addq %rsi, %rsi
movq 0x10(%rsp), %rcx
shrq %cl, %rsi
jmp 0x13214a
movq %r13, %rcx
shlq $0x5, %rcx
movq 0x8(%rsp), %rsi
movq 0x8(%rsi,%rcx), %rdx
addq %rdx, %rdx
movq 0x10(%rsp), %rcx
shrq %cl, %rdx
movq %r10, %r8
shlq $0x5, %r8
movq 0x8(%rsi,%r8), %rsi
addq %rsi, %rsi
shrq %cl, %rsi
cmpq %rsi, %rdx
je 0x13224a
movq 0x58(%rsp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rsi, %rax
leaq (%rcx,%rax), %rdx
movq %rdx, 0x20(%rsp)
movl 0x10(%rcx,%rax), %eax
movq %rax, 0x30(%rsp)
xorl %r9d, %r9d
movq %r10, 0x50(%rsp)
movq %r10, %r8
xorl %r10d, %r10d
xorl %edx, %edx
cmpq 0x30(%rsp), %r9
je 0x132228
movq 0x20(%rsp), %rax
movq 0x18(%rax), %r11
movq (%r11,%r9,8), %rax
shrq $0x21, %rax
movq 0x68(%rsp), %rcx
movq 0x20(%rcx), %rcx
movl (%rcx,%rax,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%r10,%rcx), %rsi
decq %rsi
movq 0x60(%rsp), %rcx
shlq %cl, %rax
shrq %rax
movq %r8, %rcx
shlq $0x5, %rcx
addq 0x28(%rsp), %rcx
cmpq %r13, %r8
jae 0x132219
movq (%rcx), %rdi
movq %rdi, %r14
andq %rbx, %r14
movq %rsi, %r15
subq %r14, %r15
jb 0x132219
subq %r10, %r14
jb 0x132210
movq (%r11,%r9,8), %rbp
testq %r12, %rbp
cmoveq %r14, %r15
shlq $0x1f, %rbp
xorq %rdi, %rbp
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
andq %rdi, %rbp
orq %rax, %rbp
andq 0x38(%rsp), %r15
orq %rbp, %r15
movq %r15, (%rcx)
incq %rdx
incq %r8
addq $0x20, %rcx
jmp 0x1321c5
movl (%r11,%r9,8), %eax
addq %rax, %r10
incq %r9
jmp 0x132179
cmpq %r13, %r8
movq 0x40(%rsp), %rax
movq 0x18(%rsp), %rdi
movq 0x50(%rsp), %rsi
jne 0x132252
movq %r13, %rcx
subq %rsi, %rcx
movq %r13, %r10
cmpq %rcx, %rdx
jne 0x132252
incq %r13
jmp 0x1320ea
movq 0x120d87(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xbd45d(%rip), %rdi     # 0x1ef6c0
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x18(%rsp), %rdi
movq 0x40(%rsp), %rax
movq %r13, %r10
jmp 0x13224a
movq 0x70(%rsp), %rcx
movq %rcx, 0x48(%rax)
movq 0x38(%rsp), %rcx
movq %rcx, 0x50(%rax)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq