// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/link.c
// Function: calc_link_directs
void calc_link_directs(inter_link_mat_t *link_mat, double min_norm, asm_dict_t *dict, int8_t *directs)
{
    uint32_t i, j, k, n, b, b0, b1, r, n_ma;
    int8_t t;
    inter_link_t *inter_link;
    double area[4]; // area under the cumsum of linkb
    double ma, sma;
    uint32_t max_b = UINT32_MAX;

    r = link_mat->r;
    for (i = 0; i < link_mat->n; ++i) {
        inter_link = &link_mat->links[i];
        n = inter_link->n;
        if (n == 0)
            continue;
        b0 = inter_link->b0;
        b1 = inter_link->b1;
        for (j = 0; j < n; ++j) {
            b = j / b1 + j % b1;
            if (b <= b0 && b <= b1 && b <= max_b && b < r)
                for (k = 0; k < 4; ++k)
                    if (inter_link->link[k][j] > .0)
                        inter_link->linkb[k][b] += inter_link->link[k][j];
        }
    }

    for (i = 0; i < link_mat->n; ++i) {
        inter_link = &link_mat->links[i];
        if (inter_link->n == 0)
            continue;
        r = inter_link->r;
        memset(area, 0, sizeof(area));
        for (j = 0; j < r; ++j)
            for (k = 0; k < 4; ++k)
                area[k] += inter_link->linkb[k][j]; //* (r - j);
        // select the one with maximum cumsum area as linkt
        t = 0;
        ma = sma = 0;
        n_ma = 0;
        for (k = 0; k < 4; ++k) {
            if (area[k] > ma) {
                sma = ma;
                ma = area[k];
                n_ma = 1;
                t = k;
            } else if (area[k] == ma) {
                ++n_ma;
            } else if (area[k] > sma) {
                sma = area[k];
            }
        }
        if (n_ma == 1 && ma * .9 > sma) {
            inter_link->linkt = 1 << t;
        } else {
            t = 0;
            for (k = 0; k < 4; ++k)
                if (inter_link->norms[k] >= min_norm)
                    t |= (1 << k);
            inter_link->linkt = t;
        }

#ifdef DEBUG_ORIEN
        fprintf(stderr, "[DEBUG_ORIEN::%s] %d %.0f %.0f %d %d [%.3f %.3f %.3f %.3f] [%.0f  %.0f  %.0f  %.0f] %s %s\n", __func__, inter_link->linkt, ma, sma, inter_link->b0, inter_link->b1, inter_link->norms[0], inter_link->norms[1], inter_link->norms[2], inter_link->norms[3], area[0], area[1], area[2], area[3], dict->s[inter_link->c0].name, dict->s[inter_link->c1].name);
#endif
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl (%rdi), %esi
movl 0x4(%rdi), %r8d
movabsq $0x40, %r9
xorl %r10d, %r10d
movabsq $-0x20, %r11
xorpd %xmm1, %xmm1
cmpq %rsi, %r10
je 0xbb06
movq 0x10(%rdi), %rbx
movq %r10, %rax
shlq $0x7, %rax
movl 0x14(%rbx,%rax), %r14d
testq %r14, %r14
je 0xbafa
addq %rbx, %rax
movl 0x8(%rax), %ecx
movl 0xc(%rax), %ebp
cmpl %ebp, %ecx
movl %ebp, %r15d
cmovbl %ecx, %r15d
addq %r9, %rbx
xorl %ecx, %ecx
cmpq %r14, %rcx
je 0xbafa
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
addl %eax, %edx
cmpl %r15d, %edx
ja 0xbaf5
cmpl %r8d, %edx
jae 0xbaf5
movl %edx, %eax
movq %r11, %rdx
testq %rdx, %rdx
je 0xbaf5
movq (%rbx,%rdx), %r12
movsd (%r12,%rcx,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0xbaef
movq 0x20(%rbx,%rdx), %r12
addsd (%r12,%rax,8), %xmm2
movsd %xmm2, (%r12,%rax,8)
addq $0x8, %rdx
jmp 0xbac9
incq %rcx
jmp 0xbaad
incq %r10
subq $-0x80, %r9
jmp 0xba78
movabsq $0x40, %rax
movabsq $0x60, %rdx
xorl %r8d, %r8d
movl $0x80, %r9d
xorpd %xmm1, %xmm1
movsd 0x12b11(%rip), %xmm2    # 0x1e640
movl $0x1, %r10d
cmpq %rsi, %r8
je 0xbc51
movq 0x10(%rdi), %rbx
movq %r8, %r11
shlq $0x7, %r11
cmpl $0x0, 0x14(%rbx,%r11)
je 0xbc14
addq %rbx, %r11
movl 0x10(%r11), %ecx
movapd %xmm1, -0x18(%rsp)
movapd %xmm1, -0x28(%rsp)
leaq (%rbx,%rax), %r14
xorl %r15d, %r15d
cmpq %rcx, %r15
je 0xbba0
xorl %r12d, %r12d
cmpq $0x4, %r12
je 0xbb9b
movq (%r14,%r12,8), %r13
movsd (%r13,%r15,8), %xmm3
addsd -0x28(%rsp,%r12,8), %xmm3
movsd %xmm3, -0x28(%rsp,%r12,8)
incq %r12
jmp 0xbb77
incq %r15
jmp 0xbb6f
xorpd %xmm3, %xmm3
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %ecx, %ecx
xorpd %xmm4, %xmm4
cmpq $0x4, %r14
je 0xbbfb
movsd -0x28(%rsp,%r14,8), %xmm6
ucomisd %xmm3, %xmm6
jbe 0xbbce
movl %r10d, %ebp
movl %r14d, %ecx
movapd %xmm6, %xmm5
jmp 0xbbee
jne 0xbbd6
jp 0xbbd6
incl %ebp
jmp 0xbbe6
ucomisd %xmm4, %xmm6
jbe 0xbbe6
movapd %xmm3, %xmm5
movapd %xmm6, %xmm3
jmp 0xbbee
movapd %xmm3, %xmm5
movapd %xmm4, %xmm3
movapd %xmm3, %xmm4
incq %r14
movapd %xmm5, %xmm3
jmp 0xbbaf
mulsd %xmm2, %xmm3
cmpl $0x1, %ebp
jne 0xbc22
ucomisd %xmm4, %xmm3
jbe 0xbc22
movl %r10d, %r14d
shll %cl, %r14d
movb %r14b, 0x1c(%r11)
incq %r8
addq %r9, %rax
addq %r9, %rdx
jmp 0xbb35
addq %rdx, %rbx
xorl %ecx, %ecx
xorl %r14d, %r14d
cmpq $0x4, %rcx
je 0xbc10
movl %r10d, %r15d
shll %cl, %r15d
movsd (%rbx,%rcx,8), %xmm3
xorl %ebp, %ebp
ucomisd %xmm0, %xmm3
jb 0xbc46
movl %r15d, %ebp
orb %r14b, %bpl
incq %rcx
movl %ebp, %r14d
jmp 0xbc2a
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq