// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %rdi, 0x10(%rsp)
movl (%rsi), %r13d
movl %r8d, 0xc(%rsp)
movq %rdx, %rdi
movl %ecx, %esi
testl %r8d, %r8d
je 0x9e9e
movq %r14, %rdx
callq 0x1946f
jmp 0x9ea3
callq 0x19316
movq %rax, %rbx
movl %r12d, %eax
movq %rax, 0x18(%rsp)
cvtsi2sd %rax, %xmm0
xorl %ecx, %ecx
movq 0x142f3(%rip), %xmm1    # 0x1e1b0
movapd 0x142fb(%rip), %xmm2    # 0x1e1c0
movsd 0x1474b(%rip), %xmm3    # 0x1e618
movsd 0x1430b(%rip), %xmm4    # 0x1e1e0
movsd 0x14743(%rip), %xmm5    # 0x1e620
movsd 0x14743(%rip), %xmm6    # 0x1e628
movsd 0x14743(%rip), %xmm7    # 0x1e630
movq %r14, 0x20(%rsp)
cmpq %r13, %rcx
je 0xa05e
movq 0x8(%r14), %rdx
cmpl $0x0, 0xc(%rsp)
je 0x9f11
imulq $0x58, %rcx, %rsi
movq 0x8(%rdx,%rsi), %rsi
jmp 0x9f19
imulq $0x18, %rcx, %rsi
movl 0x10(%rdx,%rsi), %esi
cmpq 0x18(%rsp), %rsi
jae 0x9f2a
incq %rcx
movq 0x20(%rsp), %r14
jmp 0x9ef2
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
movq %rcx, %r8
shlq $0x4, %r8
movl 0x4(%rdi,%r8), %edx
movq %rsi, %xmm9
punpckldq %xmm1, %xmm9    # xmm9 = xmm9[0],xmm1[0],xmm9[1],xmm1[1]
subpd %xmm2, %xmm9
movapd %xmm9, %xmm8
unpckhpd %xmm9, %xmm8            # xmm8 = xmm8[1],xmm9[1]
addsd %xmm9, %xmm8
leal -0x1(%rdx), %esi
cvtsi2sd %rsi, %xmm9
addq %r8, %rdi
mulsd %xmm0, %xmm9
subsd %xmm9, %xmm8
divsd %xmm0, %xmm8
leal -0x1(,%rdx,2), %r15d
xorl %r9d, %r9d
xorl %r10d, %r10d
xorl %r11d, %r11d
movq %rdx, %r12
cmpq %rdx, %r11
cmovaq %r11, %r12
je 0x9f20
movapd %xmm3, %xmm9
cmpq %rsi, %r11
jb 0x9fa3
movapd %xmm8, %xmm9
addq %r10, %r12
movl %r15d, %r8d
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0xa04f
leaq (%r11,%rbp), %r14
movapd %xmm3, %xmm10
cmpq %rsi, %r14
jb 0x9fc7
movapd %xmm8, %xmm10
mulsd %xmm9, %xmm10
movapd %xmm10, %xmm11
cmpltsd %xmm4, %xmm11
movapd %xmm3, %xmm12
testq %rbx, %rbx
je 0x9ff4
movq (%rbx,%rcx,8), %r14
movsd (%r14,%r11,8), %xmm12
addq %r9, %r14
mulsd (%r14,%rbp,8), %xmm12
movl %r8d, %eax
imulq %rbp, %rax
shlq $0x2, %rax
andq $-0x8, %rax
andnpd %xmm10, %xmm11
movapd %xmm12, %xmm10
cmpltsd %xmm5, %xmm10
andnpd %xmm12, %xmm10
mulsd %xmm11, %xmm10
movq 0x8(%rdi), %r14
addq %r9, %r14
addq %rax, %r14
ucomisd %xmm10, %xmm6
movapd %xmm7, %xmm11
ja 0xa03e
movsd (%r14,%rbp,8), %xmm11
divsd %xmm10, %xmm11
movsd %xmm11, (%r14,%rbp,8)
incq %rbp
decl %r8d
jmp 0x9fab
incq %r11
decq %r10
addq $0x8, %r9
jmp 0x9f88
testq %rbx, %rbx
je 0xa08f
xorl %r14d, %r14d
cmpq %r14, %r13
je 0xa079
movq (%rbx,%r14,8), %rdi
callq 0x2060
incq %r14
jmp 0xa066
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