// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movl %ecx, %r12d
movq %rsi, %r14
movq %rdi, 0x18(%rsp)
movl (%rsi), %r15d
movl %r8d, 0xc(%rsp)
movq %rdx, %rdi
movl %ecx, %esi
testl %r8d, %r8d
je 0xce3e
movq %r14, %rdx
callq 0x1d170
jmp 0xce43
callq 0x1cff5
movq %rax, %rbx
testq %r15, %r15
je 0xd02f
movl %r12d, %eax
movq %rax, 0x20(%rsp)
cvtsi2sd %rax, %xmm0
xorl %ecx, %ecx
movq 0x1435a(%rip), %xmm1    # 0x211c0
movapd 0x14362(%rip), %xmm2    # 0x211d0
movsd 0x147ba(%rip), %xmm3    # 0x21630
movsd 0x1437a(%rip), %xmm4    # 0x211f8
movsd 0x147b2(%rip), %xmm5    # 0x21638
movsd 0x147b2(%rip), %xmm6    # 0x21640
movabsq $-0x10000000000001, %rdx # imm = 0xFFEFFFFFFFFFFFFF
movq %r15, 0x30(%rsp)
movq %r14, 0x28(%rsp)
cmpl $0x0, 0xc(%rsp)
je 0xceb8
movq 0x8(%r14), %rax
imulq $0x58, %rcx, %rsi
movq 0x8(%rax,%rsi), %rax
jmp 0xcec4
movq 0x8(%r14), %rax
leaq (%rcx,%rcx,2), %rsi
movl 0x10(%rax,%rsi,8), %eax
cmpq 0x20(%rsp), %rax
jb 0xd019
movq 0x18(%rsp), %rsi
movq 0x8(%rsi), %rsi
movq %rcx, %r8
shlq $0x4, %r8
movl 0x4(%rsi,%r8), %edi
movq %rax, %xmm8
punpckldq %xmm1, %xmm8    # xmm8 = xmm8[0],xmm1[0],xmm8[1],xmm1[1]
subpd %xmm2, %xmm8
movq %rdi, 0x10(%rsp)
movl %edi, %eax
subl $0x1, %eax
jb 0xd019
addq %r8, %rsi
movapd %xmm8, %xmm7
unpckhpd %xmm8, %xmm7            # xmm7 = xmm7[1],xmm8[1]
addsd %xmm8, %xmm7
xorps %xmm8, %xmm8
cvtsi2sd %rax, %xmm8
mulsd %xmm0, %xmm8
subsd %xmm8, %xmm7
divsd %xmm0, %xmm7
movl %eax, %r8d
movq 0x10(%rsp), %r10
leal -0x1(,%r10,2), %eax
negq %r10
xorl %r14d, %r14d
xorl %r12d, %r12d
movapd %xmm3, %xmm8
cmpq %r8, %r12
jb 0xcf54
movapd %xmm7, %xmm8
leaq (%r10,%r12), %r15
xorl %ebp, %ebp
movq %r14, %r11
movl %eax, %r9d
leaq (%r12,%rbp), %r13
movapd %xmm3, %xmm9
cmpq %r8, %r13
jb 0xcf73
movapd %xmm7, %xmm9
mulsd %xmm8, %xmm9
movapd %xmm9, %xmm10
cmpltsd %xmm4, %xmm10
movapd %xmm3, %xmm11
testq %rbx, %rbx
je 0xcfa2
movq (%rbx,%rcx,8), %r13
movsd (%r13,%r12,8), %xmm11
addq %r14, %r13
mulsd (%r13,%rbp,8), %xmm11
movl %r9d, %r13d
imulq %rbp, %r13
andq $-0x2, %r13
leaq (%r11,%r13,4), %r13
andnpd %xmm9, %xmm10
movapd %xmm11, %xmm9
cmpltsd %xmm5, %xmm9
andnpd %xmm11, %xmm9
mulsd %xmm10, %xmm9
ucomisd %xmm9, %xmm6
jbe 0xcfdc
movq 0x8(%rsi), %rdi
movq %rdx, (%rdi,%r13)
jmp 0xcff1
movq 0x8(%rsi), %rdi
movsd (%rdi,%r13), %xmm10
divsd %xmm9, %xmm10
movsd %xmm10, (%rdi,%r13)
decl %r9d
addq $0x8, %r11
incq %rbp
movq %r15, %rdi
addq %rbp, %rdi
jne 0xcf60
incq %r12
addq $0x8, %r14
cmpq 0x10(%rsp), %r12
jne 0xcf45
incq %rcx
movq 0x30(%rsp), %r15
cmpq %r15, %rcx
movq 0x28(%rsp), %r14
jne 0xcea2
testq %rbx, %rbx
je 0xd063
testq %r15, %r15
je 0xd04d
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
callq 0x2060
incq %r14
cmpq %r14, %r15
jne 0xd03c
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2060
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq