// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %r8, 0x38(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, %r13
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %rbx
testq %rax, %rax
je 0x135db5
movq $-0x1, (%rax)
testq %rbx, %rbx
je 0x136198
movq 0xa0(%rsp), %rbp
movq 0x30(%rsp), %rdx
leaq -0x1(%rdx), %rax
movq %rax, 0x48(%rsp)
movq %rdx, %rax
shlq $0x20, %rax
movq 0x28(%rsp), %rsi
orq %rsi, %rax
movq %rax, 0x60(%rsp)
movq %rdi, 0x18(%rsp)
leaq 0x1(%rsi), %rax
testq %r13, %r13
setg %cl
cmpq %r13, %rax
setl %dil
andb %cl, %dil
movq %rsi, %rax
subq %rdx, %rax
movq %rax, 0x20(%rsp)
leaq 0x2(%rsi), %rax
movq %rax, 0x40(%rsp)
movq 0x18(%rsp), %rax
addq %rsi, %rax
incq %rax
movq %rax, 0x58(%rsp)
xorb $0x1, %dil
movb %dil, 0xf(%rsp)
xorl %r14d, %r14d
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rax
cmpq %rax, (%rcx)
jne 0x135e69
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq 0x10(%rsp), %rbx
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq 0x10(%rsp), %rax
movq (%rax), %r11
movq 0x10(%rax), %r12
leaq 0x1(%r11), %rcx
movq %rcx, (%rax)
shlq $0x4, %r11
movq 0xa8(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq %rdx, %rsi
shrq $0x3f, %rsi
movq 0x30(%rbp), %r8
movq %r8, %rdi
andq %rdx, %rdi
addq %rdx, %rdx
xorl %ecx, %ecx
subb 0x18(%rbp), %cl
shrq %cl, %rdx
movq (%rbp), %rcx
movq 0x10(%rcx), %r9
leaq (%rdx,%rdx,4), %r10
movl (%r9,%r10,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdi, %rbx
notq %rbx
addq 0x48(%rsp), %rbx
addq %rdx, %rbx
cmpq 0x38(%rsp), %rsi
cmoveq %rdi, %rbx
movq 0x60(%rsp), %rax
movq %rax, 0x8(%r12,%r11)
movq %rbx, %rdi
subq 0x28(%rsp), %rdi
movq %r14, 0x50(%rsp)
jae 0x135f0c
movq %r12, %rax
movq 0x28(%rsp), %r12
subq %rbx, %r12
movb 0x28(%rbp), %cl
decb %cl
movl $0x1, %edi
shlq %cl, %rdi
addq %r12, %rdi
movq %rax, %r12
addq %r11, %r12
xorl %ecx, %ecx
movq 0x38(%rsp), %r15
cmpq %r15, %rsi
setne %cl
movq %rdi, (%r12)
movq %rbp, %r14
movq 0x20(%rbp), %r11
movq 0xa8(%rsp), %rbp
movq %r12, %rax
movq 0x50(%rsp), %r12
andq (%rbp,%r12,8), %r11
movq %rax, %r12
shlq $0x3f, %rcx
orq %rcx, %r11
andq %rdi, %r8
orq %r11, %r8
leaq 0x1(%rbx), %rdi
movq %r8, (%rax)
movq 0x20(%r9,%r10,8), %rcx
cmpq %r15, %rsi
jne 0x135fc7
cmpq %rdx, %rdi
setge %dil
orb 0xf(%rsp), %dil
jne 0x136043
leaq 0x2(%rbx), %rdi
leaq (%rcx,%rbx), %r8
incq %r8
xorl %r10d, %r10d
movq %r14, %rbp
movq 0x40(%rsp), %r14
movq 0x18(%rsp), %rax
movq 0x58(%rsp), %r9
movb (%r9,%r10), %r9b
cmpb (%r8,%r10), %r9b
jne 0x136050
leaq 0x1(%r10), %r9
cmpq %r13, %r9
jge 0x136058
leaq (%r14,%r10), %r11
cmpq %r13, %r11
jge 0x136058
addq %rdi, %r10
cmpq %rdx, %r10
movq %r9, %r10
jl 0x135f8a
jmp 0x136058
cmpq %rdx, %rdi
setge %dil
orb 0xf(%rsp), %dil
jne 0x136043
leaq 0x2(%rbx), %r8
movq %rdx, %rdi
subq %rbx, %rdi
leaq (%rcx,%rdi), %r10
addq $-0x2, %r10
xorl %edi, %edi
movq %r14, %rbp
movq 0x40(%rsp), %r14
movq 0x18(%rsp), %rax
movq 0x58(%rsp), %r9
movsbl (%r9,%rdi), %r9d
movzbl (%r10), %r11d
leaq 0xf7a05(%rip), %r15     # 0x22da10
movzbl (%r15,%r11), %r11d
leaq 0xf9037(%rip), %r15     # 0x22f04e
movzbl (%r11,%r15), %r11d
cmpl %r11d, %r9d
jne 0x136055
leaq 0x1(%rdi), %r9
cmpq %r13, %r9
jge 0x136058
leaq (%r14,%rdi), %r11
cmpq %r13, %r11
jge 0x136058
addq %r8, %rdi
decq %r10
cmpq %rdx, %rdi
movq %r9, %rdi
jl 0x135ff6
jmp 0x136058
xorl %r9d, %r9d
movq %r14, %rbp
movq 0x18(%rsp), %rax
jmp 0x136058
movq %r10, %r9
jmp 0x136058
movq %rdi, %r9
movq 0xb8(%rsp), %r8
testq %r8, %r8
je 0x13607b
movq 0xb0(%rsp), %rdi
cmpq %rdi, 0x48(%rbp)
jne 0x13607b
cmpq (%r8), %r9
jae 0x13607b
movq %r9, (%r8)
movq %r9, %r8
shlq $0x20, %r8
addq %r9, %r8
addq 0x8(%r12), %r8
movq %r8, 0x8(%r12)
movq 0x28(%rsp), %rdi
cmpq 0x30(%rsp), %rdi
movq 0x50(%rsp), %r14
jb 0x136184
movq %rbx, %r9
subq 0x30(%rsp), %r9
jb 0x136184
cmpq 0x38(%rsp), %rsi
jne 0x13610c
testq %r13, %r13
jle 0x136176
movq %r9, %rdx
orq 0x20(%rsp), %rdx
movl $0x0, %esi
js 0x136178
xorl %esi, %esi
movq 0x20(%rsp), %rdx
movb (%rax,%rdx), %dil
cmpb (%rcx,%r9), %dil
jne 0x136178
incq %rsi
cmpq %r13, %rsi
jge 0x136178
testq %rdx, %rdx
jle 0x136178
leaq -0x1(%r9), %rdi
decq %rdx
testq %r9, %r9
movq %rdi, %r9
jg 0x1360dc
jmp 0x136178
testq %r13, %r13
jle 0x136176
movq %r9, %rsi
orq 0x20(%rsp), %rsi
movl $0x0, %esi
js 0x136178
addq 0x48(%rsp), %rcx
subq %rbx, %rdx
addq %rcx, %rdx
xorl %esi, %esi
movq 0x20(%rsp), %rcx
movsbl (%rax,%rcx), %edi
movzbl (%rdx,%rsi), %r10d
leaq 0xf78ce(%rip), %r11     # 0x22da10
movzbl (%r11,%r10), %r10d
leaq 0xf8f00(%rip), %r11     # 0x22f04e
movzbl (%r10,%r11), %r10d
cmpl %r10d, %edi
jne 0x136178
incq %rsi
cmpq %r13, %rsi
jge 0x136178
testq %rcx, %rcx
jle 0x136178
decq %rcx
leaq -0x1(%r9), %rdi
testq %r9, %r9
movq %rdi, %r9
jg 0x136132
jmp 0x136178
xorl %esi, %esi
shlq $0x20, %rsi
addq %r8, %rsi
movq %rsi, 0x8(%r12)
incq %r14
movq 0xb0(%rsp), %rbx
cmpq %rbx, %r14
jne 0x135e31
movq 0x10(%rsp), %rax
movq (%rax), %r15
movq 0x10(%rax), %r13
shlq $0x4, %r15
leaq (%r15,%r13), %rsi
movq %rbx, %rbp
shlq $0x4, %rbp
movq %rsi, %r14
subq %rbp, %r14
cmpq $0x2, %rbx
jb 0x1361c7
movq %r14, %rdi
callq 0x1339a2
testq %rbx, %rbx
je 0x13626b
leaq 0x1(%rbx), %rax
cmpq $0x3, %rax
movl $0x2, %ecx
cmovaeq %rax, %rcx
movq %r15, %rax
subq %rbp, %rax
leaq (%rax,%r13), %rdx
addq $0x18, %rdx
movl $0x1, %esi
xorl %r9d, %r9d
xorl %edi, %edi
xorl %r8d, %r8d
cmpq %rsi, %rbx
je 0x136212
movq (%rdx), %rax
movq %r8, %r10
shlq $0x4, %r10
cmpq 0x8(%r14,%r10), %rax
je 0x136257
movq %r9, %r10
shrq $0x20, %r10
movq %r8, %rax
shlq $0x4, %rax
movq 0x8(%r14,%rax), %rax
movq %rax, %r11
shrq $0x20, %r11
cmpq %r11, %r10
jae 0x136239
movq %rsi, %rdi
subq %r8, %rdi
jmp 0x13624f
cmpl %r11d, %r10d
jne 0x136254
movq %rsi, %r10
subq %r8, %r10
cmpq %rdi, %r10
cmovaq %r10, %rdi
cmovbeq %r9, %rax
movq %rsi, %r8
jmp 0x13625a
movq %rsi, %r8
movq %r9, %rax
incq %rsi
addq $0x10, %rdx
movq %rax, %r9
cmpq %rsi, %rcx
jne 0x1361fc
jmp 0x13626d
xorl %eax, %eax
movq 0x10(%rsp), %rcx
subq %rbx, (%rcx)
testq %rbx, %rbx
je 0x1362ad
subq %rbp, %r15
leaq (%r15,%r13), %rdx
addq $0x8, %rdx
xorl %ecx, %ecx
cmpq %rax, (%rdx)
jne 0x1362a2
leaq -0x8(%rdx), %rsi
movq %rcx, %rdi
shlq $0x4, %rdi
movups (%rsi), %xmm0
movups %xmm0, (%r14,%rdi)
incq %rcx
addq $0x10, %rdx
decq %rbx
jne 0x136287
jmp 0x1362af
xorl %ecx, %ecx
movq 0x10(%rsp), %rdx
addq %rcx, (%rdx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq