// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/yahs.c
// Function: build_graph_from_links
graph_t *build_graph_from_links(inter_link_mat_t *link_mat, asm_dict_t *dict, double min_norm, double la, int8_t *telo_ends)
{
    int32_t i, j, n, c0, c1;
    uint32_t v, w;
    int8_t t;
    double norm, qla;
    inter_link_t *link;
    graph_t *g;
    graph_arc_t *arc;

    g = graph_init();
    g->sdict = dict;

    // build graph
    n = link_mat->n;
    for (i = 0; i < n; ++i) {
        link = &link_mat->links[i];
        if (link->n == 0 || link->n0 == 0)
            continue;
        c0 = link->c0;
        c1 = link->c1;
        t = link->linkt;
        if (!t)
            continue;
        
        qla = qbinom(.99, link->n0, la, 1, 0) / link->n0;
        for (j = 0; j < 4; ++j) {
            if (1 << j & t) {
                norm = link->norms[j];
                if (norm >= min_norm) {
                    if (norm < qla) {
#ifdef DEBUG_QLF
                        fprintf(stderr, "[DEBUG_QLF::%s] #Edge rejected by QL filter: %s %s %u %u %.3f (< %.3f)\n", __func__, dict->s[c0].name, dict->s[c1].name, j, link->n0, norm, qla);
#endif
                        continue;
                    }
                    v = c0<<1 | (j>>1);
                    w = c1<<1 | (j&1);
                    if (telo_ends[v^1] || telo_ends[w])
                        continue;
                    arc = graph_add_arc(g, v, w, -1, 0, norm);
                    graph_add_arc(g, w^1, v^1, arc->link_id, 0, norm);
                }
            }
        }
    }

    graph_arc_sort(g);
    graph_arc_index(g);

    return g;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %rbx
movsd %xmm1, 0x30(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rsi, %r12
movq %rdi, %r14
callq 0x5dad
movq %rax, (%rsp)
movq %r12, 0x8(%rax)
movq %r14, 0x48(%rsp)
movl (%r14), %eax
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
movq %rax, 0x50(%rsp)
pushq $0x60
popq %rdi
movq %rbx, 0x18(%rsp)
cmpq 0x50(%rsp), %rdx
je 0x1a6aa
movq 0x48(%rsp), %rax
movq 0x10(%rax), %r12
movq %rdx, %rbx
shlq $0x7, %rbx
cmpl $0x0, 0x14(%r12,%rbx)
je 0x1a69e
addq %r12, %rbx
movl 0x18(%rbx), %eax
testl %eax, %eax
je 0x1a69e
movsbl 0x1c(%rbx), %r15d
testl %r15d, %r15d
je 0x1a69e
movl (%rbx), %r14d
movl 0x4(%rbx), %ebp
movl %eax, %eax
cvtsi2sd %rax, %xmm1
movsd 0x5df8(%rip), %xmm0     # 0x20388
movsd 0x30(%rsp), %xmm2
movq %rdi, 0x20(%rsp)
pushq $0x1
popq %rdi
xorl %esi, %esi
movq %rdx, 0x28(%rsp)
callq 0x1736e
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rcx
movapd %xmm0, %xmm1
movl 0x18(%rbx), %eax
cvtsi2sd %rax, %xmm0
movl %ebp, %eax
divsd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm2
addl %r14d, %r14d
addl %ebp, %eax
addq %rdi, %r12
xorl %ebp, %ebp
movl %eax, 0xc(%rsp)
movsd %xmm1, 0x38(%rsp)
cmpq $0x4, %rbp
je 0x1a69e
btl %ebp, %r15d
jae 0x1a696
movsd (%r12,%rbp,8), %xmm0
ucomisd %xmm2, %xmm0
jb 0x1a696
ucomisd %xmm0, %xmm1
ja 0x1a696
movl %ebp, %esi
shrl %esi
orl %r14d, %esi
movq %rsi, %r13
xorq $0x1, %r13
cmpb $0x0, (%rcx,%r13)
jne 0x1a696
movl %ebp, %ebx
andl $0x1, %ebx
orl %eax, %ebx
cmpb $0x0, (%rcx,%rbx)
jne 0x1a696
movq (%rsp), %rdi
movl %ebx, %edx
pushq $-0x1
popq %rcx
xorl %r8d, %r8d
movsd %xmm0, 0x40(%rsp)
callq 0x5f0a
xorl $0x1, %ebx
movq 0x18(%rax), %rcx
movabsq $0x1fffffffffffffff, %rax # imm = 0x1FFFFFFFFFFFFFFF
andq %rax, %rcx
movq (%rsp), %rdi
movl %ebx, %esi
movl %r13d, %edx
xorl %r8d, %r8d
movsd 0x40(%rsp), %xmm0
callq 0x5f0a
movsd 0x38(%rsp), %xmm1
movl 0xc(%rsp), %eax
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
movsd 0x10(%rsp), %xmm2
movq 0x18(%rsp), %rcx
incq %rbp
jmp 0x1a5e5
incq %rdx
subq $-0x80, %rdi
jmp 0x1a538
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x6057
movq %rbx, %rdi
callq 0x60d0
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq