// File: /workspace/github_c_makefile_star2_O1/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), %rbp
testq %rbp, %rbp
je 0x4e1c
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 %rbp, %rdi
callq 0x2250
movq %rax, 0x8(%rsp)
movq %rax, %r14
addq $0x20, %r14
movq %r15, 0x18(%rsp)
addq $0x10, %r15
xorl %r12d, %r12d
movl $0xc0000000, %r13d      # imm = 0xC0000000
movq %rbp, 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 0x2320
movq %rax, -0x18(%r14)
movq $0x1, -0x20(%r14)
movq %r12, (%rax)
movl (%r15), %eax
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl %ecx, %eax
movl (%r14), %ecx
andl %r13d, %ecx
orl %eax, %ecx
movl %ecx, (%r14)
movl (%r15), %eax
shrl %eax
andl $0x40000000, %eax       # imm = 0x40000000
andl $0xbfffffff, %ecx       # imm = 0xBFFFFFFF
orl %eax, %ecx
movl %ecx, (%r14)
addq $0x2, %r12
addq $0x28, %r14
addq $0x20, %r15
decq %rbp
jne 0x4a00
movl $0x1, %esi
movl $0x20, %edx
xorl %edi, %edi
callq 0x1b528
movq %rax, %r12
movq $0x0, 0x18(%rax)
movq (%rsp), %rax
cmpq $0x0, (%rax)
je 0x4b85
xorl %r15d, %r15d
leaq 0x20(%rsp), %rbx
movq 0x10(%rax), %r13
imulq $0x58, %r15, %rax
cmpl $0x0, 0x38(%r13,%rax)
je 0x4b75
addq %rax, %r13
cmpl $0x2, 0x38(%r13)
jb 0x4b75
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 0x4b2b
movq %r14, %rax
xorq $0x1, %rax
xorq $0x1, %rdx
movq %r12, %rdi
movq %rdx, %rsi
movq %rax, %rdx
leaq 0x24(%rsp), %rcx
callq 0xc38a
cmpl $0x0, 0x24(%rsp)
jmp 0x4b3e
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rcx
callq 0xc38a
cmpl $0x0, 0x20(%rsp)
movl %eax, %eax
je 0x4b54
movq 0x10(%r12), %rcx
leaq (%rax,%rax,2), %rdx
movq $0x0, 0x10(%rcx,%rdx,8)
movq 0x10(%r12), %rcx
leaq (%rax,%rax,2), %rax
incq 0x10(%rcx,%rax,8)
incq %rbp
movl 0x38(%r13), %eax
movq %r14, %rdx
cmpq %rax, %rbp
jb 0x4ae6
incq %r15
movq (%rsp), %rax
cmpq (%rax), %r15
jb 0x4aa8
movl $0x10, %ebx
xorl %ebp, %ebp
xorps %xmm2, %xmm2
xorl %r10d, %r10d
movq $0x0, (%rsp)
xorl %eax, %eax
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r9
movq 0x10(%r12), %rsi
testq %rsi, %rsi
je 0x4bbe
movb (%r12), %cl
movl $0x1, %edx
shlq %cl, %rdx
jmp 0x4bc0
xorl %edx, %edx
cmpq %rdx, %rbp
jae 0x4d82
movq 0x8(%r12), %rcx
movl %ebp, %edx
shrl $0x5, %edx
movl (%rcx,%rdx,4), %ecx
btl %ebp, %ecx
jae 0x4d76
movq -0x10(%rsi,%rbx), %r13
movq -0x8(%rsi,%rbx), %r14
movl (%rsi,%rbx), %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
movq %r14, %rcx
shrq %rcx
movq %rcx, %rdx
shlq $0x5, %rdx
movl 0x10(%r9,%rdx), %esi
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl %r11d, %esi
movq %r13, %rdx
shrq %rdx
movq %rdx, %rdi
shlq $0x5, %rdi
movl 0x10(%r9,%rdi), %edi
andl %r11d, %edi
cmpl %edi, %esi
cmovbl %esi, %edi
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
mulsd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x4d76
leaq (%rcx,%rcx,4), %rcx
testb $0x40, 0x23(%r8,%rcx,8)
jne 0x4d76
leaq (%rdx,%rdx,4), %rcx
testb $0x40, 0x23(%r8,%rcx,8)
jne 0x4d76
movq (%rsp), %rdx
cmpq %rdx, %r10
jne 0x4cac
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, (%rsp)
movq %rsi, %rcx
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
movq %r10, 0x10(%rsp)
callq 0x2360
xorps %xmm2, %xmm2
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
movq (%rsp), %rdx
andl $0x3fffffff, %r15d      # imm = 0x3FFFFFFF
leaq 0x1(%r10), %rsi
leaq (%r10,%r10,2), %rcx
shlq $0x4, %rcx
movq %r14, (%rax,%rcx)
movq %r13, 0x8(%rax,%rcx)
movups %xmm2, 0x10(%rax,%rcx)
movl %r15d, 0x20(%rax,%rcx)
movq $-0x1, 0x28(%rax,%rcx)
xorq $0x1, %r13
cmpq %r14, %r13
jne 0x4cf0
movq %rdx, (%rsp)
movq %rsi, %r10
jmp 0x4d76
cmpq %rdx, %rsi
jne 0x4d3f
leaq (%rdx,%rdx), %rdi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rdi
movq %rdi, (%rsp)
movq %rdi, %rcx
shlq $0x4, %rcx
movq %rsi, 0x28(%rsp)
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
movq %r10, 0x10(%rsp)
callq 0x2360
movq 0x28(%rsp), %rsi
xorps %xmm2, %xmm2
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
jmp 0x4d43
movq %rdx, (%rsp)
xorq $0x1, %r14
orl $0x80000000, %r15d      # imm = 0x80000000
addq $0x2, %r10
leaq (%rsi,%rsi,2), %rcx
shlq $0x4, %rcx
movq %r13, (%rax,%rcx)
movq %r14, 0x8(%rax,%rcx)
movups %xmm2, 0x10(%rax,%rcx)
movl %r15d, 0x20(%rax,%rcx)
movq $-0x1, 0x28(%rax,%rcx)
incq %rbp
addq $0x18, %rbx
jmp 0x4ba6
movq %rax, %r15
movq %r10, %r14
testq %r12, %r12
je 0x4db2
movq 0x18(%r12), %rbx
movq %rbx, %rdi
callq 0x1b5ca
movq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x1b5ca
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1b5ca
movl $0x1, %edi
movl $0x40, %esi
callq 0x2250
movq %rax, %rbx
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rbx)
movq 0x30(%rsp), %rax
movq %rax, (%rbx)
movq %rax, 0x8(%rbx)
movq %r15, %rdi
movq %r15, 0x28(%rbx)
movq %r14, 0x18(%rbx)
movq %r14, 0x20(%rbx)
shlq $0x4, %r14
leaq (%r14,%r14,2), %rsi
callq 0x2360
movq %rax, 0x28(%rbx)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x15237
movq 0x38(%rsp), %rax
movq %rbx, 0x8(%rax)
movq %rax, %rbx
movq %rax, %rdi
callq 0x4e30
jmp 0x4e1e
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq