// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/link.c
// Function: intra_link_mat_normalise
static void intra_link_mat_normalise(intra_link_mat_t *link_mat, void *dict, re_cuts_t *re_cuts, uint32_t resolution, int use_gap_seq)
{
    intra_link_t *link;
    uint32_t i, j, k, n, b;
    uint64_t len;
    double a, a0, a1, s;
    double **re_dens;

    n = use_gap_seq? ((asm_dict_t *) dict)->n : ((sdict_t *) dict)->n;

    re_dens = use_gap_seq? calc_re_cuts_density1(re_cuts, resolution, dict) : calc_re_cuts_density(re_cuts, resolution);
    
    for (i = 0; i < n; ++i) {
        len = use_gap_seq? ((asm_dict_t *) dict)->s[i].len : ((sdict_t *) dict)->s[i].len;
        if (len < resolution) continue;
        
        link = &link_mat->links[i];
        b = link->n;
        // relative size of the last cell
        a = ((double) len - (double) (b - 1) * resolution) / resolution;

        for (j = 0; j < b; ++j) {
            a0 = j < b-1? 1. : a;
            for (k = j; k < b; ++k) {
                a1 = k < b-1? 1. : a;
                a1 *= a0;
                a1 = a1 < .5? .0 : a1;
                s = re_dens? re_dens[i][j] * re_dens[i][k] : 1.;
                s = s < MIN_RE_DENS? .0 : s;
                s *= a1;
                if (s < FLT_EPSILON)
                    link->link[(long) (b * 2 - k + j - 1) * (k - j) / 2 + k] = -DBL_MAX;
                else
                    link->link[(long) (b * 2 - k + j - 1) * (k - j) / 2 + k] /= s;
            }
        }
    }
    
    if (re_dens) {
        for (i = 0; i < n; ++i)
            free(re_dens[i]);
        free(re_dens);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r12d
movq %rsi, %r15
movq %rdi, 0x18(%rsp)
movl (%rsi), %r13d
movl %r8d, 0xc(%rsp)
movq %rdx, %rdi
movl %ecx, %esi
testl %r8d, %r8d
je 0xdcd3
movq %r15, %rdx
callq 0x1df39
jmp 0xdcd8
callq 0x1ddbd
movq %rax, %rbx
testq %r13, %r13
je 0xdec2
movl %r12d, %eax
movq %rax, 0x20(%rsp)
cvtsi2sd %rax, %xmm0
movq 0x8(%r15), %rax
movq %rax, 0x10(%rsp)
xorl %edx, %edx
movq 0x144dc(%rip), %xmm1    # 0x221e0
movapd 0x144e4(%rip), %xmm2    # 0x221f0
movsd 0x1494c(%rip), %xmm3    # 0x22660
movsd 0x144fc(%rip), %xmm4    # 0x22218
movsd 0x14944(%rip), %xmm5    # 0x22668
movsd 0x14944(%rip), %xmm6    # 0x22670
movsd 0x14944(%rip), %xmm7    # 0x22678
cmpl $0x0, 0xc(%rsp)
je 0xdd4b
imulq $0x58, %rdx, %rax
movq 0x10(%rsp), %rcx
movq 0x8(%rcx,%rax), %rax
jmp 0xdd58
leaq (%rdx,%rdx,2), %rax
movq 0x10(%rsp), %rcx
movl 0x10(%rcx,%rax,8), %eax
cmpq 0x20(%rsp), %rax
jb 0xde96
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rdi
movq %rdx, %rcx
shlq $0x4, %rcx
movl 0x4(%rdi,%rcx), %esi
movq %rax, %xmm9
punpckldq %xmm1, %xmm9    # xmm9 = xmm9[0],xmm1[0],xmm9[1],xmm1[1]
subpd %xmm2, %xmm9
movl %esi, %eax
subl $0x1, %eax
jb 0xde96
addq %rcx, %rdi
movapd %xmm9, %xmm8
unpckhpd %xmm9, %xmm8            # xmm8 = xmm8[1],xmm9[1]
addsd %xmm9, %xmm8
xorps %xmm9, %xmm9
cvtsi2sd %rax, %xmm9
mulsd %xmm0, %xmm9
subsd %xmm9, %xmm8
divsd %xmm0, %xmm8
leal -0x1(%rsi,%rsi), %ecx
movl %eax, %r8d
movq 0x8(%rdi), %rbp
movq %rsi, %r10
negq %r10
xorl %r11d, %r11d
xorl %r15d, %r15d
movapd %xmm3, %xmm9
cmpq %r8, %r15
jb 0xdde1
movapd %xmm8, %xmm9
leaq (%r10,%r15), %r12
xorl %r14d, %r14d
movq %rbp, %r9
movl %ecx, %edi
leaq (%r15,%r14), %rax
movapd %xmm3, %xmm10
cmpq %r8, %rax
jb 0xde00
movapd %xmm8, %xmm10
mulsd %xmm9, %xmm10
movapd %xmm10, %xmm11
cmpltsd %xmm4, %xmm11
movapd %xmm3, %xmm12
testq %rbx, %rbx
je 0xde2d
movq (%rbx,%rdx,8), %rax
movsd (%rax,%r15,8), %xmm12
addq %r11, %rax
mulsd (%rax,%r14,8), %xmm12
movl %edi, %eax
imulq %r14, %rax
shrq %rax
andnpd %xmm10, %xmm11
movapd %xmm12, %xmm10
cmpltsd %xmm5, %xmm10
andnpd %xmm12, %xmm10
mulsd %xmm11, %xmm10
ucomisd %xmm10, %xmm7
movapd %xmm6, %xmm11
ja 0xde67
movsd (%r9,%rax,8), %xmm11
divsd %xmm10, %xmm11
movsd %xmm11, (%r9,%rax,8)
decl %edi
addq $0x8, %r9
incq %r14
movq %r12, %rax
addq %r14, %rax
jne 0xdded
incq %r15
addq $0x8, %rbp
addq $0x8, %r11
cmpq %rsi, %r15
jne 0xddd2
incq %rdx
cmpq %r13, %rdx
jne 0xdd34
testq %rbx, %rbx
je 0xdedd
testq %r13, %r13
je 0xdec7
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
callq 0x2060
incq %r14
cmpq %r14, %r13
jne 0xdeaf
jmp 0xdec7
testq %rbx, %rbx
je 0xdedd
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2060
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq