// File: /workspace/github_c_makefile_star2_O1/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:
movl (%rdi), %esi
testq %rsi, %rsi
je 0xed1c
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
movl 0x4(%rdi), %r8d
movq 0x10(%rdi), %r9
leaq 0x40(%r9), %r10
xorl %r11d, %r11d
xorpd %xmm1, %xmm1
movq %r11, %rax
shlq $0x7, %rax
movl 0x14(%r9,%rax), %ebx
testq %rbx, %rbx
je 0xebe1
addq %r9, %rax
movl 0x8(%rax), %ecx
movl 0xc(%rax), %ebp
cmpl %ebp, %ecx
movl %ebp, %r14d
cmovbl %ecx, %r14d
xorl %ecx, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
addl %eax, %edx
cmpl %r14d, %edx
ja 0xebd9
cmpl %r8d, %edx
jae 0xebd9
movl %edx, %eax
movq $-0x4, %rdx
movq (%r10,%rdx,8), %r15
movsd (%r15,%rcx,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0xebd4
movq 0x20(%r10,%rdx,8), %r15
addsd (%r15,%rax,8), %xmm2
movsd %xmm2, (%r15,%rax,8)
incq %rdx
jne 0xebb3
incq %rcx
cmpq %rbx, %rcx
jne 0xeb98
incq %r11
subq $-0x80, %r10
cmpq %rsi, %r11
jne 0xeb73
movq 0x10(%rdi), %rax
leaq 0x40(%rax), %rdx
leaq 0x60(%rax), %rdi
xorl %r8d, %r8d
movl $0x80, %r9d
xorpd %xmm1, %xmm1
movsd 0x12a42(%rip), %xmm2    # 0x21650
movq %r8, %r10
shlq $0x7, %r10
cmpl $0x0, 0x14(%rax,%r10)
je 0xed04
addq %rax, %r10
movl 0x10(%r10), %ecx
movapd %xmm1, -0x18(%rsp)
movapd %xmm1, -0x28(%rsp)
testq %rcx, %rcx
je 0xec65
xorl %r11d, %r11d
xorl %ebx, %ebx
movq (%rdx,%rbx,8), %r14
movsd (%r14,%r11,8), %xmm3
addsd -0x28(%rsp,%rbx,8), %xmm3
movsd %xmm3, -0x28(%rsp,%rbx,8)
incq %rbx
cmpq $0x4, %rbx
jne 0xec3e
incq %r11
cmpq %rcx, %r11
jne 0xec3c
xorl %r11d, %r11d
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
xorl %ecx, %ecx
xorl %ebx, %ebx
movapd %xmm4, %xmm5
movsd -0x28(%rsp,%r11,8), %xmm6
ucomisd %xmm4, %xmm6
jbe 0xec97
movl $0x1, %ebx
movl %r11d, %ecx
movapd %xmm6, %xmm4
movapd %xmm5, %xmm3
jmp 0xecb3
jne 0xec9f
jp 0xec9f
incl %ebx
jmp 0xecaf
ucomisd %xmm3, %xmm6
jbe 0xecaf
movapd %xmm5, %xmm4
movapd %xmm6, %xmm3
jmp 0xecb3
movapd %xmm5, %xmm4
incq %r11
cmpq $0x4, %r11
jne 0xec74
mulsd %xmm2, %xmm4
cmpl $0x1, %ebx
jne 0xecd6
ucomisd %xmm3, %xmm4
jbe 0xecd6
movl $0x1, %r11d
shll %cl, %r11d
jmp 0xed00
xorl %ecx, %ecx
xorl %r11d, %r11d
movl $0x1, %ebx
shll %cl, %ebx
movsd (%rdi,%rcx,8), %xmm3
ucomisd %xmm0, %xmm3
movl $0x0, %ebp
jb 0xecf4
movl %ebx, %ebp
orb %bpl, %r11b
incq %rcx
cmpq $0x4, %rcx
jne 0xecdb
movb %r11b, 0x1c(%r10)
incq %r8
addq %r9, %rdx
addq %r9, %rdi
cmpq %rsi, %r8
jne 0xec0e
popq %rbx
popq %r14
popq %r15
popq %rbp
retq