// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x48(%rbp)
jbe 0x630f
cmpq $0x0, -0x50(%rbp)
jbe 0x630f
jmp 0x632e
leaq 0x27974(%rip), %rdi     # 0x2dc8a
leaq 0x27696(%rip), %rsi     # 0x2d9b3
movl $0x1ea, %edx            # imm = 0x1EA
leaq 0x2796e(%rip), %rcx     # 0x2dc97
callq 0x21e0
cmpq $0x0, -0x30(%rbp)
je 0x6342
movq -0x30(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0x634e
callq 0x7630
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
callq 0xe320
movl $0x0, -0x9c(%rbp)
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpq -0x48(%rbp), %rax
jae 0x676a
movq -0x38(%rbp), %rax
movl -0x98(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movl -0x98(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x58, -0x58(%rbp), %rcx
addq %rcx, %rax
movq 0x50(%rax), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x63e8
jmp 0x6756
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x58, -0x58(%rbp), %rcx
addq %rcx, %rax
movq 0x40(%rax), %rax
movq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, %eax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movl -0x98(%rbp), %ecx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
movq %rax, -0x88(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq -0x50(%rbp), %rcx
movb %al, -0xc9(%rbp)
jae 0x645d
movq -0x40(%rbp), %rax
movl -0x9c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x60(%rbp)
cmpq -0x58(%rbp), %rax
setb %al
movb %al, -0xc9(%rbp)
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0x6469
jmp 0x647a
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x6424
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x6489
jmp 0x6756
movl -0x9c(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpq -0x50(%rbp), %rax
jae 0x6754
movl $0x0, -0xa0(%rbp)
movq -0x40(%rbp), %rax
movl -0x94(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x64d4
jmp 0x6754
movq -0x40(%rbp), %rax
movl -0x94(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x58, -0x60(%rbp), %rcx
addq %rcx, %rax
movq 0x50(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x651a
jmp 0x6740
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x58, -0x60(%rbp), %rcx
addq %rcx, %rax
movq 0x40(%rax), %rax
movq -0x70(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movq -0x40(%rbp), %rax
movl -0x94(%rbp), %ecx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jne 0x65a8
movq -0x88(%rbp), %rax
cmpq -0x18(%rbp), %rax
je 0x65a8
movq -0x90(%rbp), %rax
cmpq -0x28(%rbp), %rax
je 0x65a8
movq -0xa8(%rbp), %rdi
movq -0x78(%rbp), %rax
subq -0x80(%rbp), %rax
movl %eax, %esi
callq 0xe3a0
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x65f7
movq -0x68(%rbp), %rax
addq $0x1, %rax
cmpq -0x70(%rbp), %rax
jne 0x65f5
movq -0x88(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x65f5
movq -0x90(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x65f5
movq -0xa8(%rbp), %rdi
movq -0x80(%rbp), %rax
subq -0x78(%rbp), %rax
movl %eax, %esi
callq 0xe3a0
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x65f7
cmpl $0x1, -0xa0(%rbp)
jbe 0x673e
movq 0x2d9ed(%rip), %rax     # 0x33ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x58, -0x58(%rbp), %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
leaq 0x276c8(%rip), %rsi     # 0x2dcf1
leaq 0x276eb(%rip), %rdx     # 0x2dd1b
movb $0x0, %al
callq 0x2290
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
imulq $0x58, -0x58(%rbp), %rax
addq %rax, %rdi
movq 0x2d9aa(%rip), %rax     # 0x33ff8
movq (%rax), %rsi
callq 0x14480
movq 0x2d99b(%rip), %rax     # 0x33ff8
movq (%rax), %rdi
movq -0x58(%rbp), %rcx
movq -0x68(%rbp), %r8
movq -0x78(%rbp), %r9
leaq 0x276bd(%rip), %rsi     # 0x2dd30
leaq 0x276a1(%rip), %rdx     # 0x2dd1b
movb $0x0, %al
callq 0x2290
movl -0x9c(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpq -0x50(%rbp), %rax
jae 0x673c
movq -0x40(%rbp), %rax
movl -0x94(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x66bf
jmp 0x673c
movq -0x40(%rbp), %rax
movl -0x94(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x58, -0x60(%rbp), %rcx
addq %rcx, %rax
movq 0x40(%rax), %rax
movq -0x70(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl %eax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movq 0x2d8f4(%rip), %rax     # 0x33ff8
movq (%rax), %rdi
movq -0x60(%rbp), %rcx
movq -0x70(%rbp), %r8
movq -0x80(%rbp), %r9
leaq 0x2762e(%rip), %rsi     # 0x2dd48
leaq 0x275fa(%rip), %rdx     # 0x2dd1b
movb $0x0, %al
callq 0x2290
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x668d
jmp 0x673e
jmp 0x6740
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x6495
jmp 0x6756
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x637c
movl $0x0, -0xb8(%rbp)
movl $0x0, -0xb4(%rbp)
movl $0x0, -0xb0(%rbp)
movl $0x0, -0xac(%rbp)
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
movl %eax, -0xd0(%rbp)
movq -0xa8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x67ce
movq -0xa8(%rbp), %rax
movl (%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0xd4(%rbp)
jmp 0x67d8
xorl %eax, %eax
movl %eax, -0xd4(%rbp)
jmp 0x67d8
movl -0xd0(%rbp), %eax
movl -0xd4(%rbp), %ecx
cmpl %ecx, %eax
jae 0x68d3
movq -0xa8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0xc0(%rbp), %ecx
shrl $0x5, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl -0xc0(%rbp), %ecx
andl $0x1f, %ecx
shrl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x68bd
movq -0xa8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc0(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %eax
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
cmpl -0xb0(%rbp), %eax
jle 0x6885
movl -0xb0(%rbp), %eax
movl %eax, -0xb8(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, -0xb0(%rbp)
movq -0xa8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc0(%rbp), %ecx
movl (%rax,%rcx,8), %eax
movl %eax, -0xac(%rbp)
jmp 0x68bb
movl -0xbc(%rbp), %eax
cmpl -0xb8(%rbp), %eax
jle 0x68b9
movl -0xbc(%rbp), %eax
movl %eax, -0xb8(%rbp)
movq -0xa8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xc0(%rbp), %ecx
movl (%rax,%rcx,8), %eax
movl %eax, -0xb4(%rbp)
jmp 0x68bb
jmp 0x68bd
jmp 0x68bf
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x679c
cmpq $0x0, -0x30(%rbp)
jne 0x68e6
movq -0xa8(%rbp), %rdi
callq 0x7640
movl -0xac(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)