// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]yahs/break.c
// Function: detect_break_points_local_joint
bp_t *detect_break_points_local_joint(link_mat_t *link_mat, uint32_t bin_size, double fold_thres, uint32_t flank_size, asm_dict_t *dict, uint32_t *bp_n)
{
    uint32_t i, j, b_n, b_m;
    double mcnt;
    int64_t *link;
    uint32_t s, e, t;
    int8_t a;
    bp_t *bp, *bp1;
    sd_seg_t *segs, seg;
    sd_aseq_t seq;
    
    segs = dict->seg;
    b_n = 0;
    b_m = 16;
    bp = (bp_t *) malloc(b_m * sizeof(bp_t));
    bp1 = 0;
    for (i = 0; i < link_mat->n; ++i) {
        seq = dict->s[i];
        link = link_mat->link[i].link;
        a = 0;
        for (j = 1; j < seq.n; ++j) {
            seg = segs[seq.s + j];
            s = (seg.a - MIN(flank_size, segs[seq.s + j - 1].y)) / bin_size;
            e = (seg.a + MIN(flank_size, seg.y) - 1) / bin_size;
            // s = (MAX(seg.a - MIN(flank_size, segs[seq.s + j - 1].y), 1) - 1) / bin_size;
            // e = (MAX(seg.a + MIN(flank_size, seg.y), 1) - 1) / bin_size;
            t = e - s + 1;
            qsort(link + s, t, sizeof(int64_t), cnt_cmp);
            mcnt = t & 1? (int32_t) link[(e + s) / 2] : ((int32_t) link[(e + s) / 2] + (int32_t) link[(e + s) / 2 + 1]) / 2.;
            mcnt *= fold_thres;
            qsort(link + s, t, sizeof(int64_t), pos_cmp);

            if ((int32_t) link[seg.a / bin_size] < mcnt) {
                if (!a) {
                    if (b_n == b_m) {
                        b_m <<= 1;
                        bp = (bp_t *) realloc(bp, b_m * sizeof(bp_t));
                    }
                    bp1 = bp + b_n;
                    bp1->s = i;
                    bp1->n = 0;
                    bp1->m = 4;
                    bp1->p = (uint64_t *) malloc(bp1->m * sizeof(uint64_t));
                    ++b_n;
                    a = 1;
                }
                add_break_point(bp1, seg.a);
#ifdef DEBUG_LOCAL_BREAK
                fprintf(stderr, "[DEBUG_LOCAL_BREAK::%s] break local joint: %s at %lu (link number %d < %.3f)\n", __func__,
                        seq.name, seg.a, (int32_t) link[seg.a / bin_size], mcnt);
#endif
            }
        }
    }

    *bp_n = b_n;

    return bp;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, 0x48(%rsp)
movq %rcx, %r14
movl %edx, 0x24(%rsp)
movsd %xmm0, 0x70(%rsp)
movl %esi, %ebx
movq %rdi, %r15
movq 0x20(%rcx), %rax
movq %rax, 0x68(%rsp)
movl $0x180, %edi            # imm = 0x180
callq 0x2330
movq %rax, 0x18(%rsp)
movl 0x4(%r15), %eax
testl %eax, %eax
je 0x621a
movl %ebx, %r12d
movl $0x10, %ecx
movq %rcx, 0x28(%rsp)
xorl %ecx, %ecx
movq $0x0, (%rsp)
movq $0x0, 0x30(%rsp)
movq %r14, 0x58(%rsp)
movq %r15, 0x50(%rsp)
movq %r12, 0x60(%rsp)
movq %rcx, %rsi
movq 0x8(%r14), %rcx
imulq $0x58, %rsi, %rdx
movl 0x18(%rcx,%rdx), %edi
cmpq $0x2, %rdi
movq %rsi, 0x10(%rsp)
jb 0x6202
movl 0x1c(%rcx,%rdx), %edx
movq %rsi, %rax
shlq $0x4, %rax
movq 0x8(%r15), %rcx
movq 0x8(%rcx,%rax), %r13
decq %rdi
movl $0x0, 0xc(%rsp)
movq %rdi, 0x40(%rsp)
movl %edx, %eax
movl %edx, %ecx
incl %eax
movq %rax, 0x38(%rsp)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq 0x68(%rsp), %rdx
movq 0x8(%rdx,%rax), %r8
movl 0x20(%rdx,%rax), %esi
leaq (%rcx,%rcx,2), %rax
shlq $0x4, %rax
movl 0x20(%rdx,%rax), %ecx
movl 0x24(%rsp), %edi
cmpl %edi, %ecx
cmovael %edi, %ecx
movq %r8, %rax
subq %rcx, %rax
xorl %edx, %edx
divq %r12
movq %rax, %rbx
cmpl %edi, %esi
cmovael %edi, %esi
movq %r8, 0x80(%rsp)
leaq (%r8,%rsi), %rax
decq %rax
xorl %edx, %edx
divq %r12
movq %rax, %rbp
movl %ebp, %r14d
subl %ebx, %r14d
leal 0x1(%r14), %r12d
movl %ebx, %eax
leaq (,%rax,8), %r15
addq %r13, %r15
movl $0x8, %edx
movq %r15, %rdi
movq %r12, %rsi
leaq -0x157(%rip), %rcx      # 0x5f6d
callq 0x20e0
testb $0x1, %r14b
jne 0x60df
addl %ebx, %ebp
shrl %ebp
xorps %xmm0, %xmm0
cvtsi2sdl (%r13,%rbp,8), %xmm0
jmp 0x60fd
leal (%rbx,%rbp), %eax
shrl %eax
movl 0x8(%r13,%rax,8), %ecx
addl (%r13,%rax,8), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
mulsd 0x1c11b(%rip), %xmm0    # 0x22218
mulsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movl $0x8, %edx
movq %r15, %rdi
movq %r12, %rsi
leaq -0x1c1(%rip), %rcx      # 0x5f5a
callq 0x20e0
movq 0x80(%rsp), %r15
movq %r15, %rax
xorl %edx, %edx
movq 0x60(%rsp), %r12
divq %r12
xorps %xmm0, %xmm0
cvtsi2sdl (%r13,%rax,8), %xmm0
movsd 0x78(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x61e1
cmpb $0x0, 0xc(%rsp)
je 0x615d
movq 0x30(%rsp), %rdi
jmp 0x61ce
movq (%rsp), %rax
cmpl 0x28(%rsp), %eax
movq 0x18(%rsp), %rdi
jne 0x618f
movq (%rsp), %rax
addl %eax, %eax
movq %rax, 0x28(%rsp)
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rsi
callq 0x2390
movq %rax, %rdi
movq (%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rax, %rbx
movl %ebx, %eax
leaq (%rax,%rax,2), %rax
movq %rdi, 0x18(%rsp)
leaq (%rdi,%rax,8), %r14
movl %ecx, 0x8(%r14)
movabsq $0x400000000, %rax      # imm = 0x400000000
movq %rax, (%r14)
movl $0x20, %edi
callq 0x2330
movq %rax, 0x10(%r14)
movq %r14, %rdi
incl %ebx
movq %rbx, (%rsp)
movq %rdi, 0x30(%rsp)
movq %r15, %rsi
callq 0x6244
movb $0x1, %al
movl %eax, 0xc(%rsp)
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rdx
decq %rdi
jne 0x6036
movq 0x50(%rsp), %r15
movl 0x4(%r15), %eax
movq 0x58(%rsp), %r14
movq 0x10(%rsp), %rdx
incq %rdx
movl %eax, %ecx
cmpq %rcx, %rdx
movq %rdx, %rcx
jb 0x5ff9
jmp 0x6222
movq $0x0, (%rsp)
movq 0x48(%rsp), %rax
movq (%rsp), %rcx
movl %ecx, (%rax)
movq 0x18(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq