// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, %rbp
movq 0x98(%rsp), %rax
movq 0x90(%rsp), %rcx
movq 0x88(%rsp), %rdx
movq %rdx, 0x28(%rsp)
orq $-0x1, (%rax)
orq $-0x1, (%rcx)
movq 0x8(%rbp), %rbx
leaq 0x1(%rbx), %rax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
pushq $0x64
popq %r15
movq %rbx, 0x38(%rsp)
xorl %r13d, %r13d
movq %rsi, 0x20(%rsp)
movq %rdi, 0x30(%rsp)
movq %r9, 0x8(%rsp)
movl 0x10(%rdi), %ecx
cmpq %rcx, %r13
jae 0x1338d0
movq 0x18(%rdi), %rdx
movq (%rdx,%r13,8), %r11
movq %r15, %r14
cmpq $-0x1, %r11
je 0x133800
movq 0x20(%rsi), %r10
movq %r11, %r8
shrq $0x21, %r8
movl (%r10,%r8,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmpq $-0x1, %r11
movl %r11d, %r8d
cmoveq %r15, %r8
leaq (%r8,%rax), %r15
cmpq $-0x1, %r11
leaq -0x64(%r8,%rax), %r12
cmovneq %r15, %r12
addq %rax, %r14
cmpq %r12, %r14
ja 0x13383c
leaq 0x1(%r13), %rax
cmpq %rcx, %rax
jae 0x13383c
leaq 0x64(%r12), %rax
cmpq $-0x1, 0x8(%rdx,%r13,8)
cmoveq %rax, %r14
pushq $0x8
popq %rdx
movq 0x10(%rsp), %rcx
decq %rcx
je 0x1338c1
movq (%rbp), %rax
movq -0x8(%rax,%rdx), %r8
movq (%rax,%rdx), %r10
cmpq %r10, %r14
cmovbq %r14, %r10
cmpq %r8, %r12
cmovaq %r12, %r8
addq $0x18, %rdx
cmpq %r8, %r10
jbe 0x133844
cmpq 0x10(%rbp), %rbx
jne 0x1338a8
leaq (%rbx,%rbx), %rcx
testq %rbx, %rbx
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%rbp)
imulq $0x18, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r9
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rax, (%rbp)
movq 0x8(%rbp), %rbx
imulq $0x18, %rbx, %rcx
incq %rbx
movq %rbx, 0x8(%rbp)
movq %r12, (%rax,%rcx)
movq %r14, 0x8(%rax,%rcx)
movq %r13, 0x10(%rax,%rcx)
incq %r13
movq %r15, %rax
pushq $0x64
popq %r15
jmp 0x1337cd
movq (%rbp), %r14
movq 0x38(%rsp), %r15
subq %r15, %rbx
je 0x133bb0
movq 0x40(%rsp), %r13
andq $0x0, 0x8(%r13)
decq 0x28(%rsp)
imulq $0x18, %r15, %rax
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rsp)
leaq (%rax,%r14), %r12
addq $0x8, %r12
xorl %edx, %edx
movq 0x18(%rsp), %r14
cmpq 0x80(%rsp), %r9
jae 0x133ac1
movq 0x10(%r14), %rdi
movq %r9, %rax
shlq $0x5, %rax
movq (%rdi,%rax), %r8
leaq (%r8,%r8), %r11
movb 0x48(%r14), %sil
xorl %ecx, %ecx
subb %sil, %cl
shrq %cl, %r11
movq 0x8(%rdi,%rax), %rsi
leaq (%rsi,%rsi), %r10
shrq %cl, %r10
cmpq %r10, %r11
jne 0x133ab9
addq %rax, %rdi
movq 0x50(%r14), %rcx
movq %rcx, %rax
andq %r8, %rax
testq %r8, %r8
js 0x133976
movq 0x18(%rdi), %rdi
movq %rdi, %r8
shrq $0x20, %r8
leaq (%rax,%r8), %rbp
decq %rbp
jmp 0x133991
leaq 0x1(%rax), %r8
movq 0x18(%rdi), %rdi
movq %rdi, %r10
shrq $0x20, %r10
subq %r10, %r8
movl $0x0, %ebp
cmovaeq %r8, %rbp
andq %rsi, %rcx
testq %rsi, %rsi
js 0x1339a3
movl %edi, %esi
addq %rcx, %rsi
decq %rsi
jmp 0x1339b5
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 %rbp, %rax
movq %rbp, %r14
cmovbq %rax, %r14
cmovaq %rax, %rbp
cmpq %rdi, %r14
cmovaeq %rdi, %r14
movq 0x28(%rsp), %rax
cmpq %rax, %r14
cmovaeq %rax, %r14
cmpq %rsi, %rbp
cmovbeq %rsi, %rbp
cmpq %rax, %rbp
cmovaeq %rax, %rbp
incq %rbp
movq %r12, %rax
movq %rbx, %rcx
subq $0x1, %rcx
jb 0x133ab4
cmpq -0x8(%rax), %r14
ja 0x133a10
cmpq (%rax), %rbp
jae 0x133a16
addq $0x18, %rax
jmp 0x1339fb
movq (%r13), %rax
movq 0x10(%r13), %rcx
movq %r9, %r15
cmpq %rcx, %rdx
jne 0x133a50
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%r13)
imulq $0x18, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r13)
movq 0x8(%r13), %rdx
movq 0x10(%r13), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x8(%r13)
imulq $0x18, %rdx, %rdx
leaq (%r14,%r14), %rdi
movq %rdi, (%rax,%rdx)
movq %rbp, 0x8(%rax,%rdx)
cmpq %rcx, %rsi
jne 0x133a94
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r13)
imulq $0x18, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r13)
movq 0x8(%r13), %rsi
movq %r15, %r9
leaq 0x1(%rsi), %rdx
movq %rdx, 0x8(%r13)
imulq $0x18, %rsi, %rcx
leaq 0x1(,%rbp,2), %rsi
movq %rsi, (%rax,%rcx)
movq %r14, 0x8(%rax,%rcx)
movq 0x18(%rsp), %r14
incq %r9
jmp 0x13390e
movq (%r13), %rdi
imulq $0x18, %rdx, %rsi
addq %rdi, %rsi
callq 0x130013
movq 0x8(%rsp), %rcx
movq %rcx, %rbp
movq 0x10(%rsp), %rax
leaq (%rax,%rcx), %r15
addq $0x10, %r15
pushq $-0x1
popq %r14
movq %rbx, %r12
subq $0x1, %r12
jb 0x133b14
movq -0x10(%r15), %rdi
movq -0x8(%r15), %rsi
movq %r13, %rdx
callq 0x1336f1
movl %eax, 0x4(%r15)
cmpq %r14, %rax
cmovbq %rax, %r14
addq $0x18, %r15
jmp 0x133aed
movq 0x10(%rsp), %rax
addq %rbp, %rax
addq $0x10, %rax
pushq $-0x1
popq %rcx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rcx, %rdx
movq 0x20(%rsp), %r11
subq $0x1, %rbx
jb 0x133b85
movq (%rax), %rdi
movq %rdi, %r8
shrq $0x20, %r8
cmpq %r14, %r8
jne 0x133b7f
movl %edi, %edi
movq 0x30(%rsp), %r8
movq 0x18(%r8), %r8
movq (%r8,%rdi,8), %r9
cmpq $-0x1, %r9
je 0x133b71
movq 0x20(%r11), %r8
movq %r9, %r10
shrq $0x21, %r10
movl (%r8,%r10,8), %r8d
andl %esi, %r8d
subl %r9d, %r8d
jmp 0x133b74
xorl %r8d, %r8d
cmpq %rcx, %r8
cmovbq %r8, %rcx
cmovbq %rdi, %rdx
addq $0x18, %rax
jmp 0x133b30
cmpq $-0x1, %rdx
je 0x133ba1
movq 0x90(%rsp), %rax
movq %rdx, (%rax)
movq 0x98(%rsp), %rax
movq %r14, (%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x11f429(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xbbbe9(%rip), %rdi     # 0x1ef7aa
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x8(%rsp), %r9
jmp 0x1338e2