// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]oatk/syncasm.c
// Function: scg_consensus
void scg_consensus(sr_db_t *sr_db, scg_t *scg, int hoco_seq, int save_seq, FILE *fo)
{
    uint64_t i, v, t, z, n;
    int64_t l;
    double cov;
    int w;
    syncmer_t *scm;
    asmg_t *utg_asmg;
    asmg_arc_t *a;
    scg_utg_t *s;
    kstring_t c_seq = {0, 0, 0};
#ifdef DEBUG_CONSENSUS
    uint64_t j;
    int64_t l1;
    kstring_t c_seq1 = {0, 0, 0};
#endif

    w = sr_db->k;
    scm = scg_a_scm(scg);
    utg_asmg = scg->utg_asmg;
    asmg_clean_consensus(utg_asmg); // clean consensus sequences
    
    if (fo) fprintf(fo, "H\tVN:Z:1.0\n");
    for (i = 0, n = utg_asmg->n_vtx; i < n; ++i) {
        s = &utg_asmg->vtx[i];
        if (s->del) continue;
        c_seq.l = 0;
        l = scg_unitig_consensus(sr_db, s->a, s->n, scm, &c_seq, hoco_seq);
#ifdef DEBUG_CONSENSUS
        scg_utg_t s1 = {s->n, 0, 0, s->len, s->cov, s->del, s->circ};
        MYMALLOC(s1.a, s1.n);
        for (j = 0; j < s1.n; ++j) s1.a[s1.n-1-j] = s->a[j]^1;
        c_seq1.l = 0;
        l1 = scg_unitig_consensus(sr_db, s1.a, s1.n, scm, &c_seq1, hoco_seq);
        str_reverse_complement(c_seq1.s, c_seq1.l);
        if (l1 != l || strncmp(c_seq.s, c_seq1.s, l)) {
            fprintf(stderr, "[DEBUG_CONSENSUS::%s] different forward backward consensus sequences: u%lu [%ld %ld] %.*s %.*s\n",
                    __func__, i, l, l1, (int) l, c_seq.s, (int) l1, c_seq1.s);
        }
        free(s1.a);
#endif
        cov = s->cov? s->cov : utg_avg_cov(scg, s);
        s->cov = cov;
        s->len = l;
        if (save_seq) {
            if (s->seq) free(s->seq);
            MYMALLOC(s->seq, l+1);
            strncpy(s->seq, c_seq.s, l);
            s->seq[l] = 0;
        }
        if (fo) fprintf(fo, "S\tu%lu\t%.*s\tLN:i:%ld\tKC:i:%ld\tSC:f:%.3f\n", i, (int) l, c_seq.s, l, (int64_t) (l*cov), cov);

#ifdef DEBUG_UTG_COVERAGE
        uint64_t j;
        fprintf(stderr, "[DEBUG_UTG_COVERAGE::%s] u%lu [N=%lu]:", __func__, i, s->n);
        for (j = 0; j < s->n; ++j) fprintf(stderr, " s%lu%c", s->a[j]>>1, "+-"[s->a[j]&1]);
        fputc('\n', stderr);
        fprintf(stderr, "[DEBUG_UTG_COVERAGE::%s] u%lu [N=%lu]:", __func__, i, s->n);
        for (j = 0; j < s->n; ++j) fprintf(stderr, " %u", scm[s->a[j]>>1].cov);
        fputc('\n', stderr);
#endif
    }

    for (i = 0, n = utg_asmg->n_arc; i < n; ++i) {
        a = &utg_asmg->arc[i];
        if (a->del || a->comp) continue;
        
        if (a->ln > 0) {
            // need to calculate the consensus length of the overlaping syncmers
            s = &utg_asmg->vtx[a->v>>1];
            c_seq.l = 0;
            l = scg_unitig_consensus(sr_db, (a->v&1)? s->a : &s->a[s->n - a->ln], a->ln, scm, &c_seq, hoco_seq);
#ifdef DEBUG_CONSENSUS
            fprintf(stderr, "[DEBUG_CONSENSUS::%s] syncmer overlap (ln=%lu) for arc u%lu%c->u%lu%c v_n=%lu v_s=%lu\n", 
                    __func__, a->ln, a->v>>1, "+-"[a->v&1], a->w>>1, "+-"[a->w&1], s->n, (a->v&1)? 0 : s->n - a->ln);
#endif
        } else {
            // need to calculate the consensus overlap between the two end syncmers
            s = &utg_asmg->vtx[a->v>>1];
            z = a->v&1;
            v = s->a[(s->n-1) * (!z)]^z;
            s = &utg_asmg->vtx[a->w>>1];
            z = a->w&1;
            t = s->a[(s->n-1) * z]^z;
            l = calc_syncmer_overlap(sr_db, &scm[v>>1], v&1, &scm[t>>1], t&1, 0);
            if (l < w) {
                c_seq.l = 0;
                l = scg_syncmer_consensus(sr_db, &scm[v>>1], v&1, l, &c_seq, hoco_seq);
            } else {
                l = 0;
            }
        }
        // FIXME: consensus problem seql < lo
        l = MIN((uint64_t) l, utg_asmg->vtx[a->v>>1].len);
        l = MIN((uint64_t) l, utg_asmg->vtx[a->w>>1].len);
        a->ls = l;
        asmg_arc(utg_asmg, a->w^1, a->v^1)->ls = l;
        if (fo) {
            fprintf(fo, "L\tu%lu\t%c\tu%lu\t%c\t%ldM\tEC:i:%u\n", a->v>>1, "+-"[a->v&1], a->w>>1, "+-"[a->w&1], l, a->cov);
            fprintf(fo, "L\tu%lu\t%c\tu%lu\t%c\t%ldM\tEC:i:%u\n", a->w>>1, "-+"[a->w&1], a->v>>1, "-+"[a->v&1], l, a->cov);
        }
    }
    
    free(c_seq.s);
#ifdef DEBUG_CONSENSUS
    free(c_seq1.s);
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, 0x8(%rsp)
movl %ecx, 0x2c(%rsp)
movl %edx, 0x4(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq %rdi, 0x18(%rsp)
movl 0x18(%rdi), %eax
movl %eax, 0x28(%rsp)
movq (%rsi), %rax
movq %rsi, 0x50(%rsp)
movq 0x8(%rsi), %r14
movq 0x10(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x5c5c
movq 0x28(%r14), %rcx
addq $0x18, %rcx
movq $0x0, (%rcx)
addq $0x30, %rcx
decq %rax
jne 0x5c4c
movq (%r14), %rbx
testq %rbx, %rbx
je 0x5c9d
xorl %r15d, %r15d
movq 0x10(%r14), %rax
movq 0x10(%rax,%r15), %rdi
testq %rdi, %rdi
je 0x5c87
callq 0x2060
movq 0x10(%r14), %rax
movq $0x0, 0x10(%rax,%r15)
movq 0x10(%r14), %rax
movq $0x0, 0x18(%rax,%r15)
addq $0x28, %r15
decq %rbx
jne 0x5c67
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0x5cbd
leaq 0x17c73(%rip), %rdi     # 0x1d921
movl $0xb, %esi
movl $0x1, %edx
callq 0x2470
movq (%r14), %r15
testq %r15, %r15
je 0x5df0
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x10(%r14), %rbx
testb $0x40, 0x23(%rbx,%r12)
jne 0x5de0
movq $0x0, 0x30(%rsp)
movq (%rbx,%r12), %rdx
movq 0x8(%rbx,%r12), %rsi
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rcx
leaq 0x30(%rsp), %r8
movl 0x4(%rsp), %r9d
callq 0x6081
movq %rax, %rbp
movl 0x20(%rbx,%r12), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
je 0x5d24
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
jmp 0x5d32
leaq (%rbx,%r12), %rsi
movq 0x50(%rsp), %rdi
callq 0x5a8e
cvttsd2si %xmm0, %rax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl 0x20(%rbx,%r12), %ecx
movl $0xc0000000, %edx       # imm = 0xC0000000
andl %edx, %ecx
orl %eax, %ecx
movl %ecx, 0x20(%rbx,%r12)
movq %rbp, 0x18(%rbx,%r12)
cmpl $0x0, 0x2c(%rsp)
je 0x5d9d
movsd %xmm0, 0x10(%rsp)
movq 0x10(%rbx,%r12), %rdi
testq %rdi, %rdi
je 0x5d70
callq 0x2060
leaq 0x1(%rbp), %rdi
callq 0x2320
movq %rax, 0x10(%rbx,%r12)
movq 0x40(%rsp), %rsi
movq %rax, %rdi
movq %rbp, %rdx
callq 0x2090
movq 0x10(%rbx,%r12), %rax
movb $0x0, (%rax,%rbp)
movsd 0x10(%rsp), %xmm0
cmpq $0x0, 0x8(%rsp)
je 0x5de0
xorps %xmm1, %xmm1
cvtsi2sd %rbp, %xmm1
movq 0x40(%rsp), %r8
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %r10
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
leaq 0x17b62(%rip), %rsi     # 0x1d92d
movq %r13, %rdx
movl %ebp, %ecx
movq %rbp, %r9
movb $0x1, %al
pushq %r10
callq 0x2290
addq $0x10, %rsp
incq %r13
addq $0x28, %r12
cmpq %r13, %r15
jne 0x5ccf
movq 0x18(%r14), %rbp
testq %rbp, %rbp
je 0x6069
xorl %r15d, %r15d
movq %rbp, 0x10(%rsp)
movq 0x8(%rsp), %rbx
movq 0x28(%r14), %r13
leaq (%r15,%r15,2), %rax
shlq $0x4, %rax
cmpl $0x3fffffff, 0x20(%r13,%rax) # imm = 0x3FFFFFFF
ja 0x605d
addq %rax, %r13
movq (%r13), %r12
movq 0x10(%r13), %rdx
movq %r12, %rax
shrq %rax
leaq (%rax,%rax,4), %rax
testq %rdx, %rdx
je 0x5e83
movq 0x10(%r14), %rcx
movq $0x0, 0x30(%rsp)
movq 0x8(%rcx,%rax,8), %rsi
testb $0x1, %r12b
jne 0x5e65
leaq (%rcx,%rax,8), %rax
movq (%rax), %rax
subq %rdx, %rax
leaq (%rsi,%rax,8), %rsi
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rcx
leaq 0x30(%rsp), %r8
movl 0x4(%rsp), %r9d
callq 0x6081
jmp 0x5f3d
movq 0x10(%r14), %rcx
movl %r12d, %edx
notl %edx
movq (%rcx,%rax,8), %rsi
movq 0x8(%rcx,%rax,8), %rax
decq %rsi
andl $0x1, %edx
imulq %rsi, %rdx
movq (%rax,%rdx,8), %rbp
xorl %ebp, %r12d
movq 0x8(%r13), %r8
movq %r8, %rax
shrq %rax
leaq (%rax,%rax,4), %rax
movq (%rcx,%rax,8), %rdx
movq 0x8(%rcx,%rax,8), %rax
decq %rdx
testb $0x1, %r8b
movl $0x0, %ecx
cmoveq %rcx, %rdx
movq (%rax,%rdx,8), %rcx
xorl %ecx, %r8d
shlq $0x4, %rbp
andq $-0x20, %rbp
movq 0x20(%rsp), %rax
addq %rax, %rbp
andl $0x1, %r12d
shlq $0x4, %rcx
andq $-0x20, %rcx
addq %rax, %rcx
andl $0x1, %r8d
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
xorl %r9d, %r9d
callq 0x623e
movl %eax, %ecx
movl $0x0, %eax
cmpl 0x28(%rsp), %ecx
jge 0x5f3d
movslq %ecx, %rcx
movq $0x0, 0x30(%rsp)
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movl %r12d, %edx
leaq 0x30(%rsp), %r8
movl 0x4(%rsp), %r9d
callq 0x653f
movq 0x10(%r14), %rsi
movq (%r13), %rcx
movq %rcx, %rdx
shrq %rdx
leaq (%rdx,%rdx,4), %rdi
movq 0x18(%rsi,%rdi,8), %r12
cmpq %r12, %rax
cmovbq %rax, %r12
movq 0x8(%r13), %rax
movq %rax, %r8
shrq %r8
leaq (%r8,%r8,4), %rdi
movq 0x18(%rsi,%rdi,8), %rsi
cmpq %rsi, %r12
cmovaeq %rsi, %r12
movq %r12, 0x18(%r13)
movq %rax, %rsi
xorq $0x1, %rsi
movq 0x28(%r14), %rdi
movq 0x30(%r14), %r9
movq (%r9,%rsi,8), %rsi
leaq (%rsi,%rsi,2), %r9
shlq $0x4, %r9
leaq (%rdi,%r9), %rsi
movq 0x8(%rdi,%r9), %rdi
xorq %rcx, %rdi
cmpq $0x1, %rdi
jne 0x5fad
movq 0x10(%rsp), %rbp
jmp 0x5fc3
movq 0x10(%rsp), %rbp
movq 0x38(%rsi), %rdi
xorq %rcx, %rdi
addq $0x30, %rsi
cmpq $0x1, %rdi
jne 0x5fb2
movq %r12, 0x18(%rsi)
cmpq $0x0, 0x8(%rsp)
je 0x605d
andl $0x1, %ecx
leaq 0x17999(%rip), %rsi     # 0x1d976
movsbl (%rcx,%rsi), %ecx
andl $0x1, %eax
movsbl (%rax,%rsi), %r9d
movl 0x20(%r13), %r10d
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
andl %eax, %r10d
movq %rbx, %rdi
leaq 0x17957(%rip), %rsi     # 0x1d956
xorl %eax, %eax
pushq %r10
pushq %r12
callq 0x2290
addq $0x10, %rsp
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rcx, %rdx
shrq %rdx
andl $0x1, %ecx
leaq 0x17953(%rip), %rsi     # 0x1d979
movsbl (%rcx,%rsi), %ecx
movq %rax, %r8
shrq %r8
andl $0x1, %eax
movsbl (%rax,%rsi), %r9d
movl 0x20(%r13), %r10d
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
andl %eax, %r10d
movq %rbx, %rdi
leaq 0x17908(%rip), %rsi     # 0x1d956
xorl %eax, %eax
pushq %r10
pushq %r12
callq 0x2290
addq $0x10, %rsp
incq %r15
cmpq %rbp, %r15
jne 0x5e0a
movq 0x40(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2060