// File: /workspace/github_cpp_makefile_star2_O3/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 0x7bf3e
xorl %eax, %eax
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r12
movq 0x48(%rsi), %rbx
testq %rbx, %rbx
je 0x7c411
movq %r8, %r14
movq %rdx, %r15
movl 0x8(%r12), %esi
subl 0x4(%r12), %esi
incl %esi
movq 0x8(%r8), %rdx
movl %esi, %esi
cvtsi2sd %rsi, %xmm2
mulsd %xmm0, %xmm2
movl %eax, %eax
cvtsi2sd %rax, %xmm1
ucomisd %xmm2, %xmm1
movq %r8, 0x8(%rsp)
movq %rdx, 0x28(%rsp)
jae 0x7c418
movq %rdi, %r13
movsd %xmm0, 0x20(%rsp)
movq %r15, 0x50(%rsp)
movq 0x20(%rdi), %rax
movq 0x10(%rax), %rsi
movl 0x10(%r12), %edx
movq (%rsi,%rdx,8), %rdx
movl %edx, %esi
movq %rsi, 0x18(%rsp)
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %r15
shlq $0x2, %r15
addq 0x28(%rax), %r15
movq 0x58(%r12), %rax
movslq (%rax), %r8
movq %r8, 0x10(%rsp)
leaq -0x1(%rbx), %rdx
movq %rdx, 0x40(%rsp)
shlq $0x5, %rdx
movq %rcx, 0x30(%rsp)
movslq 0x4(%rax,%rdx), %rbp
incq %rbp
movq %r12, %rsi
movq 0x8(%rsp), %rdx
movq %r8, %rcx
movq %rbp, %r8
callq 0x7bcdc
movq %rbp, (%rsp)
movq 0x30(%rsp), %rbp
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq 0x28(%rsp), %r15
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %rcx
movq %r14, %r8
movq 0x10(%rsp), %r9
callq 0x7be45
movq 0x8(%r14), %rcx
movq %rcx, %rax
subq %r15, %rax
movq %rax, (%rbp)
cmpq %rax, 0x8(%rbp)
jae 0x7c09f
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(%rbp)
movq 0x10(%rbp), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbp)
movq 0x8(%r14), %rcx
movq $0x0, (%rbp)
cmpq %rcx, %r15
jae 0x7c451
movq (%r14), %rcx
movq 0x10(%rbp), %rax
leaq (%r15,%r15,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %r15, %rdx
addq %rdx, %rcx
addq $0x8, %rcx
movl $0x1, %edx
xorl %esi, %esi
movq %r15, %rdi
movsd 0x20(%rsp), %xmm3
movl -0x4(%rcx), %r8d
cmpq %r15, %rdi
jbe 0x7c0ea
cmpl -0x20(%rcx), %r8d
cmovbq %rsi, %rdx
shlq $0x20, %r8
movl (%rcx), %r9d
orq %r8, %r9
movq (%rbp), %r8
leaq 0x1(%r8), %r10
movq %r10, (%rbp)
movq %r9, (%rax,%r8,8)
incq %rdi
addq $0x1c, %rcx
cmpq 0x8(%r14), %rdi
jb 0x7c0d9
movq (%rbp), %r8
testq %rdx, %rdx
jne 0x7c131
movq 0x10(%rbp), %rdi
leaq (%rdi,%r8,8), %rsi
callq 0x45373
movsd 0x20(%rsp), %xmm3
movq (%rbp), %r8
movq %r15, 0x8(%r14)
testq %r8, %r8
je 0x7c4b6
xorl %ecx, %ecx
movq 0x1b5138(%rip), %xmm4   # 0x231280
movapd 0x1b5140(%rip), %xmm5   # 0x231290
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r12, 0x48(%rsp)
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%rbp,8), %rdi
movq %rdi, %r15
shrq $0x20, %r15
cmpq %rbx, %r13
movq %r13, %rax
movq 0x40(%rsp), %r13
cmovgeq %r13, %rax
cmpq %rbx, %rax
jge 0x7c1b3
movq 0x58(%r12), %rdx
movq %rax, %rsi
shlq $0x5, %rsi
addq %rsi, %rdx
addq $0x4, %rdx
movslq (%rdx), %rsi
cmpq %rsi, %r15
jle 0x7c1b0
incq %rax
addq $0x20, %rdx
cmpq %rax, %rbx
jne 0x7c195
movq 0x40(%rsp), %r13
jmp 0x7c1b3
movq %rax, %r13
testq %r13, %r13
js 0x7c1e3
movq %r13, %rax
shlq $0x5, %rax
addq 0x58(%r12), %rax
movslq (%rax), %rdx
cmpq %rdx, %r15
jge 0x7c1e3
leaq -0x1(%r13), %rdx
addq $-0x20, %rax
testq %r13, %r13
movq %rdx, %r13
jg 0x7c1c4
movq $-0x1, %r13
cmpq %rbx, %r13
jge 0x7c3fb
movq %r8, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movl %edi, %esi
movq %rsi, %rax
subq %r15, %rax
movq %r13, %rdi
shlq $0x5, %rdi
addq 0x58(%r12), %rdi
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %edx, %edx
movq %r13, %r9
xorl %r8d, %r8d
movslq (%rdi), %r10
cmpq %r10, %rsi
jle 0x7c261
cmpl $-0x1, 0xc(%rdi)
je 0x7c255
movslq 0x4(%rdi), %r11
incq %r11
cmpq %r10, %r15
cmovgq %r15, %r10
cmpq %r11, %rsi
cmovlq %rsi, %r11
subq %r10, %r11
jbe 0x7c255
cmovbq %rcx, %r11
addq %r11, %r8
cmpq %r14, %r9
cmovlq %r9, %r14
cmpq %rdx, %r9
cmovgq %r9, %rdx
incq %r9
addq $0x20, %rdi
cmpq %r9, %rbx
jne 0x7c218
movq %r8, %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
movq %rax, %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
mulsd %xmm3, %xmm2
ucomisd %xmm2, %xmm1
movq 0x18(%rsp), %r8
jb 0x7c3fb
cmpq %r14, %rdx
jl 0x7c3fb
leaq 0x1(%rdx), %r10
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdi
cmpq 0x28(%rsp), %rdi
movq %r10, 0x38(%rsp)
jbe 0x7c348
leaq (%rdi,%rdi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rdi, %r8
movl -0x18(%rax,%r8), %r11d
movl -0x14(%rax,%r8), %ecx
cmpq %r11, %r15
movq %r11, %r9
cmovaq %r15, %r9
cmpq %rcx, %rsi
movq %rcx, %r10
cmovbq %rsi, %r10
cmpl %r9d, %r10d
movq 0x38(%rsp), %r10
jbe 0x7c348
leaq (%rax,%r8), %r9
cmpl %r11d, %r15d
jae 0x7c30b
movl %r15d, -0x18(%r9)
cmpl %ecx, %esi
jbe 0x7c318
movq 0x10(%rsp), %rcx
movl %ecx, -0x14(%r9)
movl -0xc(%r9), %ecx
cmpq %rcx, %r14
jge 0x7c325
movl %r14d, -0xc(%r9)
movl -0x8(%r9), %ecx
cmpq %rcx, %rdx
jl 0x7c332
movl %r10d, -0x8(%r9)
leaq (%rax,%r8), %rcx
addq $-0x1c, %rcx
testq %rcx, %rcx
movq 0x18(%rsp), %r8
jne 0x7c3fb
movq 0x8(%rsp), %r12
cmpq 0x10(%r12), %rdi
jne 0x7c3a0
leaq (%rdi,%rdi), %rcx
testq %rdi, %rdi
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%r12)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r10
movapd 0x1b4f07(%rip), %xmm5   # 0x231290
movq 0x1b4eef(%rip), %xmm4   # 0x231280
movsd 0x20(%rsp), %xmm3
movq %rax, (%r12)
movq 0x8(%r12), %rdi
leaq 0x1(%rdi), %rcx
movq %rcx, 0x8(%r12)
leaq (%rdi,%rdi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdi, %rcx
movq 0x50(%rsp), %rdx
movl %edx, (%rax,%rcx)
movq 0x48(%rsp), %r12
movl 0x10(%r12), %edx
movl %edx, 0xc(%rax,%rcx)
andb $-0x41, 0x1b(%rax,%rcx)
movl 0x1c(%r12), %edx
shll $0x1f, %edx
movl %edx, 0x18(%rax,%rcx)
movl %r15d, 0x4(%rax,%rcx)
movq 0x10(%rsp), %rdx
movl %edx, 0x8(%rax,%rcx)
movl %r14d, 0x10(%rax,%rcx)
movl %r10d, 0x14(%rax,%rcx)
movq 0x30(%rsp), %rax
movq (%rax), %r8
incq %rbp
cmpq %r8, %rbp
movl $0x0, %ecx
jb 0x7c15a
jmp 0x7c4b6
xorl %eax, %eax
jmp 0x7c4c4
movq (%r14), %rax
cmpq 0x10(%r14), %rdx
jne 0x7c457
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
movl $0x2, %edx
cmovneq %rcx, %rdx
movq %rdx, 0x10(%r14)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r14)
movq 0x8(%r14), %rcx
jmp 0x7c45a
movq %r15, 0x8(%r14)
jmp 0x7c4b6
movq %rdx, %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r14)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl %r15d, (%rax,%rdx)
movl 0x10(%r12), %ecx
movl %ecx, 0xc(%rax,%rdx)
orb $0x40, 0x1b(%rax,%rdx)
movl 0x1c(%r12), %ecx
shll $0x1f, %ecx
orl $0x40000000, %ecx       # imm = 0x40000000
movl %ecx, 0x18(%rax,%rdx)
movl 0x4(%r12), %ecx
movl %ecx, 0x4(%rax,%rdx)
movl 0x8(%r12), %ecx
incl %ecx
movl %ecx, 0x8(%rax,%rdx)
movl $0x0, 0x10(%rax,%rdx)
movl 0x48(%r12), %ecx
movl %ecx, 0x14(%rax,%rdx)
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
subq 0x28(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq