// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]oatk/syncasm.c
// Function: scg_unitig_consensus
int64_t scg_unitig_consensus(sr_db_t *sr_db, uint64_t *v, uint64_t n, syncmer_t *scm, kstring_t *c_seq, int hoco_seq)
{
    if (n == 0) return 0;

    uint64_t i;
    int64_t beg_pos, end_pos, l;
    int w;
    kh_generic_t *h;

    w = sr_db->k;
    h = kh_generic_init();
    int64_t *pos;
    MYMALLOC(pos, n);
    pos[0] = 0;
    for (i = 1; i < n; ++i) {
        syncmer_t *m1 = &scm[v[i-1]>>1];
        syncmer_t *m2 = &scm[v[i]>>1];
        pos[i] = pos[i-1] + calc_syncmer_overlap(sr_db, m1, v[i-1]&1, m2, v[i]&1, h);
    }

#ifdef DEBUG_KMER_OVERLAP
    for (i = 0; i < n; ++i)
        fprintf(stderr, "[DEBUG_KMER_OVERLAP::%s] overlap [%lu %lu]\n", __func__, i, pos[i]);
#endif

    beg_pos = end_pos = l = 0;
    for (i = 0; i < n; ++i) {
        while (i+1 < n && pos[i+1] <= end_pos) ++i;
        beg_pos = pos[i];
        l += scg_syncmer_consensus(sr_db, &scm[v[i]>>1], v[i]&1, end_pos - beg_pos, c_seq, hoco_seq);
#ifdef DEBUG_KMER_OVERLAP
        fprintf(stderr, "[DEBUG_KMER_OVERLAP::%s] consensus start position [%lu %lu%c %ld %lu]\n", 
                __func__, i, v[i]>>1, "+-"[v[i]&1], end_pos-beg_pos, scm[v[i]>>1].h);
#endif
        end_pos = beg_pos + w;
    }
    free(pos);
    kh_generic_destroy(h);

    assert(l >= 0 && (uint64_t) l == c_seq->l);

    return l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, 0xc(%rsp)
movq %r8, 0x18(%rsp)
testq %rdx, %rdx
je 0x670b
movq %rcx, %r15
movq %rdx, %r12
movslq 0x18(%rdi), %rax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
movl $0x1, %ebp
movq %rsi, %r13
movl $0x1, %esi
movl $0x20, %edx
movq %rdi, 0x20(%rsp)
xorl %edi, %edi
callq 0x1d660
movq %rax, 0x10(%rsp)
movq %r14, 0x18(%rax)
leaq (,%r12,8), %rdi
callq 0x2310
movq %rax, %rbx
movq %r14, (%rax)
cmpq $0x1, %r12
je 0x6625
xorl %r14d, %r14d
movq -0x8(%r13,%rbp,8), %rcx
movq (%r13,%rbp,8), %rax
movq %rcx, %rdx
shlq $0x4, %rdx
andq $-0x20, %rdx
movq %rax, %rdi
shlq $0x4, %rdi
andq $-0x20, %rdi
andl $0x1, %ecx
andl $0x1, %eax
movl 0x10(%r15,%rdx), %esi
movq 0x18(%r15,%rdx), %rdx
movl 0x10(%r15,%rdi), %r8d
movq 0x18(%r15,%rdi), %r9
movq 0x20(%rsp), %rdi
pushq 0x10(%rsp)
pushq %rax
callq 0x671f
addq $0x10, %rsp
cltq
addq %rax, %r14
movq %r14, (%rbx,%rbp,8)
incq %rbp
cmpq %rbp, %r12
jne 0x65c7
movq %r13, 0x30(%rsp)
xorl %r14d, %r14d
xorl %ecx, %ecx
xorl %ebp, %ebp
leaq 0x1(%r14), %rdx
cmpq %rdx, %r12
cmovaq %r12, %rdx
leaq -0x1(%rdx), %rax
movq %r14, %rsi
movq 0x20(%rsp), %rdi
cmpq %rsi, %rax
je 0x6661
leaq 0x1(%rsi), %r14
cmpq %rcx, 0x8(%rbx,%rsi,8)
movq %r14, %rsi
jle 0x6648
leaq -0x1(%r14), %rax
jmp 0x6664
movq %rdx, %r14
movq (%rbx,%rax,8), %r13
movq 0x30(%rsp), %rdx
movq (%rdx,%rax,8), %rdx
movq %rdx, %rsi
shlq $0x4, %rsi
andq $-0x20, %rsi
addq %r15, %rsi
andl $0x1, %edx
subq %r13, %rcx
movq 0x18(%rsp), %r8
movl 0xc(%rsp), %r9d
callq 0x6a07
addq %rax, %rbp
addq 0x28(%rsp), %r13
movq %r13, %rcx
cmpq %r12, %r14
jb 0x6631
movq %rbx, %rdi
callq 0x2060
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x66dd
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rbx
movq %rbx, %rdi
callq 0x1d6fb
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x1d6fb
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1d6fb
testq %rbp, %rbp
js 0x66ec
movq 0x18(%rsp), %rax
cmpq (%rax), %rbp
je 0x670d
leaq 0x193b8(%rip), %rdi     # 0x1faab
leaq 0x19351(%rip), %rsi     # 0x1fa4b
leaq 0x193cd(%rip), %rcx     # 0x1face
movl $0x413, %edx            # imm = 0x413
callq 0x21d0
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq