// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]oatk/syncasm.c
// Function: calc_syncmer_overlap
static int calc_syncmer_overlap(sr_db_t *sr_db, syncmer_t *m1, uint64_t rc1, syncmer_t *m2, uint64_t rc2, void *hm)
{
    // more precisely the distance between adjacent syncmers
    // always m1 -> m2
    // rc1 and rc2 specify the strand of m1 and m2
    uint64_t *pos1, *pos2, n1, n2, r1, r2, i1, i2, l1, l2, c1, c2;
    uint32_t i, p1, p2, c;

    pos1 = m1->m_pos;
    pos2 = m2->m_pos;
    n1 = m1->cov;
    n2 = m2->cov;

    assert(n1>0 && n2>0);

    // compare each position pair to calculate overlap
    kh_generic_t *h;
    h = hm? (kh_generic_t *) hm : kh_generic_init();
    kh_generic_m_clear(h);
    for (p1 = p2 = 0; p1 < n1; ++p1) {
        r1 = pos1[p1]>>32;
        i1 = pos1[p1]>>1&MAX_RD_SCM;
        if (sr_db->a[r1].k_mer[i1]&1) continue; // this mer is error-corrected
        l1 = sr_db->a[r1].m_pos[i1]>>1;
        c1 = pos1[p1]&1;
        while (p2 < n2 && (r2 = pos2[p2]>>32) < r1) ++p2;
        if (r1 != r2) continue;
        for (i = p2; i < n2; ++i) {
            c = 0;

            r2 = pos2[i]>>32;
            if (r1 != r2) break;
            i2 = pos2[i]>>1&MAX_RD_SCM;
            if (sr_db->a[r2].k_mer[i2]&1) continue; // this mer is error-corrected
            l2 = sr_db->a[r2].m_pos[i2]>>1;
            c2 = pos2[i]&1;

            // rc1 == 0 && rc2 == 0
            //   c1 == 0 && c2 == 0 && i1+1 == i2
            //   c1 == 1 && c2 == 1 && i1 == i2+1
            //
            // rc1 == 0 && rc2 == 1
            //   c1 == 0 && c2 == 1 && i1+1 == i2
            //   c1 == 1 && c2 == 0 && i1 == i2+1
            //
            // rc1 == 1 && rc2 == 0
            //   c1 == 1 && c2 == 0 && i1+1 == i2
            //   c1 == 0 && c2 == 1 && i1 == i2+1
            //
            // rc1 == 1 && rc2 == 1
            //   c1 == 1 && c2 == 1 && i1+1 == i2
            //   c1 == 0 && c2 == 0 && i1 == i2+1

            if (i1 == i2+1 && c1 != rc1 && c2 != rc2) {
                add_ovl_count(h, l1 - l2);
                ++c;
            } else if (i1+1 == i2 && c1 == rc1 && c2 == rc2) {
                add_ovl_count(h, l2 - l1);
                ++c;
            }

            if (c > 1) {
                // this should never happen
                // for checking only
                fprintf(stderr, "[W::%s] multiple syncmer order found: %s ", __func__, sr_db->a[r1].sname);
                print_hoco_seq(&sr_db->a[r1], stderr);
                fprintf(stderr, "[W::%s] == %lu %lu %lu\n", __func__, r1, i1, l1);
                for (i = p2; i < n2; ++i) {
                    r2 = pos2[i]>>32;
                    if (r1 != r2) break;
                    i2 = pos2[i]>>1&MAX_RD_SCM;
                    l2 = sr_db->a[r2].m_pos[i2]>>1;
                    fprintf(stderr, "[W::%s] ## %lu %lu %lu\n", __func__, r2, i2, l2);
                }
            }
        }
    }

    int movl, mcnt, smovl, smcnt, cnt;
    khint_t k;
    movl = mcnt = smovl = smcnt = 0;
    for (k = (khint_t) 0; k < kh_end(h); ++k) {
        if (kh_exist(h, k)) {
            cnt = kh_val(h, k);
            if (cnt > mcnt) {
                smcnt = mcnt;
                smovl = movl;
                mcnt = cnt;
                movl = kh_key(h, k);
            } else if (cnt > smcnt) {
                smcnt = cnt;
                smovl = kh_key(h, k);
            }
        }
    }

#ifdef DEBUG_KMER_OVERLAP
    fprintf(stderr, "[DEBUG_KMER_OVERLAP::%s] overlap count: %d[%d], %d[%d]\n", __func__, movl, mcnt, smovl, smcnt);
#endif
    
    if (!hm) kh_generic_destroy(h);

    // TODO overlap/gap length could be a indication of haplotypic differences
    // return smovl? 0 : movl;
    return movl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, 0x28(%rsp)
movq %rdx, 0x48(%rsp)
movl %esi, %ebp
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ebp
je 0x69e8
movl %r8d, %ebx
andl %eax, %ebx
je 0x69e8
movq %r9, %r14
movq %rdi, %r9
movq 0xa8(%rsp), %rax
movq %rax, %r8
testq %rax, %rax
movq %rdi, 0x10(%rsp)
jne 0x678d
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1d660
movq 0x10(%rsp), %r9
movq %rax, %r8
movq $0x0, 0x18(%rax)
movq 0x8(%r8), %rdi
testq %rdi, %rdi
movq %r8, 0x20(%rsp)
je 0x67cf
movl (%r8), %ecx
movl $0x1, %eax
shll %cl, %eax
shrl $0x3, %eax
andl $-0x4, %eax
cmpl $0x5, %ecx
movl $0x4, %edx
cmovael %eax, %edx
xorl %esi, %esi
callq 0x21e0
movq 0x10(%rsp), %r9
movq 0x20(%rsp), %r8
movl $0x0, 0x4(%r8)
movq 0xa0(%rsp), %rdi
movl %ebx, 0x8(%rsp)
movl %ebx, %eax
movq %rax, 0x30(%rsp)
movl %ebp, %eax
movq %rax, 0x40(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r14, 0x18(%rsp)
movq 0x48(%rsp), %rcx
movq (%rcx,%r12,8), %r10
movq %r10, %rbx
shrq $0x20, %rbx
movl %r10d, %r13d
shrl %r13d
movq 0x10(%r9), %rcx
imulq $0x58, %rbx, %rbp
movq 0x50(%rcx,%rbp), %rdx
testb $0x1, (%rdx,%r13,8)
jne 0x6950
addq %rbp, %rcx
movq 0x40(%rcx), %rcx
movl (%rcx,%r13,4), %ecx
movl %ecx, 0xc(%rsp)
cmpl 0x8(%rsp), %r15d
jae 0x6861
movl %r15d, %r15d
movq 0x30(%rsp), %rcx
movq (%r14,%r15,8), %rax
shrq $0x20, %rax
cmpq %rbx, %rax
jae 0x6861
incq %r15
cmpq %r15, %rcx
jne 0x6840
movl 0x8(%rsp), %ecx
movl %ecx, %r15d
jmp 0x6950
cmpq %rax, %rbx
sete %cl
movl 0x8(%rsp), %edx
movl %edx, %r11d
subl %r15d, %r11d
seta %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x6950
shrl 0xc(%rsp)
andl $0x1, %r10d
leaq 0x1(%r13), %rax
movq %rax, 0x38(%rsp)
movl %r15d, %eax
leaq (%r14,%rax,8), %rax
movq %rax, 0x60(%rsp)
xorl %r14d, %r14d
movq %r10, 0x58(%rsp)
movq %r11, 0x50(%rsp)
movq 0x60(%rsp), %rax
movq (%rax,%r14,8), %rcx
movq %rcx, %rax
shrq $0x20, %rax
cmpl %eax, %ebx
jne 0x694b
movl %ecx, %eax
shrl %eax
movq 0x10(%r9), %rdx
movq 0x50(%rdx,%rbp), %rsi
testb $0x1, (%rsi,%rax,8)
jne 0x693c
addq %rbp, %rdx
movq 0x40(%rdx), %rdx
movl (%rdx,%rax,4), %edx
shrl %edx
andl $0x1, %ecx
cmpq %rdi, %rcx
je 0x6900
cmpq 0x28(%rsp), %r10
je 0x6900
leaq 0x1(%rax), %rsi
cmpl %esi, %r13d
jne 0x6900
movl 0xc(%rsp), %esi
subl %edx, %esi
jmp 0x6918
cmpq %rdi, %rcx
jne 0x693c
cmpq 0x28(%rsp), %r10
jne 0x693c
cmpl %eax, 0x38(%rsp)
jne 0x693c
subl 0xc(%rsp), %edx
movl %edx, %esi
movq %r8, %rdi
callq 0xd242
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %r9
movq 0xa0(%rsp), %rdi
movq 0x58(%rsp), %r10
movq 0x50(%rsp), %r11
incq %r14
cmpl %r14d, %r11d
jne 0x68a9
movq %rbx, %rax
movq 0x18(%rsp), %r14
incq %r12
cmpq 0x40(%rsp), %r12
jne 0x67f4
movq 0x10(%r8), %rsi
xorl %eax, %eax
xorl %ebp, %ebp
xorl %edx, %edx
testq %rsi, %rsi
je 0x697a
movb (%r8), %cl
movl $0x1, %edi
shlq %cl, %rdi
jmp 0x697c
xorl %edi, %edi
cmpq %rdi, %rax
jae 0x69a6
movq 0x8(%r8), %rcx
movq %rax, %rdi
shrq $0x5, %rdi
movl (%rcx,%rdi,4), %ecx
btl %eax, %ecx
jae 0x69a1
movl 0x4(%rsi,%rax,8), %ecx
cmpl %edx, %ecx
jle 0x69a1
movl (%rsi,%rax,8), %ebp
movl %ecx, %edx
incq %rax
jmp 0x6968
cmpq $0x0, 0xa8(%rsp)
jne 0x69d7
movq 0x18(%r8), %r14
movq %r14, %rdi
movq %r8, %rbx
callq 0x1d6fb
movq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x1d6fb
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1d6fb
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19333(%rip), %rdi     # 0x1fd22
leaq 0x19055(%rip), %rsi     # 0x1fa4b
leaq 0x19332(%rip), %rcx     # 0x1fd2f
movl $0x1ea, %edx            # imm = 0x1EA
callq 0x21d0