// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: get_longest_hit(char*, unsigned long, unsigned long, unsigned long, unsigned long, kvec_vote*, ha_ug_index const*, unsigned long*, unsigned long, unsigned long*)
uint64_t get_longest_hit(char *r, uint64_t len, uint64_t k_mer, uint64_t self_p, uint64_t self_rev, kvec_vote* buf, const ha_ug_index* idx, 
uint64_t *pos_list, uint64_t cnt, uint64_t* c_sfx)
{
    uint64_t max_p, map_p_occ, i, j, m, rev, ref_p, u_len, uID, k_len;
    s_hit *p = NULL;
    ///each k-mer at different unitigs
    ///rev:uID:pos
    if(c_sfx) (*c_sfx) = (uint64_t)-1;
    for (j = 0; j < cnt; j++)
    {
        ///get
        kv_pushp(s_hit, buf->a, &p);
        rev = (pos_list[j]>>63) != self_rev;
        ref_p = pos_list[j] & idx->pos_mode;
        uID = (pos_list[j] << 1) >> (64 - idx->uID_bits);
        u_len = idx->ug->u.a[uID].len;
        if(rev) ref_p = u_len - 1 - (ref_p + 1 - k_mer);
        p->off_cnt = self_p | ((uint64_t)k_mer << 32); ///high bits should be the legnth

        p->ref = ref_p >= self_p? (ref_p-self_p) 
                        : (self_p-ref_p) + ((uint64_t)1 << (idx->pos_bits - 1));
        p->ref = (rev << 63)|(pos_list[j] & idx->uID_mode)|(p->ref&idx->pos_mode);


        ///extend
        k_len = check_exact_match(r, self_p + 1, len, idx->ug->u.a[uID].s, ref_p + 1, u_len, len, rev, 0);
        if(c_sfx && cnt == idx->hap_cnt && k_len < (*c_sfx)) (*c_sfx) = k_len;
        
        p->off_cnt += ((uint64_t)k_len << 32) + k_len;

        if(self_p >= k_mer && ref_p >= k_mer)
        {
            k_len = check_exact_match(r, self_p - k_mer, len, idx->ug->u.a[uID].s, 
                                                    ref_p - k_mer, u_len, len, rev, 1);
            p->off_cnt += ((uint64_t)k_len << 32);
        }
        // if(cnt > 0) fprintf(stderr, "inner j: %lu, rev: %lu, uID: %lu, ref_p: %lu, self_p: %u, len: %lu\n", j, rev, uID, ref_p, (uint32_t)p->off_cnt, p->off_cnt>>32);
    }

    p = buf->a.a + buf->a.n - cnt;
    if(cnt > 1) radix_sort_hc_s_hit_off_cnt(p, p + cnt);
    max_p = map_p_occ = 0; 
    for (j = 1, i = 0; j <= cnt; ++j)
    {
        if(j == cnt || p[j].off_cnt != p[i].off_cnt)
        {
            if((max_p>>32) < (p[i].off_cnt>>32))
            {
                max_p = p[i].off_cnt;
                map_p_occ = j - i;
            }
            else if(((max_p>>32) == (p[i].off_cnt>>32)) && ((j - i) > map_p_occ))
            {
                max_p = p[i].off_cnt;
                map_p_occ = j - i;
            }
            i = j;///must
        }
    }

    buf->a.n -= cnt;
    for (j = m = 0; j < cnt; j++)
    {
        if(p[j].off_cnt == max_p)
        {
            p[m] = p[j];
            m++;
        }
    }
    cnt = m;
    buf->a.n += cnt;
    
    // if(cnt > 0) fprintf(stderr, "max_p_offset: %u, max_p_len: %lu, map_p_occ: %lu\n", (uint32_t)max_p, max_p>>32, map_p_occ);
    return max_p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x8(%rsp)
movq %r8, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %rbp
movq 0xb8(%rsp), %rax
testq %rax, %rax
je 0x13bcf0
movq $-0x1, (%rax)
movq 0xb0(%rsp), %rax
testq %rax, %rax
je 0x13c1e0
movq 0xa0(%rsp), %r14
movq 0x20(%rsp), %rax
leaq -0x1(%rax), %rdx
movq %rdx, 0x48(%rsp)
movq %rax, %rsi
shlq $0x20, %rsi
orq %rcx, %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rsp)
movq %rcx, %rdx
subq %rax, %rdx
movq %rdx, 0x18(%rsp)
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rax
movq %rax, 0x10(%rsp)
leaq 0x2(%rcx), %rax
movq %rax, 0x40(%rsp)
movq %rcx, 0x50(%rsp)
leaq (%rdi,%rcx), %rax
incq %rax
movq %rax, 0x58(%rsp)
xorl %r15d, %r15d
movq %rdi, 0x60(%rsp)
movq %rsi, 0x30(%rsp)
movq (%rdx), %r12
cmpq 0x8(%rdx), %r12
jne 0x13bda3
leaq (%r12,%r12), %rsi
testq %r12, %r12
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdx)
shlq $0x4, %rsi
movq 0x10(%rsp), %rdi
callq 0x2370
movq 0x8(%rsp), %rdx
movq %rax, 0x10(%rdx)
movq (%rdx), %r12
jmp 0x13bda8
movq 0x10(%rsp), %rax
leaq 0x1(%r12), %rcx
movq %rcx, (%rdx)
shlq $0x4, %r12
movq 0xa8(%rsp), %rcx
movq (%rcx,%r15,8), %rdx
movq %rdx, %rsi
shrq $0x3f, %rsi
movq 0x30(%r14), %r8
movq %r8, %r11
andq %rdx, %r11
addq %rdx, %rdx
xorl %ecx, %ecx
subb 0x18(%r14), %cl
shrq %cl, %rdx
movq (%r14), %rcx
movq 0x10(%rcx), %r9
leaq (%rdx,%rdx,4), %r10
movl (%r9,%r10,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %r11, %rdi
notq %rdi
addq 0x48(%rsp), %rdi
addq %rdx, %rdi
cmpq 0x28(%rsp), %rsi
cmoveq %r11, %rdi
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rcx
movq %rcx, 0x8(%rax,%r12)
movq %rdi, %r11
movq 0x50(%rsp), %r13
subq %r13, %r11
jae 0x13be3a
subq %rdi, %r13
movb 0x28(%r14), %cl
decb %cl
movl $0x1, %r11d
shlq %cl, %r11
addq %r13, %r11
addq 0x10(%rsp), %r12
xorl %ecx, %ecx
movq 0x28(%rsp), %rax
cmpq %rax, %rsi
setne %cl
movq %r11, (%r12)
movq %r14, %rbx
movq 0x20(%r14), %r13
movq 0xa8(%rsp), %r14
andq (%r14,%r15,8), %r13
shlq $0x3f, %rcx
orq %rcx, %r13
andq %r11, %r8
orq %r13, %r8
leaq 0x1(%rdi), %r11
movq %r8, (%r12)
movq 0x20(%r9,%r10,8), %r8
cmpq %rax, %rsi
jne 0x13bef7
testq %rbp, %rbp
movq 0x40(%rsp), %rax
jle 0x13bf7d
cmpq %rbp, 0x38(%rsp)
movq %rbx, %r14
jge 0x13bf84
movl $0x0, %ecx
cmpq %rdx, %r11
jge 0x13bf86
leaq 0x2(%rdi), %r9
leaq (%r8,%rdi), %r10
incq %r10
xorl %r11d, %r11d
movq 0x58(%rsp), %rcx
movb (%rcx,%r11), %cl
cmpb (%r10,%r11), %cl
jne 0x13bf78
leaq 0x1(%r11), %rcx
cmpq %rbp, %rcx
jge 0x13bf86
leaq (%rax,%r11), %r13
cmpq %rbp, %r13
jge 0x13bf86
addq %r9, %r11
cmpq %rdx, %r11
movq %rcx, %r11
jl 0x13beba
jmp 0x13bf86
testq %rbp, %rbp
movq 0x40(%rsp), %rax
jle 0x13bf7d
cmpq %rbp, 0x38(%rsp)
movq %rbx, %r14
jge 0x13bf84
movl $0x0, %ecx
cmpq %rdx, %r11
jge 0x13bf86
leaq 0x2(%rdi), %r9
movq %rdx, %rcx
subq %rdi, %rcx
leaq (%r8,%rcx), %r10
addq $-0x2, %r10
xorl %r11d, %r11d
movq 0x58(%rsp), %rcx
movsbl (%rcx,%r11), %ecx
movzbl (%r10), %r13d
leaq 0xfdce1(%rip), %rbx     # 0x239c20
movzbl (%rbx,%r13), %r13d
leaq 0xff353(%rip), %rbx     # 0x23b29e
movzbl (%r13,%rbx), %r13d
cmpl %r13d, %ecx
jne 0x13bf78
leaq 0x1(%r11), %rcx
cmpq %rbp, %rcx
jge 0x13bf86
leaq (%rax,%r11), %r13
cmpq %rbp, %r13
jge 0x13bf86
addq %r9, %r11
decq %r10
cmpq %rdx, %r11
movq %rcx, %r11
jl 0x13bf2a
jmp 0x13bf86
movq %r11, %rcx
jmp 0x13bf86
xorl %ecx, %ecx
movq %rbx, %r14
jmp 0x13bf86
xorl %ecx, %ecx
movq 0x30(%rsp), %rax
movq %rax, %r9
movq 0xb8(%rsp), %r10
testq %r10, %r10
je 0x13bfc1
movq %rax, %r9
movq 0xb0(%rsp), %rax
cmpq %rax, 0x48(%r14)
movq 0x30(%rsp), %rax
jne 0x13bfc1
movq %rax, %r9
cmpq (%r10), %rcx
jae 0x13bfc1
movq %rcx, (%r10)
movq 0x8(%r12), %r9
addq %rcx, %r9
shlq $0x20, %rcx
addq %r9, %rcx
movq %rcx, 0x8(%r12)
movq 0x20(%rsp), %rax
cmpq %rax, 0x50(%rsp)
movq 0x60(%rsp), %rax
jb 0x13c0c6
movq %rdi, %r9
subq 0x20(%rsp), %r9
jb 0x13c0c6
cmpq 0x28(%rsp), %rsi
jne 0x13c04d
testq %rbp, %rbp
jle 0x13c0b8
movq %r9, %rdx
orq 0x18(%rsp), %rdx
movl $0x0, %esi
js 0x13c0ba
xorl %esi, %esi
movq 0x18(%rsp), %rdx
movb (%rax,%rdx), %dil
cmpb (%r8,%r9), %dil
jne 0x13c0ba
incq %rsi
cmpq %rbp, %rsi
jge 0x13c0ba
testq %rdx, %rdx
jle 0x13c0ba
leaq -0x1(%r9), %rdi
decq %rdx
testq %r9, %r9
movq %rdi, %r9
jg 0x13c01d
jmp 0x13c0ba
testq %rbp, %rbp
jle 0x13c0b8
movq %r9, %rsi
orq 0x18(%rsp), %rsi
movl $0x0, %esi
js 0x13c0ba
addq 0x48(%rsp), %r8
subq %rdi, %rdx
addq %r8, %rdx
xorl %esi, %esi
movq 0x18(%rsp), %rdi
movsbl (%rax,%rdi), %r8d
movzbl (%rdx,%rsi), %r10d
leaq 0xfdb9c(%rip), %r11     # 0x239c20
movzbl (%r11,%r10), %r10d
leaq 0xff20e(%rip), %r11     # 0x23b29e
movzbl (%r10,%r11), %r10d
cmpl %r10d, %r8d
jne 0x13c0ba
incq %rsi
cmpq %rbp, %rsi
jge 0x13c0ba
testq %rdi, %rdi
jle 0x13c0ba
decq %rdi
leaq -0x1(%r9), %r8
testq %r9, %r9
movq %r8, %r9
jg 0x13c073
jmp 0x13c0ba
xorl %esi, %esi
shlq $0x20, %rsi
addq %rcx, %rsi
movq %rsi, 0x8(%r12)
incq %r15
cmpq 0xb0(%rsp), %r15
movq 0x8(%rsp), %rdx
jne 0x13bd6a
movq (%rdx), %rbx
shlq $0x4, %rbx
movq 0x10(%rsp), %r12
leaq (%r12,%rbx), %rsi
movq 0xb0(%rsp), %r13
movq %r13, %r14
shlq $0x4, %r14
movq %rsi, %r15
subq %r14, %r15
cmpq $0x1, %r13
je 0x13c10f
movq %r15, %rdi
callq 0x139b6c
leaq 0x1(%r13), %rax
cmpq $0x3, %rax
movl $0x2, %ecx
cmovaeq %rax, %rcx
subq %r14, %rbx
leaq (%rbx,%r12), %rdx
addq $0x18, %rdx
movl $0x1, %esi
xorl %eax, %eax
xorl %edi, %edi
xorl %r8d, %r8d
movq %r8, %r9
shlq $0x4, %r9
cmpq %rsi, %r13
jne 0x13c14a
movq 0x8(%r15,%r9), %r9
jmp 0x13c154
movq 0x8(%r15,%r9), %r9
cmpq %r9, (%rdx)
je 0x13c18e
movq %rax, %r10
shrq $0x20, %r10
movq %r9, %r11
shrq $0x20, %r11
cmpq %r11, %r10
jae 0x13c175
movq %rsi, %rdi
subq %r8, %rdi
movq %rsi, %r8
movq %r9, %rax
jmp 0x13c18e
cmpl %r11d, %r10d
jne 0x13c18b
movq %rsi, %r10
subq %r8, %r10
cmpq %rdi, %r10
cmovaq %r10, %rdi
cmovaq %r9, %rax
movq %rsi, %r8
incq %rsi
addq $0x10, %rdx
cmpq %rsi, %rcx
jne 0x13c137
movq 0x8(%rsp), %r8
movq (%r8), %rdx
subq %r13, %rdx
movq %rdx, (%r8)
testq %r13, %r13
je 0x13c1ed
leaq (%r12,%rbx), %rdx
addq $0x8, %rdx
xorl %ecx, %ecx
cmpq %rax, (%rdx)
jne 0x13c1d2
leaq -0x8(%rdx), %rsi
movq %rcx, %rdi
shlq $0x4, %rdi
movups (%rsi), %xmm0
movups %xmm0, (%r15,%rdi)
incq %rcx
addq $0x10, %rdx
decq %r13
jne 0x13c1b7
movq (%r8), %rdx
jmp 0x13c1ef
movq 0x8(%rsp), %r8
movq (%r8), %rdx
subq %rax, %rdx
xorl %eax, %eax
xorl %ecx, %ecx
addq %rcx, %rdx
movq %rdx, (%r8)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq