// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %esi, %r14d
movl (%rdi), %ebx
movq %rbx, %rax
shlq $0x4, %rax
xorl %ecx, %ecx
xorl %r15d, %r15d
cmpq %rcx, %rax
je 0xabf5
movq 0x8(%rdi), %rdx
movl 0x4(%rdx,%rcx), %edx
cmpl %r15d, %edx
cmoval %edx, %r15d
addq $0x10, %rcx
jmp 0xabdb
cmpl $0x1, %r15d
jbe 0xaed6
movsd %xmm0, 0x38(%rsp)
decl %r15d
pushq $0x4
popq %rsi
movq %rdi, 0x18(%rsp)
movq %r15, %rdi
callq 0x2290
movq 0x18(%rsp), %rdi
movq %rax, %r12
xorl %eax, %eax
cmpq %rbx, %rax
je 0xac58
movq 0x8(%rdi), %rcx
movq %rax, %rdx
shlq $0x4, %rdx
movl 0x4(%rcx,%rdx), %ecx
testl %ecx, %ecx
je 0xac53
decl %ecx
movq %r12, %rdx
movq %rcx, %rsi
subq $0x1, %rsi
jb 0xac53
addl %ecx, (%rdx)
addq $0x4, %rdx
movq %rsi, %rcx
jmp 0xac42
incq %rax
jmp 0xac22
pushq $-0x1
popq %rcx
incl %ecx
cmpl %r14d, (%r12,%rcx,4)
jae 0xac5b
cmpl $0x9, %ecx
jbe 0xaefa
movq %rcx, 0x30(%rsp)
leaq (,%r15,8), %r14
movq %r14, %rdi
callq 0x2330
movq %rax, %rbp
movq %r14, %rdi
callq 0x2330
movq %rax, 0x8(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ebx, %ebx
movq %r15, 0x50(%rsp)
movq %r12, 0x48(%rsp)
movq %rbp, 0x40(%rsp)
cmpq %r15, %rbx
je 0xadf3
movl (%r12,%rbx,4), %eax
movq %rax, 0x60(%rsp)
leaq (,%rax,8), %rdi
callq 0x2330
movq %rax, %r13
movq 0x18(%rsp), %rcx
movl (%rcx), %r12d
leaq 0x1(%rbx), %rax
movq %rax, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
notl %ebx
shlq $0x4, %r12
xorl %ebp, %ebp
xorl %r15d, %r15d
cmpq %rbp, %r12
je 0xad51
movq 0x8(%rcx), %rax
movl 0x4(%rax,%rbp), %r14d
cmpq %r14, 0x28(%rsp)
jae 0xad4b
movl %r15d, %ecx
leal (%rbx,%r14,2), %edx
movq 0x20(%rsp), %rsi
imulq %rsi, %rdx
andq $-0x2, %rdx
shlq $0x2, %rdx
addq 0x8(%rax,%rbp), %rdx
leaq (,%rcx,8), %rdi
addq %r13, %rdi
leaq (%rdx,%rsi,8), %rsi
addl %ebx, %r14d
leaq (,%r14,8), %rdx
callq 0x22e0
movq 0x18(%rsp), %rcx
addl %r15d, %r14d
movl %r14d, %r15d
addq $0x10, %rbp
jmp 0xacef
xorpd %xmm0, %xmm0
xorl %eax, %eax
movq 0x50(%rsp), %r15
movq 0x48(%rsp), %r12
movq 0x40(%rsp), %rbp
movq 0x20(%rsp), %rbx
movq 0x60(%rsp), %r14
cmpq %rax, %r14
je 0xad81
addsd (%r13,%rax,8), %xmm0
incq %rax
jmp 0xad70
movq 0x8(%rsp), %rax
movsd %xmm0, 0x58(%rsp)
movsd %xmm0, (%rax,%rbx,8)
movq %r13, %rdi
movq %r14, %rsi
pushq $0x8
popq %rdx
leaq -0x207(%rip), %rcx      # 0xab9a
callq 0x20e0
movl %r14d, %eax
shrl %eax
movsd (%r13,%rax,8), %xmm0
testb $0x1, %r14b
jne 0xadc9
decl %eax
addsd (%r13,%rax,8), %xmm0
mulsd 0x13417(%rip), %xmm0    # 0x1e1e0
movsd 0x10(%rsp), %xmm1
addsd 0x58(%rsp), %xmm1
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, (%rbp,%rbx,8)
movq %r13, %rdi
callq 0x2060
movq 0x28(%rsp), %rbx
jmp 0xacac
movq 0x8(%rsp), %rax
movsd 0x10(%rsp), %xmm3
subsd (%rax), %xmm3
xorpd %xmm0, %xmm0
pushq $0x1
popq %rax
movsd 0x38(%rsp), %xmm2
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rcx
cmpq %rax, %r15
je 0xae36
addsd (%rcx,%rax,8), %xmm0
movapd %xmm0, %xmm1
divsd %xmm3, %xmm1
ucomisd %xmm2, %xmm1
jae 0xae39
incq %rax
jmp 0xae19
movl %r15d, %eax
cmpl %edi, %eax
cmovbl %eax, %edi
xorl %ecx, %ecx
pushq $-0x1
popq %rax
movsd 0x137cd(%rip), %xmm0    # 0x1e618
xorl %edx, %edx
cmpq %rcx, %r15
je 0xae71
xorl %esi, %esi
ucomisd (%rbp,%rcx,8), %xmm0
jbe 0xae65
cmpl $0x9, %edx
jge 0xae6c
incl %edx
movl %edx, %esi
incq %rcx
movl %esi, %edx
jmp 0xae4d
addl $-0x9, %ecx
movl %ecx, %eax
cmpl %eax, %edi
cmovbl %edi, %eax
cmpl $0x64, %eax
pushq $0x64
popq %rbx
cmovbl %eax, %ebx
xorl %eax, %eax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
cmpq %rbx, %rax
ja 0xaea2
ucomisd (%rbp,%rax,8), %xmm0
jbe 0xae9d
movq %rcx, (%rbp,%rax,8)
incq %rax
jmp 0xae8b
pushq $0x28
popq %rdi
callq 0x2330
movq %rax, %r14
movl %r15d, (%rax)
movl %ebx, 0x20(%rax)
movq %r12, 0x8(%rax)
movq 0x8(%rsp), %rax
movq %rax, 0x18(%r14)
movq %rbp, 0x10(%r14)
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1d11b(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x1385c(%rip), %rsi     # 0x1e743
leaq 0x13895(%rip), %rdx     # 0x1e783
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x22c0
jmp 0xaec4
movq 0x1d0f7(%rip), %rax     # 0x27ff8
movq (%rax), %rdi
leaq 0x13883(%rip), %rsi     # 0x1e78e
leaq 0x13871(%rip), %rdx     # 0x1e783
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x22c0
movq %r12, %rdi
callq 0x2060
jmp 0xaec4