// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movq %r8, 0x48(%rsp)
movl %ecx, %ebp
movsd %xmm0, 0x8(%rsp)
movl %edx, %r14d
movl %esi, %ebx
movq %rdi, %r15
movabsq $0x3ffffffffffffff, %r13 # imm = 0x3FFFFFFFFFFFFFF
movl $0x180, %edi            # imm = 0x180
callq 0x2330
movq %rax, %r12
movl %r14d, %eax
xorl %edx, %edx
divl %ebx
movl %eax, 0x3c(%rsp)
movl %ebp, %eax
xorl %edx, %edx
divl %ebx
movq %rax, 0x50(%rsp)
pushq $0x1
popq %rdi
pushq $0x20
popq %rbp
movq %rbp, %rsi
callq 0x2290
movq %rax, %r14
movabsq $0x800000000000000, %rax # imm = 0x800000000000000
movq %rax, (%r14)
movq $0x3, 0x10(%r14)
movq %rbp, %rdi
callq 0x2330
movq %r14, %r11
xorl %ecx, %ecx
movq %rax, 0x18(%r14)
movl %ebx, %eax
movq %rax, 0x60(%rsp)
pushq $0x10
popq %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
movq %r15, 0x30(%rsp)
movq %r14, (%rsp)
movl 0x4(%r15), %eax
cmpq %rax, %rcx
jae 0x45b8
movq 0x8(%r15), %rax
movq %rcx, 0x20(%rsp)
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %r15
movl 0x4(%rax,%rcx), %ebx
movq %r15, %rdi
movq %rbx, %rsi
pushq $0x8
popq %rdx
leaq -0x3eb(%rip), %rcx      # 0x3d28
callq 0x20e0
movl %ebx, %eax
shrl %eax
movl (%r15,%rax,8), %ecx
testb $0x1, %bl
jne 0x4139
decl %eax
addl (%r15,%rax,8), %ecx
cvtsi2sd %ecx, %xmm0
mulsd 0x1a0a9(%rip), %xmm0    # 0x1e1e0
jmp 0x413d
cvtsi2sd %ecx, %xmm0
movsd 0x8(%rsp), %xmm3
mulsd %xmm3, %xmm0
xorl %r14d, %r14d
movq 0x20(%rsp), %rcx
movq (%rsp), %r11
cmpq %r14, %rbx
je 0x41dd
cvtsi2sdl (%r15,%r14,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x416d
incq %r14
jmp 0x4153
testq %r14, %r14
je 0x41dd
cmpl %r14d, %ebx
je 0x41dd
movq %rbx, 0x10(%rsp)
movq %r15, %rdi
movq %r14, %rsi
pushq $0x8
popq %rdx
leaq -0x477(%rip), %rcx      # 0x3d15
callq 0x20e0
movq (%rsp), %rdi
movabsq $-0x400000000000000, %rax # imm = 0xFC00000000000000
andq %rax, (%rdi)
andq $0x0, 0x8(%rdi)
movl 0x4(%r15), %eax
pushq $0x1
popq %rbx
movl %eax, %ecx
movl %ecx, %esi
cmpq %rbx, %r14
je 0x41e7
movl 0x4(%r15,%rbx,8), %ebp
addl 0x3c(%rsp), %ecx
cmpl %ebp, %ecx
jae 0x41d6
shlq $0x20, %rax
orq %rax, %rsi
callq 0x45ea
movq (%rsp), %rdi
movl %ebp, %eax
incq %rbx
movl %ebp, %ecx
jmp 0x41b0
movq 0x30(%rsp), %r15
jmp 0x45b0
shlq $0x20, %rax
orq %rax, %rsi
callq 0x45ea
movq %r15, %rdi
movq 0x10(%rsp), %rsi
pushq $0x8
popq %rdx
leaq -0x4f0(%rip), %rcx      # 0x3d15
callq 0x20e0
movq 0x18(%rsp), %rdx
cmpl 0x40(%rsp), %edx
jne 0x4231
leal (%rdx,%rdx), %eax
movq %rax, 0x40(%rsp)
imulq $0x18, %rax, %rsi
movq %r12, %rdi
callq 0x2390
movq 0x18(%rsp), %rdx
movq %rax, %r12
movq 0x20(%rsp), %rcx
movl %edx, %eax
imulq $0x18, %rax, %rbx
leaq (%r12,%rbx), %rax
movq %rax, 0x28(%rsp)
movl %ecx, 0x8(%r12,%rbx)
movabsq $0x400000000, %rax      # imm = 0x400000000
movq %rax, (%r12,%rbx)
pushq $0x20
popq %rdi
callq 0x2330
movq %r12, 0x58(%rsp)
movq %rax, 0x10(%r12,%rbx)
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
movsd 0x8(%rsp), %xmm3
movq 0x50(%rsp), %r12
movq (%rsp), %r11
movsd 0x19f59(%rip), %xmm4    # 0x1e1e8
movl %ebx, %ebx
cmpq %rbx, 0x8(%r11)
jbe 0x4408
movq 0x18(%r11), %rax
movq (%r11), %rcx
andq %r13, %rcx
addq %rbx, %rcx
andq 0x10(%r11), %rcx
movq (%rax,%rcx,8), %r14
movq %r14, %rax
shrq $0x20, %rax
movl %r14d, %ecx
subl %eax, %ecx
cmpl %r12d, %ecx
jbe 0x4387
xorl %esi, %esi
movl %eax, %edx
pushq $-0x1
popq %rdi
xorl %ecx, %ecx
cmpl %r14d, %edx
ja 0x431f
movl %edx, %r8d
movl (%r15,%r8,8), %r8d
xorl %r9d, %r9d
cmpl %r8d, %edi
jbe 0x42e6
movl %edx, %ecx
jmp 0x4315
movl %r8d, %r8d
cvtsi2sd %r8, %xmm0
movl %edi, %r8d
cvtsi2sd %r8, %xmm1
divsd %xmm3, %xmm1
movapd %xmm4, %xmm2
maxsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0x430f
incl %esi
cmpl %r12d, %esi
jae 0x431f
movl %edi, %r8d
movl %esi, %r9d
incl %edx
movl %r8d, %edi
movl %r9d, %esi
jmp 0x42ce
xorl %edi, %edi
movl %r14d, %edx
movl %eax, %esi
pushq $-0x1
popq %r8
xorl %ebp, %ebp
cmpl %r14d, %esi
ja 0x437f
movl %edx, %r9d
movl (%r15,%r9,8), %r9d
xorl %r10d, %r10d
cmpl %r9d, %r8d
jbe 0x4344
movl %edx, %ebp
jmp 0x4373
movl %r9d, %r9d
cvtsi2sd %r9, %xmm0
movl %r8d, %r9d
cvtsi2sd %r9, %xmm1
divsd %xmm3, %xmm1
movapd %xmm4, %xmm2
maxsd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jbe 0x436d
incl %edi
cmpl %r12d, %edi
jae 0x437f
movl %r8d, %r9d
movl %edi, %r10d
incl %esi
decl %edx
movl %r9d, %r8d
movl %r10d, %edi
jmp 0x432c
leal (%rcx,%r12), %edx
cmpl %edx, %ebp
jae 0x43dc
testq %rax, %rax
je 0x43d5
cmpl %r14d, 0x10(%rsp)
je 0x43d5
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
pushq $-0x1
popq %rbp
cmpl %r14d, %eax
ja 0x43b2
movl %eax, %edx
movl (%r15,%rdx,8), %edx
cmpl %edx, %ecx
cmovael %edx, %ecx
cmoval %eax, %ebp
incl %eax
jmp 0x439b
cmpl $-0x1, %ebp
je 0x43d5
movl %ebp, %esi
movq 0x28(%rsp), %rdi
callq 0x3ff7
movsd 0x19e1d(%rip), %xmm4    # 0x1e1e8
movq (%rsp), %r11
movsd 0x8(%rsp), %xmm3
incl %ebx
jmp 0x428f
testq %rax, %rax
je 0x43ff
movl %ecx, %esi
movq 0x28(%rsp), %rdi
callq 0x3ff7
movsd 0x19df3(%rip), %xmm4    # 0x1e1e8
movq (%rsp), %r11
movsd 0x8(%rsp), %xmm3
cmpl %r14d, 0x10(%rsp)
jne 0x43b7
jmp 0x43d5
movq 0x28(%rsp), %r10
movl (%r10), %eax
cmpq $0x1, %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jbe 0x4553
movabsq $-0x400000000000000, %rax # imm = 0xFC00000000000000
andq %rax, (%r11)
andq $0x0, 0x8(%r11)
xorl %ebp, %ebp
movl (%r10), %eax
cmpq %rax, %rbp
jae 0x4525
testq %rbp, %rbp
je 0x444f
movq 0x10(%r10), %rcx
movl -0x8(%rcx,%rbp,8), %r13d
jmp 0x4452
xorl %r13d, %r13d
movq 0x10(%rsp), %r12
decl %eax
cmpq %rax, %rbp
jae 0x4467
movq 0x10(%r10), %rax
movl 0x8(%rax,%rbp,8), %r12d
movl %r12d, %ebx
subl %r13d, %ebx
leal 0x1(%rbx), %esi
movl %r13d, %eax
leaq (%r15,%rax,8), %r14
movq %r14, %rdi
movq %rsi, 0x70(%rsp)
pushq $0x8
popq %rdx
leaq -0x761(%rip), %rcx      # 0x3d28
callq 0x20e0
addl %r13d, %r12d
shrl %r12d
movl (%r15,%r12,8), %eax
testb $0x1, %bl
jne 0x44a3
cvtsi2sd %eax, %xmm1
jmp 0x44b7
movl %r12d, %ecx
addl 0x8(%r15,%rcx,8), %eax
cvtsi2sd %eax, %xmm1
mulsd 0x19d29(%rip), %xmm1    # 0x1e1e0
movsd 0x8(%rsp), %xmm0
movabsq $0x3ffffffffffffff, %r13 # imm = 0x3FFFFFFFFFFFFFF
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
mulsd %xmm0, %xmm1
movsd %xmm1, 0x68(%rsp)
movq %r14, %rdi
movq 0x70(%rsp), %rsi
pushq $0x8
popq %rdx
leaq -0x7d3(%rip), %rcx      # 0x3d15
callq 0x20e0
movq 0x28(%rsp), %r10
movq 0x10(%r10), %rax
movq (%rax,%rbp,8), %rax
cvtsi2sdl (%r15,%rax,8), %xmm0
ucomisd 0x68(%rsp), %xmm0
jbe 0x4519
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x45ea
movq 0x28(%rsp), %r10
incq %rbp
movq (%rsp), %r11
jmp 0x4433
movq 0x8(%r11), %rcx
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0x4553
movq 0x10(%r10), %rsi
movq (%r11), %rdi
andq %r13, %rdi
addq %rdx, %rdi
andq 0x10(%r11), %rdi
movq 0x18(%r11), %r8
movq (%r8,%rdi,8), %rdi
movq %rbx, (%rsi,%rdi,8)
incl %edx
jmp 0x452b
movq 0x18(%rsp), %rcx
incl %ecx
xorl %edx, %edx
xorl %esi, %esi
movq 0x58(%rsp), %r12
movq 0x30(%rsp), %r15
cmpq %rdx, %rax
je 0x458e
movq 0x10(%r10), %rdi
movq (%rdi,%rdx,8), %r8
cmpq %rbx, %r8
je 0x4589
imulq 0x60(%rsp), %r8
movl %esi, %r9d
incl %esi
movq %r8, (%rdi,%r9,8)
incq %rdx
jmp 0x4568
movl %esi, (%r10)
testl %esi, %esi
je 0x459e
movl %ecx, %eax
movq %rax, 0x18(%rsp)
jmp 0x45ab
movq 0x10(%r10), %rdi
callq 0x2060
movq (%rsp), %r11
movq 0x20(%rsp), %rcx
incq %rcx
jmp 0x40e0
movq 0x18(%r11), %rdi
movq %r11, %rbx
callq 0x2060
movq %rbx, %rdi
callq 0x2060
movq 0x48(%rsp), %rax
movq 0x18(%rsp), %rcx
movl %ecx, (%rax)
movq %r12, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq