// File: /workspace/github_c_makefile_star2_O1/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, 0x50(%rsp)
movl %edx, 0x1c(%rsp)
movsd %xmm0, 0x70(%rsp)
movl %esi, %ebx
movq %rdi, %r14
movq %rcx, 0x58(%rsp)
movq 0x20(%rcx), %rax
movq %rax, 0x68(%rsp)
movl $0x180, %edi            # imm = 0x180
callq 0x2330
movq %rax, 0x10(%rsp)
movq %r14, 0x20(%rsp)
cmpl $0x0, 0x4(%r14)
je 0x628e
movl %ebx, %r14d
movl $0x10, %eax
movq %rax, 0x28(%rsp)
xorl %edx, %edx
movl $0x0, 0x8(%rsp)
movq $0x0, 0x38(%rsp)
movq %r14, 0x60(%rsp)
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rax
imulq $0x58, %rdx, %rcx
movl 0x18(%rax,%rcx), %esi
cmpq $0x2, %rsi
movq %rdx, 0x30(%rsp)
jb 0x6273
movl 0x1c(%rax,%rcx), %edi
movq %rdx, %rax
shlq $0x4, %rax
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx,%rax), %r13
decq %rsi
movl $0x0, 0xc(%rsp)
movq %rsi, 0x48(%rsp)
movl %edi, %eax
movl %edi, %ecx
incl %eax
movq %rax, 0x40(%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 0x1c(%rsp), %edi
cmpl %edi, %ecx
cmovael %edi, %ecx
movq %r8, %rax
subq %rcx, %rax
xorl %edx, %edx
divq %r14
movq %rax, %rbx
cmpl %edi, %esi
cmovael %edi, %esi
movq %r8, 0x80(%rsp)
leaq (%r8,%rsi), %rax
decq %rax
xorl %edx, %edx
divq %r14
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 -0x15a(%rip), %rcx      # 0x5fec
callq 0x20e0
testb $0x1, %r14b
jne 0x6161
addl %ebx, %ebp
shrl %ebp
xorps %xmm0, %xmm0
cvtsi2sdl (%r13,%rbp,8), %xmm0
jmp 0x617f
leal (%rbx,%rbp), %eax
shrl %eax
movl 0x8(%r13,%rax,8), %ecx
addl (%r13,%rax,8), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
mulsd 0x1b079(%rip), %xmm0    # 0x211f8
mulsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movl $0x8, %edx
movq %r15, %rdi
movq %r12, %rsi
leaq -0x1c4(%rip), %rcx      # 0x5fd9
callq 0x20e0
movq 0x80(%rsp), %r15
movq %r15, %rax
xorl %edx, %edx
movq 0x60(%rsp), %r14
divq %r14
xorps %xmm0, %xmm0
cvtsi2sdl (%r13,%rax,8), %xmm0
movsd 0x78(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x6260
cmpb $0x0, 0xc(%rsp)
je 0x61df
movq 0x38(%rsp), %rdi
jmp 0x624d
movq 0x28(%rsp), %rax
cmpl %eax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
jne 0x620a
addl %eax, %eax
movq %rax, 0x28(%rsp)
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rsi
callq 0x2390
movq %rax, %rdi
movl 0x8(%rsp), %ebx
movl %ebx, %eax
leaq (%rax,%rax,2), %rax
movq %rdi, 0x10(%rsp)
leaq (%rdi,%rax,8), %r12
movq 0x30(%rsp), %rax
movl %eax, 0x8(%r12)
movabsq $0x400000000, %rax      # imm = 0x400000000
movq %rax, (%r12)
movl $0x20, %edi
callq 0x2330
movq %rax, 0x10(%r12)
movq %r12, %rdi
incl %ebx
movl %ebx, 0x8(%rsp)
movq %rdi, 0x38(%rsp)
movq %r15, %rsi
callq 0x62b8
movb $0x1, %al
movl %eax, 0xc(%rsp)
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdi
decq %rsi
jne 0x60b8
movq 0x30(%rsp), %rdx
incq %rdx
movq 0x20(%rsp), %rax
movl 0x4(%rax), %eax
cmpq %rax, %rdx
jb 0x6074
jmp 0x6296
movl $0x0, 0x8(%rsp)
movq 0x50(%rsp), %rax
movl 0x8(%rsp), %ecx
movl %ecx, (%rax)
movq 0x10(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq