// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]yahs/enzyme.c
// Function: calc_re_cuts_density2
double **calc_re_cuts_density2(re_cuts_t *re_cuts, uint32_t resolution, asm_dict_t *dict)
{
    if (!re_cuts)
        return 0;

    uint32_t i, j, b, n, e, a;
    uint64_t l, p;
    double **dens, *ds;
    re_t re;
    sd_aseq_t seq;
    sd_seg_t seg;
    n = dict->n;
    dens = (double **) malloc(n * sizeof(double *));
    for (i = 0; i < n; ++i) {
        seq = dict->s[i];
        l = div_ceil(seq.len, 2); // split sequence into two parts
        b = div_ceil(l, resolution);
        ds = (double *) calloc(b << 1, sizeof(double));
        for (j = 0; j < seq.n; ++j) {
            seg = dict->seg[seq.s + j];
            re = re_cuts->re[seg.c >> 1]; // get seq re cuts
            e = seg.x + seg.y; // seq end position, exclusive
            // binary search to get starting re cuts
            // first cutting site no smaller than seg.x
            // not many seq breaks - linear search might be faster on average?
            a = bin_search(re.sites, re.n, seg.x);
            if (seg.c & 1) {
                // reverse complement
                while (a < re.n && re.sites[a] < e) {
                    p = seg.a + (seg.y - 1) - (re.sites[a] - seg.x); // position on seq
                    if (p < l)
                        ds[p / resolution << 1] += 1.;
                    else
                        ds[(seq.len - p - 1) / resolution << 1 | 1] += 1.;
                    ++a;
                }
            } else {
                while (a < re.n && re.sites[a] < e) {
                    p = seg.a + re.sites[a] - seg.x; // position on seq
                    if (p < l)
                        ds[p / resolution << 1] += 1.;
                    else
                        ds[(seq.len - p - 1) / resolution << 1 | 1] += 1.;
                    ++a;
                }
            }
        }
        for (j = 0; j < (b - 1) << 1; ++j)
            ds[j] /= (double) resolution * re_cuts->density;
        ds[(b - 1) << 1] /= ((double) l - (double) (b - 1) * resolution) * re_cuts->density;
        ds[(b - 1) << 1 | 1] /= ((double) l - (double) (b - 1) * resolution) * re_cuts->density;
        
        for (j = 0; j < b << 1; ++j)
            if (ds[j] < MIN_RE_DENS || ds[j] > MAX_RE_DENS)
                 ds[j] = .0;

        dens[i] = ds;
#ifdef DEBUG_ENZ
        fprintf(stderr, "[DEBUG_ENZ::%s] DENS2 [%u/%u] (%u):", __func__, i, n, b);
        for (j = 0; j < b << 1; ++j)
            fprintf(stderr, " %.6f", ds[j]);
        fprintf(stderr, "\n");
#endif

    }

    return dens;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %esi, 0x4(%rsp)
movq %rdi, 0x8(%rsp)
testq %rdi, %rdi
je 0x1d74f
movq %rdx, %r14
movl (%rdx), %ebx
leaq (,%rbx,8), %rdi
callq 0x2330
movq %rbx, 0x30(%rsp)
testq %rbx, %rbx
je 0x1d751
movl 0x4(%rsp), %ebx
cvtsi2sd %rbx, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ecx, %ecx
movq %r14, 0x18(%rsp)
movq %rax, 0x28(%rsp)
movq 0x8(%r14), %rax
movq %rcx, 0x48(%rsp)
imulq $0x58, %rcx, %rcx
movq 0x8(%rax,%rcx), %r15
movl 0x18(%rax,%rcx), %r14d
movl 0x1c(%rax,%rcx), %eax
movq %rax, 0x58(%rsp)
movq %r15, %rdi
movl $0x2, %esi
callq 0x4bf1
movl %eax, %r13d
movq %r13, %rdi
movl 0x4(%rsp), %esi
callq 0x4bf1
movq %rax, 0x38(%rsp)
leal (%rax,%rax), %edi
movl $0x8, %esi
movq %rdi, 0x40(%rsp)
callq 0x2290
movsd 0x4155(%rip), %xmm1     # 0x21630
movq %rax, %rcx
movq %r14, 0x60(%rsp)
testq %r14, %r14
je 0x1d665
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rdi
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x50(%rsp)
xorl %r8d, %r8d
movq %r15, 0x20(%rsp)
movq 0x58(%rsp), %rax
addl %r8d, %eax
leaq (%rax,%rax,2), %r11
shlq $0x4, %r11
movl 0x18(%rdi,%r11), %ebp
movq %rdi, %rsi
movl 0x1c(%rdi,%r11), %r14d
movl %ebp, %eax
shrl %eax
shlq $0x4, %rax
movq 0x50(%rsp), %rdx
movl 0x4(%rdx,%rax), %r9d
movq 0x8(%rdx,%rax), %rax
xorl %edx, %edx
testl %r9d, %r9d
je 0x1d56e
movl %r9d, %r10d
movl %edx, %r12d
shrl %r12d
movl %r10d, %edi
shrl %edi
leal (%rdi,%r12), %r15d
cmpl %r14d, (%rax,%r15,4)
jb 0x1d563
movl %r15d, %r10d
jmp 0x1d569
leal (%rdi,%r12), %edx
incl %edx
cmpl %r10d, %edx
jne 0x1d549
movq %rsi, %rdi
movq 0x8(%rsi,%r11), %r10
movl 0x20(%rsi,%r11), %r12d
leal (%r12,%r14), %r11d
testb $0x1, %bpl
jne 0x1d5ee
subl %edx, %r9d
movq 0x20(%rsp), %r15
jbe 0x1d657
subq %r14, %r10
movl %edx, %edx
leaq (%rax,%rdx,4), %r14
xorl %r12d, %r12d
movl (%r14,%r12,4), %eax
cmpl %r11d, %eax
jae 0x1d657
addq %r10, %rax
cmpq %r13, %rax
jae 0x1d5c2
xorl %edx, %edx
divq %rbx
shlq $0x4, %rax
addq %rcx, %rax
jmp 0x1d5d8
notq %rax
addq %r15, %rax
xorl %edx, %edx
divq %rbx
shlq $0x4, %rax
addq %rcx, %rax
addq $0x8, %rax
movsd (%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
incq %r12
cmpl %r12d, %r9d
jne 0x1d59f
jmp 0x1d657
subl %edx, %r9d
movq 0x20(%rsp), %r15
jbe 0x1d657
decl %r12d
addq %r12, %r10
movl %edx, %edx
leaq (%rax,%rdx,4), %rbp
xorl %r12d, %r12d
movl (%rbp,%r12,4), %edx
cmpl %r11d, %edx
jae 0x1d657
subl %r14d, %edx
movq %r10, %rax
subq %rdx, %rax
cmpq %r13, %rax
jae 0x1d62d
xorl %edx, %edx
divq %rbx
shlq $0x4, %rax
addq %rcx, %rax
jmp 0x1d643
notq %rax
addq %r15, %rax
xorl %edx, %edx
divq %rbx
shlq $0x4, %rax
addq %rcx, %rax
addq $0x8, %rax
movsd (%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
incq %r12
cmpl %r12d, %r9d
jne 0x1d607
incq %r8
cmpq 0x60(%rsp), %r8
jne 0x1d50b
movq 0x38(%rsp), %rsi
leal -0x2(,%rsi,2), %edx
movl %edx, %eax
testl %edx, %edx
je 0x1d69f
movq 0x8(%rsp), %rdx
movsd 0x8(%rdx), %xmm0
mulsd 0x10(%rsp), %xmm0
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x1d689
decl %esi
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
movl %esi, %edx
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd 0x10(%rsp), %xmm1
subsd %xmm1, %xmm0
movq 0x8(%rsp), %rdx
mulsd 0x8(%rdx), %xmm0
movsd (%rcx,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rcx,%rax,8)
movq %rax, %rdx
orq $0x1, %rdx
movsd (%rcx,%rdx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rcx,%rax,8)
movq 0x40(%rsp), %rax
testl %eax, %eax
movq 0x18(%rsp), %r14
movsd 0x3f39(%rip), %xmm1     # 0x21638
movsd 0x59e9(%rip), %xmm2     # 0x230f0
je 0x1d72e
movl %eax, %eax
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm0
ucomisd %xmm0, %xmm1
ja 0x1d71e
ucomisd %xmm2, %xmm0
jbe 0x1d726
movq $0x0, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x1d70d
movq 0x28(%rsp), %rax
movq 0x48(%rsp), %rdx
movq %rcx, (%rax,%rdx,8)
movq %rdx, %rcx
incq %rcx
cmpq 0x30(%rsp), %rcx
jne 0x1d480
jmp 0x1d751
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq