// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]yahs/break.c
// Function: detect_break_points
bp_t *detect_break_points(link_mat_t *link_mat, uint32_t bin_size, uint32_t merge_size, double fold_thres, uint32_t dual_break_thres, uint32_t *bp_n)
{
    uint32_t i, j, k, n, m, d, b, b_n, b_m;
    double mcnt;
    int64_t *link;
    uint32_t s, e, t, min_c, p, bp_s, bp_e;
    kdq_t(int64_t) *q;
    bp_t *bp, *bp1;
    
    b_n = 0;
    b_m = 16;
    bp = (bp_t *) malloc(b_m * sizeof(bp_t));
    bp1 = 0;
    m = merge_size / bin_size;
    d = dual_break_thres / bin_size;
    q = kdq_init(int64_t);
    for (i = 0; i < link_mat->n; ++i) {
        link = link_mat->link[i].link;
        n = link_mat->link[i].n;
        // sort by link count
        qsort(link, n, sizeof(int64_t), cnt_cmp);
        // find median
        mcnt = n & 1? (int32_t) link[n / 2] : ((int32_t) link[n / 2] + (int32_t) link[n / 2 - 1]) / 2.;
        // find count threshold
        mcnt *= fold_thres;
        // find positions below threshold
        b = 0;
        while (b < n && (int32_t) link[b] < mcnt)
            ++b;
        if (!b || b == n)
            continue;
        // sort by position for positions below threshold
        qsort(link, b, sizeof(int64_t), pos_cmp);
        // detect blocks for break points
        kdq_clean(q);
        s = link[0] >> 32;
        e = s;
        for (j = 1; j < b; ++j) {
            t = link[j] >> 32;
            if (e + m < t) {
                // new block
                kdq_push(int64_t, q, (int64_t) s << 32 | e);
                s = t;
                e = s;
            } else {
                e = t;
            }
        }
        // add last block
        kdq_push(int64_t, q, (int64_t) s << 32 | e);
        // sort by position for all positions
        qsort(link, n, sizeof(int64_t), pos_cmp);
        // detect precise break points
        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;

        for (j = 0; j < kdq_size(q); ++j) {
            s = kdq_at(q, j) >> 32;
            e = (int32_t) kdq_at(q, j);
            
            if (e - s > d && make_dual_break(link, s, e, d, fold_thres, &bp_s, &bp_e)) {
                // dual break
                if (s != 0)
                    add_break_point(bp1, bp_s);
                if (e != n - 1)
                    add_break_point(bp1, bp_e);
                continue;
            }

            // skip the first and last block
            if (s == 0 || e == n - 1)
                continue;
            
            // find the position of the minimum value in this valley
            // which will be a break point
            min_c = INT32_MAX;
            p = UINT32_MAX;
            for (k = s; k <= e; ++k) {
                if ((int32_t) link[k] < min_c) {
                    min_c = (int32_t) link[k];
                    p = k;
                }
            }

            if (p != UINT32_MAX)
                add_break_point(bp1, p);
        }

        if (bp1->n > 1) {
            // revisit to remove false positives
            kdq_clean(q);
            for (k = 0; k < bp1->n; ++k) {
                s = k > 0? bp1->p[k - 1] : 0;
                e = k < bp1->n - 1? bp1->p[k + 1] : n - 1;
                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[bp1->p[k]] > mcnt)
                    kdq_push(int64_t, q, k);
            }
            for (k = 0; k < kdq_size(q); ++k)
                bp1->p[kdq_at(q, k)] = UINT32_MAX;
        }
        
        j = 0;
        for (k = 0; k < bp1->n; ++k)
            if (bp1->p[k] != UINT32_MAX)
                bp1->p[j++] = bp1->p[k] * bin_size;
        bp1->n = j;
        
        if (!bp1->n) {
            free(bp1->p);
            --b_n;
        }
    }
    kdq_destroy(int64_t, q);
    *bp_n = b_n;
    
    return bp;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, 0x68(%rsp)
movl %ecx, %ebp
movsd %xmm0, 0x10(%rsp)
movl %edx, %r14d
movl %esi, %ebx
movq %rdi, %r15
movabsq $0x3ffffffffffffff, %r12 # imm = 0x3FFFFFFFFFFFFFF
movl $0x180, %edi            # imm = 0x180
callq 0x2330
movq %rax, 0x20(%rsp)
movl %r14d, %eax
xorl %edx, %edx
divl %ebx
movq %rax, 0x78(%rsp)
movl %ebp, %eax
xorl %edx, %edx
divl %ebx
movq %rax, 0x48(%rsp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x2290
movq %rax, %rbp
movq (%rax), %rax
andq %r12, %rax
btsq $0x3b, %rax
movq %rax, (%rbp)
movq $0x3, 0x10(%rbp)
movl $0x20, %edi
callq 0x2330
movq %rax, 0x18(%rbp)
cmpl $0x0, 0x4(%r15)
je 0x6943
movl %ebx, %ebx
xorl %ecx, %ecx
movl $0x10, %eax
movq %rax, 0x58(%rsp)
movl $0x0, 0xc(%rsp)
movq %r15, 0x40(%rsp)
movq %rbp, 0x28(%rsp)
movq %rbx, 0x70(%rsp)
movq 0x8(%r15), %rax
movq %rcx, 0x60(%rsp)
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %r15
movl 0x4(%rax,%rcx), %r14d
movl $0x8, %edx
movq %r15, %rdi
movq %r14, %rsi
leaq -0x3f0(%rip), %rcx      # 0x5fec
callq 0x20e0
movl %r14d, %eax
shrl %eax
movl (%r15,%rax,8), %ecx
testb $0x1, %r14b
jne 0x6407
decl %eax
addl (%r15,%rax,8), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
mulsd 0x1adf3(%rip), %xmm0    # 0x211f8
jmp 0x640e
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd 0x10(%rsp), %xmm1
testq %r14, %r14
je 0x643b
mulsd %xmm1, %xmm0
xorl %ebx, %ebx
xorps %xmm1, %xmm1
cvtsi2sdl (%r15,%rbx,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x643d
incq %rbx
cmpq %rbx, %r14
jne 0x641f
movl %r14d, %ebx
jmp 0x643d
xorl %ebx, %ebx
testl %ebx, %ebx
sete %al
cmpl %r14d, %ebx
sete %cl
orb %al, %cl
je 0x6456
movq 0x40(%rsp), %r15
jmp 0x692c
movq %r14, 0x30(%rsp)
movl %ebx, %r14d
movl $0x8, %edx
movq %r15, %rdi
movq %r14, %rsi
leaq -0x497(%rip), %rcx      # 0x5fd9
callq 0x20e0
movabsq $-0x400000000000000, %rax # imm = 0xFC00000000000000
andq %rax, (%rbp)
movq $0x0, 0x8(%rbp)
movq (%r15), %rax
shrq $0x20, %rax
cmpl $0x2, %ebx
jb 0x64d5
movl $0x1, %ebp
movl %eax, %esi
movq (%r15,%rbp,8), %rbx
shrq $0x20, %rbx
movq 0x78(%rsp), %rcx
addl %eax, %ecx
cmpl %ebx, %ecx
jae 0x64c9
shlq $0x20, %rsi
movl %eax, %eax
orq %rsi, %rax
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x697e
movl %ebx, %esi
incq %rbp
movl %ebx, %eax
cmpq %rbp, %r14
jne 0x649e
jmp 0x64da
movl %eax, %esi
movq %rax, %rbx
shlq $0x20, %rsi
orq %rbx, %rsi
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
callq 0x697e
movl $0x8, %edx
movq %r15, %rdi
movq 0x30(%rsp), %rbx
movq %rbx, %rsi
leaq -0x52c(%rip), %rcx      # 0x5fd9
callq 0x20e0
movl 0xc(%rsp), %ecx
movq 0x58(%rsp), %rax
cmpl %eax, %ecx
jne 0x653a
addl %eax, %eax
movq %rax, 0x58(%rsp)
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rsi
movq 0x20(%rsp), %rdi
callq 0x2390
movl 0xc(%rsp), %ecx
jmp 0x653f
movq 0x20(%rsp), %rax
movl %ecx, %ecx
leaq (%rcx,%rcx,2), %rcx
movq %rax, 0x20(%rsp)
leaq (%rax,%rcx,8), %r14
movq 0x60(%rsp), %rax
movl %eax, 0x8(%r14)
movabsq $0x400000000, %rax      # imm = 0x400000000
movq %rax, (%r14)
movl $0x20, %edi
callq 0x2330
movq %r14, 0x38(%rsp)
movq %rax, 0x10(%r14)
cmpq $0x0, 0x8(%rbp)
je 0x674c
movl %ebx, %r12d
decl %r12d
xorl %r14d, %r14d
movsd 0x10(%rsp), %xmm3
movq 0x48(%rsp), %r11
movsd 0x1ac62(%rip), %xmm4    # 0x21200
movq 0x18(%rbp), %rax
movq (%rbp), %rcx
movabsq $0x3ffffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFFF
andq %rdx, %rcx
addq %r14, %rcx
andq 0x10(%rbp), %rcx
movq (%rax,%rcx,8), %rbx
movq %rbx, %rax
shrq $0x20, %rax
movl %ebx, %ecx
subl %eax, %ecx
cmpl %r11d, %ecx
jbe 0x66af
xorl %ecx, %ecx
movl $0x0, %edx
cmpl %eax, %ebx
jb 0x6635
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
xorl %r8d, %r8d
movl %eax, %edi
movl %edi, %r9d
movl (%r15,%r9,8), %r9d
cmpl %r9d, %esi
jbe 0x65ff
movl %r9d, %esi
xorl %r8d, %r8d
movl %edi, %edx
jmp 0x662f
movl %r9d, %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9, %xmm0
movl %esi, %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9, %xmm1
divsd %xmm3, %xmm1
movapd %xmm4, %xmm2
maxsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0x662f
incl %r8d
cmpl %r11d, %r8d
jae 0x6635
incl %edi
cmpl %ebx, %edi
jbe 0x65e9
cmpl %eax, %ebx
jb 0x6698
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movl %ebx, %edi
xorl %r9d, %r9d
movl %eax, %r8d
movl %edi, %r10d
movl (%r15,%r10,8), %r10d
cmpl %r10d, %esi
jbe 0x665e
movl %r10d, %esi
xorl %r9d, %r9d
movl %edi, %ecx
jmp 0x668e
movl %r10d, %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r10, %xmm0
movl %esi, %r10d
xorps %xmm1, %xmm1
cvtsi2sd %r10, %xmm1
divsd %xmm3, %xmm1
movapd %xmm4, %xmm2
maxsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0x668e
incl %r9d
cmpl %r11d, %r9d
jae 0x6698
incl %r8d
decl %edi
cmpl %ebx, %r8d
jbe 0x6648
leal (%rdx,%r11), %esi
cmpl %esi, %ecx
movl 0x1c(%rsp), %esi
cmovael %edx, %esi
movl %esi, 0x1c(%rsp)
cmovael %ecx, %r13d
jae 0x671c
movl %r13d, %esi
testq %rax, %rax
je 0x670a
cmpl %ebx, %r12d
je 0x670a
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl %eax, %ebx
jb 0x66e0
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %eax, %edx
movl (%r15,%rdx,8), %edx
cmpl %edx, %ecx
cmoval %eax, %r13d
cmovael %edx, %ecx
incl %eax
cmpl %ebx, %eax
jbe 0x66cb
cmpl $-0x1, %r13d
je 0x670a
movl %esi, %ebx
movl %r13d, %esi
movq 0x38(%rsp), %rdi
callq 0x62b8
movl %ebx, %esi
movsd 0x1ab01(%rip), %xmm4    # 0x21200
movq 0x48(%rsp), %r11
movsd 0x10(%rsp), %xmm3
incl %r14d
movl %esi, %r13d
cmpq %r14, 0x8(%rbp)
ja 0x659e
jmp 0x674f
testq %rax, %rax
je 0x6742
movl 0x1c(%rsp), %esi
movq 0x38(%rsp), %rdi
callq 0x62b8
movsd 0x1aac9(%rip), %xmm4    # 0x21200
movq 0x48(%rsp), %r11
movsd 0x10(%rsp), %xmm3
movl %r13d, %esi
cmpl %ebx, %r12d
jne 0x66e6
jmp 0x670a
movl %r13d, %esi
movl %esi, 0x18(%rsp)
movq 0x38(%rsp), %r14
cmpl $0x2, (%r14)
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
jb 0x68d0
movabsq $-0x400000000000000, %rax # imm = 0xFC00000000000000
andq %rax, (%rbp)
movq $0x0, 0x8(%rbp)
movl (%r14), %eax
testl %eax, %eax
je 0x6888
movq 0x30(%rsp), %rcx
decl %ecx
movl %ecx, 0x54(%rsp)
xorl %ebx, %ebx
testq %rbx, %rbx
je 0x67a6
movq 0x10(%r14), %rcx
movl -0x8(%rcx,%rbx,8), %r12d
jmp 0x67a9
xorl %r12d, %r12d
decl %eax
movl 0x54(%rsp), %ecx
movl %ecx, %ebp
cmpq %rax, %rbx
jae 0x67be
movq 0x10(%r14), %rax
movl 0x8(%rax,%rbx,8), %ebp
movl %ebp, %r13d
subl %r12d, %r13d
leal 0x1(%r13), %esi
movl %r12d, %eax
leaq (%r15,%rax,8), %r14
movl $0x8, %edx
movq %r14, %rdi
movq %rsi, 0x30(%rsp)
leaq -0x7f7(%rip), %rcx      # 0x5fec
callq 0x20e0
addl %r12d, %ebp
shrl %ebp
movl (%r15,%rbp,8), %eax
testb $0x1, %r13b
jne 0x6800
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
jmp 0x6816
movl %ebp, %ecx
addl 0x8(%r15,%rcx,8), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd 0x1a9e2(%rip), %xmm1    # 0x211f8
movsd 0x10(%rsp), %xmm0
movq 0x28(%rsp), %rbp
mulsd %xmm0, %xmm1
movsd %xmm1, 0x80(%rsp)
movl $0x8, %edx
movq %r14, %rdi
movq 0x30(%rsp), %rsi
leaq -0x869(%rip), %rcx      # 0x5fd9
callq 0x20e0
movq 0x38(%rsp), %r14
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%r15,%rax,8), %xmm0
ucomisd 0x80(%rsp), %xmm0
jbe 0x6873
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x697e
incq %rbx
movl (%r14), %eax
cmpq %rax, %rbx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
jb 0x6796
movq 0x8(%rbp), %rax
testq %rax, %rax
je 0x68d0
movq 0x10(%r14), %rcx
movq 0x10(%rbp), %rdx
movq 0x18(%rbp), %rsi
movq (%rbp), %rdi
movabsq $0x3ffffffffffffff, %r8 # imm = 0x3FFFFFFFFFFFFFF
andq %r8, %rdi
movl $0x1, %r8d
movq %rdi, %r9
andq %rdx, %r9
movq (%rsi,%r9,8), %r9
movq %r10, (%rcx,%r9,8)
movl %r8d, %r9d
incq %rdi
incl %r8d
cmpq %r9, %rax
ja 0x68b4
movl (%r14), %eax
testq %rax, %rax
je 0x690a
movq 0x10(%r14), %rdx
xorl %esi, %esi
xorl %ecx, %ecx
movq 0x40(%rsp), %r15
movq 0x70(%rsp), %rbx
movq (%rdx,%rsi,8), %rdi
cmpq %r10, %rdi
je 0x6900
imulq %rbx, %rdi
movl %ecx, %r8d
incl %ecx
movq %rdi, (%rdx,%r8,8)
incq %rsi
cmpq %rsi, %rax
jne 0x68ea
jmp 0x6911
xorl %ecx, %ecx
movq 0x40(%rsp), %r15
movl %ecx, (%r14)
testl %ecx, %ecx
je 0x691e
incl 0xc(%rsp)
jmp 0x6927
movq 0x10(%r14), %rdi
callq 0x2060
movl 0x18(%rsp), %r13d
movq 0x60(%rsp), %rcx
incq %rcx
movl 0x4(%r15), %eax
cmpq %rax, %rcx
jb 0x63b3
jmp 0x694b
movl $0x0, 0xc(%rsp)
movq 0x18(%rbp), %rdi
callq 0x2060
movq %rbp, %rdi
callq 0x2060
movq 0x68(%rsp), %rax
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
movq 0x20(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq