// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %r9, 0x8(%rsp)
movq %r8, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movl 0x10(%rsi), %r13d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r13
je 0x4f89
movl 0x10(%rcx), %ebx
andq %rax, %rbx
testl %ebx, %ebx
je 0x4f89
movq %rdi, %r14
movq 0x18(%rsi), %rax
movq %rax, 0x38(%rsp)
movq 0x18(%rcx), %rbp
movq 0x8(%rsp), %rax
movq %rax, %r9
testq %rax, %rax
movq %rdi, 0x18(%rsp)
jne 0x4d88
callq 0x56cb
movq %rax, %r9
testq %rax, %rax
je 0x4dc1
movq 0x8(%r9), %rdi
testq %rdi, %rdi
je 0x4dc4
movl (%r9), %ecx
pushq $0x1
popq %rax
shll %cl, %eax
shrl $0x3, %eax
andl $-0x4, %eax
cmpl $0x5, %ecx
pushq $0x4
popq %rdx
cmovael %eax, %edx
xorl %esi, %esi
movq %r9, %r14
callq 0x21e0
movq %r14, %r9
movq 0x18(%rsp), %r14
andl $0x0, 0x4(%r9)
jmp 0x4dc4
xorl %r9d, %r9d
xorl %edx, %edx
movl $0x0, (%rsp)
movq %r13, 0x10(%rsp)
cmpq %r13, %rdx
je 0x4f1d
movq 0x38(%rsp), %rcx
movq %rdx, 0x50(%rsp)
movq (%rcx,%rdx,8), %rdi
movq %rdi, %r15
shrq $0x20, %r15
movl %edi, %r8d
shrl %r8d
movq 0x10(%r14), %rcx
imulq $0x58, %r15, %r12
movq 0x50(%rcx,%r12), %rdx
testb $0x1, (%rdx,%r8,8)
jne 0x4f10
addq %r12, %rcx
movq 0x40(%rcx), %rcx
movl (%rcx,%r8,4), %ecx
shrl %ecx
movl %ecx, 0x4(%rsp)
andl $0x1, %edi
movl (%rsp), %edx
movl %edx, %ecx
cmpl %ebx, %edx
cmovbel %ebx, %edx
movl %edx, (%rsp)
cmpq %rbx, %rcx
jae 0x4e4a
movq (%rbp,%rcx,8), %rax
shrq $0x20, %rax
cmpq %r15, %rax
jae 0x4e47
incq %rcx
jmp 0x4e2f
movl %ecx, (%rsp)
cmpq %rax, %r15
jne 0x4f10
leaq 0x1(%r8), %rax
movq %rax, 0x30(%rsp)
movl (%rsp), %r13d
movq %rdi, 0x48(%rsp)
movq %r8, 0x40(%rsp)
cmpq %rbx, %r13
jae 0x4f08
movq (%rbp,%r13,8), %rcx
movq %rcx, %rax
shrq $0x20, %rax
cmpl %eax, %r15d
jne 0x4f0b
movl %ecx, %eax
shrl %eax
movq 0x10(%r14), %rdx
movq 0x50(%rdx,%r12), %rsi
testb $0x1, (%rsi,%rax,8)
jne 0x4f00
addq %r12, %rdx
movq 0x40(%rdx), %rdx
movl (%rdx,%rax,4), %edx
shrl %edx
andl $0x1, %ecx
cmpq 0x28(%rsp), %rcx
je 0x4ec9
cmpq 0x20(%rsp), %rdi
je 0x4ec9
leaq 0x1(%rax), %rsi
cmpl %esi, %r8d
jne 0x4ec9
movl 0x4(%rsp), %esi
subl %edx, %esi
jmp 0x4ee3
cmpq 0x28(%rsp), %rcx
jne 0x4f00
cmpq 0x20(%rsp), %rdi
jne 0x4f00
cmpl %eax, 0x30(%rsp)
jne 0x4f00
subl 0x4(%rsp), %edx
movl %edx, %esi
movq %r9, %rdi
movq %r9, %r14
callq 0x9ebe
movq %r14, %r9
movq 0x18(%rsp), %r14
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %r8
incq %r13
jmp 0x4e6a
movq %r15, %rax
movq 0x10(%rsp), %r13
movq 0x50(%rsp), %rdx
incq %rdx
jmp 0x4dd2
movq 0x10(%r9), %rax
xorl %edx, %edx
pushq $0x1
popq %rsi
xorl %ebp, %ebp
xorl %edi, %edi
testq %rax, %rax
je 0x4f3a
movb (%r9), %cl
movq %rsi, %r8
shlq %cl, %r8
jmp 0x4f3d
xorl %r8d, %r8d
cmpq %r8, %rdx
jae 0x4f68
movq 0x8(%r9), %rcx
movq %rdx, %r8
shrq $0x5, %r8
movl (%rcx,%r8,4), %ecx
btl %edx, %ecx
jae 0x4f63
movl 0x4(%rax,%rdx,8), %ecx
cmpl %edi, %ecx
jle 0x4f63
movl (%rax,%rdx,8), %ebp
movl %ecx, %edi
incq %rdx
jmp 0x4f2a
cmpq $0x0, 0x8(%rsp)
jne 0x4f78
movq %r9, %rdi
callq 0x56e0
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14d5a(%rip), %rdi     # 0x19cea
leaq 0x14a7c(%rip), %rsi     # 0x19a13
leaq 0x14d59(%rip), %rcx     # 0x19cf7
movl $0x1ea, %edx            # imm = 0x1EA
callq 0x21d0