// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/horder.cpp
// Function: get_r_hits(kvec_pe_hit*, kvec_pe_hit*, asg_t*, ma_ug_t*, bubble_type*, unsigned long, unsigned long)
void get_r_hits(kvec_pe_hit *u_hits, kvec_pe_hit *r_hits, asg_t* r_g, ma_ug_t* ug, bubble_type* bub, uint64_t uID_bits, uint64_t pos_mode)
{
    uint64_t k, l, i, r_i, offset, rid, rev, rBeg, rEnd, ubits, p_mode, upos, rpos, update;
    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);

    kv_malloc(r_hits->a, u_hits->a.n); r_hits->a.n = r_hits->a.m = u_hits->a.n;
    memcpy(r_hits->a.a, u_hits->a.a, r_hits->a.n*sizeof(pe_hit));
    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) {
                        if(bub) {
                            r_hits->a.a[r_i].id = (uint32_t)r_hits->a.a[r_i].id;
                            if(!IF_HOM(get_hit_suid(*r_hits, r_i), *bub)) {
                                r_hits->a.a[r_i].id += ((uint64_t)(1)<<32);
                            }
                        }

                        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];
            }
            assert((r_i == k) && (update == (k-l)));
            // 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) {
                        if(bub) {
                            r_hits->a.a[r_i].id >>= 32;
                            r_hits->a.a[r_i].id <<= 32;
                            if(!IF_HOM(get_hit_euid(*r_hits, r_i), *bub)) {
                                r_hits->a.a[r_i].id += 1;
                            }
                        }

                        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];
            }
            assert((r_i == k) && (update == (k - l)));
            // 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;
    idx_hits(r_hits, r_g->n_seq);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdi, %r15
