// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x10, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
imulq $0x18, %rax, %rdi
callq 0x2340
movq %rax, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movl -0x10(%rbp), %eax
xorl %edx, %edx
divl -0xc(%rbp)
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
xorl %edx, %edx
divl -0xc(%rbp)
movl %eax, -0x40(%rbp)
callq 0x77d0
movq %rax, -0x88(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x5eda
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x2c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x2c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x60(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
leaq -0x52d(%rip), %rcx      # 0x52a0
callq 0x20e0
movl -0x38(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x57fa
movq -0x60(%rbp), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xa0(%rbp)
jmp 0x5835
movq -0x60(%rbp), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq -0x60(%rbp), %rcx
movl -0x38(%rbp), %edx
shrl %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
addl %ecx, %eax
cvtsi2sd %eax, %xmm0
movsd 0x2b997(%rip), %xmm1    # 0x311c0
divsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x18(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %ecx
xorl %eax, %eax
cmpl -0x38(%rbp), %ecx
movb %al, -0xa1(%rbp)
jae 0x5889
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cvtsi2sd %eax, %xmm1
movsd -0x58(%rbp), %xmm0
ucomisd %xmm1, %xmm0
seta %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x5895
jmp 0x58a0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x5858
cmpl $0x0, -0x44(%rbp)
je 0x58ae
movl -0x44(%rbp), %eax
cmpl -0x38(%rbp), %eax
jne 0x58b3
jmp 0x5ecc
movq -0x60(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
leaq -0x668(%rip), %rcx      # 0x5260
callq 0x20e0
movq -0x88(%rbp), %rax
movabsq $-0x400000000000000, %rcx # imm = 0xFC00000000000000
andq (%rax), %rcx
orq $0x0, %rcx
movq %rcx, (%rax)
movq -0x88(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x60(%rbp), %rax
movq (%rax), %rax
sarq $0x20, %rax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x68(%rbp)
movl $0x1, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x5973
movq -0x60(%rbp), %rax
movl -0x30(%rbp), %ecx
movq (%rax,%rcx,8), %rax
sarq $0x20, %rax
movl %eax, -0x6c(%rbp)
movl -0x68(%rbp), %eax
addl -0x3c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jae 0x5960
movq -0x88(%rbp), %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
shlq $0x20, %rsi
movl -0x68(%rbp), %eax
orq %rax, %rsi
callq 0x5f00
movl -0x6c(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x68(%rbp)
jmp 0x5966
movl -0x6c(%rbp), %eax
movl %eax, -0x68(%rbp)
jmp 0x5968
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x5912
movq -0x88(%rbp), %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
shlq $0x20, %rsi
movl -0x68(%rbp), %eax
orq %rax, %rsi
callq 0x5f00
movq -0x60(%rbp), %rdi
movl -0x38(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
leaq -0x743(%rip), %rcx      # 0x5260
callq 0x20e0
movl -0x48(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jne 0x59d2
movl -0x4c(%rbp), %eax
shll %eax
movl %eax, -0x4c(%rbp)
movq -0x90(%rbp), %rdi
movl -0x4c(%rbp), %eax
imulq $0x18, %rax, %rsi
callq 0x23a0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movl -0x48(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movl -0x2c(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x98(%rbp), %rax
movl $0x0, (%rax)
movq -0x98(%rbp), %rax
movl $0x4, 0x4(%rax)
movq -0x98(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x2340
movq %rax, %rcx
movq -0x98(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x88(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x5bba
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x88(%rbp), %rdx
movabsq $0x3ffffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFFF
andq (%rdx), %rcx
movl -0x30(%rbp), %edx
addq %rdx, %rcx
movq -0x88(%rbp), %rdx
andq 0x10(%rdx), %rcx
movq (%rax,%rcx,8), %rax
sarq $0x20, %rax
movl %eax, -0x64(%rbp)
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x88(%rbp), %rdx
movabsq $0x3ffffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFFF
andq (%rdx), %rcx
movl -0x30(%rbp), %edx
addq %rdx, %rcx
movq -0x88(%rbp), %rdx
andq 0x10(%rdx), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
subl -0x64(%rbp), %eax
cmpl -0x40(%rbp), %eax
jbe 0x5b31
movq -0x60(%rbp), %rdi
movl -0x64(%rbp), %esi
movl -0x68(%rbp), %edx
movl -0x40(%rbp), %ecx
movsd -0x18(%rbp), %xmm0
leaq -0x78(%rbp), %r8
leaq -0x7c(%rbp), %r9
callq 0x5fa0
cmpl $0x0, %eax
je 0x5b31
cmpl $0x0, -0x64(%rbp)
je 0x5b11
movq -0x98(%rbp), %rdi
movl -0x78(%rbp), %eax
movl %eax, %esi
callq 0x5680
movl -0x68(%rbp), %eax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x5b2f
movq -0x98(%rbp), %rdi
movl -0x7c(%rbp), %eax
movl %eax, %esi
callq 0x5680
jmp 0x5bac
cmpl $0x0, -0x64(%rbp)
je 0x5b44
movl -0x68(%rbp), %eax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x5b46
jmp 0x5bac
movl $0x7fffffff, -0x70(%rbp) # imm = 0x7FFFFFFF
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movl -0x64(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x68(%rbp), %eax
ja 0x5b93
movq -0x60(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpl -0x70(%rbp), %eax
jae 0x5b86
movq -0x60(%rbp), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x70(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x5b88
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5b5a
cmpl $-0x1, -0x74(%rbp)
je 0x5baa
movq -0x98(%rbp), %rdi
movl -0x74(%rbp), %eax
movl %eax, %esi
callq 0x5680
jmp 0x5bac
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x5a45
movq -0x98(%rbp), %rax
cmpl $0x1, (%rax)
jbe 0x5e22
movq -0x88(%rbp), %rax
movabsq $-0x400000000000000, %rcx # imm = 0xFC00000000000000
andq (%rax), %rcx
orq $0x0, %rcx
movq %rcx, (%rax)
movq -0x88(%rbp), %rax
movq $0x0, 0x8(%rax)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x98(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x5db6
cmpl $0x0, -0x34(%rbp)
jbe 0x5c33
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
jmp 0x5c3e
xorl %eax, %eax
movq %rax, -0xb0(%rbp)
jmp 0x5c3e
movq -0xb0(%rbp), %rax
movl %eax, -0x64(%rbp)
movl -0x34(%rbp), %eax
movq -0x98(%rbp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0x5c7b
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb8(%rbp)
jmp 0x5c8a
movl -0x38(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
subl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movq -0x60(%rbp), %rdi
movl -0x64(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
leaq -0xa1f(%rip), %rcx      # 0x52a0
callq 0x20e0
movl -0x6c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x5cef
movq -0x60(%rbp), %rax
movl -0x68(%rbp), %ecx
addl -0x64(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xc0(%rbp)
jmp 0x5d30
movq -0x60(%rbp), %rax
movl -0x68(%rbp), %ecx
addl -0x64(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq -0x60(%rbp), %rcx
movl -0x68(%rbp), %edx
addl -0x64(%rbp), %edx
shrl %edx
addl $0x1, %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
addl %ecx, %eax
cvtsi2sd %eax, %xmm0
movsd 0x2b49c(%rip), %xmm1    # 0x311c0
divsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x18(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x60(%rbp), %rdi
movl -0x64(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
movl $0x8, %edx
leaq -0xb0b(%rip), %rcx      # 0x5260
callq 0x20e0
movq -0x60(%rbp), %rax
movq -0x98(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x34(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movq (%rax,%rcx,8), %rax
cvtsi2sd %eax, %xmm0
ucomisd -0x58(%rbp), %xmm0
jbe 0x5da6
movq -0x88(%rbp), %rdi
movl -0x34(%rbp), %eax
movl %eax, %esi
callq 0x5f00
jmp 0x5da8
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5bfb
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x88(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x5e20
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x88(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x88(%rbp), %rsi
movabsq $0x3ffffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFFF
andq (%rsi), %rdx
movl -0x34(%rbp), %esi
addq %rsi, %rdx
movq -0x88(%rbp), %rsi
andq 0x10(%rsi), %rdx
movq (%rcx,%rdx,8), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rdx, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5dbd
jmp 0x5e22
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x98(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x5e99
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, (%rax,%rcx,8)
je 0x5e8c
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rdx
movl -0xc(%rbp), %eax
imulq %rax, %rdx
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x30(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x30(%rbp)
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
jmp 0x5e8e
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5e30
movl -0x30(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, (%rax)
movq -0x98(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x5eca
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x2050
movl -0x48(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x5ecc
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x5776
movq -0x88(%rbp), %rdi
callq 0x61a0
movl -0x48(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x90(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nop