// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]oatk/syncasm.c
// Function: make_syncmer_graph
scg_t *make_syncmer_graph(sr_db_t *sr_db, syncmer_db_t *scm_db, uint32_t min_k_cov, double min_a_cov_f)
{
    if (scm_db->n == 0) return 0;

    scg_t *scg;
    MYCALLOC(scg, 1);
    scg->scm_db = scm_db;
    
    size_t i, j;
    uint64_t n_scm;
    syncmer_t *scm;
    sr_t *s;
    
    scm = scg_a_scm(scg);
    n_scm = scg_n_scm(scg);
    
    // add utg vtx
    // each single scm is a utg
    asmg_vtx_t *vtx, *vtx1;
    MYCALLOC(vtx, n_scm);
    for (i = 0; i < n_scm; ++i) {
        // filter by kmer coverage
        scm[i].del |= scm[i].cov < min_k_cov;
        vtx1 = &vtx[i];
        MYMALLOC(vtx1->a, 1);
        vtx1->n = 1;
        vtx1->a[0] = i<<1;
        vtx1->cov = scm[i].cov;
        vtx1->del = scm[i].del;
    }

    // add arcs
    kvec_t(asmg_arc_t) arc;
    kv_init(arc);
    uint64_t v0, v1;
    uint32_t r0, r1, v_v;
    uint128_t v_p; // v0, v1 packed
    kh_u128_t *a_cov; // arc cov
    a_cov = kh_u128_init();
    for (i = 0; i < sr_db->n; ++i) {
        s = &sr_db->a[i];
        if (s->n == 0) continue;
        // the last bit of k_mer indicates if it is corrected mer
        v0 = s->k_mer[0] >> 1;
        r0 = s->m_pos[0] & 1;
        v0 = v0 << 1 | r0;

        for (j = 1; j < s->n; ++j) {
            // the last bit of k_mer indicates if it is corrected mer
            v1 = s->k_mer[j] >> 1;
            r1 = s->m_pos[j] & 1;
            v1 = v1 << 1 | r1;

            v0 <= v1? add_a_cov(a_cov, (uint128_t) v0 << 64 | v1, 1) :
                add_a_cov(a_cov, ((uint128_t) v1^1) << 64 | (v0^1), 1);

            v0 = v1;
        }
    }

    khint_t k;
    for (k = (khint_t) 0; k < kh_end(a_cov); ++k) {
        if (kh_exist(a_cov, k)) {
            v_p = kh_key(a_cov, k);
            v_v = kh_val(a_cov, k);
            v0 = (uint64_t) (v_p >> 64);
            v1 = (uint64_t) v_p;
            if (v_v < min_a_cov_f * MIN(scm[v0>>1].cov, scm[v1>>1].cov)
                    || vtx[v0>>1].del || vtx[v1>>1].del)
                continue;
            asmg_arc_t a = {v0, v1, 0, 0, v_v, 0, 0, UINT64_MAX};
            kv_push(asmg_arc_t, arc, a);
            if ((v1^1) != v0 || (v0^1) != v1) {
                // to avoid multi-arcs
                // for arcs like (v+)->(v-)
                asmg_arc_t a_c = {v1^1, v0^1, 0, 0, v_v, 0, 1, UINT64_MAX};
                kv_push(asmg_arc_t, arc, a_c);
            }
        }
    }
    kh_u128_destroy(a_cov);

    asmg_t *utg_asmg;
    MYCALLOC(utg_asmg, 1);
    utg_asmg->vtx = vtx;
    utg_asmg->m_vtx = utg_asmg->n_vtx = n_scm;
    utg_asmg->arc = arc.a;
    utg_asmg->m_arc = utg_asmg->n_arc = arc.n;
    MYREALLOC(utg_asmg->arc, utg_asmg->n_arc);
    // sort arc and build index
    asmg_finalize(utg_asmg, 1);

    scg->utg_asmg = utg_asmg;
    scg_scm_utg_index(scg);

    return scg;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x40(%rsp)
movq (%rsi), %r12
testq %r12, %r12
je 0x5127
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, (%rsp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x2250
movq %rax, 0x38(%rsp)
movq %r14, (%rax)
movq 0x10(%r14), %r15
movl $0x28, %esi
movq %r12, %rdi
callq 0x2250
movq %rax, 0x18(%rsp)
movq %rax, %r14
addq $0x20, %r14
movq %r15, 0x10(%rsp)
addq $0x10, %r15
movq %r12, %r13
xorl %r12d, %r12d
movl $0x80000000, %ebp       # imm = 0x80000000
movq %r13, 0x30(%rsp)
movl (%r15), %eax
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ebx, %ecx
setb %dl
testl %eax, %eax
sets %al
orb %dl, %al
movzbl %al, %eax
shll $0x1f, %eax
orl %ecx, %eax
movl %eax, (%r15)
movl $0x8, %edi
callq 0x2310
movq %rax, -0x18(%r14)
movq $0x1, -0x20(%r14)
movq %r12, (%rax)
movl (%r15), %eax
movl %eax, %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl (%r14), %edx
andl %ebp, %edx
orl %ecx, %edx
shrl %eax
andl $0x40000000, %eax       # imm = 0x40000000
orl %edx, %eax
movl %eax, (%r14)
addq $0x2, %r12
addq $0x28, %r14
addq $0x20, %r15
decq %r13
jne 0x4d16
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1d660
movq %rax, %r12
movq $0x0, 0x18(%rax)
movq (%rsp), %rsi
movq (%rsi), %rax
testq %rax, %rax
je 0x4e8f
xorl %r15d, %r15d
leaq 0x20(%rsp), %rbx
movq 0x10(%rsi), %r13
imulq $0x58, %r15, %rcx
movl 0x38(%r13,%rcx), %edx
testl %edx, %edx
je 0x4e83
cmpl $0x1, %edx
je 0x4e83
addq %rcx, %r13
movq 0x40(%r13), %rax
movq 0x50(%r13), %rcx
movq (%rcx), %rcx
andq $-0x2, %rcx
movl (%rax), %edx
andl $0x1, %edx
orq %rcx, %rdx
movl $0x1, %ebp
movq 0x40(%r13), %rax
movq 0x50(%r13), %rcx
movq (%rcx,%rbp,8), %rcx
andq $-0x2, %rcx
movl (%rax,%rbp,4), %r14d
andl $0x1, %r14d
orq %rcx, %r14
cmpq %r14, %rdx
jbe 0x4e36
movq %r14, %rax
xorq $0x1, %rax
xorq $0x1, %rdx
movq %r12, %rdi
movq %rdx, %rsi
movq %rax, %rdx
leaq 0x24(%rsp), %rcx
callq 0xcd0b
cmpl $0x0, 0x24(%rsp)
jmp 0x4e49
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rcx
callq 0xcd0b
cmpl $0x0, 0x20(%rsp)
movq 0x10(%r12), %rcx
movl %eax, %eax
leaq (%rax,%rax,2), %rax
leaq 0x10(%rcx,%rax,8), %rax
movl $0x1, %ecx
jne 0x4e66
movq (%rax), %rcx
incq %rcx
movq %rcx, (%rax)
incq %rbp
movl 0x38(%r13), %eax
movq %r14, %rdx
cmpq %rax, %rbp
jb 0x4df1
movq (%rsp), %rsi
movq (%rsi), %rax
incq %r15
cmpq %rax, %r15
jb 0x4db4
movl $0x10, %ebx
xorl %ebp, %ebp
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
xorps %xmm2, %xmm2
xorl %r9d, %r9d
movq $0x0, (%rsp)
xorl %r10d, %r10d
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
movq 0x10(%r12), %rsi
testq %rsi, %rsi
je 0x4ecf
movb (%r12), %cl
movl $0x1, %eax
shlq %cl, %rax
jmp 0x4ed1
xorl %eax, %eax
cmpq %rax, %rbp
jae 0x5094
movq 0x8(%r12), %rax
movq %rbp, %rcx
shrq $0x5, %rcx
movl (%rax,%rcx,4), %eax
btl %ebp, %eax
jae 0x5088
movq -0x10(%rsi,%rbx), %r13
movq -0x8(%rsi,%rbx), %r14
movl (%rsi,%rbx), %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
movq %r14, %rax
shrq %rax
movq %rax, %rcx
shlq $0x5, %rcx
movl 0x10(%r8,%rcx), %edx
andl %r11d, %edx
movq %r13, %rcx
shrq %rcx
movq %rcx, %rsi
shlq $0x5, %rsi
movl 0x10(%r8,%rsi), %esi
andl %r11d, %esi
cmpl %esi, %edx
cmovbl %edx, %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
mulsd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x5088
leaq (%rax,%rax,4), %rax
testb $0x40, 0x23(%rdi,%rax,8)
jne 0x5088
leaq (%rcx,%rcx,4), %rax
testb $0x40, 0x23(%rdi,%rax,8)
jne 0x5088
cmpq (%rsp), %r9
jne 0x4fb9
leaq (%r9,%r9), %rcx
testq %r9, %r9
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, %rax
movq %rcx, (%rsp)
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
movq %r10, %rdi
movq %r9, 0x8(%rsp)
callq 0x2350
xorps %xmm2, %xmm2
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, %r10
andl $0x3fffffff, %r15d      # imm = 0x3FFFFFFF
leaq 0x1(%r9), %rcx
leaq (%r9,%r9,2), %rax
shlq $0x4, %rax
movq %r14, (%r10,%rax)
movq %r13, 0x8(%r10,%rax)
movups %xmm2, 0x10(%r10,%rax)
movl %r15d, 0x20(%r10,%rax)
movq $-0x1, 0x28(%r10,%rax)
xorq $0x1, %r13
cmpq %r14, %r13
jne 0x4ffa
movq %rcx, %r9
jmp 0x5088
movq (%rsp), %rax
cmpq %rax, %rcx
jne 0x5054
leaq (%rax,%rax), %rdx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rdx
movq %rdx, (%rsp)
movq %rdx, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rsi
movq %r10, %rdi
movq %r9, 0x8(%rsp)
movq %rcx, 0x28(%rsp)
callq 0x2350
movq 0x28(%rsp), %rcx
xorps %xmm2, %xmm2
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
movq %rax, %r10
xorq $0x1, %r14
orl $0x80000000, %r15d      # imm = 0x80000000
addq $0x2, %r9
leaq (%rcx,%rcx,2), %rax
shlq $0x4, %rax
movq %r13, (%r10,%rax)
movq %r14, 0x8(%r10,%rax)
movups %xmm2, 0x10(%r10,%rax)
movl %r15d, 0x20(%r10,%rax)
movq $-0x1, 0x28(%r10,%rax)
incq %rbp
addq $0x18, %rbx
jmp 0x4eb7
movq 0x18(%r12), %rbx
movq %rdi, %r14
movq %rbx, %rdi
movq %r9, %r15
movq %r10, %r13
callq 0x1d6fb
movq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x1d6fb
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1d6fb
movl $0x1, %edi
movl $0x40, %esi
callq 0x2250
movq %rax, %rbx
movq %r14, 0x10(%rax)
movq 0x30(%rsp), %rax
movq %rax, (%rbx)
movq %rax, 0x8(%rbx)
movq %r13, 0x28(%rbx)
movq %r15, 0x18(%rbx)
movq %r15, 0x20(%rbx)
shlq $0x4, %r15
leaq (%r15,%r15,2), %rsi
movq %r13, %rdi
callq 0x2350
movq %rax, 0x28(%rbx)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x15d06
movq 0x38(%rsp), %rax
movq %rbx, 0x8(%rax)
movq %rax, %rbx
movq %rax, %rdi
callq 0x513b
jmp 0x5129
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq