// File: /workspace/github_c_makefile_star2_O3/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, 0x4(%rsp)
movq %rdi, 0x18(%rsp)
testq %rdi, %rdi
je 0x1e1f7
movq %rdx, %rbx
movl (%rdx), %r14d
leaq (,%r14,8), %rdi
callq 0x2330
movq %r14, 0x30(%rsp)
testq %r14, %r14
je 0x1e1f9
movl 0x4(%rsp), %ebp
cvtsi2sd %rbp, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ecx, %ecx
movq %rbx, 0x8(%rsp)
movq %rax, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
movq 0x8(%rbx), %rax
movq %rcx, 0x48(%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, 0x38(%rsp)
movl 0x4(%rsp), %esi
callq 0x4b01
movl %eax, %edi
movl $0x8, %esi
movq %rdi, 0x40(%rsp)
callq 0x2290
movsd 0x4680(%rip), %xmm1     # 0x22660
movq %rax, %rcx
movq %rbx, 0x60(%rsp)
testq %rbx, %rbx
je 0x1e10f
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rsi
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x50(%rsp)
xorl %r8d, %r8d
movq 0x58(%rsp), %rax
addl %r8d, %eax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq 0x8(%rsi,%rax), %r9
movl 0x18(%rsi,%rax), %edx
movl 0x1c(%rsi,%rax), %r15d
movl 0x20(%rsi,%rax), %eax
movl %edx, %edi
shrl %edi
shlq $0x4, %rdi
movq 0x50(%rsp), %rbx
movl 0x4(%rbx,%rdi), %r10d
leal (%rax,%r15), %r11d
movq 0x8(%rbx,%rdi), %r13
testq %r10, %r10
je 0x1e084
xorl %r14d, %r14d
movl %r10d, %r12d
movl %r14d, %ebx
shrl %ebx
movl %r12d, %edi
shrl %edi
leal (%rdi,%rbx), %ebp
cmpl %r15d, (%r13,%rbp,4)
jb 0x1e06c
movl %ebp, %r12d
jmp 0x1e073
leal (%rdi,%rbx), %r14d
incl %r14d
cmpl %r12d, %r14d
jne 0x1e053
testb $0x1, %dl
jne 0x1e0c1
movq 0x20(%rsp), %rbp
jmp 0x1e08c
xorl %r12d, %r12d
testb $0x1, %dl
jne 0x1e101
cmpl %r10d, %r12d
jae 0x1e101
subq %r15, %r9
movl %r12d, %r14d
movl (%r13,%r14,4), %eax
cmpl %r11d, %eax
jae 0x1e101
addq %r9, %rax
xorl %edx, %edx
divq %rbp
movsd (%rcx,%rax,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %r14
cmpq %r14, %r10
jne 0x1e097
jmp 0x1e101
cmpl %r10d, %r12d
movq 0x20(%rsp), %rbp
jae 0x1e101
decl %eax
addq %rax, %r9
movl %r12d, %r12d
movl (%r13,%r12,4), %edx
cmpl %r11d, %edx
jae 0x1e101
subl %r15d, %edx
movq %r9, %rax
subq %rdx, %rax
xorl %edx, %edx
divq %rbp
movsd (%rcx,%rax,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %r12
cmpq %r12, %r10
jne 0x1e0d3
incq %r8
cmpq 0x60(%rsp), %r8
jne 0x1e00b
movq 0x40(%rsp), %rsi
movl %esi, %eax
movq 0x18(%rsp), %rdx
movsd 0x8(%rdx), %xmm0
decl %eax
movl %eax, %eax
je 0x1e152
movapd %xmm0, %xmm1
mulsd 0x10(%rsp), %xmm1
xorl %edx, %edx
movq 0x8(%rsp), %rbx
movsd (%rcx,%rdx,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x1e137
movq %rax, %rdx
jmp 0x1e159
xorl %edx, %edx
movq 0x8(%rsp), %rbx
movsd 0x38(%rsp), %xmm1
unpcklps 0x407a(%rip), %xmm1     # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x4082(%rip), %xmm1     # 0x221f0
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
xorps %xmm3, %xmm3
cvtsi2sd %rax, %xmm3
addsd %xmm1, %xmm2
mulsd 0x10(%rsp), %xmm3
subsd %xmm3, %xmm2
mulsd %xmm0, %xmm2
movsd (%rcx,%rdx,8), %xmm0
divsd %xmm2, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
testq %rsi, %rsi
movsd 0x44bf(%rip), %xmm1     # 0x22668
movsd 0x607f(%rip), %xmm2     # 0x24230
je 0x1e1d6
xorl %eax, %eax
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
ja 0x1e1c6
ucomisd %xmm2, %xmm0
jbe 0x1e1ce
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %rsi
jne 0x1e1b5
movq 0x28(%rsp), %rax
movq 0x48(%rsp), %rdx
movq %rcx, (%rax,%rdx,8)
movq %rdx, %rcx
incq %rcx
cmpq 0x30(%rsp), %rcx
jne 0x1df9a
jmp 0x1e1f9
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq