// File: /workspace/github_c_makefile_star2_O0/c-zhou[P]yahs/link.c
// Function: calc_norms
norm_t *calc_norms(intra_link_mat_t *link_mat, uint32_t d_min_cell, double d_mass_frac)
{
    uint32_t i, j, n, b, r, r0, t;
    uint32_t *bs;
    double intra_c, tmp_c;
    double *norms, *link, *linkc;
    norm_t *norm;
    
    // find maximum numebr of bands
    n = 0;
    for (i = 0; i < link_mat->n; ++i)
        n = MAX(link_mat->links[i].n, n);
    if (n <= 1) {
        fprintf(stderr, "[E::%s] no bands for norm calculation, try a higher resolution\n", __func__);
        return 0;
    }
    n -= 1; // do not use the last one as it might be an incomplete cell
    // calculate number of cells for each band
    bs = (uint32_t *) calloc(n, sizeof(uint32_t));
    for (i = 0; i < link_mat->n; ++i) {
        if (link_mat->links[i].n) {
            b = link_mat->links[i].n - 1;
            for (j = 0; j < b; ++j)
                bs[j] += b - j;
        }
    }

    r0 = 0;
    while (bs[r0] >= d_min_cell)
        ++r0;
    if (r0 < 10) {
        fprintf(stderr, "[E::%s] no enough bands (%d) for norm calculation, try a higher resolution\n", __func__, r0);
        free(bs);
        return 0;
    }

    // caluclate links in each band and radius
    // calculate norms - using median or mean?
    norms = (double *) malloc(n * sizeof(double));
    linkc = (double *) malloc(n * sizeof(double));
    intra_c = .0;
    for (i = 0; i < n; ++i) {
        link = (double *) malloc(bs[i] * sizeof(double));
        t = 0;
        for (j = 0; j < link_mat->n; ++j) {
            b = link_mat->links[j].n;
            if (b > i + 1) {
                memcpy(link + t, link_mat->links[j].link + (long) (b * 2 - i - 1) * i / 2 + i, (b - 1 - i) * sizeof(double));
                t += b - 1 - i;
            }
        }

        tmp_c = .0;
        for (j = 0; j < bs[i]; ++j)
            tmp_c += link[j];
        linkc[i] = tmp_c;
        intra_c += tmp_c;
        
#ifdef USE_MEDIAN_NORM
        qsort(link, bs[i], sizeof(double), dcmp);
        norms[i] = bs[i] & 1? link[bs[i] / 2] : (link[bs[i] / 2] + link[bs[i] / 2 - 1]) / 2;
#else
        norms[i] = MAX(linkc[i], 1.) / bs[i];
#endif

        free(link);
    }

    intra_c -= linkc[0];
    tmp_c = .0;
    for (r = 1; r < n; ++r) {
        tmp_c += linkc[r];
        if (tmp_c / intra_c >= d_mass_frac)
            break;
    }
    
    r = MIN(MIN(r, r0), MAX_RADIUS);

#ifdef USE_MEDIAN_NORM
    // adjust radius by norms
    // change radius if norm drops to zero before reaching r
    // find the first position with 10 consective zeros
    int zeros = 0;
    int p = -1;
    for (i = 0; i < n; ++i) {
        if (norms[i] < 1) {
            ++zeros;
            if (zeros >= 10) {
                p = i - 9;
                break;
            }
        } else {
            zeros = 0;
        }
    }
    if (p >= 0 && p < r)
        r = p;
    for (i = 0; i <= r; ++i) {
        if(norms[i] < 1)
            norms[i] = 1;
    }
#endif

    norm = (norm_t *) malloc(sizeof(norm_t));
    norm->n = n;
    norm->r = r;
    norm->bs = bs;
    //norm->link = link;
    norm->linkc = linkc;
    norm->norms = norms;
    return norm;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movsd %xmm0, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x101a4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x2c(%rbp), %eax
jbe 0x10187
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x84(%rbp)
jmp 0x10190
movl -0x2c(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x10145
cmpl $0x1, -0x2c(%rbp)
ja 0x101d6
movq 0x28e47(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
leaq 0x21570(%rip), %rsi     # 0x3172b
leaq 0x215a9(%rip), %rdx     # 0x3176b
movb $0x0, %al
callq 0x22c0
movq $0x0, -0x8(%rbp)
jmp 0x106ed
movl -0x2c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %edi
movl $0x4, %esi
callq 0x2290
movq %rax, -0x48(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x10273
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x0, 0x4(%rax)
je 0x10266
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
subl $0x1, %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x30(%rbp), %eax
jae 0x10264
movl -0x30(%rbp), %edx
subl -0x28(%rbp), %edx
movq -0x48(%rbp), %rax
movl -0x28(%rbp), %ecx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1023e
jmp 0x10266
jmp 0x10268
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x101f9
movl $0x0, -0x38(%rbp)
movq -0x48(%rbp), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x14(%rbp), %eax
jb 0x10294
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1027a
cmpl $0xa, -0x38(%rbp)
jae 0x102d2
movq 0x28d57(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
movl -0x38(%rbp), %ecx
leaq 0x214c8(%rip), %rsi     # 0x31776
leaq 0x214b6(%rip), %rdx     # 0x3176b
movb $0x0, %al
callq 0x22c0
movq -0x48(%rbp), %rdi
callq 0x2050
movq $0x0, -0x8(%rbp)
jmp 0x106ed
movl -0x2c(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x2340
movq %rax, -0x60(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x2340
movq %rax, -0x70(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x10517
movq -0x48(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x2340
movq %rax, -0x68(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x103f4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x28(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jbe 0x103e4
movq -0x68(%rbp), %rdi
movl -0x3c(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x28(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rsi
movl -0x30(%rbp), %eax
shll %eax
subl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl -0x24(%rbp), %ecx
imulq %rcx, %rax
movl $0x2, %ecx
cqto
idivq %rcx
shlq $0x3, %rax
addq %rax, %rsi
movl -0x24(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rsi
movl -0x30(%rbp), %eax
subl $0x1, %eax
subl -0x24(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
shlq $0x3, %rdx
callq 0x22e0
movl -0x30(%rbp), %eax
subl $0x1, %eax
subl -0x24(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x103e6
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x10338
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x48(%rbp), %rcx
movl -0x24(%rbp), %edx
cmpl (%rcx,%rdx,4), %eax
jae 0x10433
movq -0x68(%rbp), %rax
movl -0x28(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x10403
movsd -0x58(%rbp), %xmm0
movq -0x70(%rbp), %rax
movl -0x24(%rbp), %ecx
movsd %xmm0, (%rax,%rcx,8)
movsd -0x58(%rbp), %xmm0
addsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x68(%rbp), %rdi
movq -0x48(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %esi
movl $0x8, %edx
leaq -0x3af(%rip), %rcx      # 0x100c0
callq 0x20e0
movq -0x48(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x104a7
movq -0x68(%rbp), %rax
movq -0x48(%rbp), %rcx
movl -0x24(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x90(%rbp)
jmp 0x104ec
movq -0x68(%rbp), %rax
movq -0x48(%rbp), %rcx
movl -0x24(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movq -0x68(%rbp), %rax
movq -0x48(%rbp), %rcx
movl -0x24(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
subl $0x1, %ecx
movl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
movsd 0x20ce0(%rip), %xmm1    # 0x311c0
divsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x90(%rbp), %xmm0
movq -0x60(%rbp), %rax
movl -0x24(%rbp), %ecx
movsd %xmm0, (%rax,%rcx,8)
movq -0x68(%rbp), %rdi
callq 0x2050
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x10305
movq -0x70(%rbp), %rax
movsd (%rax), %xmm1
movsd -0x50(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x1057a
movq -0x70(%rbp), %rax
movl -0x34(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
divsd -0x50(%rbp), %xmm0
ucomisd -0x20(%rbp), %xmm0
jb 0x1056d
jmp 0x1057a
jmp 0x1056f
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1053c
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x1058d
movl -0x34(%rbp), %eax
movl %eax, -0x94(%rbp)
jmp 0x10596
movl -0x38(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl 0x28d3e(%rip), %eax     # 0x392e0
jae 0x105ce
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x105b7
movl -0x34(%rbp), %eax
movl %eax, -0x98(%rbp)
jmp 0x105c0
movl -0x38(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
movl %eax, -0x9c(%rbp)
jmp 0x105da
movl 0x28d0c(%rip), %eax     # 0x392e0
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x7c(%rbp)
movl $0xffffffff, -0x80(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x10646
movq -0x60(%rbp), %rax
movl -0x24(%rbp), %ecx
movsd 0x20fe9(%rip), %xmm0    # 0x315f8
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x10632
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
cmpl $0xa, -0x7c(%rbp)
jl 0x10630
movl -0x24(%rbp), %eax
subl $0x9, %eax
movl %eax, -0x80(%rbp)
jmp 0x10646
jmp 0x10639
movl $0x0, -0x7c(%rbp)
jmp 0x1063b
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x105f8
cmpl $0x0, -0x80(%rbp)
jl 0x1065a
movl -0x80(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x1065a
movl -0x80(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x34(%rbp), %eax
ja 0x106a0
movq -0x60(%rbp), %rax
movl -0x24(%rbp), %ecx
movsd 0x20f80(%rip), %xmm0    # 0x315f8
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x10693
movq -0x60(%rbp), %rax
movl -0x24(%rbp), %ecx
movsd 0x20f6a(%rip), %xmm0    # 0x315f8
movsd %xmm0, (%rax,%rcx,8)
jmp 0x10695
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x10661
movl $0x28, %edi
callq 0x2340
movq %rax, -0x78(%rbp)
movl -0x2c(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, (%rax)
movl -0x34(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x48(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x60(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x78(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw (%rax,%rax)