// File: /workspace/github_c_makefile_star2_O1/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, %r15
movl %edx, -0x50(%rbp)
movq %rdi, -0x30(%rbp)
movslq 0x18(%rdi), %rcx
movq %rcx, -0x38(%rbp)
cmpq %r15, %rcx
jle 0x6b22
movl %r9d, %r13d
movq %r8, %rbx
movq %rsi, %r14
movq %r15, %r12
testq %r15, %r15
jns 0x65eb
movq %r15, %r12
movq (%rbx), %rax
leaq 0x1(%rax), %rcx
cmpq 0x8(%rbx), %rcx
jbe 0x65d1
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2360
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movb $0x4e, (%rcx,%rax)
incq %r12
jne 0x657f
xorl %r12d, %r12d
movl %r13d, -0x58(%rbp)
movl 0x10(%r14), %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq 0x18(%r14), %r10
movq -0x38(%rbp), %rcx
subq %r12, %rcx
movq %r15, %rax
sarq $0x3f, %rax
andq %r15, %rax
movq %rcx, -0x40(%rbp)
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdx
subq %rcx, %rdx
movq %rdx, -0x60(%rbp)
movq %rdx, %rsp
testq %r13, %r13
je 0x6667
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %r8
xorl %ecx, %ecx
xorl %edi, %edi
movq (%r10,%rdi,8), %rsi
movq %rsi, %rdx
shrq $0x20, %rdx
imulq $0x58, %rdx, %r9
leaq (%r8,%r9), %rdx
shrl %esi
movq 0x50(%r8,%r9), %r9
testb $0x1, (%r9,%rsi,8)
je 0x666f
incq %rdi
cmpq %rdi, %r13
jne 0x6639
movl %r13d, %edi
jmp 0x6686
xorl %edi, %edi
xorl %ecx, %ecx
xorl %esi, %esi
jmp 0x6686
movq 0x40(%rdx), %rcx
movl (%rcx,%rsi,4), %esi
movl %esi, %r8d
andl $0x1, %r8d
movslq -0x50(%rbp), %rcx
xorq %r8, %rcx
shrl %esi
movq -0x40(%rbp), %r14
movq %r14, %r15
subq %rax, %r15
cmpl %r13d, %edi
jne 0x6727
cmpq %r12, -0x38(%rbp)
je 0x6b10
movq %r15, %r12
movl $0x1, %r14d
movq -0x40(%rbp), %r15
movq (%rbx), %rax
leaq 0x1(%rax), %rcx
cmpq 0x8(%rbx), %rcx
jbe 0x6702
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2360
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movb $0x4e, (%rcx,%rax)
movl %r14d, %eax
incl %r14d
cmpq %rax, %r15
ja 0x66b0
movq %r12, %r15
jmp 0x6b10
movq %r10, -0x68(%rbp)
xorl %eax, %eax
testq %rcx, %rcx
movl %r12d, %edi
cmovnel %eax, %edi
addl %edi, %esi
movq 0x18(%rdx), %rdi
movl %r14d, %edx
movq -0x60(%rbp), %r8
callq 0xed8a
cmpl $0x0, -0x58(%rbp)
je 0x67f7
cmpq %r12, -0x38(%rbp)
je 0x6b10
movq %r15, -0x48(%rbp)
movl $0x1, %r14d
xorl %eax, %eax
leaq 0x177a1(%rip), %r15     # 0x1df10
movq -0x40(%rbp), %r13
movq -0x60(%rbp), %rcx
movzbl (%rcx,%rax), %eax
movb (%r15,%rax), %r12b
movq (%rbx), %rax
leaq 0x1(%rax), %rcx
cmpq 0x8(%rbx), %rcx
jbe 0x67d1
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2360
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movb %r12b, (%rcx,%rax)
movl %r14d, %eax
incl %r14d
cmpq %rax, %r13
ja 0x6773
movq -0x48(%rbp), %r15
jmp 0x6b10
movq %r15, -0x48(%rbp)
movq %rsp, %r15
leaq 0xf(,%r14,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
leaq (,%r14,8), %rdx
xorl %r14d, %r14d
movq %r15, %rdi
xorl %esi, %esi
callq 0x21e0
testl %r13d, %r13d
movq %r15, -0x58(%rbp)
je 0x699b
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x50(%rbp), %rcx
movq %rcx, -0x30(%rbp)
movl %r13d, %edx
movq -0x38(%rbp), %rsi
leaq (,%rsi,8), %rsi
leaq (,%r12,8), %rdi
subq %rdi, %rsi
leaq (%rsi,%r15), %rcx
addq $-0x8, %rcx
movq %rcx, -0x50(%rbp)
xorl %edi, %edi
movq -0x68(%rbp), %rsi
movq -0x58(%rbp), %r15
movq (%rsi,%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 0x6985
movq %r14, %rcx
addq %rax, %r10
movq 0x40(%r10), %r9
movl (%r9,%r8,4), %r9d
movl %r9d, %r14d
andl $0x1, %r14d
shrl %r9d
xorl %r8d, %r8d
cmpq -0x30(%rbp), %r14
movl $0x0, %r11d
movq %r12, %rsi
cmoveq %r12, %r11
addq %r9, %r11
movq 0x20(%r10), %r9
movq 0x28(%r10), %r10
testq %r10, %r10
je 0x68f5
testq %r11, %r11
je 0x68f5
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %r13d, %r13d
cmpb $-0x1, (%r9,%r12)
sete %r13b
addl %r13d, %r8d
incq %r12
movl %r12d, %r13d
cmpq %r13, %r11
ja 0x68db
cmpq -0x30(%rbp), %r14
jne 0x6937
cmpq %rsi, -0x38(%rbp)
movq -0x40(%rbp), %r12
je 0x6978
addq %r11, %r9
xorl %r11d, %r11d
movzbl (%r9,%r11), %r14d
cmpl $0xff, %r14d
jne 0x6923
movl %r8d, %r14d
incl %r8d
movl (%r10,%r14,4), %r14d
movl %r14d, %r14d
addq %r14, (%r15,%r11,8)
incq %r11
movl %r11d, %r14d
cmpq %r14, %r12
ja 0x690b
jmp 0x6978
cmpq %rsi, -0x38(%rbp)
movq -0x40(%rbp), %r13
je 0x6978
addq %r11, %r9
movq -0x50(%rbp), %r11
xorl %r14d, %r14d
movzbl (%r9,%r14), %r12d
cmpl $0xff, %r12d
jne 0x6963
movl %r8d, %r12d
incl %r8d
movl (%r10,%r12,4), %r12d
movl %r12d, %r12d
addq %r12, (%r11)
incq %r14
movl %r14d, %r12d
addq $-0x8, %r11
cmpq %r12, %r13
ja 0x694b
movq %rcx, %r14
incl %r14d
movq %rsi, %r12
movq -0x68(%rbp), %rsi
incq %rdi
cmpq %rdx, %rdi
jne 0x6872
movl %r14d, %eax
cvtsi2sd %rax, %xmm0
jmp 0x699f
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
cmpq %r12, -0x38(%rbp)
movq -0x48(%rbp), %r15
je 0x6b10
xorl %r13d, %r13d
movq %r15, -0x48(%rbp)
movq -0x60(%rbp), %rax
movzbl (%rax,%r13), %eax
leaq 0x17547(%rip), %rcx     # 0x1df10
movb (%rcx,%rax), %r15b
movq (%rbx), %rax
leaq 0x1(%rax), %rcx
cmpq 0x8(%rbx), %rcx
movq -0x58(%rbp), %r14
jbe 0x6a23
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2360
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movb %r15b, (%rcx,%rax)
movsd (%r14,%r13,8), %xmm1
unpcklps 0x16dce(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x16dd6(%rip), %xmm1    # 0x1d820
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
divsd -0x30(%rbp), %xmm0
callq 0x2110
movq %rax, %r15
testq %rax, %rax
jle 0x6af9
movl $0x1, %r12d
movq -0x60(%rbp), %rax
movzbl (%rax,%r13), %eax
leaq 0x1748e(%rip), %rcx     # 0x1df10
movb (%rcx,%rax), %r14b
movq (%rbx), %rax
leaq 0x1(%rax), %rcx
cmpq 0x8(%rbx), %rcx
jbe 0x6ad8
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rsi
shrq $0x10, %rsi
orq %rax, %rsi
incq %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2360
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movb %r14b, (%rcx,%rax)
movl %r12d, %eax
incl %r12d
cmpq %rax, %r15
jg 0x6a72
movq -0x48(%rbp), %rax
addq %r15, %rax
movq %rax, %r15
incl %r13d
cmpq %r13, -0x40(%rbp)
ja 0x69b5
movq %r15, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16ee2(%rip), %rdi     # 0x1da0b
leaq 0x16ee3(%rip), %rsi     # 0x1da13
leaq 0x16ee6(%rip), %rcx     # 0x1da1d
movl $0x37d, %edx            # imm = 0x37D
callq 0x21d0