// File: /workspace/github_c_makefile_star2_O2/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:
testq %rdi, %rdi
je 0x19a23
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r12
movl (%rdx), %eax
movq %rax, 0x40(%rsp)
leaq (,%rax,8), %rdi
callq 0x2330
movl %ebp, %esi
cvtsi2sd %rsi, %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %ecx, %ecx
movq %rbx, 0x68(%rsp)
movl %ebp, 0x8(%rsp)
movq %r12, 0x60(%rsp)
movq %rax, 0x38(%rsp)
movq %rsi, 0x10(%rsp)
cmpq 0x40(%rsp), %rcx
je 0x19a11
movq 0x8(%rbx), %rax
movq %rcx, 0x58(%rsp)
imulq $0x58, %rcx, %rcx
movq 0x8(%rax,%rcx), %r15
movl 0x18(%rax,%rcx), %edx
movq %rdx, 0x78(%rsp)
movl 0x1c(%rax,%rcx), %eax
movq %rax, 0x70(%rsp)
movq %r15, %rdi
pushq $0x2
popq %rsi
callq 0x28ff
movl %eax, %r14d
movq %r14, %rdi
movl %ebp, %esi
callq 0x28ff
movq %rax, 0x48(%rsp)
leal (%rax,%rax), %edi
movq %rdi, 0x50(%rsp)
pushq $0x8
popq %rsi
callq 0x2290
movq %r14, %rdi
movq %rax, %r13
xorl %r9d, %r9d
movq %r14, 0x20(%rsp)
movq %r15, 0x18(%rsp)
cmpq 0x78(%rsp), %r9
je 0x19950
movq 0x20(%rbx), %rax
movq 0x70(%rsp), %rcx
movq %r9, 0x30(%rsp)
addl %r9d, %ecx
imulq $0x30, %rcx, %rcx
movq 0x8(%rax,%rcx), %r15
movl 0x18(%rax,%rcx), %esi
movl %esi, 0xc(%rsp)
movl 0x1c(%rax,%rcx), %r14d
movl 0x20(%rax,%rcx), %edx
movq 0x10(%r12), %rax
movl %esi, %ecx
shrl %ecx
shlq $0x4, %rcx
movl 0x4(%rax,%rcx), %ebx
movq 0x8(%rax,%rcx), %r12
movq %rdx, 0x80(%rsp)
leal (%rdx,%r14), %ebp
movq %r12, %rdi
movl %ebx, %esi
movl %r14d, %edx
callq 0x196f0
movl %eax, %ecx
testb $0x1, 0xc(%rsp)
jne 0x198c5
subq %r14, %r15
movq 0x10(%rsp), %rsi
movsd 0x4db4(%rip), %xmm1     # 0x1e618
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %r8
movq 0x30(%rsp), %r9
cmpq %rbx, %rcx
jae 0x1993e
movl (%r12,%rcx,4), %eax
cmpl %ebp, %eax
jae 0x1993e
addq %r15, %rax
cmpq %rdi, %rax
jae 0x1989e
xorl %edx, %edx
divq %rsi
shlq $0x4, %rax
addq %r13, %rax
jmp 0x198b4
notq %rax
addq %r8, %rax
xorl %edx, %edx
divq %rsi
shlq $0x4, %rax
addq %r13, %rax
addq $0x8, %rax
movsd (%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
incq %rcx
jmp 0x19873
movq 0x80(%rsp), %rax
decl %eax
addq %rax, %r15
movq 0x10(%rsp), %rsi
movsd 0x4d39(%rip), %xmm1     # 0x1e618
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %r8
movq 0x30(%rsp), %r9
cmpq %rbx, %rcx
jae 0x1993e
movl (%r12,%rcx,4), %edx
cmpl %ebp, %edx
jae 0x1993e
subl %r14d, %edx
movq %r15, %rax
subq %rdx, %rax
cmpq %rdi, %rax
jae 0x19917
xorl %edx, %edx
divq %rsi
shlq $0x4, %rax
addq %r13, %rax
jmp 0x1992d
notq %rax
addq %r8, %rax
xorl %edx, %edx
divq %rsi
shlq $0x4, %rax
addq %r13, %rax
addq $0x8, %rax
movsd (%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
incq %rcx
jmp 0x198ee
incq %r9
movq 0x68(%rsp), %rbx
movq 0x60(%rsp), %r12
jmp 0x197e6
movq 0x48(%rsp), %r8
movl %r8d, %eax
decl %r8d
leal -0x2(,%rax,2), %eax
xorl %ecx, %ecx
movq 0x50(%rsp), %rdx
cmpq %rcx, %rax
je 0x19992
movsd 0x8(%r12), %xmm0
mulsd 0x28(%rsp), %xmm0
movsd (%r13,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r13,%rcx,8)
incq %rcx
jmp 0x19969
cvtsi2sd %rdi, %xmm0
movl %r8d, %ecx
cvtsi2sd %rcx, %xmm1
mulsd 0x28(%rsp), %xmm1
subsd %xmm1, %xmm0
mulsd 0x8(%r12), %xmm0
movupd (%r13,%rax,8), %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
divpd %xmm0, %xmm1
movupd %xmm1, (%r13,%rax,8)
xorl %eax, %eax
movq 0x58(%rsp), %rcx
movsd 0x4c4b(%rip), %xmm1     # 0x1e620
cmpq %rax, %rdx
je 0x199fc
movsd (%r13,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
ja 0x199f1
ucomisd 0x66f1(%rip), %xmm0     # 0x200e0
jbe 0x199f7
andq $0x0, (%r13,%rax,8)
incq %rax
jmp 0x199d5
movq 0x38(%rsp), %rax
movq %r13, (%rax,%rcx,8)
incq %rcx
movl 0x8(%rsp), %ebp
jmp 0x19777
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
retq
nop