// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]yahs/enzyme.c
// Function: calc_re_cuts_density1
double **calc_re_cuts_density1(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;
    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];
        b = div_ceil(seq.len, resolution);
        ds = (double *) calloc(b, 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) {
                    ds[(seg.a + (seg.y - 1) - (re.sites[a] - seg.x)) / resolution] += 1.;
                    ++a;
                }
            } else {
                while (a < re.n && re.sites[a] < e) {
                    ds[(seg.a + re.sites[a] - seg.x) / resolution] += 1.;
                    ++a;
                }
            }
        }
        for (j = 0; j < b - 1; ++j)
            ds[j] /= (double) resolution * re_cuts->density;
        ds[b - 1] /= ((double) seq.len - (double) (b - 1) * resolution) * re_cuts->density;
        
        for (j = 0; j < b; ++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] DENS1 [%u/%u] (%u):", __func__, i, n, b);
        for (j = 0; j < b; ++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, 0xc(%rsp)
movq %rdi, 0x10(%rsp)
testq %rdi, %rdi
je 0x1d414
movq %rdx, %rbx
movl (%rdx), %r14d
leaq (,%r14,8), %rdi
callq 0x2330
movq %r14, 0x38(%rsp)
testq %r14, %r14
je 0x1d416
movl 0xc(%rsp), %r15d
cvtsi2sd %r15, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %ecx, %ecx
movq %rbx, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movq 0x8(%rbx), %rax
movq %rcx, 0x50(%rsp)
imulq $0x58, %rcx, %rcx
movq 0x8(%rax,%rcx), %rdi
movl 0x18(%rax,%rcx), %ebx
movl 0x1c(%rax,%rcx), %eax
movq %rax, 0x58(%rsp)
movq %rdi, 0x40(%rsp)
movl 0xc(%rsp), %esi
callq 0x4bf1
movl %eax, 0x1c(%rsp)
movl %eax, %edi
movl $0x8, %esi
movq %rdi, 0x48(%rsp)
callq 0x2290
movsd 0x4419(%rip), %xmm1     # 0x21630
movq %rax, %rcx
movq %rbx, 0x60(%rsp)
testq %rbx, %rbx
je 0x1d333
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rsi
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdi
xorl %r8d, %r8d
movq 0x58(%rsp), %rax
addl %r8d, %eax
leaq (%rax,%rax,2), %r13
shlq $0x4, %r13
movl 0x18(%rsi,%r13), %ebx
movl 0x1c(%rsi,%r13), %r10d
movl %ebx, %eax
shrl %eax
shlq $0x4, %rax
movl 0x4(%rdi,%rax), %r9d
movq 0x8(%rdi,%rax), %rax
xorl %edx, %edx
testl %r9d, %r9d
je 0x1d298
movl %r9d, %r11d
movl %edx, %r12d
shrl %r12d
movl %r11d, %ebp
shrl %ebp
leal (%r12,%rbp), %r14d
cmpl %r10d, (%rax,%r14,4)
jb 0x1d28d
movl %r14d, %r11d
jmp 0x1d293
leal (%r12,%rbp), %edx
incl %edx
cmpl %r11d, %edx
jne 0x1d273
movq 0x8(%rsi,%r13), %r11
movl 0x20(%rsi,%r13), %r12d
leal (%r12,%r10), %r13d
testb $0x1, %bl
jne 0x1d2e4
subl %edx, %r9d
jbe 0x1d325
subq %r10, %r11
movl %edx, %edx
leaq (%rax,%rdx,4), %r10
xorl %ebx, %ebx
movl (%r10,%rbx,4), %eax
cmpl %r13d, %eax
jae 0x1d325
addq %r11, %rax
xorl %edx, %edx
divq %r15
movsd (%rcx,%rax,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %rbx
cmpl %ebx, %r9d
jne 0x1d2bb
jmp 0x1d325
subl %edx, %r9d
jbe 0x1d325
decl %r12d
addq %r12, %r11
movl %edx, %edx
leaq (%rax,%rdx,4), %rbx
xorl %r12d, %r12d
movl (%rbx,%r12,4), %edx
cmpl %r13d, %edx
jae 0x1d325
subl %r10d, %edx
movq %r11, %rax
subq %rdx, %rax
xorl %edx, %edx
divq %r15
movsd (%rcx,%rax,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %r12
cmpl %r12d, %r9d
jne 0x1d2f8
incq %r8
cmpq 0x60(%rsp), %r8
jne 0x1d23d
movl 0x1c(%rsp), %esi
movl %esi, %eax
decl %eax
movl %eax, %eax
je 0x1d367
movq 0x10(%rsp), %rdx
movsd 0x8(%rdx), %xmm0
mulsd 0x20(%rsp), %xmm0
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x1d351
movsd 0x40(%rsp), %xmm0
unpcklps 0x3e4c(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x3e54(%rip), %xmm0     # 0x211d0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x20(%rsp), %xmm0
subsd %xmm0, %xmm1
movq 0x10(%rsp), %rdx
mulsd 0x8(%rdx), %xmm1
movsd (%rcx,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rax,8)
testl %esi, %esi
movq 0x28(%rsp), %rbx
movsd 0x4277(%rip), %xmm1     # 0x21638
movsd 0x5d27(%rip), %xmm2     # 0x230f0
movq 0x48(%rsp), %rdx
je 0x1d3f3
xorl %eax, %eax
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
ja 0x1d3e3
ucomisd %xmm2, %xmm0
jbe 0x1d3eb
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %rdx
jne 0x1d3d2
movq 0x30(%rsp), %rax
movq 0x50(%rsp), %rdx
movq %rcx, (%rax,%rdx,8)
movq %rdx, %rcx
incq %rcx
cmpq 0x38(%rsp), %rcx
jne 0x1d1cd
jmp 0x1d416
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq