// File: /workspace/github_c_makefile_star2_O2/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:
testq %rdi, %rdi
je 0x196ed
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r15
movl (%rdx), %eax
movq %rax, 0x30(%rsp)
leaq (,%rax,8), %rdi
callq 0x2330
movl %ebp, %esi
cvtsi2sd %rsi, %xmm0
movsd %xmm0, 0x18(%rsp)
xorl %ecx, %ecx
movq %rbx, 0x58(%rsp)
movl %ebp, 0x8(%rsp)
movq %r15, 0x50(%rsp)
movq %rax, 0x28(%rsp)
movq %rsi, 0x10(%rsp)
cmpq 0x30(%rsp), %rcx
je 0x196de
movq 0x8(%rbx), %rax
movq %rcx, 0x48(%rsp)
imulq $0x58, %rcx, %rcx
movq 0x8(%rax,%rcx), %rdi
movl 0x18(%rax,%rcx), %edx
movq %rdx, 0x68(%rsp)
movl 0x1c(%rax,%rcx), %eax
movq %rax, 0x60(%rsp)
movq %rdi, 0x38(%rsp)
movl %ebp, %esi
callq 0x28ff
movl %eax, %edi
movq %rdi, 0x40(%rsp)
pushq $0x8
popq %rsi
callq 0x2290
movq %rax, %r12
xorl %edi, %edi
cmpq 0x68(%rsp), %rdi
je 0x1961f
movq 0x20(%rbx), %rax
movq 0x60(%rsp), %rcx
movq %rdi, 0x20(%rsp)
addl %edi, %ecx
imulq $0x30, %rcx, %rcx
movq 0x8(%rax,%rcx), %r14
movl 0x18(%rax,%rcx), %esi
movl %esi, 0xc(%rsp)
movl 0x1c(%rax,%rcx), %ebx
movl 0x20(%rax,%rcx), %edx
movq 0x10(%r15), %rax
movl %esi, %ecx
shrl %ecx
shlq $0x4, %rcx
movl 0x4(%rax,%rcx), %r15d
movq 0x8(%rax,%rcx), %rbp
movq %rdx, 0x70(%rsp)
leal (%rdx,%rbx), %r13d
movq %rbp, %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x196f0
movl %eax, %ecx
testb $0x1, 0xc(%rsp)
jne 0x195c1
subq %rbx, %r14
movq 0x10(%rsp), %rsi
movsd 0x5087(%rip), %xmm1     # 0x1e618
movq 0x20(%rsp), %rdi
cmpq %r15, %rcx
jae 0x1960d
movl (%rbp,%rcx,4), %eax
cmpl %r13d, %eax
jae 0x1960d
addq %r14, %rax
xorl %edx, %edx
divq %rsi
movsd (%r12,%rax,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r12,%rax,8)
incq %rcx
jmp 0x19596
movq 0x70(%rsp), %rax
decl %eax
addq %rax, %r14
movq 0x10(%rsp), %rsi
movsd 0x5040(%rip), %xmm1     # 0x1e618
movq 0x20(%rsp), %rdi
cmpq %r15, %rcx
jae 0x1960d
movl (%rbp,%rcx,4), %edx
cmpl %r13d, %edx
jae 0x1960d
subl %ebx, %edx
movq %r14, %rax
subq %rdx, %rax
xorl %edx, %edx
divq %rsi
movsd (%r12,%rax,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r12,%rax,8)
incq %rcx
jmp 0x195dd
incq %rdi
movq 0x58(%rsp), %rbx
movq 0x50(%rsp), %r15
jmp 0x19518
movq 0x40(%rsp), %rdx
leal -0x1(%rdx), %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x1964f
movsd 0x8(%r15), %xmm0
mulsd 0x18(%rsp), %xmm0
movsd (%r12,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r12,%rcx,8)
incq %rcx
jmp 0x19629
movsd 0x38(%rsp), %xmm0
unpcklps 0x4b54(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x4b5c(%rip), %xmm0     # 0x1e1c0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
cvtsi2sd %rax, %xmm0
mulsd 0x18(%rsp), %xmm0
subsd %xmm0, %xmm1
mulsd 0x8(%r15), %xmm1
movsd (%r12,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%r12,%rax,8)
xorl %eax, %eax
movq 0x48(%rsp), %rcx
movsd 0x4f7c(%rip), %xmm1     # 0x1e620
cmpq %rax, %rdx
je 0x196c9
movsd (%r12,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
ja 0x196bf
ucomisd 0x6a23(%rip), %xmm0     # 0x200e0
jbe 0x196c4
andq $0x0, (%r12,%rax,8)
incq %rax
jmp 0x196a4
movq 0x28(%rsp), %rax
movq %r12, (%rax,%rcx,8)
incq %rcx
movl 0x8(%rsp), %ebp
jmp 0x194c9
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
retq