// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movl %esi, 0x4(%rsp)
movq %rdi, 0x18(%rsp)
testq %rdi, %rdi
je 0x1e584
movq %rdx, %rbx
movl (%rdx), %r14d
leaq (,%r14,8), %rdi
callq 0x2330
movq %r14, 0x38(%rsp)
testq %r14, %r14
je 0x1e586
movl 0x4(%rsp), %r15d
cvtsi2sd %r15, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ecx, %ecx
movq %rbx, 0x8(%rsp)
movq %rax, 0x30(%rsp)
movq %r15, 0x20(%rsp)
movq 0x8(%rbx), %rax
movq %rcx, 0x50(%rsp)
imulq $0x58, %rcx, %rcx
movq 0x8(%rax,%rcx), %rbx
movl 0x18(%rax,%rcx), %r14d
movl 0x1c(%rax,%rcx), %eax
movq %rax, 0x68(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
callq 0x4b01
movl %eax, %r13d
movq %r13, %rdi
movl 0x4(%rsp), %esi
callq 0x4b01
movq %rax, 0x40(%rsp)
leal (%rax,%rax), %edi
movl $0x8, %esi
movq %rdi, 0x48(%rsp)
callq 0x2290
movsd 0x439b(%rip), %xmm1     # 0x22660
movq %rax, %rcx
movq %r14, 0x70(%rsp)
testq %r14, %r14
je 0x1e469
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x58(%rsp)
xorl %r8d, %r8d
movq %rbx, 0x28(%rsp)
movq 0x68(%rsp), %rax
addl %r8d, %eax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq 0x60(%rsp), %rdx
movq 0x8(%rdx,%rax), %r9
movl 0x18(%rdx,%rax), %ebp
movl 0x1c(%rdx,%rax), %r12d
movl 0x20(%rdx,%rax), %edx
movl %ebp, %eax
shrl %eax
shlq $0x4, %rax
movq 0x58(%rsp), %rsi
movl 0x4(%rsi,%rax), %r10d
movq 0x8(%rsi,%rax), %rax
leal (%rdx,%r12), %r11d
testl %r10d, %r10d
je 0x1e37d
xorl %edi, %edi
movl %r10d, %r14d
movl %edi, %r15d
shrl %r15d
movl %r14d, %esi
shrl %esi
leal (%rsi,%r15), %ebx
cmpl %r12d, (%rax,%rbx,4)
jb 0x1e360
movl %ebx, %r14d
jmp 0x1e366
leal (%rsi,%r15), %edi
incl %edi
cmpl %r14d, %edi
jne 0x1e346
testb $0x1, %bpl
jne 0x1e3ef
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rbx
jmp 0x1e38a
xorl %r14d, %r14d
testb $0x1, %bpl
jne 0x1e45b
subl %r14d, %r10d
jbe 0x1e45b
subq %r12, %r9
movl %r14d, %edx
leaq (%rax,%rdx,4), %r14
xorl %r12d, %r12d
movl (%r14,%r12,4), %eax
cmpl %r11d, %eax
jae 0x1e45b
addq %r9, %rax
cmpq %r13, %rax
jae 0x1e3c3
xorl %edx, %edx
divq %r15
shlq $0x4, %rax
addq %rcx, %rax
jmp 0x1e3d9
notq %rax
addq %rbx, %rax
xorl %edx, %edx
divq %r15
shlq $0x4, %rax
addq %rcx, %rax
addq $0x8, %rax
movsd (%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
incq %r12
cmpl %r12d, %r10d
jne 0x1e3a0
jmp 0x1e45b
subl %r14d, %r10d
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rbx
jbe 0x1e45b
decl %edx
addq %rdx, %r9
movl %r14d, %edx
leaq (%rax,%rdx,4), %r14
xorl %ebp, %ebp
movl (%r14,%rbp,4), %edx
cmpl %r11d, %edx
jae 0x1e45b
subl %r12d, %edx
movq %r9, %rax
subq %rdx, %rax
cmpq %r13, %rax
jae 0x1e431
xorl %edx, %edx
divq %r15
shlq $0x4, %rax
addq %rcx, %rax
jmp 0x1e447
notq %rax
addq %rbx, %rax
xorl %edx, %edx
divq %r15
shlq $0x4, %rax
addq %rcx, %rax
addq $0x8, %rax
movsd (%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
incq %rbp
cmpl %ebp, %r10d
jne 0x1e40c
incq %r8
cmpq 0x70(%rsp), %r8
jne 0x1e2fa
movq 0x40(%rsp), %rsi
leal -0x2(,%rsi,2), %edx
movq 0x18(%rsp), %rax
movsd 0x8(%rax), %xmm0
movl %edx, %eax
testl %edx, %edx
movapd 0x5d95(%rip), %xmm3     # 0x24220
movq 0x48(%rsp), %rdi
je 0x1e4c3
movapd %xmm0, %xmm1
mulsd 0x10(%rsp), %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
xorl %edx, %edx
movq 0x8(%rsp), %rbx
movupd (%rcx,%rdx,8), %xmm2
divpd %xmm1, %xmm2
movupd %xmm2, (%rcx,%rdx,8)
addq $0x2, %rdx
cmpq %rdx, %rax
jne 0x1e4a7
movq %rax, %rdx
jmp 0x1e4ca
xorl %edx, %edx
movq 0x8(%rsp), %rbx
xorps %xmm1, %xmm1
cvtsi2sd %r13, %xmm1
decl %esi
movl %esi, %esi
xorps %xmm2, %xmm2
cvtsi2sd %rsi, %xmm2
mulsd 0x10(%rsp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
movsd (%rcx,%rdx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movq %rax, %rdx
orq $0x1, %rdx
movsd (%rcx,%rdx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rcx,%rax,8)
testq %rdi, %rdi
movapd 0x5cf5(%rip), %xmm2     # 0x24210
je 0x1e563
xorl %eax, %eax
movupd (%rcx,%rax,8), %xmm1
movapd %xmm3, %xmm0
cmpltpd %xmm1, %xmm0
cmpltpd %xmm2, %xmm1
orpd %xmm1, %xmm0
movd %xmm0, %edx
testb $0x1, %dl
je 0x1e547
movq $0x0, (%rcx,%rax,8)
pextrw $0x4, %xmm0, %edx
testb $0x1, %dl
je 0x1e55a
movq $0x0, 0x8(%rcx,%rax,8)
addq $0x2, %rax
cmpq %rax, %rdi
jne 0x1e51f
movq 0x30(%rsp), %rax
movq 0x50(%rsp), %rdx
movq %rcx, (%rax,%rdx,8)
movq %rdx, %rcx
incq %rcx
cmpq 0x38(%rsp), %rcx
jne 0x1e26a
jmp 0x1e586
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)