// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/horder.cpp
// Function: gen_r_hits(kvec_pe_hit*, kvec_pe_hit*, asg_t*, ma_ug_t*, bubble_type*, unsigned long, unsigned long)
void gen_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, m, 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 = m = 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
            if((!bub) || ((!IF_HOM(get_hit_suid(*r_hits, l), *bub)))) {
                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);
                            r_hits->a.a[r_i].id = 1; r_hits->a.a[m++] = r_hits->a.a[r_i];
                            
                            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->a.n = m;

    radix_sort_pe_hit_idx_hn2(r_hits->a.a, r_hits->a.a + r_hits->a.n);
    for (k = 1, l = m = 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
            if((!bub) || ((!IF_HOM(get_hit_euid(*r_hits, l), *bub)))) {
                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);
                            r_hits->a.a[r_i].id = 1; r_hits->a.a[m++] = r_hits->a.a[r_i];
                            
                            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->a.n = m;

    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 $0x58, %rsp
movq %r8, %r15
movq %rcx, 0x30(%rsp)
movq %rsi, %rbx
movq %rdi, %r12
movq 0x90(%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 %rax, 0x50(%rsi)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rdx, 0x18(%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 0x1782d6
movq %rsi, 0x40(%rsp)
movq $-0x1, %rax
movq %rcx, 0x48(%rsp)
shrq %cl, %rax
movq %rax, 0x28(%rsp)
movq $0x0, (%rbx)
movq (%r12), %r14
movq %r14, 0x8(%rbx)
movq %r14, %r13
shlq $0x5, %r13
movq %r13, %rdi
callq 0x2280
movq %rax, %rbp
movq %rax, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movq %r14, (%rbx)
movq 0x10(%r12), %rsi
movq %rax, %rdi
movq %r13, %rdx
callq 0x2160
addq %rbp, %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x16bfb8
movq (%rbx), %rcx
testq %rcx, %rcx
movq %r15, 0x38(%rsp)
je 0x1785ac
movl $0x1, %r9d
xorl %esi, %esi
xorl %r11d, %r11d
cmpq %rcx, %r9
je 0x1783a3
movq 0x10(%rbx), %rdx
movq %r9, %rcx
shlq $0x5, %rcx
movq (%rdx,%rcx), %rdi
addq %rdi, %rdi
movb 0x48(%rbx), %r8b
xorl %ecx, %ecx
subb %r8b, %cl
shrq %cl, %rdi
movq %r11, %r8
shlq $0x5, %r8
movq (%rdx,%r8), %rdx
addq %rdx, %rdx
shrq %cl, %rdx
cmpq %rdx, %rdi
je 0x17859b
testq %r15, %r15
je 0x1783e3
movq 0x10(%rbx), %rcx
movq %r11, %rdx
shlq $0x5, %rdx
movq (%rcx,%rdx), %rdx
addq %rdx, %rdx
movb 0x48(%rbx), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rdx
movq (%r15), %rcx
movq 0x80(%r15), %rdi
movl (%rcx,%rdx,4), %ecx
incq %rdi
cmpq %rcx, %rdi
jae 0x1783e3
movq %r9, %r11
jmp 0x17859b
movq 0x10(%rbx), %rcx
movq %r11, %rdx
shlq $0x5, %rdx
movq (%rcx,%rdx), %rdx
addq %rdx, %rdx
movb 0x48(%rbx), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rdx
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rcx
leaq (%rdx,%rdx,4), %rdx
cmpl $0x0, 0x10(%rcx,%rdx,8)
movq %r11, 0x50(%rsp)
je 0x178571
leaq (%rcx,%rdx,8), %rax
movq %rax, 0x10(%rsp)
xorl %edx, %edx
xorl %r10d, %r10d
movq %r11, %r8
movq $0x0, 0x8(%rsp)
cmpq %r9, %r8
jae 0x178547
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rcx
movq 0x8(%rsp), %rax
movq (%rcx,%rax,8), %rdi
shrq $0x21, %rdi
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rcx
movl (%rcx,%rdi,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movq 0x40(%rsp), %rcx
shlq %cl, %rdi
leaq (%r10,%r14), %r12
decq %r12
shrq %rdi
movq %rdi, 0x20(%rsp)
movq %r8, %r13
shlq $0x5, %r13
movq 0x10(%rbx), %rbp
movq (%rbp,%r13), %r14
movq 0x50(%rbx), %r15
andq %r14, %r15
movq %r12, %rcx
subq %r15, %rcx
jb 0x178547
subq %r10, %r15
jb 0x178537
movq %rbp, %rax
addq %r13, %rax
movq %rdx, %r11
movq %rbx, %rdx
movq %r9, %rbx
movq 0x10(%rsp), %rdi
movq 0x18(%rdi), %r9
movq 0x8(%rsp), %rdi
movq (%r9,%rdi,8), %r9
movabsq $0x100000000, %rdi      # imm = 0x100000000
testq %rdi, %r9
cmoveq %r15, %rcx
shlq $0x1f, %r9
xorq %r14, %r9
movabsq $-0x8000000000000000, %r14 # imm = 0x8000000000000000
andq %r14, %r9
orq 0x20(%rsp), %r9
andq 0x28(%rsp), %rcx
orq %r9, %rcx
movq %rbx, %r9
movq %rdx, %rbx
movq %r11, %rdx
movq %rcx, (%rbp,%r13)
movq $0x1, 0x10(%rbp,%r13)
leaq 0x1(%rsi), %rcx
shlq $0x5, %rsi
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x10(%rbp,%rsi)
movups %xmm0, (%rbp,%rsi)
incq %rdx
movq %rcx, %rsi
incq %r8
addq $0x20, %r13
cmpq %r8, %r9
jne 0x178489
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rax
movq 0x8(%rsp), %rdi
movl (%rax,%rdi,8), %eax
addq %rax, %r10
incq %rdi
movl 0x10(%rcx), %eax
movq %rdi, 0x8(%rsp)
cmpq %rax, %rdi
jb 0x178438
jmp 0x178576
movq %r11, %r8
xorl %edx, %edx
cmpq %r9, %r8
movq 0x38(%rsp), %r15
movq 0x50(%rsp), %rcx
jne 0x178838
movq %r9, %rax
subq %rcx, %rax
movq %r9, %r11
cmpq %rax, %rdx
jne 0x178838
incq %r9
movq (%rbx), %rcx
cmpq %rcx, %r9
jbe 0x178366
jmp 0x1785ae
xorl %esi, %esi
movq %rsi, (%rbx)
movq 0x10(%rbx), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x16c24b
movq (%rbx), %rcx
testq %rcx, %rcx
je 0x1787fa
movl $0x1, %edx
xorl %eax, %eax
xorl %r9d, %r9d
cmpq %rcx, %rdx
je 0x178616
movq 0x10(%rbx), %rsi
movq %rdx, %rcx
shlq $0x5, %rcx
movq 0x8(%rsi,%rcx), %rdi
addq %rdi, %rdi
movb 0x48(%rbx), %r8b
xorl %ecx, %ecx
subb %r8b, %cl
shrq %cl, %rdi
movq %r9, %r8
shlq $0x5, %r8
movq 0x8(%rsi,%r8), %rsi
addq %rsi, %rsi
shrq %cl, %rsi
cmpq %rsi, %rdi
je 0x1787e9
testq %r15, %r15
je 0x178657
movq 0x10(%rbx), %rcx
movq %r9, %rsi
shlq $0x5, %rsi
movq 0x8(%rcx,%rsi), %rsi
addq %rsi, %rsi
movb 0x48(%rbx), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rsi
movq (%r15), %rcx
movq 0x80(%r15), %rdi
movl (%rcx,%rsi,4), %ecx
incq %rdi
cmpq %rcx, %rdi
jae 0x178657
movq %rdx, %r9
jmp 0x1787e9
movq 0x10(%rbx), %rcx
movq %r9, %rsi
shlq $0x5, %rsi
movq 0x8(%rcx,%rsi), %rsi
addq %rsi, %rsi
movb 0x48(%rbx), %dil
xorl %ecx, %ecx
subb %dil, %cl
shrq %cl, %rsi
movq 0x30(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%rsi,%rsi,4), %rsi
cmpl $0x0, 0x10(%rcx,%rsi,8)
movq %r9, 0x20(%rsp)
je 0x1787be
leaq (%rcx,%rsi,8), %rcx
movq %rcx, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r10d, %r10d
movq %r9, %r8
xorl %r11d, %r11d
cmpq %rdx, %r8
jae 0x17879d
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%r11,8), %rsi
shrq $0x21, %rsi
movq 0x18(%rsp), %rcx
movq 0x20(%rcx), %rcx
movl (%rcx,%rsi,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%r10,%rcx), %r13
decq %r13
movq 0x40(%rsp), %rcx
shlq %cl, %rsi
shrq %rsi
movq %r8, %rcx
shlq $0x5, %rcx
orq $0x10, %rcx
movq 0x10(%rbx), %rbp
movq -0x8(%rbp,%rcx), %r14
movq 0x50(%rbx), %r15
andq %r14, %r15
movq %r13, %r12
subq %r15, %r12
jb 0x17879d
subq %r10, %r15
jb 0x17878d
movq %rdx, %rdi
movq 0x8(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq (%rdx,%r11,8), %rdx
movabsq $0x100000000, %r9       # imm = 0x100000000
testq %r9, %rdx
cmoveq %r15, %r12
shlq $0x1f, %rdx
xorq %r14, %rdx
movabsq $-0x8000000000000000, %r14 # imm = 0x8000000000000000
andq %r14, %rdx
orq %rsi, %rdx
andq 0x28(%rsp), %r12
orq %rdx, %r12
movq %r12, -0x8(%rbp,%rcx)
movq $0x1, (%rbp,%rcx)
leaq 0x1(%rax), %rdx
shlq $0x5, %rax
movups (%rbp,%rcx), %xmm0
movups %xmm0, 0x10(%rbp,%rax)
movups -0x10(%rbp,%rcx), %xmm0
movups %xmm0, (%rbp,%rax)
incq 0x10(%rsp)
movq %rdx, %rax
movq %rdi, %rdx
incq %r8
addq $0x20, %rcx
cmpq %r8, %rdx
jne 0x1786f7
movq 0x8(%rsp), %rsi
movq 0x18(%rsi), %rcx
movl (%rcx,%r11,8), %ecx
addq %rcx, %r10
incq %r11
movl 0x10(%rsi), %ecx
cmpq %rcx, %r11
jb 0x1786ae
jmp 0x1787ca
movq %r9, %r8
movq $0x0, 0x10(%rsp)
cmpq %rdx, %r8
movq 0x38(%rsp), %r15
movq 0x20(%rsp), %rsi
jne 0x178857
movq %rdx, %rcx
subq %rsi, %rcx
movq %rdx, %r9
cmpq %rcx, 0x10(%rsp)
jne 0x178857
incq %rdx
movq (%rbx), %rcx
cmpq %rcx, %rdx
jbe 0x1785d7
jmp 0x1787fc
xorl %eax, %eax
movq 0x48(%rsp), %rcx
decq %rcx
movq %rax, (%rbx)
movq %rcx, 0x48(%rbx)
movq 0x28(%rsp), %rax
movq %rax, 0x50(%rbx)
movq 0x18(%rsp), %rax
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x16eaec
leaq 0xb8d7c(%rip), %rdi     # 0x2315bb
leaq 0xb8d95(%rip), %rsi     # 0x2315db
leaq 0xb9453(%rip), %rcx     # 0x231ca0
movl $0x1178, %edx           # imm = 0x1178
callq 0x2110
leaq 0xb8ded(%rip), %rdi     # 0x23164b
leaq 0xb8d76(%rip), %rsi     # 0x2315db
leaq 0xb9434(%rip), %rcx     # 0x231ca0
movl $0x1199, %edx           # imm = 0x1199
callq 0x2110