// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_r_aln(ul_idx_t const*, overlap_region*, unsigned long, asg64_v*, double, kv_ul_ov_t*, unsigned long)
uint64_t gen_r_aln(const ul_idx_t *udb, overlap_region *z, uint64_t zid, asg64_v *idx, double o_rate, kv_ul_ov_t *aln, uint64_t min_ovlp)
{
    uint64_t ol = z->x_pos_e+1-z->x_pos_s, aln_ol = z->align_length, k, cn, is_srt; 
    ul_ov_t *p; ul_contain *ct = udb->ct; utg_ct_t *ca; uint64_t os, oe, ovlp, salnl, sol, raln = aln->n;
    int64_t sk, ek, wn, s, e, ws, we, minw, maxw;
    if((aln_ol < min_ovlp) || (!(z->w_list.n))) return 0;
    if((ol*o_rate) <= aln_ol) {
        kv_pushp(ul_ov_t, *aln, &p);
        p->qn = zid; p->tn = z->y_id; p->el = 1; p->rev = z->y_pos_strand; p->sec = 0;
        p->qs = z->x_pos_s; p->qe = z->x_pos_e+1; //[qs, qe)
        p->ts = 0; p->te = z->w_list.n; //[ts, te)
    } else {
        cn = ((uint32_t)(ct->idx.a[z->y_id])); 
        ca = ct->rids.a + ((ct->idx.a[z->y_id])>>32);
        wn = z->w_list.n; 
        ws = z->w_list.a[0].x_start; 
        we = z->w_list.a[z->w_list.n-1].x_end+1;

        gen_sub_ov(udb, z, aln, ws, we); gen_conta_ov(udb, z, ca, cn, aln, ws, we);

        idx->n = aln->n-raln; kv_resize(uint64_t, *idx, idx->n);
        for (k = raln, idx->n = 0, is_srt = 1; k < aln->n; k++) {
            if(k > raln && aln->a[k].qs < aln->a[k-1].qs) is_srt = 0;
            idx->a[idx->n++] = (((uint64_t)(aln->a[k].qs))<<32)|((uint64_t)(aln->a[k].qe));
        }
        if(!is_srt) radix_sort_bc64(idx->a, idx->a+idx->n);
        aln->n = raln;

        for (k = sk = ek = 0; k < idx->n; k++) {///idx->a is sorted by s, not by e
            s = idx->a[k]>>32; e = (uint32_t)idx->a[k]; 
            salnl = 0; sol = e - s; minw = INT32_MAX; maxw = 0;
            for (sk = ((sk<wn)?(sk):(wn-1)); sk < wn && s > z->w_list.a[sk].x_end; sk++); 
            for (sk = ((sk<wn)?(sk):(wn-1)); sk >= 0 && s < z->w_list.a[sk].x_start; sk--); 
            if(k < 0) k = 0; ///s >= z->w_list.a[sk].x_start && s <= z->w_list.a[sk].x_end            
            for (ek = sk; ek < wn; ek++) {
                ws = z->w_list.a[ek].x_start;
                we = z->w_list.a[ek].x_end + 1;
                if(ws >= e) break;
                if(z->w_list.a[ek].y_end == -1) continue;
                os = MAX(s, ws); oe = MIN(e, we);
                ovlp = ((oe>os)? (oe-os):0);
                if(!ovlp) continue;
                salnl += ovlp; 
                if(ek < minw) minw = ek;
                if(ek > maxw) maxw = ek;
            }
            p = NULL; 
            if(((sol*o_rate)<=salnl) && (maxw >= minw)) {
                ws = z->w_list.a[minw].x_start; we = z->w_list.a[maxw].x_end + 1; maxw++;
                if(aln->n > raln) {
                    p = &(aln->a[aln->n-1]);
                    os = MAX(s, p->qs); oe = MIN(e, p->qe);
                    if(oe>os) {
                        if(s < p->qs) p->qs = s;
                        if(e > p->qe) p->qe = e;
                        if(minw < p->ts) p->ts = minw;
                        if(maxw > p->te) p->te = maxw;
                    } else {
                        p = NULL;
                    }
                }
                if(!p) {
                    kv_pushp(ul_ov_t, *aln, &p);
                    p->qn = zid; p->tn = z->y_id; p->el = 0; 
                    p->rev = z->y_pos_strand; p->sec = 0;
                    p->qs = s; p->qe = e; ///[qs, qe)
                    p->ts = minw; p->te = maxw; ///[ts, te)
                }
            }
        }
    }

    return aln->n - raln;
}

// Assembly:
movl 0x28(%rsi), %eax
cmpq %r9, %rax
jae 0x7c3ae
xorl %eax, %eax
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0x48(%rsi), %r13
testq %r13, %r13
je 0x7c896
movq %rcx, %r12
movq %rdx, %rbx
movl 0x8(%rsi), %ecx
subl 0x4(%rsi), %ecx
incl %ecx
movq 0x8(%r8), %r14
movl %ecx, %ecx
cvtsi2sd %rcx, %xmm2
mulsd %xmm0, %xmm2
movl %eax, %eax
cvtsi2sd %rax, %xmm1
ucomisd %xmm2, %xmm1
movq %r8, 0x8(%rsp)
movq %rsi, 0x18(%rsp)
movq %r14, 0x28(%rsp)
jae 0x7c89d
movsd %xmm0, 0x20(%rsp)
movq %rbx, 0x48(%rsp)
movq 0x20(%rdi), %rax
movq 0x10(%rax), %rcx
movl 0x10(%rsi), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
movq %rdx, 0x10(%rsp)
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r15
shlq $0x2, %r15
addq 0x28(%rax), %r15
movq 0x58(%rsi), %rax
movslq (%rax), %r9
movq %r9, 0x40(%rsp)
leaq -0x1(%r13), %rcx
movq %rcx, 0x30(%rsp)
shlq $0x5, %rcx
movslq 0x4(%rax,%rcx), %rbp
incq %rbp
movq %rdi, 0x38(%rsp)
movq %rsi, %rbx
movq %r8, %rdx
movq %r9, %rcx
movq %rbp, %r8
callq 0x7c170
movq %rbp, (%rsp)
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0x7c2bb
movq 0x8(%rsp), %r8
movq 0x8(%r8), %rax
subq %r14, %rax
movq %rax, (%r12)
cmpq %rax, 0x8(%r12)
jae 0x7c501
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r12)
movq 0x10(%r12), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r12)
movq $0x0, (%r12)
cmpq 0x8(%r8), %r14
movsd 0x20(%rsp), %xmm3
jae 0x7c59c
movq (%r8), %rax
movq 0x10(%r12), %rcx
leaq (%r14,%r14,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r14, %rdx
movl $0x1, %esi
xorl %edi, %edi
movq %r14, %rbx
cmpq %r14, %rbx
jbe 0x7c54c
movq (%r8), %r9
movl 0x4(%r9,%rdx), %r10d
cmpl -0x18(%r9,%rdx), %r10d
cmovbq %rdi, %rsi
movl 0x4(%rax,%rdx), %r9d
movl 0x8(%rax,%rdx), %r10d
shlq $0x20, %r9
orq %r9, %r10
movq (%r12), %r9
leaq 0x1(%r9), %r11
movq %r11, (%r12)
movq %r10, (%rcx,%r9,8)
incq %rbx
addq $0x1c, %rdx
cmpq 0x8(%r8), %rbx
jb 0x7c536
testq %rsi, %rsi
jne 0x7c59c
movq (%r12), %rax
movq 0x10(%r12), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x45d47
movsd 0x20(%rsp), %xmm3
movq 0x8(%rsp), %r8
movq %r14, 0x8(%r8)
cmpq $0x0, (%r12)
movq 0x18(%rsp), %rsi
je 0x7c932
xorl %r11d, %r11d
movq 0x1a8c85(%rip), %xmm4   # 0x225240
movapd 0x1a8c8d(%rip), %xmm5   # 0x225250
xorl %ebp, %ebp
xorl %ebx, %ebx
movq %r12, 0x50(%rsp)
movq 0x10(%r12), %rax
movq (%rax,%rbp,8), %r14
movq %r14, %r15
shrq $0x20, %r15
cmpq %r13, %rbx
movq %rbx, %rax
movq 0x30(%rsp), %rbx
cmovgeq %rbx, %rax
cmpq %r13, %rax
jge 0x7c620
movq 0x58(%rsi), %rcx
movq %rax, %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
addq $0x4, %rcx
movslq (%rcx), %rdx
cmpq %rdx, %r15
jle 0x7c61d
incq %rax
addq $0x20, %rcx
cmpq %rax, %r13
jne 0x7c602
movq 0x30(%rsp), %rbx
jmp 0x7c620
movq %rax, %rbx
testq %rbx, %rbx
js 0x7c64f
movq %rbx, %rax
shlq $0x5, %rax
addq 0x58(%rsi), %rax
movslq (%rax), %rcx
cmpq %rcx, %r15
jge 0x7c64f
leaq -0x1(%rbx), %rcx
addq $-0x20, %rax
testq %rbx, %rbx
movq %rcx, %rbx
jg 0x7c630
movq $-0x1, %rbx
movl %r14d, %eax
cmpq %r13, %rbx
jge 0x7c6dc
movq %rbx, %rdx
shlq $0x5, %rdx
addq 0x58(%rsi), %rdx
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movq %rbx, %rdi
xorl %esi, %esi
movslq (%rdx), %r10
cmpq %r10, %rax
jle 0x7c6bc
cmpl $-0x1, 0xc(%rdx)
je 0x7c6b0
movslq 0x4(%rdx), %r9
incq %r9
cmpq %r10, %r15
cmovgq %r15, %r10
cmpq %r9, %rax
cmovlq %rax, %r9
subq %r10, %r9
jbe 0x7c6b0
cmovbq %r11, %r9
addq %r9, %rsi
cmpq %r12, %rdi
cmovlq %rdi, %r12
cmpq %rcx, %rdi
cmovgq %rdi, %rcx
incq %rdi
addq $0x20, %rdx
cmpq %rdi, %r13
jne 0x7c673
movq %rsi, %xmm1
punpckldq %xmm4, %xmm1    # xmm1 = xmm1[0],xmm4[0],xmm1[1],xmm4[1]
subpd %xmm5, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movq 0x18(%rsp), %rsi
jmp 0x7c6e8
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movq %rax, %rdx
subq %r15, %rdx
movq %rdx, %xmm1
punpckldq %xmm4, %xmm1    # xmm1 = xmm1[0],xmm4[0],xmm1[1],xmm4[1]
subpd %xmm5, %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd %xmm3, %xmm2
ucomisd %xmm2, %xmm0
jb 0x7c87f
cmpq %r12, %rcx
jl 0x7c87f
leaq 0x1(%rcx), %rdi
movq 0x8(%r8), %rdx
cmpq 0x28(%rsp), %rdx
jbe 0x7c7b3
movq %rdi, 0x10(%rsp)
movq (%r8), %rsi
leaq (%rdx,%rdx,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rdx, %rdi
movl -0x18(%rsi,%rdi), %r10d
movl -0x14(%rsi,%rdi), %r9d
cmpq %r10, %r15
movq %r10, %r8
cmovaq %r15, %r8
cmpq %r9, %rax
movq %r9, %r11
cmovbq %rax, %r11
cmpl %r8d, %r11d
jbe 0x7c7b7
leaq (%rsi,%rdi), %r11
cmpl %r10d, %r15d
jae 0x7c774
movl %r15d, -0x18(%r11)
cmpl %r9d, %eax
movq 0x8(%rsp), %r8
jbe 0x7c782
movl %r14d, -0x14(%r11)
movl -0xc(%r11), %eax
cmpq %rax, %r12
jge 0x7c78f
movl %r12d, -0xc(%r11)
leaq (%rsi,%rdi), %rax
addq $-0x1c, %rax
movl -0x8(%r11), %esi
cmpq %rsi, %rcx
jl 0x7c7be
movq 0x10(%rsp), %rdi
movl %edi, -0x8(%r11)
movq 0x18(%rsp), %rsi
xorl %r11d, %r11d
jmp 0x7c7cb
xorl %eax, %eax
jmp 0x7c7cb
xorl %eax, %eax
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %rsi
xorl %r11d, %r11d
movq 0x10(%rsp), %rdi
testq %rax, %rax
jne 0x7c87f
movq 0x10(%r8), %rax
cmpq %rax, %rdx
jne 0x7c834
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%r8)
movq %rdi, 0x10(%rsp)
movq (%r8), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq 0x10(%rsp), %rdi
movapd 0x1a8a3a(%rip), %xmm5   # 0x225250
movq 0x1a8a22(%rip), %xmm4   # 0x225240
xorl %r11d, %r11d
movsd 0x20(%rsp), %xmm3
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %r8
movq %rax, (%r8)
movq (%r8), %rax
movq 0x8(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movq 0x48(%rsp), %rcx
movl %ecx, (%rax,%rdx)
movl 0x10(%rsi), %ecx
movl %ecx, 0xc(%rax,%rdx)
andb $-0x41, 0x1b(%rax,%rdx)
movl 0x1c(%rsi), %ecx
shll $0x1f, %ecx
movl %ecx, 0x18(%rax,%rdx)
movl %r15d, 0x4(%rax,%rdx)
movl %r14d, 0x8(%rax,%rdx)
movl %r12d, 0x10(%rax,%rdx)
movl %edi, 0x14(%rax,%rdx)
incq %rbp
movq 0x50(%rsp), %r12
cmpq (%r12), %rbp
jb 0x7c5cc
jmp 0x7c932
xorl %eax, %eax
jmp 0x7c93b
movq 0x10(%r8), %rax
cmpq %rax, %r14
jne 0x7c8da
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmovneq %rcx, %rax
movq %rax, 0x10(%r8)
movq (%r8), %rdi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rax, %rsi
callq 0x2370
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %r8
movq %rax, (%r8)
movq (%r8), %rax
movq 0x8(%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl %ebx, (%rax,%rdx)
movl 0x10(%rsi), %ecx
movl %ecx, 0xc(%rax,%rdx)
orb $0x40, 0x1b(%rax,%rdx)
movl 0x1c(%rsi), %ecx
shll $0x1f, %ecx
orl $0x40000000, %ecx       # imm = 0x40000000
movl %ecx, 0x18(%rax,%rdx)
movl 0x4(%rsi), %ecx
movl %ecx, 0x4(%rax,%rdx)
movl 0x8(%rsi), %ecx
incl %ecx
movl %ecx, 0x8(%rax,%rdx)
movl $0x0, 0x10(%rax,%rdx)
movl 0x48(%rsi), %ecx
movl %ecx, 0x14(%rax,%rdx)
movq 0x8(%r8), %rax
subq 0x28(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq