// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]oatk/syncasm.c
// Function: scg_syncmer_consensus
int64_t scg_syncmer_consensus(sr_db_t *sr_db, syncmer_t *scm, int rev, int64_t beg, kstring_t *c_seq, int hoco_seq)
{
    int w;
    w = sr_db->k;

    assert(beg < w);
    
    uint32_t i, n_seq;
    uint64_t *m_pos, p, r, l;
    int64_t bl;
    sr_t *s;

    bl = beg<0? -beg : 0;
    while (beg < 0) {kputc_('N', c_seq), ++beg;}

    n_seq = scm->cov;
    m_pos = scm->m_pos;
    l = w - beg;
    bl += l;

    // get the kmer sequence
    uint8_t kmer_s[l];
    p = r = 0;
    for (i = 0; i < n_seq; ++i) {
        s = &sr_db->a[m_pos[i]>>32];
        p = m_pos[i]>>1&MAX_RD_SCM;
        if (s->k_mer[p]&1)
            continue;
        // not error corrected
        p = s->m_pos[p];
        r = (p&1)^rev;
        p >>= 1;
        break;
    }
    
    // assert(i < n_seq);
    if (i == n_seq) {
        // TODO fix this
        // fill with 'N's
        for (i = 0; i < l; ++i)
            kputc_('N', c_seq);
        return bl;
    }

    if (!r) p += beg;
    get_kmer_seq(s->hoco_s, p, l, r, kmer_s);

    if (hoco_seq) {
        // do not need to do run length consensus
        for (i = 0; i < l; ++i)
            kputc_(char_nt4_table[kmer_s[i]], c_seq);
        return bl;
    }

    uint32_t j, k, rl, m_seq;
    uint8_t *ho_rl;
    uint32_t *ho_l_rl;

    uint64_t tot_rl[l];
#ifdef DEBUG_CONSENSUS
    uint8_t kmer_s1[l];
#endif

    MYBZERO(tot_rl, l);
    m_seq = 0;
    for (i = 0; i < n_seq; ++i) {
        s = &sr_db->a[m_pos[i]>>32];
        p = m_pos[i]>>1&MAX_RD_SCM;
        // skip error corrected mers
        if (s->k_mer[p] & 1) 
            continue;
        p = s->m_pos[p];
        r = (p&1)^rev;
        p >>= 1;
        if (!r) p += beg;

#ifdef DEBUG_CONSENSUS
        get_kmer_seq(s->hoco_s, p, l, r, kmer_s1);
        for (j = 0; j < l; ++j) assert(kmer_s[j] == kmer_s1[j]);
#endif
        ho_rl = s->ho_rl;
        ho_l_rl = s->ho_l_rl;
        k = 0;
        if (ho_l_rl)
            for (j = 0; j < p; ++j)
                if (ho_rl[j] == 255) ++k;
        if (r) {
            for (j = 0; j < l; ++j) {
                rl = ho_rl[p+j];
                if (rl == 255)
                    rl = ho_l_rl[k++];
                tot_rl[l-1-j] += rl;
            }
        } else {
            for (j = 0; j < l; ++j) {
                rl = ho_rl[p+j];
                if (rl == 255)
                    rl = ho_l_rl[k++];
                tot_rl[j] += rl;
            }
        }
        ++m_seq;
    }

    for (i = 0; i < l; ++i) {
        kputc_(char_nt4_table[kmer_s[i]], c_seq);
        long b = lround((double) tot_rl[i] / m_seq);
        for (j = 0; j < b; ++j)
            kputc_(char_nt4_table[kmer_s[i]], c_seq);
        bl += b;
    }

    return bl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r14
movl %edx, -0x50(%rbp)
movq %rdi, -0x30(%rbp)
movslq 0x18(%rdi), %rcx
movq %rcx, -0x40(%rbp)
cmpq %r14, %rcx
jle 0x6fea
movl %r9d, %r13d
movq %r8, %rbx
movq %rsi, %r12
movq %r14, %rax
testq %r14, %r14
jns 0x6ab4
movq %r14, %r15
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
cmpq 0x8(%rbx), %rdx
jbe 0x6aa2
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2350
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
jmp 0x6aa6
movq 0x10(%rbx), %rax
movq %rdx, (%rbx)
movb $0x4e, (%rax,%rcx)
incq %r15
jne 0x6a47
xorl %eax, %eax
movl %r13d, -0x48(%rbp)
movl 0x10(%r12), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq 0x18(%r12), %r13
movq -0x40(%rbp), %r12
movq %rax, -0x38(%rbp)
subq %rax, %r12
movq %r14, %rax
sarq $0x3f, %rax
andq %r14, %rax
movq %r12, %r14
subq %rax, %r14
leaq 0xf(%r12), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
testq %r15, %r15
je 0x6b30
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
xorl %edx, %edx
movq (%r13,%rdx,8), %rcx
movq %rcx, %rsi
shrq $0x20, %rsi
imulq $0x58, %rsi, %rsi
shrl %ecx
movq 0x50(%rax,%rsi), %rdi
testb $0x1, (%rdi,%rcx,8)
je 0x6b38
incq %rdx
cmpq %rdx, %r15
jne 0x6b09
jmp 0x6b59
xorl %edx, %edx
xorl %ecx, %ecx
xorl %esi, %esi
jmp 0x6b50
addq %rsi, %rax
movq 0x40(%rax), %rsi
movl (%rsi,%rcx,4), %esi
movl %esi, %edi
andl $0x1, %edi
movslq -0x50(%rbp), %rcx
xorq %rdi, %rcx
shrl %esi
cmpl %r15d, %edx
jne 0x6be9
movq -0x38(%rbp), %rax
cmpq %rax, -0x40(%rbp)
je 0x6fd8
movq %r14, %r15
movl $0x1, %r14d
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
cmpq 0x8(%rbx), %rdx
jbe 0x6bcb
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2350
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
jmp 0x6bcf
movq 0x10(%rbx), %rax
movq %rdx, (%rbx)
movb $0x4e, (%rax,%rcx)
movl %r14d, %eax
incl %r14d
cmpq %rax, %r12
ja 0x6b70
movq %r15, %r14
jmp 0x6fd8
xorl %edx, %edx
testq %rcx, %rcx
movq -0x38(%rbp), %rdi
cmovnel %edx, %edi
addl %edi, %esi
movq 0x18(%rax), %rdi
movl %r12d, %edx
movq -0x58(%rbp), %r8
callq 0xf5c9
cmpl $0x0, -0x48(%rbp)
movq %r12, -0x48(%rbp)
je 0x6cbb
movq -0x38(%rbp), %rax
cmpq %rax, -0x40(%rbp)
je 0x6fd8
movq %r14, %r13
movl $0x1, %r14d
xorl %eax, %eax
leaq 0x1931b(%rip), %r15     # 0x1ff50
movq -0x58(%rbp), %rcx
movzbl (%rcx,%rax), %eax
movb (%r15,%rax), %r12b
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
cmpq 0x8(%rbx), %rdx
jbe 0x6c9c
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2350
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
jmp 0x6ca0
movq 0x10(%rbx), %rax
movq %rdx, (%rbx)
movb %r12b, (%rax,%rcx)
movl %r14d, %eax
incl %r14d
cmpq %rax, -0x48(%rbp)
ja 0x6c35
movq %r13, %r14
jmp 0x6fd8
movq %r14, -0x60(%rbp)
movq %rsp, %r14
leaq 0xf(,%r12,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
leaq (,%r12,8), %rdx
xorl %r12d, %r12d
movq %r14, %rdi
xorl %esi, %esi
callq 0x21e0
testl %r15d, %r15d
movq %r14, -0x68(%rbp)
je 0x6e5b
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x50(%rbp), %rcx
movq %rcx, -0x30(%rbp)
movl %r15d, %edx
movq -0x40(%rbp), %rsi
leaq (,%rsi,8), %rsi
movq -0x38(%rbp), %rdi
leaq (,%rdi,8), %rdi
subq %rdi, %rsi
leaq (%rsi,%r14), %rcx
addq $-0x8, %rcx
movq %rcx, -0x50(%rbp)
xorl %edi, %edi
movq -0x68(%rbp), %r14
movq (%r13,%rdi,8), %r8
movq %r8, %r9
shrq $0x20, %r9
imulq $0x58, %r9, %r10
shrl %r8d
movq 0x50(%rax,%r10), %r9
testb $0x1, (%r9,%r8,8)
jne 0x6e45
movq %r12, %rcx
addq %rax, %r10
movq 0x40(%r10), %r9
movl (%r9,%r8,4), %r9d
movl %r9d, %r15d
andl $0x1, %r15d
shrl %r9d
xorl %r8d, %r8d
cmpq -0x30(%rbp), %r15
movl $0x0, %r11d
cmoveq -0x38(%rbp), %r11
addq %r9, %r11
movq 0x20(%r10), %r9
movq 0x28(%r10), %r10
testq %r10, %r10
je 0x6db7
testq %r11, %r11
je 0x6db7
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %esi, %esi
cmpb $-0x1, (%r9,%r12)
sete %sil
addl %esi, %r8d
incq %r12
movl %r12d, %esi
cmpq %rsi, %r11
ja 0x6d9e
cmpq -0x30(%rbp), %r15
jne 0x6dfd
movq -0x38(%rbp), %rsi
cmpq %rsi, -0x40(%rbp)
movq -0x48(%rbp), %r12
je 0x6e3f
addq %r11, %r9
xorl %r11d, %r11d
movzbl (%r9,%r11), %r15d
cmpl $0xff, %r15d
jne 0x6de9
movl %r8d, %esi
incl %r8d
movl (%r10,%rsi,4), %r15d
movl %r15d, %esi
addq %rsi, (%r14,%r11,8)
incq %r11
movl %r11d, %esi
cmpq %rsi, %r12
ja 0x6dd1
jmp 0x6e3f
movq -0x38(%rbp), %rsi
cmpq %rsi, -0x40(%rbp)
je 0x6e3f
addq %r11, %r9
movq -0x50(%rbp), %r11
xorl %r15d, %r15d
movzbl (%r9,%r15), %r12d
cmpl $0xff, %r12d
jne 0x6e29
movl %r8d, %esi
incl %r8d
movl (%r10,%rsi,4), %r12d
movl %r12d, %esi
addq %rsi, (%r11)
incq %r15
movl %r15d, %esi
addq $-0x8, %r11
cmpq %rsi, -0x48(%rbp)
ja 0x6e11
movq %rcx, %r12
incl %r12d
incq %rdi
cmpq %rdx, %rdi
jne 0x6d36
movl %r12d, %eax
cvtsi2sd %rax, %xmm0
jmp 0x6e5f
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x38(%rbp), %rax
cmpq %rax, -0x40(%rbp)
movq -0x60(%rbp), %r14
je 0x6fd8
xorl %r13d, %r13d
movq %r14, -0x60(%rbp)
movq -0x58(%rbp), %rax
movzbl (%rax,%r13), %eax
leaq 0x190c3(%rip), %rcx     # 0x1ff50
movb (%rcx,%rax), %r14b
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
cmpq 0x8(%rbx), %rdx
jbe 0x6eec
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2350
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
jmp 0x6ef0
movq 0x10(%rbx), %rax
movq -0x68(%rbp), %rsi
movq %rdx, (%rbx)
movb %r14b, (%rax,%rcx)
movsd (%rsi,%r13,8), %xmm1
unpcklps 0x188f8(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x18900(%rip), %xmm1    # 0x1f810
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
divsd -0x30(%rbp), %xmm0
callq 0x2110
movq %rax, %r14
testq %rax, %rax
jle 0x6fc1
movl $0x1, %r12d
movq -0x58(%rbp), %rax
movzbl (%rax,%r13), %eax
leaq 0x19008(%rip), %rcx     # 0x1ff50
movb (%rcx,%rax), %r15b
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
cmpq 0x8(%rbx), %rdx
jbe 0x6fa7
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rsi
shrq $0x10, %rsi
orq %rcx, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2350
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
jmp 0x6fab
movq 0x10(%rbx), %rax
movq %rdx, (%rbx)
movb %r15b, (%rax,%rcx)
movl %r12d, %eax
incl %r12d
cmpq %rax, %r14
ja 0x6f38
movq -0x60(%rbp), %rax
addq %r14, %rax
movq %rax, %r14
incl %r13d
cmpq %r13, -0x48(%rbp)
ja 0x6e79
movq %r14, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18a52(%rip), %rdi     # 0x1fa43
leaq 0x18a53(%rip), %rsi     # 0x1fa4b
leaq 0x18a56(%rip), %rcx     # 0x1fa55
movl $0x37d, %edx            # imm = 0x37D
callq 0x21d0