movq 0xb0(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rsi)
movups %xmm0, 0x30(%rsi)
movups %xmm0, 0x20(%rsi)
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
movq %r9, 0x48(%rsi)
movq %rsi, (%rsp)
movq %rax, 0x50(%rsi)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rdx, 0x10(%rsp)
andl 0x14(%rdx), %eax
movl $0x1, %ecx
movl $0x40, %esi
movl $0x1, %edx
shll %cl, %edx
incq %rcx
decq %rsi
cmpl %eax, %edx
jb 0x16f2de
movq %rsi, 0x58(%rsp)
movq $-0x1, %rax
movq %rcx, 0x60(%rsp)
shrq %cl, %rax
movq %rax, 0x48(%rsp)
movq (%rsp), %r14
movq $0x0, (%r14)
movq (%r15), %rbx
movq %rbx, 0x8(%r14)
movq %rbx, %r12
shlq $0x5, %r12
movq %r12, %rdi
callq 0x2280
movq %rax, %r13
movq %rax, 0x10(%r14)
movq %rbx, 0x8(%r14)
movq %rbx, (%r14)
movq 0x10(%r15), %rsi
movq %rax, %rdi
movq %r12, %rdx
callq 0x2160
addq %r13, %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x16bfb8
movq (%r14), %rsi
testq %rsi, %rsi
je 0x16f5a1
movl $0x1, %r11d
xorl %r9d, %r9d
movq %rsi, 0x68(%rsp)
cmpq %rsi, %r11
je 0x16f3af
movq (%rsp), %rdx
movq 0x10(%rdx), %rax
movq %r11, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %r8
addq %r8, %r8
movb 0x48(%rdx), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %r8
movq %r9, %rdi
shlq $0x5, %rdi
movq (%rax,%rdi), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %r8
je 0x16f595
movq (%rsp), %rax
movq 0x10(%rax), %rdx
movq 0x48(%rax), %rax
movq %r9, %rcx
shlq $0x5, %rcx
movq (%rdx,%rcx), %rsi
addq %rsi, %rsi
movq %rax, %rcx
negq %rcx
shrq %cl, %rsi
movq 0x50(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%rsi,%rsi,4), %rsi
movl 0x10(%rcx,%rsi,8), %edi
movq %rdi, 0x28(%rsp)
testq %rdi, %rdi
movq %r9, 0x70(%rsp)
je 0x16f56a
movl $0x40, %edi
subq %rax, %rdi
movq %rdi, 0x30(%rsp)
leaq (%rcx,%rsi,8), %rax
movq 0x18(%rax), %rdi
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x18(%rsp)
addq $0x10, %rdx
movq %rdx, 0x20(%rsp)
xorl %r10d, %r10d
xorl %ebx, %ebx
movq %r9, %rsi
xorl %ebp, %ebp
cmpq %r11, %rsi
jae 0x16f554
movq (%rdi,%rbp,8), %rdx
shrq $0x21, %rdx
movq 0x18(%rsp), %rax
movl (%rax,%rdx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rbx,%rax), %r8
decq %r8
movq (%rsp), %rax
movq 0x50(%rax), %rax
movq 0x58(%rsp), %rcx
shlq %cl, %rdx
shrq %rdx
movq %rdx, 0x40(%rsp)
movq %rsi, %r14
shlq $0x5, %r14
addq 0x20(%rsp), %r14
movq %rbx, 0x38(%rsp)
movq -0x10(%r14), %r12
movq %rax, %r9
andq %r12, %r9
movq %r8, %r13
subq %r9, %r13
jb 0x16f554
subq %rbx, %r9
jb 0x16f544
cmpq $0x0, 0x8(%rsp)
je 0x16f507
movl (%r14), %r15d
movq %r15, (%r14)
movq %rbp, %rbx
movq %rax, %rbp
movq %r8, %rax
movq %rdi, %r8
movq %r10, %rdi
movq %r11, %r10
leaq (%r12,%r12), %r11
movq 0x30(%rsp), %rcx
shrq %cl, %r11
movq 0x8(%rsp), %rdx
movq (%rdx), %rcx
movq 0x80(%rdx), %rdx
movl (%rcx,%r11,4), %ecx
movq %r10, %r11
movq %rdi, %r10
movq %r8, %rdi
movq %rax, %r8
movq %rbp, %rax
movq %rbx, %rbp
movq 0x38(%rsp), %rbx
incq %rdx
cmpq %rcx, %rdx
jb 0x16f507
movabsq $0x100000000, %rcx      # imm = 0x100000000
orq %rcx, %r15
movq %r15, (%r14)
movq (%rdi,%rbp,8), %rcx
movabsq $0x100000000, %rdx      # imm = 0x100000000
testq %rdx, %rcx
cmoveq %r9, %r13
shlq $0x1f, %rcx
xorq %r12, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
andq %rdx, %rcx
orq 0x40(%rsp), %rcx
andq 0x48(%rsp), %r13
orq %rcx, %r13
movq %r13, -0x10(%r14)
incq %r10
incq %rsi
addq $0x20, %r14
cmpq %rsi, %r11
jne 0x16f47a
movl (%rdi,%rbp,8), %eax
addq %rax, %rbx
incq %rbp
cmpq 0x28(%rsp), %rbp
jne 0x16f42c
jmp 0x16f570
movq %r9, %rsi
xorl %r10d, %r10d
cmpq %r11, %rsi
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rcx
jne 0x16f7f0
movq %r11, %rax
subq %rcx, %rax
movq %r11, %r9
cmpq %rax, %r10
jne 0x16f7f0
incq %r11
cmpq %rsi, %r11
jbe 0x16f36e
movq (%rsp), %rbx
movq 0x10(%rbx), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x16c24b
movq (%rbx), %r8
testq %r8, %r8
je 0x16f7b6
movl $0x1, %esi
xorl %r14d, %r14d
movq %r8, 0x18(%rsp)
cmpq %r8, %rsi
je 0x16f611
movq (%rsp), %rdi
movq 0x10(%rdi), %rax
movq %rsi, %rcx
shlq $0x5, %rcx
movq 0x8(%rax,%rcx), %rdx
addq %rdx, %rdx
movb 0x48(%rdi), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rdx
movq %r14, %rdi
shlq $0x5, %rdi
movq 0x8(%rax,%rdi), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %rdx
je 0x16f7aa
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x48(%rax), %rax
movq %r14, %rcx
shlq $0x5, %rcx
movq 0x8(%rdi,%rcx), %rdx
addq %rdx, %rdx
movq %rax, %rcx
negq %rcx
shrq %cl, %rdx
movq 0x50(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%rdx,%rdx,4), %rdx
movl 0x10(%rcx,%rdx,8), %r8d
movq %r8, 0x38(%rsp)
testq %r8, %r8
movq %r14, 0x20(%rsp)
je 0x16f78a
movl $0x40, %r8d
subq %rax, %r8
movq %r8, 0x40(%rsp)
leaq (%rcx,%rdx,8), %rax
movq 0x18(%rax), %r11
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x28(%rsp)
addq $0x10, %rdi
movq %rdi, 0x30(%rsp)
xorl %r10d, %r10d
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpq %rsi, %r14
jae 0x16f773
movq (%r11,%rbp,8), %rdx
shrq $0x21, %rdx
movq 0x28(%rsp), %rax
movl (%rax,%rdx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rbx,%rax), %r13
decq %r13
movq (%rsp), %rax
movq 0x50(%rax), %r9
movq 0x58(%rsp), %rcx
shlq %cl, %rdx
shrq %rdx
movq %r14, %rdi
shlq $0x5, %rdi
addq 0x30(%rsp), %rdi
movq -0x8(%rdi), %rax
movq %r9, %r12
andq %rax, %r12
movq %r13, %r8
subq %r12, %r8
jb 0x16f773
subq %rbx, %r12
jb 0x16f763
cmpq $0x0, 0x8(%rsp)
je 0x16f727
movl 0x4(%rdi), %ecx
shlq $0x20, %rcx
movq %rcx, (%rdi)
addq %rax, %rax
movq 0x40(%rsp), %rcx
shrq %cl, %rax
movq 0x8(%rsp), %r15
movq (%r15), %rcx
movq 0x80(%r15), %r15
movl (%rcx,%rax,4), %eax
incq %r15
cmpq %rax, %r15
jb 0x16f727
incq (%rdi)
movq (%r11,%rbp,8), %rax
movabsq $0x100000000, %rcx      # imm = 0x100000000
testq %rcx, %rax
cmoveq %r12, %r8
shlq $0x1f, %rax
xorq -0x8(%rdi), %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
andq %rcx, %rax
andq 0x48(%rsp), %r8
orq %rdx, %r8
orq %rax, %r8
movq %r8, -0x8(%rdi)
incq %r10
incq %r14
addq $0x20, %rdi
cmpq %r14, %rsi
jne 0x16f6d2
movl (%r11,%rbp,8), %eax
addq %rax, %rbx
incq %rbp
cmpq 0x38(%rsp), %rbp
jne 0x16f68e
jmp 0x16f78d
xorl %r10d, %r10d
cmpq %rsi, %r14
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %rcx
jne 0x16f80f
movq %rsi, %rax
subq %rcx, %rax
movq %rsi, %r14
cmpq %rax, %r10
jne 0x16f80f
incq %rsi
cmpq %r8, %rsi
jbe 0x16f5ce
movq 0x60(%rsp), %rax
decq %rax
movq (%rsp), %rdi
movq %rax, 0x48(%rdi)
movq 0x48(%rsp), %rax
movq %rax, 0x50(%rdi)
movq 0x10(%rsp), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x16eaec
leaq 0xc1dc4(%rip), %rdi     # 0x2315bb
leaq 0xc1ddd(%rip), %rsi     # 0x2315db
leaq 0xc1de1(%rip), %rcx     # 0x2315e6
movl $0x279, %edx            # imm = 0x279
callq 0x2110
leaq 0xc1e35(%rip), %rdi     # 0x23164b
leaq 0xc1dbe(%rip), %rsi     # 0x2315db
leaq 0xc1dc2(%rip), %rcx     # 0x2315e6
movl $0x29e, %edx            # imm = 0x29E
callq 0x2110