// File: /workspace/github_c_makefile_star2_O1/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 0x622a
movq %rdx, %r12
movq %rsi, %r13
movslq 0x18(%rdi), %rax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
movl $0x1, %ebp
movl $0x1, %esi
movl $0x20, %edx
movq %rdi, 0x20(%rsp)
xorl %edi, %edi
movq %rcx, %r15
callq 0x1b528
movq %rax, 0x10(%rsp)
movq %r14, 0x18(%rax)
leaq (,%r12,8), %rdi
callq 0x2320
movq %rax, %rbx
movq %r14, (%rax)
cmpq $0x1, %r12
je 0x6146
movq -0x8(%r13,%rbp,8), %rdx
movq (%r13,%rbp,8), %r8
movq %rdx, %rsi
shlq $0x4, %rsi
andq $-0x20, %rsi
addq %r15, %rsi
movq %r8, %rcx
shlq $0x4, %rcx
andq $-0x20, %rcx
addq %r15, %rcx
movq -0x8(%rbx,%rbp,8), %r14
andl $0x1, %edx
andl $0x1, %r8d
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %r9
callq 0x623e
cltq
addq %r14, %rax
movq %rax, (%rbx,%rbp,8)
incq %rbp
cmpq %rbp, %r12
jne 0x60f4
movq %r15, 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 0x6182
leaq 0x1(%rsi), %r14
cmpq %rcx, 0x8(%rbx,%rsi,8)
movq %r14, %rsi
jle 0x6169
leaq -0x1(%r14), %rax
jmp 0x6185
movq %rdx, %r14
movq (%rbx,%rax,8), %r15
movq (%r13,%rax,8), %rdx
movq %rdx, %rsi
shlq $0x4, %rsi
andq $-0x20, %rsi
addq 0x30(%rsp), %rsi
andl $0x1, %edx
subq %r15, %rcx
movq 0x18(%rsp), %r8
movl 0xc(%rsp), %r9d
callq 0x653f
addq %rax, %rbp
addq 0x28(%rsp), %r15
movq %r15, %rcx
cmpq %r12, %r14
jb 0x6152
movq %rbx, %rdi
callq 0x2060
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x61fc
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rbx
movq %rbx, %rdi
callq 0x1b5ca
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x1b5ca
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1b5ca
testq %rbp, %rbp
js 0x620b
movq 0x18(%rsp), %rax
cmpq (%rax), %rbp
je 0x622c
leaq 0x17861(%rip), %rdi     # 0x1da73
leaq 0x177fa(%rip), %rsi     # 0x1da13
leaq 0x17876(%rip), %rcx     # 0x1da96
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