// File: /workspace/github_c_makefile_star2_O0/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
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
cmpq %rcx, %rax
jge 0x693b
jmp 0x695a
leaq 0x27069(%rip), %rdi     # 0x2d9ab
leaq 0x2706a(%rip), %rsi     # 0x2d9b3
movl $0x37d, %edx            # imm = 0x37D
leaq 0x27068(%rip), %rcx     # 0x2d9bd
callq 0x21e0
cmpq $0x0, -0x28(%rbp)
jge 0x6970
xorl %eax, %eax
subq -0x28(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x697b
xorl %eax, %eax
movq %rax, -0xc0(%rbp)
jmp 0x697b
movq -0xc0(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x28(%rbp)
jge 0x69a9
movq -0x30(%rbp), %rsi
movl $0x4e, %edi
callq 0x7510
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x6986
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x48(%rbp)
movslq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rcx
movq -0x68(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rsp, %rcx
movq %rcx, -0x78(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc8(%rbp)
movq %rcx, %rsp
movq %rax, -0x80(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x6ac6
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x48(%rbp), %rcx
movl -0x3c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq %rax, -0x50(%rbp)
movq -0x70(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x6a85
jmp 0x6ab8
movq -0x70(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x50(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
andq $0x1, %rax
movslq -0x1c(%rbp), %rcx
xorq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
shrq %rax
movq %rax, -0x50(%rbp)
jmp 0x6ac6
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6a23
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jne 0x6b0e
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpq -0x60(%rbp), %rax
jae 0x6af7
movq -0x30(%rbp), %rsi
movl $0x4e, %edi
callq 0x7510
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6ad5
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x84(%rbp)
jmp 0x6f7e
cmpq $0x0, -0x58(%rbp)
jne 0x6b21
movq -0x28(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0xc8(%rbp), %r8
movq -0x70(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x50(%rbp), %rax
movl %eax, %esi
movq -0x60(%rbp), %rax
movl %eax, %edx
movq -0x58(%rbp), %rax
movl %eax, %ecx
callq 0x14620
cmpl $0x0, -0x34(%rbp)
je 0x6ba3
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpq -0x60(%rbp), %rax
jae 0x6b8c
movq -0xc8(%rbp), %rax
movl -0x3c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x273ac(%rip), %rax     # 0x2df20
movsbl (%rax,%rcx), %edi
movq -0x30(%rbp), %rsi
callq 0x7510
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6b54
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x84(%rbp)
jmp 0x6f7e
movq -0x60(%rbp), %rax
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0xd0(%rbp)
movq %rdi, %rsp
movq %rax, -0xb0(%rbp)
movq -0x60(%rbp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x21f0
movl $0x0, -0x94(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x6e7d
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x48(%rbp), %rcx
movl -0x3c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x48(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq %rax, -0x50(%rbp)
movq -0x70(%rbp), %rax
movq 0x50(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x6c4f
jmp 0x6e6f
movq -0x70(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x50(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
andq $0x1, %rax
movslq -0x1c(%rbp), %rcx
xorq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
shrq %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x6c93
movq -0x28(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x70(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0xa8(%rbp)
movl $0x0, -0x8c(%rbp)
cmpq $0x0, -0xa8(%rbp)
je 0x6d17
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
cmpq -0x50(%rbp), %rax
jae 0x6d15
movq -0xa0(%rbp), %rax
movl -0x88(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xff, %eax
jne 0x6d02
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x6d04
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x6ccf
jmp 0x6d17
cmpq $0x0, -0x58(%rbp)
je 0x6dca
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
cmpq -0x60(%rbp), %rax
jae 0x6dc5
movq -0xa0(%rbp), %rax
movq -0x50(%rbp), %rcx
movl -0x88(%rbp), %edx
addq %rdx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x90(%rbp)
cmpl $0xff, -0x90(%rbp)
jne 0x6d89
movq -0xa8(%rbp), %rax
movl -0x8c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x8c(%rbp)
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x90(%rbp)
movq -0xd0(%rbp), %rax
movl -0x90(%rbp), %ecx
movl %ecx, %edx
movq -0x60(%rbp), %rcx
subq $0x1, %rcx
movl -0x88(%rbp), %esi
subq %rsi, %rcx
addq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x6d2c
jmp 0x6e60
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
cmpq -0x60(%rbp), %rax
jae 0x6e5e
movq -0xa0(%rbp), %rax
movq -0x50(%rbp), %rcx
movl -0x88(%rbp), %edx
addq %rdx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x90(%rbp)
cmpl $0xff, -0x90(%rbp)
jne 0x6e2d
movq -0xa8(%rbp), %rax
movl -0x8c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x8c(%rbp)
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x90(%rbp)
movq -0xd0(%rbp), %rax
movl -0x90(%rbp), %ecx
movl %ecx, %edx
movl -0x88(%rbp), %ecx
addq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x6dd4
jmp 0x6e60
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6bea
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpq -0x60(%rbp), %rax
jae 0x6f6c
movq -0xc8(%rbp), %rax
movl -0x3c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x27078(%rip), %rax     # 0x2df20
movsbl (%rax,%rcx), %edi
movq -0x30(%rbp), %rsi
callq 0x7510
movq -0xd0(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %xmm0
movaps 0x268f5(%rip), %xmm1    # 0x2d7c0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x268f9(%rip), %xmm1    # 0x2d7d0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movl -0x94(%rbp), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
callq 0x2110
movq %rax, -0xb8(%rbp)
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
cmpq -0xb8(%rbp), %rax
jge 0x6f4f
movq -0xc8(%rbp), %rax
movl -0x3c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x26fef(%rip), %rax     # 0x2df20
movsbl (%rax,%rcx), %edi
movq -0x30(%rbp), %rsi
callq 0x7510
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x6f0b
movq -0xb8(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6e84
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x84(%rbp)
movq -0x78(%rbp), %rax
movq %rax, %rsp
movq -0x8(%rbp), %rax
movq %rbp, %rsp
popq %rbp
retq
nop