// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movq %r8, 0x28(%rsp)
movq %rcx, 0x70(%rsp)
movq %rsi, %r14
movq %rdi, %r12
movq 0xc0(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rsi)
movups %xmm0, 0x20(%rsi)
movups %xmm0, 0x10(%rsi)
movups %xmm0, (%rsi)
movq $0x0, 0x40(%rsi)
movq %r9, 0x48(%rsi)
movq %rax, 0x50(%rsi)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rdx, 0x38(%rsp)
andl 0x14(%rdx), %eax
movl $0x1, %ecx
movl $0x40, %r15d
movl $0x1, %edx
shll %cl, %edx
incq %rcx
decq %r15
cmpl %eax, %edx
jb 0x175e16
movq $-0x1, %rax
movq %rcx, 0x68(%rsp)
shrq %cl, %rax
movq %rax, 0x60(%rsp)
movq (%r12), %rbx
movq %rbx, %r13
shlq $0x5, %r13
movq %r13, %rdi
callq 0x2280
movq %rax, %rbp
movq %rax, 0x10(%r14)
movq %rbx, 0x8(%r14)
movq %rbx, (%r14)
movq 0x10(%r12), %rsi
movq %rax, %rdi
movq %r13, %rdx
callq 0x2160
addq %rbp, %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x172a07
movq (%r14), %rsi
testq %rsi, %rsi
movq %r14, 0x80(%rsp)
movq %r15, 0x78(%rsp)
je 0x1760fc
movq 0x10(%r14), %rdi
leaq 0x10(%rdi), %rax
movq %rax, 0x48(%rsp)
movl $0x1, %r9d
xorl %r10d, %r10d
movq %rsi, 0x20(%rsp)
movq %rdi, 0x30(%rsp)
cmpq %rsi, %r9
jne 0x175edf
movq %r10, %rax
shlq $0x5, %rax
movq (%rdi,%rax), %rax
movq 0x48(%r14), %rcx
addq %rax, %rax
movl $0x40, %edx
subq %rcx, %rdx
movq %rdx, 0x8(%rsp)
negq %rcx
shrq %cl, %rax
jmp 0x175f21
movq %r9, %rax
shlq $0x5, %rax
movq (%rdi,%rax), %r8
addq %r8, %r8
movq 0x48(%r14), %rdx
movq %rdx, %rcx
negq %rcx
shrq %cl, %r8
movq %r10, %rax
shlq $0x5, %rax
movq (%rdi,%rax), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %r8
je 0x1760ee
movl $0x40, %ecx
subq %rdx, %rcx
movq %rcx, 0x8(%rsp)
movq 0x70(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
movl 0x10(%rcx,%rax,8), %esi
testq %rsi, %rsi
movq %r10, 0x40(%rsp)
je 0x1760be
leaq (%rcx,%rax,8), %rax
movq 0x18(%rax), %rbp
movq 0x38(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
xorl %edx, %edx
movq %r10, %r8
xorl %ebx, %ebx
movq %rsi, 0x58(%rsp)
movq (%rbp,%rbx,8), %rdi
cmpq %r9, %r8
jae 0x1760ab
shrq $0x21, %rdi
movq 0x50(%rsp), %rax
movl (%rax,%rdi,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leaq (%rdx,%rax), %rsi
decq %rsi
movq 0x50(%r14), %r11
movl %r15d, %ecx
shlq %cl, %rdi
shrq %rdi
movq %r8, %r14
shlq $0x5, %r14
addq 0x48(%rsp), %r14
movq %r12, %r10
movq %rdi, 0x18(%rsp)
movq %r11, 0x10(%rsp)
movq -0x10(%r14), %rax
movq %r11, %r12
andq %rax, %r12
movq %rsi, %r13
subq %r12, %r13
jb 0x176092
subq %rdx, %r12
jb 0x17607f
cmpq $0x0, 0x28(%rsp)
je 0x17603e
movl (%r14), %r15d
movq %r15, (%r14)
movq %rdx, %r11
movq %rsi, %rdx
movq %rbx, %rsi
movq %rbp, %rbx
movq %r10, %rbp
movq %r9, %r10
leaq (%rax,%rax), %r9
movq 0x8(%rsp), %rcx
shrq %cl, %r9
movq 0x28(%rsp), %rdi
movq (%rdi), %rcx
movq 0x80(%rdi), %rdi
movl (%rcx,%r9,4), %ecx
movq %r10, %r9
movq %rbp, %r10
movq %rbx, %rbp
movq %rsi, %rbx
movq %rdx, %rsi
movq %r11, %rdx
movq 0x10(%rsp), %r11
incq %rdi
cmpq %rcx, %rdi
jb 0x17603e
movabsq $0x100000000, %rcx      # imm = 0x100000000
orq %rcx, %r15
movq %r15, (%r14)
movq (%rbp,%rbx,8), %rcx
movabsq $0x100000000, %rdi      # imm = 0x100000000
testq %rdi, %rcx
cmoveq %r12, %r13
shlq $0x1f, %rcx
xorq %rax, %rcx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
andq %rax, %rcx
movq 0x18(%rsp), %rdi
orq %rdi, %rcx
andq 0x60(%rsp), %r13
orq %rcx, %r13
movq %r13, -0x10(%r14)
incq %r10
incq %r8
addq $0x20, %r14
cmpq %r8, %r9
jne 0x175fb1
movq %r9, %r8
movq %r10, %r12
movl (%rbp,%rbx,8), %edi
movq 0x80(%rsp), %r14
movq 0x78(%rsp), %r15
movq 0x58(%rsp), %rsi
movl %edi, %eax
addq %rax, %rdx
incq %rbx
cmpq %rsi, %rbx
jne 0x175f65
jmp 0x1760c4
movq %r10, %r8
xorl %r12d, %r12d
cmpq %r9, %r8
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq 0x40(%rsp), %rcx
jne 0x1763a5
movq %r9, %rax
subq %rcx, %rax
movq %r9, %r10
cmpq %rax, %r12
jne 0x1763a5
incq %r9
cmpq %rsi, %r9
jbe 0x175eb3
jmp 0x176100
movq 0x10(%r14), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x172c90
movq (%r14), %r8
testq %r8, %r8
movq 0x68(%rsp), %rdi
je 0x17636e
movq 0x10(%r14), %rax
movq %rax, 0x20(%rsp)
addq $0x10, %rax
movq %rax, 0x48(%rsp)
movl $0x1, %esi
xorl %r11d, %r11d
movq %r8, 0x30(%rsp)
cmpq %r8, %rsi
movq %rsi, 0x10(%rsp)
jne 0x176173
movq %r11, %rax
shlq $0x5, %rax
movq 0x20(%rsp), %rcx
movq 0x8(%rcx,%rax), %rax
movq 0x48(%r14), %rcx
addq %rax, %rax
movl $0x40, %edx
subq %rcx, %rdx
movq %rdx, 0x8(%rsp)
negq %rcx
shrq %cl, %rax
jmp 0x1761c7
movq %rsi, %rax
shlq $0x5, %rax
movq 0x20(%rsp), %r9
movq 0x8(%r9,%rax), %rsi
addq %rsi, %rsi
movq 0x48(%r14), %rdx
movq %rdx, %rcx
negq %rcx
shrq %cl, %rsi
movq %r11, %rax
shlq $0x5, %rax
movq 0x8(%r9,%rax), %rax
addq %rax, %rax
shrq %cl, %rax
cmpq %rax, %rsi
jne 0x1761b5
movq 0x10(%rsp), %rsi
jmp 0x176362
movl $0x40, %ecx
subq %rdx, %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rsp), %rsi
movq 0x70(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rax
movl 0x10(%rcx,%rax,8), %edx
testq %rdx, %rdx
movq %r11, 0x40(%rsp)
je 0x17633a
leaq (%rcx,%rax,8), %rax
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x38(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x50(%rsp)
xorl %r10d, %r10d
xorl %edi, %edi
movq %r11, %r9
xorl %ebx, %ebx
movq %rdx, 0x58(%rsp)
movq 0x18(%rsp), %rax
movq (%rax,%rbx,8), %rbp
cmpq %rsi, %r9
jae 0x176327
shrq $0x21, %rbp
movq 0x50(%rsp), %rax
movl (%rax,%rbp,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rdi, %rax
decq %rax
movq 0x50(%r14), %r12
movl %r15d, %ecx
shlq %cl, %rbp
shrq %rbp
movq %r9, %r8
shlq $0x5, %r8
addq 0x48(%rsp), %r8
movq -0x8(%r8), %r13
movq %r12, %r11
andq %r13, %r11
movq %rax, %rdx
subq %r11, %rdx
jb 0x17630d
subq %rdi, %r11
jb 0x1762fa
cmpq $0x0, 0x28(%rsp)
je 0x1762b5
movl 0x4(%r8), %r15d
shlq $0x20, %r15
movq %r15, (%r8)
leaq (,%r13,2), %rsi
movq 0x8(%rsp), %rcx
shrq %cl, %rsi
movq 0x28(%rsp), %r14
movq (%r14), %rcx
movq 0x80(%r14), %r14
movl (%rcx,%rsi,4), %ecx
incq %r14
cmpq %rcx, %r14
jb 0x1762b5
orq $0x1, %r15
movq %r15, (%r8)
movq 0x18(%rsp), %rcx
movq (%rcx,%rbx,8), %rcx
movabsq $0x100000000, %rsi      # imm = 0x100000000
testq %rsi, %rcx
cmoveq %r11, %rdx
shlq $0x1f, %rcx
xorq %rcx, %r13
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
andq %rcx, %r13
orq %rbp, %r13
andq 0x60(%rsp), %rdx
orq %r13, %rdx
movq %rdx, -0x8(%r8)
incq %r10
movq 0x10(%rsp), %rsi
incq %r9
addq $0x20, %r8
cmpq %r9, %rsi
jne 0x176252
movq %rsi, %r9
movq 0x18(%rsp), %rax
movl (%rax,%rbx,8), %ebp
movq 0x80(%rsp), %r14
movq 0x78(%rsp), %r15
movq 0x58(%rsp), %rdx
movl %ebp, %eax
addq %rax, %rdi
incq %rbx
cmpq %rdx, %rbx
jne 0x176210
jmp 0x176340
movq %r11, %r9
xorl %r10d, %r10d
cmpq %rsi, %r9
movq 0x68(%rsp), %rdi
movq 0x30(%rsp), %r8
movq 0x40(%rsp), %rcx
jne 0x1763c4
movq %rsi, %rax
subq %rcx, %rax
movq %rsi, %r11
cmpq %rax, %r10
jne 0x1763c4
incq %rsi
cmpq %r8, %rsi
jbe 0x17613c
decq %rdi
movq %rdi, 0x48(%r14)
movq 0x60(%rsp), %rax
movq %rax, 0x50(%r14)
movq 0x38(%rsp), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %r14, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1755fa
leaq 0xc745f(%rip), %rdi     # 0x23d80b
leaq 0xc7478(%rip), %rsi     # 0x23d82b
leaq 0xc747c(%rip), %rcx     # 0x23d836
movl $0x279, %edx            # imm = 0x279
callq 0x2110
leaq 0xc74d0(%rip), %rdi     # 0x23d89b
leaq 0xc7459(%rip), %rsi     # 0x23d82b
leaq 0xc745d(%rip), %rcx     # 0x23d836
movl $0x29e, %edx            # imm = 0x29E
callq 0x2110