// File: /workspace/github_c_makefile_star2_O2/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
movl %r9d, -0x40(%rbp)
movl %edx, -0x50(%rbp)
movq %rdi, -0x70(%rbp)
movslq 0x18(%rdi), %rax
movq %rax, -0x60(%rbp)
cmpq %rcx, %rax
jle 0x5317
movq %r8, %rbx
movq %rcx, %r12
movq %rsi, %r13
pushq $0x4e
popq %r15
movq %rcx, %r14
testq %r14, %r14
jns 0x4ffa
movl %r15d, %edi
movq %rbx, %rsi
callq 0x5655
incq %r14
jmp 0x4fe5
movq %r13, %rcx
movl 0x10(%r13), %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %eax, %eax
testq %r12, %r12
movl $0x0, %r8d
cmovgq %r12, %r8
movq 0x18(%rcx), %r9
cmovnsq %rax, %r12
movq -0x60(%rbp), %rcx
subq %r8, %rcx
movq %rcx, %rdx
subq %r12, %rdx
movq %rdx, -0x30(%rbp)
movq %rcx, %r12
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdx
subq %rcx, %rdx
movq %rdx, -0x48(%rbp)
movq %rdx, %rsp
movq -0x70(%rbp), %r15
cmpq %rax, %r13
je 0x507b
movq 0x10(%r15), %rcx
movq (%r9,%rax,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
imulq $0x58, %rsi, %rsi
shrl %edx
movq 0x50(%rcx,%rsi), %rdi
testb $0x1, (%rdi,%rdx,8)
je 0x5076
incq %rax
jmp 0x504c
cmpl %eax, %r13d
jne 0x509e
xorl %r14d, %r14d
pushq $0x4e
popq %r15
movl %r14d, %eax
cmpq %rax, %r12
jbe 0x5113
movl %r15d, %edi
movq %rbx, %rsi
callq 0x5655
incl %r14d
jmp 0x5082
movq %r9, -0x58(%rbp)
addq %rsi, %rcx
movq 0x40(%rcx), %rax
movl (%rax,%rdx,4), %eax
movl %eax, %edx
shrl %edx
andl $0x1, %eax
movslq -0x50(%rbp), %r9
xorl %r14d, %r14d
cmpq %r9, %rax
movq %r8, -0x50(%rbp)
movl %r8d, %esi
cmovnel %r14d, %esi
movq 0x18(%rcx), %rdi
addl %edx, %esi
movq %r9, -0x68(%rbp)
movl %r9d, %ecx
xorl %eax, %ecx
movl %r12d, %edx
movq -0x48(%rbp), %r8
callq 0xbfde
cmpl $0x0, -0x40(%rbp)
je 0x5126
leaq 0x14e20(%rip), %r15     # 0x19f10
movl %r14d, %r14d
cmpq %r14, %r12
jbe 0x5113
movq -0x48(%rbp), %rax
movzbl (%rax,%r14), %eax
movsbl (%r15,%rax), %edi
movq %rbx, %rsi
callq 0x5655
incl %r14d
jmp 0x50f0
movq -0x30(%rbp), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsp, %r14
leaq 0xf(,%r12,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
leaq (,%r12,8), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x21e0
xorl %esi, %esi
movq %r14, -0x40(%rbp)
leaq (%r14,%r12,8), %rax
movl $0x0, -0x34(%rbp)
movq -0x68(%rbp), %r14
cmpq %r13, %rsi
je 0x5267
movq 0x10(%r15), %rdi
movq -0x58(%rbp), %rdx
movq %rsi, %rcx
movq (%rdx,%rsi,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
imulq $0x58, %rsi, %rsi
shrl %edx
movq 0x50(%rdi,%rsi), %r8
testb $0x1, (%r8,%rdx,8)
jne 0x525c
addq %rsi, %rdi
movq 0x40(%rdi), %rsi
movl (%rsi,%rdx,4), %esi
movl %esi, %r8d
andl $0x1, %r8d
shrl %esi
xorl %edx, %edx
cmpq %r14, %r8
movl $0x0, %r9d
cmoveq -0x50(%rbp), %r9
addq %rsi, %r9
movq 0x20(%rdi), %rsi
movq 0x28(%rdi), %rdi
testq %rdi, %rdi
je 0x51eb
xorl %r10d, %r10d
xorl %edx, %edx
movl %r10d, %r10d
cmpq %r10, %r9
jbe 0x51eb
xorl %r11d, %r11d
cmpb $-0x1, (%rsi,%r10)
sete %r11b
addl %r11d, %edx
incl %r10d
jmp 0x51cf
addq %r9, %rsi
cmpq %r14, %r8
jne 0x5225
xorl %r8d, %r8d
movl %r8d, %r8d
cmpq %r8, %r12
jbe 0x5259
movzbl (%rsi,%r8), %r9d
cmpl $0xff, %r9d
jne 0x5215
movl %edx, %r9d
incl %edx
movl (%rdi,%r9,4), %r9d
movl %r9d, %r9d
movq -0x40(%rbp), %r10
addq %r9, (%r10,%r8,8)
incl %r8d
jmp 0x51f6
xorl %r8d, %r8d
movl %r8d, %r8d
cmpq %r8, %r12
jbe 0x5259
movzbl (%rsi,%r8), %r9d
cmpl $0xff, %r9d
jne 0x5247
movl %edx, %r9d
incl %edx
movl (%rdi,%r9,4), %r9d
movl %r9d, %r9d
movq %r8, %r10
notq %r10
addq %r9, (%rax,%r10,8)
incl %r8d
jmp 0x5228
incl -0x34(%rbp)
movq %rcx, %rsi
incq %rsi
jmp 0x5162
movl -0x34(%rbp), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x58(%rbp)
xorl %r15d, %r15d
leaq 0x14c92(%rip), %r14     # 0x19f10
movq -0x30(%rbp), %rax
movq %r12, -0x60(%rbp)
movl %r15d, %r15d
cmpq %r15, %r12
jbe 0x5117
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %rax
movzbl (%rax,%r15), %eax
movsbl (%r14,%rax), %edi
movq %rbx, %rsi
callq 0x5655
movq -0x40(%rbp), %rax
movsd (%rax,%r15,8), %xmm1
unpcklps 0x14553(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1455b(%rip), %xmm1    # 0x19820
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
divsd -0x58(%rbp), %xmm0
callq 0x2110
movq %rax, %r13
xorl %r12d, %r12d
movl %r12d, %eax
cmpq %rax, %r13
jle 0x5304
movq -0x48(%rbp), %rax
movzbl (%rax,%r15), %eax
movsbl (%r14,%rax), %edi
movq %rbx, %rsi
callq 0x5655
incl %r12d
jmp 0x52e1
movq -0x30(%rbp), %rax
addq %r13, %rax
incl %r15d
movq -0x60(%rbp), %r12
jmp 0x5286
leaq 0x146ed(%rip), %rdi     # 0x19a0b
leaq 0x146ee(%rip), %rsi     # 0x19a13
leaq 0x146f1(%rip), %rcx     # 0x19a1d
movl $0x37d, %edx            # imm = 0x37D
callq 0x21d0