// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %r9, %r12
movq %r8, 0x30(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %rbp
movq 0xa8(%rsp), %rcx
movq 0xa0(%rsp), %rdx
movq $-0x1, %rax
movq %rax, (%rcx)
movq %rax, (%rdx)
movq 0x8(%rbp), %r11
movl 0x10(%rdi), %eax
testq %rax, %rax
movq %rsi, 0x40(%rsp)
movq %rdi, 0x38(%rsp)
movq %r11, 0x10(%rsp)
je 0x170b56
movq %rdi, %r10
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %r13, %rdx
movq 0x18(%r10), %rcx
movq (%rcx,%rbx,8), %r9
movl $0x64, %r14d
cmpq $-0x1, %r9
je 0x170a6c
movq 0x20(%rsi), %rdi
movq %r9, %r8
shrq $0x21, %r8
movl (%rdi,%r8,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmpq $-0x1, %r9
movl %r9d, %edi
movl $0x64, %r8d
cmoveq %r8, %rdi
leaq (%rdi,%rdx), %r13
cmpq $-0x1, %r9
leaq -0x64(%rdi,%rdx), %r15
cmovneq %r13, %r15
addq %rdx, %r14
cmpq %r15, %r14
ja 0x170aad
leaq 0x1(%rbx), %rdx
cmpq %rax, %rdx
jae 0x170aad
leaq 0x64(%r15), %rax
cmpq $-0x1, 0x8(%rcx,%rbx,8)
cmoveq %rax, %r14
testq %r11, %r11
je 0x170b46
movq (%rbp), %rdi
leaq 0x8(%rdi), %rax
movq %r11, %rcx
movq -0x8(%rax), %rdx
movq (%rax), %r8
cmpq %r8, %r14
cmovbq %r14, %r8
cmpq %rdx, %r15
cmovaq %r15, %rdx
cmpq %rdx, %r8
ja 0x170ae6
addq $0x18, %rax
decq %rcx
jne 0x170ac1
jmp 0x170b46
movq 0x10(%rbp), %rax
cmpq %rax, 0x8(%rbp)
jne 0x170b24
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rbp)
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x10(%rsp), %r11
movq 0x38(%rsp), %r10
movq 0x40(%rsp), %rsi
movq %rax, (%rbp)
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbp)
leaq (%rcx,%rcx,2), %rcx
movq %r15, (%rax,%rcx,8)
movq %r14, 0x8(%rax,%rcx,8)
movq %rbx, 0x10(%rax,%rcx,8)
incq %rbx
movl 0x10(%r10), %eax
cmpq %rax, %rbx
jb 0x170a3f
movq 0x90(%rsp), %r15
movq (%rbp), %rax
movq %rax, 0x28(%rsp)
movq 0x8(%rbp), %rbp
movq %rbp, 0x18(%rsp)
subq %r11, %rbp
je 0x170ed5
movq 0x8(%rsp), %rax
movq $0x0, 0x8(%rax)
leaq (%r11,%r11,2), %rax
movq %rax, 0x20(%rsp)
cmpq %r15, %r12
movq 0x30(%rsp), %r10
jae 0x170db9
movq 0x98(%rsp), %rax
decq %rax
movq %rax, 0x50(%rsp)
movq %r11, %rbx
subq 0x18(%rsp), %rbx
setne 0x7(%rsp)
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
leaq (%rax,%rcx,8), %rax
addq $0x8, %rax
movq %rax, 0x48(%rsp)
movq 0x10(%r10), %rdx
movq %r12, %rax
shlq $0x5, %rax
movq (%rdx,%rax), %rdi
leaq (%rdi,%rdi), %r8
xorl %ecx, %ecx
subb 0x48(%r10), %cl
shrq %cl, %r8
movq 0x8(%rdx,%rax), %rsi
leaq (%rsi,%rsi), %r9
shrq %cl, %r9
cmpq %r9, %r8
jne 0x170dad
addq %rax, %rdx
movq 0x50(%r10), %rcx
movq %rcx, %rax
andq %rdi, %rax
testq %rdi, %rdi
js 0x170c20
movl 0x1c(%rdx), %edi
leaq (%rax,%rdi), %r14
decq %r14
jmp 0x170c35
leaq 0x1(%rax), %rdi
movl 0x1c(%rdx), %r8d
subq %r8, %rdi
movl $0x0, %r14d
cmovaeq %rdi, %r14
andq %rsi, %rcx
testq %rsi, %rsi
js 0x170c48
movl 0x18(%rdx), %edx
addq %rcx, %rdx
decq %rdx
jmp 0x170c5b
leaq 0x1(%rcx), %rsi
movl 0x18(%rdx), %edx
subq %rdx, %rsi
movl $0x0, %edx
cmovaeq %rsi, %rdx
cmpq %rdx, %rcx
movq %rdx, %rsi
cmovbq %rcx, %rsi
cmovaq %rcx, %rdx
cmpq %r14, %rax
movq %r14, %r13
cmovbq %rax, %r13
cmovaq %rax, %r14
cmpq %rsi, %r13
cmovaeq %rsi, %r13
movq 0x50(%rsp), %rax
cmpq %rax, %r13
cmovaeq %rax, %r13
cmpq %rdx, %r14
cmovbeq %rdx, %r14
cmpq %rax, %r14
cmovaeq %rax, %r14
cmpq %r11, 0x18(%rsp)
je 0x170dad
incq %r14
movl $0x1, %eax
movq 0x48(%rsp), %rcx
movb 0x7(%rsp), %dl
cmpq -0x8(%rcx), %r13
ja 0x170cbf
cmpq (%rcx), %r14
jae 0x170cde
cmpq %rbp, %rax
setb %dl
leaq (%rbx,%rax), %rsi
incq %rsi
incq %rax
addq $0x18, %rcx
cmpq $0x1, %rsi
jne 0x170cb4
jmp 0x170dad
testb $0x1, %dl
je 0x170dad
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rax
cmpq %rax, 0x8(%rsi)
jne 0x170d2c
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rsi)
movq (%rsi), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x8(%rsp), %rsi
movq 0x30(%rsp), %r10
movq 0x10(%rsp), %r11
movq %rax, (%rsi)
movq (%rsi), %rdi
movq 0x8(%rsi), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rsi)
leaq (%rax,%rax,2), %rax
leaq (%r13,%r13), %rdx
movq %rdx, (%rdi,%rax,8)
movq %r14, 0x8(%rdi,%rax,8)
movq 0x10(%rsi), %rax
cmpq %rax, %rcx
jne 0x170d89
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rsi)
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq 0x8(%rsp), %rsi
movq 0x30(%rsp), %r10
movq 0x10(%rsp), %r11
movq %rax, (%rsi)
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rsi)
leaq (%rcx,%rcx,2), %rcx
leaq 0x1(,%r14,2), %rdx
movq %rdx, (%rax,%rcx,8)
movq %r13, 0x8(%rax,%rcx,8)
incq %r12
cmpq %r15, %r12
jne 0x170bd1
movq 0x8(%rsp), %r13
movq (%r13), %rdi
movq 0x8(%r13), %rax
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
movq %r11, %r14
callq 0x16d2c9
movq $-0x1, %rbx
movq $-0x1, %r15
cmpq %r14, 0x18(%rsp)
je 0x170e24
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
leaq (%rax,%rcx,8), %r14
addq $0x10, %r14
movq %rbp, %r12
movq -0x10(%r14), %rdi
movq -0x8(%r14), %rsi
movq %r13, %rdx
callq 0x170971
movl %eax, 0x4(%r14)
cmpq %r15, %rax
cmovbq %rax, %r15
addq $0x18, %r14
decq %r12
jne 0x170e00
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
movq 0x40(%rsp), %r10
movq 0x38(%rsp), %r11
je 0x170eaa
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
leaq (%rax,%rcx,8), %rax
addq $0x10, %rax
movq $-0x1, %rbx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq $-0x1, %rdx
movq (%rax), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
cmpq %r15, %rdi
jne 0x170ea1
movl %esi, %esi
movq 0x18(%r11), %rdi
movq (%rdi,%rsi,8), %r8
cmpq $-0x1, %r8
je 0x170e94
movq 0x20(%r10), %rdi
movq %r8, %r9
shrq $0x21, %r9
movl (%rdi,%r9,8), %edi
andl %ecx, %edi
subl %r8d, %edi
jmp 0x170e96
xorl %edi, %edi
cmpq %rdx, %rdi
cmovbq %rdi, %rdx
cmovbq %rsi, %rbx
addq $0x18, %rax
decq %rbp
jne 0x170e5f
cmpq $-0x1, %rbx
je 0x170ec6
movq 0xa0(%rsp), %rax
movq %rbx, (%rax)
movq 0xa8(%rsp), %rax
movq %r15, (%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x229f8
movq 0x10(%rsp), %r11
jmp 0x170b79