// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/horder.cpp
// Function: get_read_breaks(ma_utg_t*, asg_t*, h_covs*, h_covs*, kvec_pe_hit*, unsigned long, unsigned long, unsigned long, unsigned long*, unsigned long*)
void get_read_breaks(ma_utg_t *u, asg_t* r_g, h_covs *cov, h_covs *hit_tmp, 
kvec_pe_hit *hits, uint64_t sidx, uint64_t eidx, uint64_t ulen, uint64_t *idx, uint64_t *rdp)
{
    (*idx) = (*rdp) = (uint64_t)-1;
    uint64_t i, k, offset, beg, end, n = cov->n, min_ovlp, o, mi, dp, min_dp;
    uint64_t p0s, p0e, p1s, p1e, span_s, span_e;
    h_cov_t *p = NULL, *a = NULL;
    for (i = offset = 0; i < u->n; i++)
    {
        end = offset + ((u->a[i] != (uint64_t)-1?r_g->seq[u->a[i]>>33].len:GAP_LEN));
        offset += (u->a[i] != (uint64_t)-1? (uint32_t)u->a[i]:GAP_LEN);
        beg = offset;

        if(u->a[i] == (uint64_t)-1) beg -= GAP_LEN;
        if(end <= beg && i + 1 < u->n && u->a[i+1] == (uint64_t)-1)
        {
            end = beg + GAP_LEN;
        }
         
        for (k = 0; k < n; k++)
        {
            if(OVL(beg, end, cov->a[k].s, cov->a[k].e) == 0) continue;
            kv_pushp(h_cov_t, *cov, &p);
            p->s = beg; p->e = end; p->dp = i; 
            break;
        }
    }
    a = cov->a + n;
    n = cov->n - n;
    if(n == 0) fprintf(stderr, "ERROR-r-break\n");
    hit_tmp->n = 0; 
    for (i = sidx; i < eidx; i++)///keep all hic hits that contain interval we want
    {
        if(get_hit_suid(*hits, i) != get_hit_euid(*hits, i)) continue;
        p0s = get_hit_spos(*hits, i);
        p0e = get_hit_spos_e(*hits, i);
        p1s = get_hit_epos(*hits, i);
        p1e = get_hit_epos_e(*hits, i);

        span_s = MIN(MIN(p0s, p0e), MIN(p1s, p1e));
        span_s = MIN(span_s, ulen-1);
        span_e = MAX(MAX(p0s, p0e), MAX(p1s, p1e));
        span_e = MIN(span_e, ulen-1) + 1;

        //if(span_e - span_s <= ulen*BREAK_CUTOFF)//need it or not?
        {
            for (k = 0; k < n; k++)
            {
                if(span_s <= a[k].s && span_e >= a[k].e) break;
            }
            if(k >= n) continue;
            kv_pushp(h_cov_t, *hit_tmp, &p);
            p->s = (span_s<<1); p->e = span_e;
            kv_pushp(h_cov_t, *hit_tmp, &p);
            p->s = ((span_e<<1)|1); p->e = span_s;
        }
    }

    radix_sort_h_cov_s(hit_tmp->a, hit_tmp->a+hit_tmp->n);
    for (k = 0, min_dp = (uint64_t)-1; k < n; k++)
    {
        //a[k].s, a[k].e
        dp = update_r_break(a[k].s, a[k].e, hit_tmp);
        a[k].dp = (uint32_t)a[k].dp;
        a[k].dp += (dp << 32);
        if(dp < min_dp) min_dp = dp;
    }

    min_ovlp = mi = (uint64_t)-1;
    for (k = 0; k < n; k++)
    {
        if((a[k].dp>>32) != min_dp) continue;
        i = (uint32_t)a[k].dp;
        o = 0;
        if(u->a[i] != (uint64_t)-1)
        {
            o = r_g->seq[u->a[i]>>33].len - (uint32_t)u->a[i];
        }
        if(o < min_ovlp)
        {
            min_ovlp = o;
            mi = i;
        }
    }
    if(mi != (uint64_t)-1) (*idx) = mi, (*rdp) = min_dp;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x18(%rsp)
movq %rcx, %r13
movq %rsi, 0x30(%rsp)
movq 0x88(%rsp), %rcx
movq 0x80(%rsp), %rsi
movq $-0x1, %rax
movq %rax, (%rcx)
movq %rax, (%rsi)
movq 0x8(%rdx), %r11
movl 0x10(%rdi), %eax
testq %rax, %rax
movq %rdi, 0x28(%rsp)
movq %r13, 0x10(%rsp)
movq %r8, 0x20(%rsp)
je 0x177a86
xorl %r15d, %r15d
movl %eax, %esi
movq %r11, 0x8(%rsp)
xorl %r13d, %r13d
movq %r11, %r12
movq %rdx, %rbx
movq %r13, %r10
movq 0x18(%rdi), %rcx
movq (%rcx,%r15,8), %r11
movl $0x64, %r14d
cmpq $-0x1, %r11
je 0x177670
movq 0x30(%rsp), %rdx
movq 0x20(%rdx), %r8
movq %r11, %r9
shrq $0x21, %r9
movl (%r8,%r9,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmpq $-0x1, %r11
movl %r11d, %r8d
movl $0x64, %edx
cmoveq %rdx, %r8
leaq (%r8,%r10), %r13
cmpq $-0x1, %r11
leaq -0x64(%r8,%r10), %rbp
cmovneq %r13, %rbp
addq %r10, %r14
cmpq %rbp, %r14
jbe 0x1776a1
movq %rbx, %rdx
movq %r12, %r11
jmp 0x1776be
leaq 0x1(%r15), %r8
cmpq %rax, %r8
movq %rbx, %rdx
movq %r12, %r11
jae 0x1776be
leaq 0x64(%rbp), %rax
cmpq $-0x1, 0x8(%rcx,%r15,8)
cmoveq %rax, %r14
testq %r11, %r11
je 0x177758
movq (%rdx), %rax
leaq 0x8(%rax), %rcx
movq %r11, %r9
movq -0x8(%rcx), %r10
movq (%rcx), %r8
cmpq %r8, %r14
cmovbq %r14, %r8
cmpq %r10, %rbp
cmovaq %rbp, %r10
cmpq %r10, %r8
ja 0x1776f6
addq $0x18, %rcx
decq %r9
jne 0x1776d1
jmp 0x177758
movq 0x8(%rsp), %r8
cmpq 0x10(%rdx), %r8
jne 0x17773a
leaq (%r8,%r8), %rcx
testq %r8, %r8
movl $0x2, %esi
cmoveq %rsi, %rcx
movq %rcx, 0x10(%rdx)
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq %r12, %r11
movq %rbx, %rdx
movq 0x28(%rsp), %rdi
movq %rax, (%rbx)
movq 0x8(%rbx), %r8
movl 0x10(%rdi), %esi
leaq (%r8,%r8,2), %rcx
incq %r8
movq %r8, 0x8(%rdx)
movq %rbp, (%rax,%rcx,8)
movq %r14, 0x8(%rax,%rcx,8)
movq %r15, 0x10(%rax,%rcx,8)
movq %r8, 0x8(%rsp)
incq %r15
movl %esi, %eax
cmpq %rax, %r15
jb 0x177638
leaq (%r11,%r11,2), %r15
shlq $0x3, %r15
addq (%rdx), %r15
subq %r11, 0x8(%rsp)
movq 0x18(%rsp), %rcx
je 0x177a9f
xorl %r11d, %r11d
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %r14
movq 0x70(%rsp), %rbx
movq $0x0, 0x8(%r13)
cmpq %rbx, %rcx
movl %r11d, 0x4(%rsp)
jae 0x17798d
movq 0x78(%rsp), %rbp
decq %rbp
leaq 0x8(%r15), %rax
movq %rax, 0x18(%rsp)
xorl %edx, %edx
movq 0x10(%r14), %rdi
movq %rcx, %r12
movq %rcx, %rax
shlq $0x5, %rax
movq (%rdi,%rax), %r8
leaq (%r8,%r8), %r9
xorl %ecx, %ecx
subb 0x48(%r14), %cl
shrq %cl, %r9
movq 0x8(%rdi,%rax), %rsi
leaq (%rsi,%rsi), %r10
shrq %cl, %r10
cmpq %r10, %r9
jne 0x1778bd
addq %rax, %rdi
movq 0x50(%r14), %rcx
movq %rcx, %rax
andq %r8, %rax
testq %r8, %r8
js 0x177816
movq 0x18(%rdi), %rdi
movq %rdi, %r8
shrq $0x20, %r8
leaq (%rax,%r8), %rbx
decq %rbx
jmp 0x177831
leaq 0x1(%rax), %r8
movq 0x18(%rdi), %rdi
movq %rdi, %r9
shrq $0x20, %r9
subq %r9, %r8
movl $0x0, %ebx
cmovaeq %r8, %rbx
andq %rsi, %rcx
testq %rsi, %rsi
js 0x177843
movl %edi, %esi
addq %rcx, %rsi
decq %rsi
jmp 0x177855
leaq 0x1(%rcx), %r8
movl %edi, %esi
subq %rsi, %r8
movl $0x0, %esi
cmovaeq %r8, %rsi
cmpq %rsi, %rcx
movq %rsi, %rdi
cmovbq %rcx, %rdi
cmovaq %rcx, %rsi
cmpq %rbx, %rax
movq %rbx, %r13
cmovbq %rax, %r13
cmovaq %rax, %rbx
cmpq %rdi, %r13
cmovaeq %rdi, %r13
cmpq %rbp, %r13
cmovaeq %rbp, %r13
cmpq %rsi, %rbx
cmovbeq %rsi, %rbx
cmpq %rbp, %rbx
cmovaeq %rbp, %rbx
testb %r11b, %r11b
jne 0x1778b3
incq %rbx
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rcx
cmpq -0x8(%rax), %r13
ja 0x1778aa
cmpq (%rax), %rbx
jae 0x1778d1
addq $0x18, %rax
decq %rcx
jne 0x17789f
movq 0x10(%rsp), %r13
movq 0x70(%rsp), %rbx
movq %r12, %rcx
incq %rcx
cmpq %rbx, %rcx
jne 0x1777bc
jmp 0x17798f
movq 0x10(%rsp), %r14
movq (%r14), %rax
movq 0x10(%r14), %rcx
cmpq %rcx, %rdx
jne 0x177911
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%r14)
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r14)
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%r14)
leaq (%rdx,%rdx,2), %rdx
leaq (,%r13,2), %rdi
movq %rdi, (%rax,%rdx,8)
movq %rbx, 0x8(%rax,%rdx,8)
cmpq %rcx, %rsi
jne 0x17795e
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r14)
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r14)
movq 0x8(%r14), %rsi
movl 0x4(%rsp), %r11d
leaq 0x1(%rsi), %rdx
movq %rdx, 0x8(%r14)
leaq (%rsi,%rsi,2), %rcx
leaq 0x1(,%rbx,2), %rsi
movq %rsi, (%rax,%rcx,8)
movq %r13, 0x8(%rax,%rcx,8)
movq %r14, %r13
movq 0x20(%rsp), %r14
jmp 0x1778b8
xorl %edx, %edx
movq (%r13), %rdi
leaq (%rdx,%rdx,2), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x173cff
cmpb $0x0, 0x4(%rsp)
jne 0x177a77
leaq 0x10(%r15), %r14
movq $-0x1, %rbx
movq 0x8(%rsp), %r12
movq -0x10(%r14), %rdi
movq -0x8(%r14), %rsi
movq %r13, %rdx
callq 0x177568
movl %eax, 0x4(%r14)
cmpq %rbx, %rax
cmovbq %rax, %rbx
addq $0x18, %r14
decq %r12
jne 0x1779bb
cmpb $0x0, 0x4(%rsp)
jne 0x177a77
addq $0x10, %r15
movq $-0x1, %rax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq $-0x1, %rdx
movq 0x30(%rsp), %r10
movq 0x28(%rsp), %r11
movq 0x8(%rsp), %r14
movq (%r15), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
cmpq %rbx, %rdi
jne 0x177a52
movl %esi, %esi
movq 0x18(%r11), %rdi
movq (%rdi,%rsi,8), %r8
cmpq $-0x1, %r8
je 0x177a45
movq 0x20(%r10), %rdi
movq %r8, %r9
shrq $0x21, %r9
movl (%rdi,%r9,8), %edi
andl %ecx, %edi
subl %r8d, %edi
jmp 0x177a47
xorl %edi, %edi
cmpq %rdx, %rdi
cmovbq %rdi, %rdx
cmovbq %rsi, %rax
addq $0x18, %r15
decq %r14
jne 0x177a10
cmpq $-0x1, %rax
je 0x177a77
movq 0x80(%rsp), %rcx
movq %rax, (%rcx)
movq 0x88(%rsp), %rax
movq %rbx, (%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r8, %r14
leaq (%r11,%r11,2), %r15
shlq $0x3, %r15
addq (%rdx), %r15
movq $0x0, 0x8(%rsp)
jmp 0x177aa9
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %r14
movq 0x70(%rsp), %rbx
movq 0x11d52b(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
leaq 0xc5e2b(%rip), %rdi     # 0x23d8ea
movl $0xe, %esi
movl $0x1, %edx
callq 0x2410
movb $0x1, %r11b
movq 0x18(%rsp), %rcx
jmp 0x177793