// File: /workspace/github_c_makefile_star2_O1/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 %r8, 0x50(%rsp)
movq %rdx, 0x40(%rsp)
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl 0x10(%rsi), %r14d
andl %r13d, %r14d
je 0x6520
andl 0x10(%rcx), %r13d
je 0x6520
movq %r9, %rbx
movq %rdi, %r8
movq 0x18(%rsi), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rcx), %rax
movq %rax, 0x30(%rsp)
testq %r9, %r9
movq %rdi, 0x18(%rsp)
jne 0x62b6
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1b528
movq 0x18(%rsp), %r8
movq %rax, %r9
movq $0x0, 0x18(%rax)
movq %rbx, 0x58(%rsp)
testq %r9, %r9
movq %r9, 0x38(%rsp)
je 0x6302
movq 0x8(%r9), %rdi
testq %rdi, %rdi
je 0x6302
movl (%r9), %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 0x18(%rsp), %r8
movq 0x38(%rsp), %r9
movl $0x0, 0x4(%r9)
movq %r14, %rdi
cmpl $0x1, %edi
adcl $0x0, %edi
xorl %r11d, %r11d
xorl %ebp, %ebp
movq 0x10(%rsp), %r10
movl %r13d, (%rsp)
movq %rdi, 0x20(%rsp)
movq (%r10,%r11,8), %rax
movq %rax, %r15
shrq $0x20, %r15
movq %rax, 0x8(%rsp)
movl %eax, %esi
shrl %esi
movq 0x10(%r8), %rax
imulq $0x58, %r15, %rcx
movq 0x50(%rax,%rcx), %rdx
movq %rsi, 0x48(%rsp)
testb $0x1, (%rdx,%rsi,8)
jne 0x6482
cmpl %r13d, %ebp
jae 0x6378
movl %ebp, %edx
movq 0x30(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
movq (%rdx), %r14
shrq $0x20, %r14
cmpq %r15, %r14
jae 0x6378
incl %ebp
addq $0x8, %rdx
cmpl %ebp, %r13d
jne 0x635e
movl %r13d, %ebp
cmpq %r14, %r15
sete %dl
movl %r13d, %r12d
subl %ebp, %r12d
seta %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x6482
movq %r11, 0x28(%rsp)
addq %rcx, %rax
movq 0x40(%rax), %rax
movq 0x48(%rsp), %rcx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, 0x4(%rsp)
movq 0x8(%rsp), %rax
andl $0x1, %eax
movq %rax, 0x8(%rsp)
leaq 0x1(%rcx), %rax
movq %rax, 0x60(%rsp)
movl %ebp, %eax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rax,8), %r13
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rcx
movq %rcx, %r14
shrq $0x20, %r14
cmpl %r14d, %r15d
jne 0x646f
movl %ecx, %eax
shrl %eax
movq 0x10(%r8), %rdx
imulq $0x58, %r14, %rsi
movq 0x50(%rdx,%rsi), %rdi
testb $0x1, (%rdi,%rax,8)
jne 0x6463
addq %rsi, %rdx
movq 0x40(%rdx), %rdx
movl (%rdx,%rax,4), %edx
shrl %edx
andl $0x1, %ecx
cmpq 0x50(%rsp), %rcx
je 0x6432
movq 0x8(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x6432
leaq 0x1(%rax), %rsi
cmpl %esi, 0x48(%rsp)
jne 0x6432
movl 0x4(%rsp), %esi
subl %edx, %esi
jmp 0x6451
cmpq 0x50(%rsp), %rcx
jne 0x6463
movq 0x8(%rsp), %rcx
cmpq 0x40(%rsp), %rcx
jne 0x6463
cmpl %eax, 0x60(%rsp)
jne 0x6463
subl 0x4(%rsp), %edx
movl %edx, %esi
movq %r9, %rdi
callq 0xc8e1
movq 0x38(%rsp), %r9
movq 0x18(%rsp), %r8
incq %rbx
cmpl %ebx, %r12d
jne 0x63d2
movl (%rsp), %r13d
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %r10
movq 0x28(%rsp), %r11
incq %r11
cmpq %rdi, %r11
jne 0x631e
movq 0x10(%r9), %rsi
xorl %eax, %eax
xorl %ebp, %ebp
xorl %edx, %edx
movq 0x58(%rsp), %r8
testq %rsi, %rsi
je 0x64af
movb (%r9), %cl
movl $0x1, %edi
shlq %cl, %rdi
jmp 0x64b1
xorl %edi, %edi
cmpq %rdi, %rax
jae 0x64d9
movq 0x8(%r9), %rcx
movl %eax, %edi
shrl $0x5, %edi
movl (%rcx,%rdi,4), %ecx
btl %eax, %ecx
jae 0x64d4
movl 0x4(%rsi,%rax,8), %ecx
cmpl %edx, %ecx
jle 0x64d4
movl (%rsi,%rax,8), %ebp
movl %ecx, %edx
incq %rax
jmp 0x649d
testq %r9, %r9
sete %al
testq %r8, %r8
setne %cl
orb %al, %cl
jne 0x650f
movq 0x18(%r9), %rbx
movq %rbx, %rdi
movq %r9, %r14
callq 0x1b5ca
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x1b5ca
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1b5ca
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x177c3(%rip), %rdi     # 0x1dcea
leaq 0x174e5(%rip), %rsi     # 0x1da13
leaq 0x177c2(%rip), %rcx     # 0x1dcf7
movl $0x1ea, %edx            # imm = 0x1EA
callq 0x21d0