// File: /workspace/github_c_makefile_star2_O0/c-zhou[P]yahs/link.c
// Function: inter_link_norms
int inter_link_norms(inter_link_mat_t *link_mat, norm_t *norm, int use_estimated_noise, double max_noise_ratio, double *la)
{
    uint32_t i, j, k, b, b1, r, n, n0;
    uint32_t *fn;
    int8_t bl;
    double l, *norms, *fr, noise;
    double c, c0, t;
    inter_link_t *inter_link;

    noise = .0;
    if (use_estimated_noise) {
        // noise = estimate_noise(link_mat);
        noise = link_mat->noise;
        fprintf(stderr, "[I::%s] using noise level %.3f\n", __func__, noise);
    }

    r = link_mat->r;
    norms = (double *) calloc(r + 1, sizeof(double));
    for (i = 0; i <= r; ++i) {
        if (norm->norms[i] * max_noise_ratio > noise) {
            norms[i] = norm->norms[i] - noise;
        } else {
            fprintf(stderr, "[I::%s] using new radius %u (%u) as noise ratio exceeds threshold %.3f\n", __func__, i, r, max_noise_ratio);
            r = i;
            break;
        }
    }
    if (r <= 1) return 1;

    /***
    // no weighting
    c = c0 = 0;
    for (i = 0; i < link_mat->n; ++i) {
        inter_link = &link_mat->links[i];
        n = inter_link->n;
        if (n == 0)
            continue;
        r = inter_link->r;
        b1 = inter_link->b1;
        n0 = 0;
        for (j = 0; j < n; ++j) {
            b = j / b1 + j % b1;
            if (b < r) {
                if (norms[b + 1] > 0) {
                    bl = 0;
                    for (k = 0; k < 4; ++k) {
                        l = inter_link->link[k][j];
                        if (l < .0)
                            break;
                        if (l >= norms[b + 1] + noise) {
                            inter_link->norms[k] += 1.;
                            ++c0;
                        }
                        bl = 1;
                    }
                    if (bl)
                        ++n0;
                }
            }
        }
        for (k = 0; k < 4; ++k)
            inter_link->norms[k] /= n0;
        inter_link->n0 = n0;
        c += n0;
    }
    **/
    
    c = c0 = 0;
    for (i = 0; i < link_mat->n; ++i) {
        inter_link = &link_mat->links[i];
        n = inter_link->n;
        if (n == 0)
            continue;
        r = inter_link->r;
        b1 = inter_link->b1;

        fr = (double *) calloc(4 * r, sizeof(double));
        fn = (uint32_t *) calloc(r, sizeof(uint32_t));

        for (j = 0; j < n; ++j) {
            b = j / b1 + j % b1;
            if (b < r) {
                if (norms[b + 1] > 0) {
                    bl = 0;
                    for (k = 0; k < 4; ++k) {
                        l = inter_link->link[k][j];
                        if(l < .0)
                            break;
                        l = MAX(.0, l - noise);
                        fr[k * r + b] += MIN(1., l / norms[b + 1]);
                        bl = 1;
                    }
                    if (bl)
                        ++fn[b];
                }
            }
        }
        if (fn[0] == 0)
            goto loop_i_end;

        // cumsum
        for (k = 0; k < 4; ++k)
            inter_link->norms[k] = fr[k * r];
        for (b = 1; b < r; ++b) {
            for (k = 0; k < 4; ++k) {
                inter_link->norms[k] += fr[k * r + b] * fr[k * r + b - 1] / fn[b - 1];
                fr[k * r + b] += fr[k * r + b - 1];
            }
            fn[b] += fn[b - 1];
        }

        n0 = fn[r - 1];
        t = 0;
        for (k = 0; k < 4; ++k) {
            if (inter_link->norms[k] > t)
                t = inter_link->norms[k];
            inter_link->norms[k] /= n0;
        }
        inter_link->n0 = n0;
        c += n0;
        c0 += t;
loop_i_end:
        free(fr);
        free(fn);
    }
    
    *la = c == 0? .0 : c0 / c;
    fprintf(stderr, "[I::%s] average link count: %.3f %.3f %.3f\n", __func__, c0, c, *la);
    
    free(norms);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movsd %xmm0, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x10dff
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x80(%rbp)
movq 0x28216(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movsd -0x80(%rbp), %xmm0
leaq 0x20a7b(%rip), %rsi     # 0x3186c
leaq 0x20a94(%rip), %rdx     # 0x3188c
movb $0x1, %al
callq 0x22c0
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
movl $0x8, %esi
callq 0x2290
movq %rax, -0x70(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x48(%rbp), %eax
ja 0x10eb6
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
mulsd -0x28(%rbp), %xmm0
ucomisd -0x80(%rbp), %xmm0
jbe 0x10e73
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
subsd -0x80(%rbp), %xmm0
movq -0x70(%rbp), %rax
movl -0x34(%rbp), %ecx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x10ea6
movq 0x2817e(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movl -0x34(%rbp), %ecx
movl -0x48(%rbp), %r8d
movsd -0x28(%rbp), %xmm0
leaq 0x20a0d(%rip), %rsi     # 0x3189d
leaq 0x209f5(%rip), %rdx     # 0x3188c
movb $0x1, %al
callq 0x22c0
movl -0x34(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x10eb6
jmp 0x10ea8
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x10e28
cmpl $0x1, -0x48(%rbp)
ja 0x10ec8
movl $0x1, -0x4(%rbp)
jmp 0x11396
xorps %xmm0, %xmm0
movsd %xmm0, -0x90(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x11307
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x10f25
jmp 0x112f9
movq -0xa0(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0xa0(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x44(%rbp)
movl -0x48(%rbp), %eax
shll $0x2, %eax
movl %eax, %eax
movl %eax, %edi
movl $0x8, %esi
callq 0x2290
movq %rax, -0x78(%rbp)
movl -0x48(%rbp), %eax
movl %eax, %edi
movl $0x4, %esi
callq 0x2290
movq %rax, -0x58(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jae 0x110f9
movl -0x38(%rbp), %eax
xorl %edx, %edx
divl -0x44(%rbp)
movl %eax, -0xa4(%rbp)
movl -0x38(%rbp), %eax
xorl %edx, %edx
divl -0x44(%rbp)
movl -0xa4(%rbp), %eax
addl %edx, %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x110e9
movq -0x70(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x110e7
movb $0x0, -0x59(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jae 0x110cf
movq -0xa0(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq 0x20(%rax,%rcx,8), %rax
movl -0x38(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x68(%rbp), %xmm0
jbe 0x11008
jmp 0x110cf
movsd -0x68(%rbp), %xmm1
subsd -0x80(%rbp), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x11028
xorps %xmm0, %xmm0
movsd %xmm0, -0xb0(%rbp)
jmp 0x1103a
movsd -0x68(%rbp), %xmm0
subsd -0x80(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
movq -0x70(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
divsd (%rax,%rcx,8), %xmm0
movsd 0x20593(%rip), %xmm1    # 0x315f8
ucomisd %xmm1, %xmm0
jbe 0x1107d
movsd 0x20585(%rip), %xmm0    # 0x315f8
movsd %xmm0, -0xb8(%rbp)
jmp 0x1109b
movsd -0x68(%rbp), %xmm0
movq -0x70(%rbp), %rax
movl -0x40(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
divsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xb8(%rbp)
movsd -0xb8(%rbp), %xmm0
movq -0x78(%rbp), %rax
movl -0x3c(%rbp), %ecx
imull -0x48(%rbp), %ecx
addl -0x40(%rbp), %ecx
movl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb $0x1, -0x59(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x10fd3
cmpb $0x0, -0x59(%rbp)
je 0x110e5
movq -0x58(%rbp), %rax
movl -0x40(%rbp), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x110e7
jmp 0x110e9
jmp 0x110eb
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x10f71
movq -0x58(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x11107
jmp 0x112e7
movl $0x0, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jae 0x11141
movq -0x78(%rbp), %rax
movl -0x3c(%rbp), %ecx
imull -0x48(%rbp), %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movq -0xa0(%rbp), %rax
movl -0x3c(%rbp), %ecx
movsd %xmm0, 0x60(%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1110e
movl $0x1, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x11221
movl $0x0, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jae 0x111f7
movq -0x78(%rbp), %rax
movl -0x3c(%rbp), %edi
movl -0x48(%rbp), %ecx
imull %ecx, %edi
movl -0x40(%rbp), %ecx
movl %edi, %edx
addl %ecx, %edx
movsd (%rax,%rdx,8), %xmm0
movl %ecx, %esi
movl %edi, %edx
leal -0x1(%rdx,%rsi), %edx
movsd (%rax,%rdx,8), %xmm1
mulsd %xmm1, %xmm0
movq -0x58(%rbp), %rax
decl %ecx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movq -0xa0(%rbp), %rax
movl -0x3c(%rbp), %ecx
addsd 0x60(%rax,%rcx,8), %xmm0
movsd %xmm0, 0x60(%rax,%rcx,8)
movq -0x78(%rbp), %rax
movl -0x3c(%rbp), %ecx
imull -0x48(%rbp), %ecx
addl -0x40(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movq -0x78(%rbp), %rax
movl -0x3c(%rbp), %ecx
imull -0x48(%rbp), %ecx
addl -0x40(%rbp), %ecx
movl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1115b
movq -0x58(%rbp), %rax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movq -0x58(%rbp), %rax
movl -0x40(%rbp), %ecx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x11148
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x98(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jae 0x112aa
movq -0xa0(%rbp), %rax
movl -0x3c(%rbp), %ecx
movsd 0x60(%rax,%rcx,8), %xmm0
ucomisd -0x98(%rbp), %xmm0
jbe 0x1127d
movq -0xa0(%rbp), %rax
movl -0x3c(%rbp), %ecx
movsd 0x60(%rax,%rcx,8), %xmm0
movsd %xmm0, -0x98(%rbp)
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0xa0(%rbp), %rax
movl -0x3c(%rbp), %ecx
movsd 0x60(%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x11245
movl -0x50(%rbp), %ecx
movq -0xa0(%rbp), %rax
movl %ecx, 0x18(%rax)
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x98(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x78(%rbp), %rdi
callq 0x2050
movq -0x58(%rbp), %rdi
callq 0x2050
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x10ee5
movsd -0x88(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x11327
jp 0x11327
xorps %xmm0, %xmm0
movsd %xmm0, -0xc0(%rbp)
jmp 0x1133f
movsd -0x90(%rbp), %xmm0
divsd -0x88(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0xc0(%rbp), %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x27ca2(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movsd -0x90(%rbp), %xmm0
movsd -0x88(%rbp), %xmm1
movq -0x30(%rbp), %rax
movsd (%rax), %xmm2
leaq 0x2056d(%rip), %rsi     # 0x318e5
leaq 0x2050d(%rip), %rdx     # 0x3188c
movb $0x3, %al
callq 0x22c0
movq -0x70(%rbp), %rdi
callq 0x2050
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)