// File: /workspace/github_c_makefile_star2_O3/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
movl %ecx, 0x2c(%rsp)
movl %edx, 0xc(%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq %rdi, 0x20(%rsp)
movl 0x18(%rdi), %eax
movl %eax, 0x28(%rsp)
movq (%rsi), %rax
movq %rsi, 0x48(%rsp)
movq 0x8(%rsi), %r14
movq 0x10(%rax), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%r14), %rdx
testq %rdx, %rdx
je 0x6139
movq 0x28(%r14), %rax
leaq 0x1(%rdx), %rcx
andq $-0x2, %rcx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
addq $0x48, %rax
movdqa 0x1970b(%rip), %xmm1    # 0x1f7d0
movdqa 0x19713(%rip), %xmm2    # 0x1f7e0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x19713(%rip), %xmm4    # 0x1f7f0
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x6116
movq $0x0, -0x30(%rax)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x612b
movq $0x0, (%rax)
paddq %xmm4, %xmm1
addq $0x60, %rax
addq $-0x2, %rcx
jne 0x60dd
movq %r8, 0x10(%rsp)
movq (%r14), %rbx
testq %rbx, %rbx
je 0x617b
movq 0x10(%r14), %rax
xorl %r15d, %r15d
movq 0x10(%rax,%r15), %rdi
testq %rdi, %rdi
je 0x6169
callq 0x2060
movq 0x10(%r14), %rax
movq $0x0, 0x10(%rax,%r15)
movq $0x0, 0x18(%rax,%r15)
addq $0x28, %r15
decq %rbx
jne 0x614d
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x619b
leaq 0x197cd(%rip), %rdi     # 0x1f959
movl $0xb, %esi
movl $0x1, %edx
callq 0x2460
movq (%r14), %r15
testq %r15, %r15
je 0x62cf
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x10(%r14), %rbx
testb $0x40, 0x23(%rbx,%r12)
jne 0x62bf
movq $0x0, 0x30(%rsp)
movq (%rbx,%r12), %rdx
movq 0x8(%rbx,%r12), %rsi
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rcx
leaq 0x30(%rsp), %r8
movl 0xc(%rsp), %r9d
callq 0x6551
movq %rax, %r13
movl 0x20(%rbx,%r12), %eax
movl %eax, %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
je 0x6202
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
jmp 0x6215
leaq (%rbx,%r12), %rsi
movq 0x48(%rsp), %rdi
callq 0x5ed8
movl 0x20(%rbx,%r12), %eax
cvttsd2si %xmm0, %rcx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl $0xc0000000, %eax       # imm = 0xC0000000
orl %ecx, %eax
movl %eax, 0x20(%rbx,%r12)
movq %r13, 0x18(%rbx,%r12)
cmpl $0x0, 0x2c(%rsp)
je 0x627b
movsd %xmm0, 0x50(%rsp)
movq 0x10(%rbx,%r12), %rdi
testq %rdi, %rdi
je 0x624d
callq 0x2060
leaq 0x1(%r13), %rdi
callq 0x2310
movq %rax, 0x10(%rbx,%r12)
movq 0x40(%rsp), %rsi
movq %rax, %rdi
movq %r13, %rdx
callq 0x2090
movq 0x10(%rbx,%r12), %rax
movb $0x0, (%rax,%r13)
movsd 0x50(%rsp), %xmm0
cmpq $0x0, 0x10(%rsp)
je 0x62bf
xorps %xmm1, %xmm1
cvtsi2sd %r13, %xmm1
movq 0x40(%rsp), %r8
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %r10
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
leaq 0x196bc(%rip), %rsi     # 0x1f965
movq %rbp, %rdx
movl %r13d, %ecx
movq %r13, %r9
movb $0x1, %al
pushq %r10
callq 0x2280
addq $0x10, %rsp
incq %rbp
addq $0x28, %r12
cmpq %rbp, %r15
jne 0x61ac
movq 0x18(%r14), %rbx
testq %rbx, %rbx
je 0x6539
xorl %r15d, %r15d
movq 0x28(%r14), %r13
leaq (%r15,%r15,2), %rax
shlq $0x4, %rax
cmpl $0x3fffffff, 0x20(%r13,%rax) # imm = 0x3FFFFFFF
ja 0x652d
addq %rax, %r13
movq (%r13), %rbp
movq 0x10(%r13), %rdx
movq %rbp, %rax
shrq %rax
movq 0x10(%r14), %rcx
leaq (%rax,%rax,4), %rax
leaq (%rcx,%rax,8), %rax
testq %rdx, %rdx
je 0x6357
movq $0x0, 0x30(%rsp)
movq 0x8(%rax), %rsi
testb $0x1, %bpl
jne 0x6339
movq (%rax), %rax
subq %rdx, %rax
leaq (%rsi,%rax,8), %rsi
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rcx
leaq 0x30(%rsp), %r8
movl 0xc(%rsp), %r9d
callq 0x6551
jmp 0x6418
movl %ebp, %edx
notl %edx
movq (%rax), %rsi
movq 0x8(%rax), %rax
decq %rsi
andl $0x1, %edx
imulq %rsi, %rdx
movq (%rax,%rdx,8), %r12
xorl %r12d, %ebp
movq 0x8(%r13), %rax
movq %rax, %rdx
shrq %rdx
leaq (%rdx,%rdx,4), %rdx
movq (%rcx,%rdx,8), %rsi
movq 0x8(%rcx,%rdx,8), %rcx
decq %rsi
testb $0x1, %al
movl $0x0, %edx
cmoveq %rdx, %rsi
movq (%rcx,%rsi,8), %rcx
xorl %ecx, %eax
shlq $0x4, %r12
andq $-0x20, %r12
andl $0x1, %ebp
shlq $0x4, %rcx
andq $-0x20, %rcx
andl $0x1, %eax
movq 0x18(%rsp), %rdi
movl 0x10(%rdi,%r12), %esi
movq 0x18(%rdi,%r12), %rdx
movl 0x10(%rdi,%rcx), %r8d
movq 0x18(%rdi,%rcx), %r9
movq 0x20(%rsp), %rdi
movq %rbp, %rcx
pushq $0x0
pushq %rax
callq 0x671f
addq $0x10, %rsp
movl %eax, %ecx
movl $0x0, %eax
cmpl 0x28(%rsp), %ecx
jge 0x6418
addq 0x18(%rsp), %r12
movslq %ecx, %rcx
movq $0x0, 0x30(%rsp)
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movl %ebp, %edx
leaq 0x30(%rsp), %r8
movl 0xc(%rsp), %r9d
callq 0x6a07
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
je 0x648e
movq 0x38(%rsi), %rdi
xorq %rcx, %rdi
addq $0x30, %rsi
jmp 0x647b
movq %r12, 0x18(%rsi)
cmpq $0x0, 0x10(%rsp)
je 0x652d
andl $0x1, %ecx
leaq 0x19506(%rip), %rsi     # 0x1f9ae
movsbl (%rcx,%rsi), %ecx
andl $0x1, %eax
movsbl (%rax,%rsi), %r9d
movl 0x20(%r13), %r10d
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
andl %eax, %r10d
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
leaq 0x194bf(%rip), %rsi     # 0x1f98e
xorl %eax, %eax
pushq %r10
pushq %r12
callq 0x2280
addq $0x10, %rsp
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq %rcx, %rdx
shrq %rdx
andl $0x1, %ecx
leaq 0x194bb(%rip), %rsi     # 0x1f9b1
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 %rbp, %rdi
leaq 0x19470(%rip), %rsi     # 0x1f98e
xorl %eax, %eax
pushq %r10
pushq %r12
callq 0x2280
addq $0x10, %rsp
incq %r15
cmpq %rbx, %r15
jne 0x62df
movq 0x40(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2060