// File: /workspace/github_c_makefile_star2_O3/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
movq %rdi, 0x18(%rsp)
movl (%rdi), %r14d
testq %r14, %r14
je 0xedfa
movl %esi, %ebp
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq %r14, %rcx
shlq $0x4, %rcx
xorl %edx, %edx
xorl %r15d, %r15d
movl 0x4(%rax,%rdx), %esi
cmpl %r15d, %esi
cmoval %esi, %r15d
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0xead0
cmpl $0x1, %r15d
jbe 0xedfa
movsd %xmm0, 0x40(%rsp)
decl %r15d
movl $0x4, %esi
movq %r15, %rdi
callq 0x2290
movq %rax, %r12
testq %r14, %r14
je 0xeb42
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
xorl %ecx, %ecx
movq %rcx, %rdx
shlq $0x4, %rdx
movl 0x4(%rax,%rdx), %edx
testl %edx, %edx
je 0xeb3a
decl %edx
je 0xeb3a
movl %edx, %edx
movq %r12, %rsi
addl %edx, (%rsi)
addq $0x4, %rsi
decq %rdx
jne 0xeb2f
incq %rcx
cmpq %r14, %rcx
jne 0xeb17
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
incl %ecx
cmpl %ebp, (%r12,%rcx,4)
jae 0xeb47
cmpl $0x9, %ecx
jbe 0xee04
movq %rcx, 0x48(%rsp)
leaq (,%r15,8), %r13
movq %r13, %rdi
callq 0x2330
movq %rax, %r14
movq %r13, %rdi
callq 0x2330
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %ebx, %ebx
movq %r15, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %r14, 0x50(%rsp)
movq %rbx, 0x8(%rsp)
movl (%r12,%rbx,4), %eax
movq %rax, 0x58(%rsp)
leaq (,%rax,8), %rdi
callq 0x2330
movq %rax, %rcx
movq 0x18(%rsp), %rax
movl (%rax), %edx
testq %rdx, %rdx
movq %rcx, 0x28(%rsp)
je 0xec4e
movq 0x18(%rsp), %rax
movq 0x8(%rax), %r12
movq 0x8(%rsp), %r14
leaq 0x1(%r14), %r15
notl %r14d
movq %r14, 0x60(%rsp)
shlq $0x4, %rdx
xorl %r13d, %r13d
xorl %ebp, %ebp
movl 0x4(%r12,%r13), %ebx
cmpq %rbx, %r15
jae 0xec45
movl %ebp, %eax
movq 0x60(%rsp), %r8
leal (%r8,%rbx,2), %ecx
movq %rdx, %r14
movq 0x8(%rsp), %rdx
imulq %rdx, %rcx
andq $-0x2, %rcx
shlq $0x2, %rcx
addq 0x8(%r12,%r13), %rcx
movq 0x28(%rsp), %rsi
leaq (%rsi,%rax,8), %rdi
leaq (%rcx,%rdx,8), %rsi
addl %r8d, %ebx
leaq (,%rbx,8), %rdx
callq 0x22e0
movq %r14, %rdx
movq 0x28(%rsp), %rcx
addl %ebp, %ebx
movl %ebx, %ebp
addq $0x10, %r13
cmpq %r13, %rdx
jne 0xebee
movq 0x58(%rsp), %r14
xorpd %xmm0, %xmm0
testq %r14, %r14
je 0xec77
xorl %eax, %eax
movq 0x38(%rsp), %r15
movq 0x8(%rsp), %rbx
addsd (%rcx,%rax,8), %xmm0
incq %rax
cmpq %rax, %r14
jne 0xec68
jmp 0xec81
movq 0x38(%rsp), %r15
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %rax
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, (%rax,%rbx,8)
movl $0x8, %edx
movq 0x28(%rsp), %rdi
movq %r14, %rsi
leaq -0x22f(%rip), %rcx      # 0xea76
callq 0x20e0
movq 0x28(%rsp), %rdi
movl %r14d, %eax
shrl %eax
movsd (%rdi,%rax,8), %xmm0
testb $0x1, %r14b
movq 0x50(%rsp), %r14
jne 0xecd3
decl %eax
addsd (%rdi,%rax,8), %xmm0
mulsd 0x13545(%rip), %xmm0    # 0x22218
movq 0x30(%rsp), %r12
movsd 0x20(%rsp), %xmm1
addsd 0x8(%rsp), %xmm1
movsd %xmm1, 0x20(%rsp)
movsd %xmm0, (%r14,%rbx,8)
callq 0x2060
incq %rbx
cmpq %r15, %rbx
jne 0xeb98
movl $0x1, %eax
cmpl $0x2, %r15d
movq 0x48(%rsp), %rsi
jb 0xed52
movq 0x10(%rsp), %rax
movsd 0x20(%rsp), %xmm3
subsd (%rax), %xmm3
xorpd %xmm0, %xmm0
movl $0x1, %eax
movsd 0x40(%rsp), %xmm2
movq 0x10(%rsp), %rcx
addsd (%rcx,%rax,8), %xmm0
movapd %xmm0, %xmm1
divsd %xmm3, %xmm1
ucomisd %xmm2, %xmm1
jae 0xed52
incq %rax
cmpq %rax, %r15
jne 0xed34
movl %r15d, %eax
cmpl %esi, %eax
cmovbl %eax, %esi
xorl %eax, %eax
movsd 0x138ff(%rip), %xmm0    # 0x22660
xorl %ecx, %ecx
ucomisd (%r14,%rax,8), %xmm0
movl $0x0, %edx
jbe 0xed79
cmpl $0x9, %ecx
jge 0xed8a
incl %ecx
movl %ecx, %edx
incq %rax
movl %edx, %ecx
cmpq %rax, %r15
jne 0xed63
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0xed8d
addl $-0x9, %eax
cmpl %eax, %esi
cmovbl %esi, %eax
cmpl $0x64, %eax
movl $0x64, %ebx
cmovbl %eax, %ebx
movq $-0x1, %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
ucomisd 0x8(%r14,%rax,8), %xmm0
jbe 0xedbc
movq %rcx, 0x8(%r14,%rax,8)
incq %rax
cmpq %rbx, %rax
jb 0xedae
movl $0x28, %edi
callq 0x2330
movq %rax, %r13
movl %r15d, (%rax)
movl %ebx, 0x20(%rax)
movq %r12, 0x8(%rax)
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 0xede8
movq 0x1d1ed(%rip), %rax     # 0x2bff8
movq (%rax), %rdi
leaq 0x139c1(%rip), %rsi     # 0x227d6
leaq 0x139af(%rip), %rdx     # 0x227cb
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x22c0
movq %r12, %rdi
callq 0x2060
jmp 0xede8