// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq %r8, %r12
movl %ecx, 0x20(%rsp)
movl %edx, 0xc(%rsp)
andq $0x0, 0x50(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movq %rdi, 0x18(%rsp)
movl 0x18(%rdi), %eax
movl %eax, 0x3c(%rsp)
movq (%rsi), %rax
movq %rsi, 0x10(%rsp)
movq 0x8(%rsi), %r15
movq 0x10(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x18(%r15), %rax
pushq $0x18
popq %rcx
subq $0x1, %rax
jb 0x47a1
movq 0x28(%r15), %rdx
andq $0x0, (%rdx,%rcx)
addq $0x30, %rcx
jmp 0x478c
movq (%r15), %rbx
xorl %r14d, %r14d
subq $0x1, %rbx
jb 0x47d6
movq 0x10(%r15), %rax
movq 0x10(%rax,%r14), %rdi
testq %rdi, %rdi
je 0x47ca
callq 0x2060
movq 0x10(%r15), %rax
andq $0x0, 0x10(%rax,%r14)
andq $0x0, 0x18(%rax,%r14)
addq $0x28, %r14
jmp 0x47a7
testq %r12, %r12
je 0x47f0
leaq 0x1513f(%rip), %rdi     # 0x19921
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x2460
movq (%r15), %rbx
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %r15, 0x30(%rsp)
cmpq %rbp, %rbx
je 0x4913
movq 0x10(%r15), %r15
testb $0x40, 0x23(%r15,%r14)
jne 0x4902
andq $0x0, 0x40(%rsp)
movq (%r15,%r14), %rdx
movq 0x8(%r15,%r14), %rsi
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %rcx
leaq 0x40(%rsp), %r8
movl 0xc(%rsp), %r9d
callq 0x4ba0
movq %rax, %r13
movl 0x20(%r15,%r14), %eax
movl %eax, %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
je 0x4856
cvtsi2sd %ecx, %xmm0
jmp 0x4869
leaq (%r15,%r14), %rsi
movq 0x10(%rsp), %rdi
callq 0x45f4
movl 0x20(%r15,%r14), %eax
cvttsd2si %xmm0, %rcx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl $0xc0000000, %eax       # imm = 0xC0000000
orl %ecx, %eax
movl %eax, 0x20(%r15,%r14)
movq %r13, 0x18(%r15,%r14)
cmpl $0x0, 0x20(%rsp)
je 0x48c8
movq 0x10(%r15,%r14), %rdi
movsd %xmm0, (%rsp)
callq 0x2060
leaq 0x1(%r13), %rdi
callq 0x2310
movq %rax, 0x10(%r15,%r14)
movq 0x50(%rsp), %rsi
movq %rax, %rdi
movq %r13, %rdx
callq 0x2090
movsd (%rsp), %xmm0
movq 0x10(%r15,%r14), %rax
movb $0x0, (%rax,%r13)
testq %r12, %r12
je 0x4902
cvtsi2sd %r13, %xmm1
movq 0x50(%rsp), %r8
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %r10
subq $0x8, %rsp
movq %r12, %rdi
leaq 0x1503f(%rip), %rsi     # 0x1992d
movq %rbp, %rdx
movl %r13d, %ecx
movq %r13, %r9
movb $0x1, %al
pushq %r10
callq 0x2280
popq %rcx
popq %rdx
incq %rbp
addq $0x28, %r14
movq 0x30(%rsp), %r15
jmp 0x47fd
movq %r12, 0x10(%rsp)
movq 0x18(%r15), %rax
pushq $0x20
popq %r12
subq $0x1, %rax
jb 0x4b88
movq 0x28(%r15), %rdi
cmpl $0x3fffffff, (%rdi,%r12) # imm = 0x3FFFFFFF
ja 0x4b7a
movq %rax, 0x20(%rsp)
movq -0x20(%rdi,%r12), %rbp
movq -0x10(%rdi,%r12), %rdx
movq 0x30(%rsp), %r14
movq 0x10(%r14), %rax
movq %rbp, %rcx
shrq %rcx
imulq $0x28, %rcx, %rcx
addq %rax, %rcx
testq %rdx, %rdx
movq %r12, (%rsp)
je 0x49a5
andq $0x0, 0x40(%rsp)
movq 0x8(%rcx), %rsi
movq %rdi, %rbx
testb $0x1, %bpl
jne 0x4987
movq (%rcx), %rax
subq %rdx, %rax
leaq (%rsi,%rax,8), %rsi
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %rcx
leaq 0x40(%rsp), %r8
movl 0xc(%rsp), %r9d
callq 0x4ba0
jmp 0x4a57
movl %ebp, %edx
notl %edx
movq (%rcx), %rsi
movq 0x8(%rcx), %rcx
decq %rsi
andl $0x1, %edx
imulq %rsi, %rdx
movq (%rcx,%rdx,8), %r13
xorl %r13d, %ebp
movq %rdi, %rbx
movq -0x18(%rdi,%r12), %r8
movq %r8, %rcx
shrq %rcx
imulq $0x28, %rcx, %rcx
movq (%rax,%rcx), %rdx
movq 0x8(%rax,%rcx), %rax
decq %rdx
testb $0x1, %r8b
movl $0x0, %ecx
cmoveq %rcx, %rdx
movq (%rax,%rdx,8), %rcx
xorl %ecx, %r8d
shlq $0x4, %r13
andq $-0x20, %r13
movq 0x28(%rsp), %rax
addq %rax, %r13
andl $0x1, %ebp
shlq $0x4, %rcx
andq $-0x20, %rcx
addq %rax, %rcx
andl $0x1, %r8d
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %r9d, %r9d
callq 0x4d1c
movl %eax, %ecx
movl $0x0, %eax
cmpl 0x3c(%rsp), %ecx
jge 0x4a57
movslq %ecx, %rcx
andq $0x0, 0x40(%rsp)
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movl %ebp, %edx
leaq 0x40(%rsp), %r8
movl 0xc(%rsp), %r9d
callq 0x4fa8
movq 0x10(%r14), %rcx
movq (%rsp), %r12
movq -0x20(%rbx,%r12), %r13
movq -0x18(%rbx,%r12), %rbp
movq %r13, %rdx
shrq %rdx
movq %rdx, 0x60(%rsp)
imulq $0x28, %rdx, %rdx
movq %r14, %rdi
movq 0x18(%rcx,%rdx), %r15
cmpq %r15, %rax
cmovbq %rax, %r15
movq %rbp, %r14
shrq %r14
imulq $0x28, %r14, %rax
movq 0x18(%rcx,%rax), %rax
cmpq %rax, %r15
cmovaeq %rax, %r15
movq %r15, -0x8(%rbx,%r12)
movq %rbp, %rsi
xorq $0x1, %rsi
movq %r13, %rdx
xorq $0x1, %rdx
callq 0x5336
movq %r15, 0x18(%rax)
cmpq $0x0, 0x10(%rsp)
movq 0x20(%rsp), %rax
je 0x4b7a
andl $0x1, %r13d
leaq 0x14ea1(%rip), %rax     # 0x19976
movsbl (%r13,%rax), %ecx
andl $0x1, %ebp
movsbl (%rbp,%rax), %r9d
movq (%rsp), %rax
movl (%rbx,%rax), %r10d
movl $0x3fffffff, %r12d      # imm = 0x3FFFFFFF
andl %r12d, %r10d
movq 0x10(%rsp), %r13
movq %r13, %rdi
leaq 0x14e52(%rip), %rbp     # 0x19956
movq %rbp, %rsi
movq 0x60(%rsp), %rdx
movq %r14, %r8
xorl %eax, %eax
pushq %r10
pushq %r15
callq 0x2280
popq %rcx
popq %rdx
movq (%rsp), %rax
movq -0x20(%rbx,%rax), %rax
movq (%rsp), %rcx
movq -0x18(%rbx,%rcx), %rcx
movq %rcx, %rdx
shrq %rdx
andl $0x1, %ecx
leaq 0x14e3b(%rip), %rsi     # 0x19979
movsbl (%rcx,%rsi), %ecx
movq %rax, %r8
shrq %r8
andl $0x1, %eax
movsbl (%rax,%rsi), %r9d
movq (%rsp), %rax
movl (%rbx,%rax), %r10d
andl %r12d, %r10d
movq %r13, %rdi
movq %rbp, %rsi
xorl %eax, %eax
pushq %r10
pushq %r15
callq 0x2280
movq 0x10(%rsp), %r12
movq 0x30(%rsp), %rax
addq $0x10, %rsp
addq $0x30, %r12
movq 0x30(%rsp), %r15
jmp 0x4920
movq 0x50(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2060