// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movl 0x28(%rsi), %eax
cmpq %r9, %rax
jb 0x49003
movq %rsi, %r14
movq 0x48(%rsi), %r15
testq %r15, %r15
je 0x49003
movl 0x8(%r14), %ecx
subl 0x4(%r14), %ecx
incl %ecx
movl %ecx, %ecx
cvtsi2sd %rcx, %xmm2
movq 0x8(%r8), %rbx
movl %eax, %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
movq %r8, 0x8(%rsp)
movq %rbx, 0x28(%rsp)
jae 0x4900a
movq %rdi, %r13
movsd %xmm0, 0x20(%rsp)
movq 0x20(%rdi), %rax
movq 0x10(%rax), %rcx
movl 0x10(%r14), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
movq %rdx, 0x18(%rsp)
shrq $0x20, %rcx
imulq $0xc, %rcx, %rcx
addq 0x28(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x58(%r14), %rax
movslq (%rax), %r12
leaq -0x1(%r15), %rcx
movq %rcx, 0x40(%rsp)
shlq $0x5, %rcx
movslq 0x4(%rax,%rcx), %rbp
incq %rbp
movq %r14, %rsi
movq %r8, %rdx
movq %r12, %rcx
movq %rbp, %r8
callq 0x48c21
movq %rbp, (%rsp)
movq %r13, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq 0x8(%rsp), %r8
movq %r12, %r9
callq 0x48d5f
movq 0x8(%rsp), %r8
movq 0x8(%r8), %rax
subq %rbx, %rax
movq 0x38(%rsp), %r13
movq %rax, (%r13)
cmpq %rax, 0x8(%r13)
jae 0x48fa0
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(%r13)
movq 0x10(%r13), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r13)
andq $0x0, (%r13)
imulq $0x1c, %rbx, %rax
addq $0x8, %rax
pushq $0x1
popq %rcx
xorl %edx, %edx
movq %rbx, %rsi
movsd 0x20(%rsp), %xmm3
cmpq 0x8(%r8), %rsi
jae 0x49046
movq (%r8), %r9
movl -0x4(%r9,%rax), %edi
cmpq %rbx, %rsi
jbe 0x48fdb
cmpl -0x20(%r9,%rax), %edi
cmovbq %rdx, %rcx
shlq $0x20, %rdi
movl (%r9,%rax), %r11d
orq %rdi, %r11
movq (%r13), %rdi
movq 0x10(%r13), %r9
leaq 0x1(%rdi), %r10
movq %r10, (%r13)
movq %r11, (%r9,%rdi,8)
incq %rsi
addq $0x1c, %rax
jmp 0x48fbb
xorl %eax, %eax
jmp 0x49348
movq (%r8), %rax
cmpq 0x10(%r8), %rbx
jne 0x492eb
leaq (%rbx,%rbx), %rcx
testq %rbx, %rbx
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%r8)
imulq $0x1c, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, (%r8)
movq 0x8(%r8), %rcx
jmp 0x492ee
testq %rcx, %rcx
jne 0x49067
movq (%r13), %rax
movq 0x10(%r13), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1905d
movsd 0x20(%rsp), %xmm3
movq 0x8(%rsp), %r8
movq %rbx, 0x8(%r8)
movq 0x19a1fd(%rip), %xmm4   # 0x1e3270
movapd 0x19a205(%rip), %xmm5   # 0x1e3280
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq (%r13), %rbp
jae 0x4933f
movq 0x10(%r13), %rax
movq (%rax,%rbp,8), %rax
movq %rax, %r13
shrq $0x20, %r13
movq %rax, 0x18(%rsp)
movl %eax, %ecx
movq %rcx, %rax
subq %r13, %rax
cmpq %r15, %r12
cmovgeq 0x40(%rsp), %r12
movq %r12, %rsi
shlq $0x5, %rsi
orq $0x4, %rsi
movq %r12, %rdx
cmpq %r15, %rdx
jge 0x490d8
movq 0x58(%r14), %rdi
movslq (%rdi,%rsi), %rdi
cmpq %rdi, %r13
jle 0x490dd
incq %rdx
addq $0x20, %rsi
jmp 0x490bd
movq 0x40(%rsp), %rdx
cmpq $-0x1, %rdx
pushq $-0x1
popq %r12
cmovlq %rdx, %r12
movq %rdx, %rsi
shlq $0x5, %rsi
testq %rdx, %rdx
js 0x4910e
movq 0x58(%r14), %rdi
movslq (%rdi,%rsi), %rdi
cmpq %rdi, %r13
jge 0x4910b
decq %rdx
addq $-0x20, %rsi
jmp 0x490f0
movq %rdx, %r12
movq %r12, %rdi
shlq $0x5, %rdi
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorl %esi, %esi
movq %r12, %r11
xorl %edx, %edx
cmpq %r15, %r11
jge 0x4917a
movq 0x58(%r14), %r10
movslq (%r10,%rdi), %r9
cmpq %r9, %rcx
jle 0x4917a
cmpl $-0x1, 0xc(%r10,%rdi)
je 0x49171
movslq 0x4(%r10,%rdi), %r10
incq %r10
cmpq %r9, %r13
cmovgq %r13, %r9
cmpq %r10, %rcx
cmovlq %rcx, %r10
subq %r9, %r10
jbe 0x49171
movl $0x0, %r9d
cmovbq %r9, %r10
addq %r10, %rsi
cmpq %rbx, %r11
cmovlq %r11, %rbx
cmpq %rdx, %r11
cmovgq %r11, %rdx
incq %r11
addq $0x20, %rdi
jmp 0x49121
movq %rax, %xmm0
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
subpd %xmm5, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd %xmm3, %xmm1
movq %rsi, %xmm0
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
subpd %xmm5, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jb 0x492de
cmpq %rbx, %rdx
jl 0x492de
leaq 0x1(%rdx), %rax
movq %rax, 0x10(%rsp)
movq (%r8), %rax
movq 0x8(%r8), %rsi
cmpq 0x28(%rsp), %rsi
jbe 0x49251
imulq $0x1c, %rsi, %rdi
movl -0x18(%rax,%rdi), %r10d
movl -0x14(%rax,%rdi), %r9d
cmpq %r10, %r13
movq %r10, %r8
cmovaq %r13, %r8
cmpq %r9, %rcx
movq %r9, %r11
cmovbq %rcx, %r11
cmpl %r8d, %r11d
movq 0x8(%rsp), %r8
jbe 0x49251
leaq (%rax,%rdi), %r11
cmpl %r10d, %r13d
jae 0x49213
movl %r13d, -0x18(%r11)
cmpl %r9d, %ecx
jbe 0x49221
movq 0x18(%rsp), %rcx
movl %ecx, -0x14(%r11)
movl -0xc(%r11), %ecx
cmpq %rcx, %rbx
jge 0x4922e
movl %ebx, -0xc(%r11)
movl -0x8(%r11), %ecx
cmpq %rcx, %rdx
jl 0x49240
movq 0x10(%rsp), %rcx
movl %ecx, -0x8(%r11)
leaq (%rax,%rdi), %rcx
addq $-0x1c, %rcx
testq %rcx, %rcx
jne 0x492de
cmpq 0x10(%r8), %rsi
jne 0x49297
leaq (%rsi,%rsi), %rcx
testq %rsi, %rsi
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%r8)
imulq $0x1c, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movapd 0x19a003(%rip), %xmm5   # 0x1e3280
movq 0x199feb(%rip), %xmm4   # 0x1e3270
movsd 0x20(%rsp), %xmm3
movq 0x8(%rsp), %r8
movq %rax, (%r8)
movq 0x8(%r8), %rsi
leaq 0x1(%rsi), %rcx
movq %rcx, 0x8(%r8)
imulq $0x1c, %rsi, %rcx
movq 0x30(%rsp), %rdx
movl %edx, (%rax,%rcx)
movl 0x10(%r14), %edx
movl %edx, 0xc(%rax,%rcx)
andb $-0x41, 0x1b(%rax,%rcx)
movl 0x1c(%r14), %edx
shll $0x1f, %edx
movl %edx, 0x18(%rax,%rcx)
movl %r13d, 0x4(%rax,%rcx)
movq 0x18(%rsp), %rdx
movl %edx, 0x8(%rax,%rcx)
movl %ebx, 0x10(%rax,%rcx)
movq 0x10(%rsp), %rdx
movl %edx, 0x14(%rax,%rcx)
incq %rbp
movq 0x38(%rsp), %r13
jmp 0x49080
movq %rbx, %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r8)
imulq $0x1c, %rcx, %rcx
movq 0x30(%rsp), %rdx
movl %edx, (%rax,%rcx)
movl 0x10(%r14), %edx
movl %edx, 0xc(%rax,%rcx)
orb $0x40, 0x1b(%rax,%rcx)
movl 0x1c(%r14), %edx
shll $0x1f, %edx
orl $0x40000000, %edx       # imm = 0x40000000
movl %edx, 0x18(%rax,%rcx)
movl 0x4(%r14), %edx
movl %edx, 0x4(%rax,%rcx)
movl 0x8(%r14), %edx
incl %edx
movl %edx, 0x8(%rax,%rcx)
andl $0x0, 0x10(%rax,%rcx)
movl 0x48(%r14), %edx
movl %edx, 0x14(%rax,%rcx)
movq 0x8(%r8), %rax
subq 0x28(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq