// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movq %rdi, %r12
movl (%rdi), %r14d
testq %r14, %r14
je 0xdc99
movq 0x8(%r12), %rax
movq %r14, %rcx
shlq $0x4, %rcx
xorl %edx, %edx
xorl %edi, %edi
movl 0x4(%rax,%rdx), %esi
cmpl %edi, %esi
cmoval %esi, %edi
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0xdc85
jmp 0xdc9b
xorl %edi, %edi
cmpl $0x1, %edi
jbe 0xdfe7
movsd %xmm0, 0x38(%rsp)
decl %edi
movl $0x4, %esi
movq %rdi, 0x20(%rsp)
callq 0x2290
movq %rax, %r15
testq %r14, %r14
je 0xdcf5
movq 0x8(%r12), %rax
xorl %ecx, %ecx
movq %rcx, %rdx
shlq $0x4, %rdx
movl 0x4(%rax,%rdx), %edx
testl %edx, %edx
je 0xdced
decl %edx
je 0xdced
movl %edx, %edx
movq %r15, %rsi
addl %edx, (%rsi)
addq $0x4, %rsi
decq %rdx
jne 0xdce2
incq %rcx
cmpq %r14, %rcx
jne 0xdcca
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
incl %ecx
cmpl %ebp, (%r15,%rcx,4)
jae 0xdcfa
cmpl $0x9, %ecx
movq 0x20(%rsp), %rax
jbe 0xdff1
movq %rcx, 0x40(%rsp)
leaq (,%rax,8), %r13
movq %rax, %rbx
movq %r13, %rdi
callq 0x2330
movq %rax, %r14
movq %r13, %rdi
callq 0x2330
movq %rax, 0x10(%rsp)
movl %ebx, %r8d
xorpd %xmm0, %xmm0
testl %ebx, %ebx
je 0xded0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movq %r15, 0x30(%rsp)
movq %r14, 0x50(%rsp)
movq %r12, 0x28(%rsp)
movq %r8, 0x48(%rsp)
movq %rbx, (%rsp)
movl (%r15,%rbx,4), %eax
movq %rax, 0x58(%rsp)
leaq (,%rax,8), %rdi
callq 0x2330
movq %rax, %rcx
movl (%r12), %edx
testq %rdx, %rdx
movq %rax, 0x18(%rsp)
je 0xde16
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rbx
movq (%rsp), %r14
leaq 0x1(%r14), %r15
notl %r14d
movq %r14, 0x60(%rsp)
shlq $0x4, %rdx
xorl %ebp, %ebp
xorl %r13d, %r13d
movl 0x4(%rbx,%rbp), %r12d
cmpq %r12, %r15
jae 0xde0d
movl %r13d, %eax
movq 0x60(%rsp), %r8
leal (%r8,%r12,2), %ecx
movq %rdx, %r14
movq (%rsp), %rdx
imulq %rdx, %rcx
andq $-0x2, %rcx
shlq $0x2, %rcx
addq 0x8(%rbx,%rbp), %rcx
movq 0x18(%rsp), %rsi
leaq (%rsi,%rax,8), %rdi
leaq (%rcx,%rdx,8), %rsi
addl %r8d, %r12d
leaq (,%r12,8), %rdx
callq 0x22e0
movq %r14, %rdx
movq 0x18(%rsp), %rcx
addl %r13d, %r12d
movl %r12d, %r13d
addq $0x10, %rbp
cmpq %rbp, %rdx
jne 0xddb4
movq 0x58(%rsp), %rsi
xorpd %xmm0, %xmm0
testq %rsi, %rsi
je 0xde43
xorl %eax, %eax
movq 0x30(%rsp), %r15
movq 0x28(%rsp), %r12
movq (%rsp), %rbx
addsd (%rcx,%rax,8), %xmm0
incq %rax
cmpq %rax, %rsi
jne 0xde34
jmp 0xde51
movq 0x30(%rsp), %r15
movq 0x28(%rsp), %r12
movq (%rsp), %rbx
movq 0x10(%rsp), %rax
movsd %xmm0, (%rsp)
movsd %xmm0, (%rax,%rbx,8)
movl $0x8, %edx
movq 0x18(%rsp), %rdi
leaq -0x23b(%rip), %rcx      # 0xdc36
callq 0x20e0
movq 0x18(%rsp), %rdi
movl (%r15,%rbx,4), %ecx
movl %ecx, %eax
shrl %eax
movsd (%rdi,%rax,8), %xmm0
testb $0x1, %cl
movq 0x50(%rsp), %r14
jne 0xdea1
decl %eax
addsd (%rdi,%rax,8), %xmm0
mulsd 0x13357(%rip), %xmm0    # 0x211f8
movsd 0x8(%rsp), %xmm1
addsd (%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%r14,%rbx,8)
callq 0x2060
incq %rbx
movq 0x48(%rsp), %r8
cmpq %r8, %rbx
jne 0xdd63
jmp 0xded6
movsd %xmm0, 0x8(%rsp)
movl $0x1, %eax
movq 0x20(%rsp), %rcx
cmpl $0x2, %ecx
movq 0x40(%rsp), %rdi
jb 0xdf2a
movq 0x10(%rsp), %rax
movsd 0x8(%rsp), %xmm3
subsd (%rax), %xmm3
xorpd %xmm0, %xmm0
movl $0x1, %eax
movsd 0x38(%rsp), %xmm2
movq 0x10(%rsp), %rdx
addsd (%rdx,%rax,8), %xmm0
movapd %xmm0, %xmm1
divsd %xmm3, %xmm1
ucomisd %xmm2, %xmm1
jae 0xdf2a
incq %rax
cmpq %rax, %r8
jne 0xdf0d
movl %ecx, %eax
cmpl %edi, %eax
cmovbl %eax, %edi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ecx, %ecx
je 0xdf6b
xorl %ecx, %ecx
movsd 0x136ee(%rip), %xmm0    # 0x21630
xorl %edx, %edx
ucomisd (%r14,%rcx,8), %xmm0
movl $0x0, %esi
jbe 0xdf5a
cmpl $0x9, %edx
jge 0xdf66
incl %edx
movl %edx, %esi
incq %rcx
movl %esi, %edx
cmpq %rcx, %r8
jne 0xdf44
jmp 0xdf6b
addl $-0x9, %ecx
movl %ecx, %eax
cmpl %eax, %edi
cmovbl %edi, %eax
cmpl $0x64, %eax
movl $0x64, %ebx
cmovbl %eax, %ebx
movq $-0x1, %rax
movsd 0x136a6(%rip), %xmm0    # 0x21630
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
ucomisd 0x8(%r14,%rax,8), %xmm0
jbe 0xdfa2
movq %rcx, 0x8(%r14,%rax,8)
incq %rax
cmpq %rbx, %rax
jb 0xdf94
movl $0x28, %edi
callq 0x2330
movq %rax, %r13
movq 0x20(%rsp), %rax
movl %eax, (%r13)
movl %ebx, 0x20(%r13)
movq %r15, 0x8(%r13)
movq 0x10(%rsp), %rax
movq %rax, 0x18(%r13)
movq %r14, 0x10(%r13)
movq %r13, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2940
xorl %r13d, %r13d
jmp 0xdfd5
movq 0x1d000(%rip), %rax     # 0x2aff8
movq (%rax), %rdi
leaq 0x1379c(%rip), %rsi     # 0x2179e
leaq 0x1378a(%rip), %rdx     # 0x21793
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x22c0
movq %r15, %rdi
callq 0x2060
jmp 0xdfd5