// File: /workspace/github_c_makefile_star2_O2/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)
movl %edx, 0x28(%rsp)
movq (%rsi), %r12
testq %r12, %r12
je 0x3cca
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
pushq $0x1
popq %r13
pushq $0x20
popq %rsi
movq %r13, %rdi
callq 0x2250
movq %rax, 0x38(%rsp)
movq %rbx, (%rax)
movq 0x10(%rbx), %rbx
pushq $0x28
popq %rsi
movq %r12, %rdi
callq 0x2250
movq %rbx, 0x18(%rsp)
leaq 0x10(%rbx), %rdx
movq %rax, 0x20(%rsp)
movq %rax, %r15
addq $0x20, %r15
xorl %ecx, %ecx
movq %r12, 0x30(%rsp)
subq $0x1, %r12
jb 0x39e9
movl (%rdx), %ebp
movl %ebp, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl 0x28(%rsp), %eax
movq %rcx, 0x8(%rsp)
setb %cl
testl %ebp, %ebp
movq %r13, %r14
movq %rdx, %r13
sets %dl
orb %cl, %dl
movzbl %dl, %ebx
shll $0x1f, %ebx
orl %ebx, %eax
movl %eax, (%r13)
pushq $0x8
popq %rdi
callq 0x2310
movq %r13, %rdx
movq %r14, %r13
movq 0x8(%rsp), %rcx
movq %rax, -0x18(%r15)
movq $0x1, -0x20(%r15)
movq %rcx, (%rax)
andl $0x3fffffff, %ebp       # imm = 0x3FFFFFFF
movl (%r15), %eax
movl $0x80000000, %esi       # imm = 0x80000000
andl %esi, %eax
orl %ebp, %eax
shrl %ebx
orl %eax, %ebx
movl %ebx, (%r15)
addq $0x20, %rdx
addq $0x28, %r15
addq $0x2, %rcx
jmp 0x396c
xorl %r14d, %r14d
callq 0x3cde
movq %rax, %r12
movq 0x10(%rsp), %rax
cmpq (%rax), %r14
jae 0x3a8d
movq 0x10(%rax), %r15
imulq $0x58, %r14, %rcx
movl 0x38(%r15,%rcx), %eax
testl %eax, %eax
je 0x3a85
addq %rcx, %r15
movq 0x40(%r15), %rcx
movq 0x50(%r15), %rdx
movq (%rdx), %rsi
andq $-0x2, %rsi
movl (%rcx), %edx
andl $0x1, %edx
orq %rsi, %rdx
movq %r13, %rbp
movl %eax, %eax
cmpq %rax, %rbp
jae 0x3a85
movq 0x40(%r15), %rax
movq 0x50(%r15), %rcx
movq (%rcx,%rbp,8), %rcx
andq $-0x2, %rcx
movl (%rax,%rbp,4), %ebx
andl $0x1, %ebx
orq %rcx, %rbx
cmpq %rbx, %rdx
jbe 0x3a6b
movq %rbx, %rax
xorq $0x1, %rax
xorq $0x1, %rdx
movq %r12, %rdi
movq %rdx, %rsi
movq %rax, %rdx
jmp 0x3a71
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rcx
callq 0x3cf3
incq %rbp
movl 0x38(%r15), %eax
movq %rbx, %rdx
jmp 0x3a30
incq %r14
jmp 0x39f4
pushq $0x10
popq %rbx
xorl %r15d, %r15d
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
xorps %xmm2, %xmm2
xorl %r9d, %r9d
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %r8
movq 0x10(%r12), %rsi
testq %rsi, %rsi
je 0x3aca
movb (%r12), %cl
pushq $0x1
popq %rdx
shlq %cl, %rdx
jmp 0x3acc
xorl %edx, %edx
cmpq %rdx, %r15
jae 0x3c5e
movq 0x8(%r12), %rcx
movq %r15, %rdx
shrq $0x5, %rdx
movl (%rcx,%rdx,4), %ecx
btl %r15d, %ecx
jae 0x3c52
movq -0x10(%rsi,%rbx), %r13
movq -0x8(%rsi,%rbx), %r14
movl (%rsi,%rbx), %ebp
cvtsi2sd %rbp, %xmm0
movq %r14, %r10
shrq %r10
movq %r10, %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
cvtsi2sd %esi, %xmm1
mulsd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3c52
imulq $0x28, %r10, %rdx
testb $0x40, 0x23(%rdi,%rdx)
jne 0x3c52
imulq $0x28, %rcx, %rcx
testb $0x40, 0x23(%rdi,%rcx)
jne 0x3c52
cmpq 0x8(%rsp), %r9
jne 0x3ba4
leaq (%r9,%r9), %rdx
testq %r9, %r9
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rsp)
imulq $0x30, %rdx, %rsi
movq %rax, %rdi
movq %r9, 0x10(%rsp)
callq 0x2350
xorps %xmm2, %xmm2
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %rdi
andl $0x3fffffff, %ebp       # imm = 0x3FFFFFFF
imulq $0x30, %r9, %rcx
movq %r14, (%rax,%rcx)
movq %r13, 0x8(%rax,%rcx)
movups %xmm2, 0x10(%rax,%rcx)
movl %ebp, 0x20(%rax,%rcx)
orq $-0x1, 0x28(%rax,%rcx)
leaq 0x1(%r9), %rdx
xorq $0x1, %r13
cmpq %r14, %r13
jne 0x3bd8
movq %rdx, %r9
jmp 0x3c52
movq 0x8(%rsp), %rcx
cmpq %rcx, %rdx
jne 0x3c28
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rsp)
imulq $0x30, %rsi, %rsi
movq %rax, %rdi
movq %r9, 0x10(%rsp)
movq %rdx, 0x28(%rsp)
callq 0x2350
movq 0x28(%rsp), %rdx
xorps %xmm2, %xmm2
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %rdi
xorq $0x1, %r14
orl $0x80000000, %ebp       # imm = 0x80000000
addq $0x2, %r9
imulq $0x30, %rdx, %rcx
movq %r13, (%rax,%rcx)
movq %r14, 0x8(%rax,%rcx)
movups %xmm2, 0x10(%rax,%rcx)
movl %ebp, 0x20(%rax,%rcx)
orq $-0x1, 0x28(%rax,%rcx)
incq %r15
addq $0x18, %rbx
jmp 0x3ab4
movq %rdi, %r14
movq %r12, %rdi
movq %r9, %r15
movq %rax, %r12
callq 0x3d41
pushq $0x1
popq %rdi
pushq $0x40
popq %rsi
callq 0x2250
movq %rax, %rbx
movq %r14, 0x10(%rax)
movq 0x30(%rsp), %rax
movq %rax, (%rbx)
movq %rax, 0x8(%rbx)
movq %r12, 0x28(%rbx)
movq %r15, 0x18(%rbx)
movq %r15, 0x20(%rbx)
imulq $0x30, %r15, %rsi
movq %r12, %rdi
callq 0x2350
movq %rax, 0x28(%rbx)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x1213f
movq 0x38(%rsp), %rax
movq %rbx, 0x8(%rax)
movq %rax, %rbx
movq %rax, %rdi
callq 0x3d7c
jmp 0x3ccc
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq