// File: /workspace/github_c_makefile_star2_O2/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 $0x98, %rsp
movq %r8, 0x50(%rsp)
movl %edx, 0x2c(%rsp)
movsd %xmm0, 0x78(%rsp)
movl %esi, %ebx
movq %rdi, %r13
movq %rcx, 0x58(%rsp)
movq 0x20(%rcx), %rax
movq %rax, 0x70(%rsp)
movl $0x180, %edi            # imm = 0x180
callq 0x2330
movq %rax, %rcx
movl %ebx, %ebp
xorl %edx, %edx
pushq $0x10
popq %rax
movq %rax, 0x30(%rsp)
xorl %esi, %esi
movl $0x0, 0x8(%rsp)
movq %r13, 0x38(%rsp)
movq %rbp, 0x68(%rsp)
movl 0x4(%r13), %eax
cmpq %rax, %rdx
jae 0x3fd7
movq %rsi, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rax
imulq $0x58, %rdx, %rcx
movl 0x18(%rax,%rcx), %esi
movq %rsi, 0x80(%rsp)
movl 0x1c(%rax,%rcx), %eax
movq 0x8(%r13), %rcx
movq %rdx, 0x18(%rsp)
shlq $0x4, %rdx
movq 0x8(%rcx,%rdx), %rbx
movl $0x0, 0xc(%rsp)
pushq $0x1
popq %rcx
movq %rbx, 0x60(%rsp)
cmpq 0x80(%rsp), %rcx
jae 0x3fc0
movq %rcx, 0x40(%rsp)
leal 0x1(%rax), %ecx
movq %rcx, 0x90(%rsp)
imulq $0x30, %rcx, %rcx
movq 0x70(%rsp), %rdx
movq 0x8(%rdx,%rcx), %rdi
movl 0x20(%rdx,%rcx), %ecx
movl %eax, %eax
imulq $0x30, %rax, %rax
movl 0x20(%rdx,%rax), %edx
movl 0x2c(%rsp), %esi
cmpl %esi, %edx
cmovael %esi, %edx
movq %rdi, %rax
subq %rdx, %rax
xorl %edx, %edx
divq %rbp
movq %rax, %r12
cmpl %esi, %ecx
cmovael %esi, %ecx
movq %rdi, 0x48(%rsp)
leaq (%rdi,%rcx), %rax
decq %rax
xorl %edx, %edx
divq %rbp
movq %rax, %rbp
movq %rbx, %r13
movl %ebp, %ebx
subl %r12d, %ebx
leal 0x1(%rbx), %r14d
movl %r12d, %eax
leaq (,%rax,8), %r15
addq %r13, %r15
movq %r15, %rdi
movq %r14, %rsi
pushq $0x8
popq %rdx
leaq -0x15b(%rip), %rcx      # 0x3d28
callq 0x20e0
testb $0x1, %bl
jne 0x3e9b
addl %r12d, %ebp
shrl %ebp
cvtsi2sdl (%r13,%rbp,8), %xmm0
jmp 0x3eb7
leal (%r12,%rbp), %eax
shrl %eax
movl 0x8(%r13,%rax,8), %ecx
addl (%r13,%rax,8), %ecx
cvtsi2sd %ecx, %xmm0
mulsd 0x1a329(%rip), %xmm0    # 0x1e1e0
movq %r13, %rbx
mulsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movq %r15, %rdi
movq %r14, %rsi
pushq $0x8
popq %rdx
leaq -0x1c4(%rip), %rcx      # 0x3d15
callq 0x20e0
movq 0x48(%rsp), %rsi
movq %rsi, %rax
xorl %edx, %edx
movq 0x68(%rsp), %rbp
divq %rbp
cvtsi2sdl (%r13,%rax,8), %xmm0
movsd 0x88(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3f19
cmpb $0x0, 0xc(%rsp)
movq 0x38(%rsp), %r13
je 0x3f23
movq 0x20(%rsp), %r14
jmp 0x3f98
movq 0x38(%rsp), %r13
jmp 0x3fab
movq 0x30(%rsp), %rax
cmpl %eax, 0x8(%rsp)
jne 0x3f45
addl %eax, %eax
movq %rax, 0x30(%rsp)
imulq $0x18, %rax, %rsi
movq 0x10(%rsp), %rdi
callq 0x2390
jmp 0x3f4a
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
movl 0x8(%rsp), %r12d
movl %r12d, %ecx
imulq $0x18, %rcx, %rbx
leaq (%rax,%rbx), %r14
movl %edx, 0x8(%rax,%rbx)
movabsq $0x400000000, %rcx      # imm = 0x400000000
movq %rcx, (%rax,%rbx)
pushq $0x20
popq %rdi
movq %rax, %r15
callq 0x2330
movq %r15, 0x10(%rsp)
movq %rax, 0x10(%r15,%rbx)
incl %r12d
movl %r12d, 0x8(%rsp)
movq 0x60(%rsp), %rbx
movq 0x48(%rsp), %rsi
movq %r14, 0x20(%rsp)
movq %r14, %rdi
callq 0x3ff7
movb $0x1, %al
movl %eax, 0xc(%rsp)
movq 0x40(%rsp), %rcx
incq %rcx
movq 0x90(%rsp), %rax
jmp 0x3def
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rsi
jmp 0x3d99
movq 0x50(%rsp), %rax
movl 0x8(%rsp), %edx
movl %edx, (%rax)
movq %rcx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq