// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: extract_subov_cigar(ul_idx_t const*, char*, UC_Read*, bit_extz_t*, long, long, overlap_region*, double, long*, long, double, unsigned long, unsigned long, rtrace_t*, ul_ov_t*)
int64_t extract_subov_cigar(const ul_idx_t *uref, char* qstr, UC_Read *tu, bit_extz_t *exz,
int64_t ts0, int64_t te0, overlap_region *o, double o_rate, int64_t *in_k, int64_t ql, 
double e_rate, uint64_t rid, uint64_t dbg_id, rtrace_t *trace, ul_ov_t *res)
{
    int64_t rev = o->y_pos_strand, t[2], q[2], k = 0, wts, wte, ii[2];
    int64_t wn = o->w_list.n, os, oe, ovlp, salnl = 0;
    t[0] = ts0; t[1] = te0; if(wn <= 0) return 0;

    if(in_k) k = *in_k;
    if(k < 0) k = 0; if(k >= wn) k = wn-1;
    for(; k < wn && t[0] > o->w_list.a[k].y_end; k++);
    if(k < 0) k = 0; if(k >= wn) k = wn-1;
    for(; k >= 0 && t[0] < o->w_list.a[k].y_start; k--);
    ///qs <= o->w_list.a[k].x_end && qs >= o->w_list.a[k].x_start
    if(k < 0) k = 0;
    if(in_k) *in_k = k;

    for (ii[0] = INT32_MAX, ii[1] = -1; k < wn; k++) {
        wts = o->w_list.a[k].y_start;
        wte = o->w_list.a[k].y_end + 1;
        if(wts >= t[1]) break;
        if((o->w_list.a[k].y_end == -1) || (is_ualn_win(o->w_list.a[k]))) continue;
        os = MAX(t[0], wts); oe = MIN(t[1], wte);
        ovlp = ((oe>os)? (oe-os):0);
        if(!ovlp) continue;
        if(k < ii[0]) ii[0] = k;
        if(k > ii[1]) ii[1] = k;
        salnl += ovlp; 
    }
    // if(dbg_id == 5525) {
    //     prt_aln_w(o);
    //     fprintf(stderr, "[M::%s::aln->%ld] ii::[%ld, %ld), q_aln0::[%d, %d), t0::[%ld, %ld)\n", 
    //     __func__, salnl, ii[0], ii[1]+1, o->w_list.a[ii[0]].x_start, o->w_list.a[ii[0]].x_end+1, 
    //     ts0, te0);
    // }
    if((!salnl) || (ii[0] == INT32_MAX) || (ii[1] < 0)) return 0;
    if((ii[0] == ii[1]) && (is_ualn_win(o->w_list.a[ii[0]]))) return 0;
    //[ii[0], ii[1]]
    win_boundary_offset(o->w_list.a, o->w_list.n, ii[0], ts0, ql, &(q[0]), &(t[0]));
    win_boundary_offset(o->w_list.a, o->w_list.n, ii[1], te0-1, ql, &(q[1]), &(t[1]));
    q[1]++; t[1]++;
    // if(dbg_id == 5525) {
    //     fprintf(stderr, "[M::%s::aln->%ld] ii::[%ld, %ld), q::[%ld, %ld), t::[%ld, %ld)\n", 
    //             __func__, salnl, ii[0], ii[1]+1, q[0], q[1], t[0], t[1]);
    // }
    if(salnl < ((t[1]-t[0])*o_rate)) return 0;
    
    gen_raln(uref, qstr, tu, o, exz, ii[0], ii[1]+1, ts0, te0, ql, o->y_id, rev, e_rate, rid, trace, res);
    assert(res->ts >= ts0 && res->te <= te0 && res->qs >= 0 && res->qe <= ql);
    double simi_thre = e_rate + MIN(r_simi_w, (e_rate/2));
    if((res->sec <= ((res->qe-res->qs)*simi_thre)) && (res->sec <= ((res->te-res->ts)*simi_thre))) {
        return 1;
    } else {
        return 0;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, %r14
movq 0xf0(%rsp), %rbp
movl 0x1c(%rbp), %r8d
movq 0x48(%rbp), %rax
movq %r14, 0x50(%rsp)
movq %r9, 0x58(%rsp)
testq %rax, %rax
jle 0x4e6f8
movq %r9, %rbx
movq %rdi, 0x98(%rsp)
movq 0xf8(%rsp), %r11
testq %r11, %r11
movq %r8, 0x90(%rsp)
je 0x4e402
movq (%r11), %rdi
jmp 0x4e404
xorl %edi, %edi
xorl %r10d, %r10d
testq %rdi, %rdi
cmovgq %rdi, %r10
leaq -0x1(%rax), %r9
cmpq %rax, %rdi
cmovgeq %r9, %r10
movq %r10, %rdi
shlq $0x5, %rdi
orq $0xc, %rdi
cmpq %rax, %r10
jge 0x4e449
movq 0x58(%rbp), %r8
movslq (%r8,%rdi), %r8
cmpq %r14, %r8
jge 0x4e43f
incq %r10
addq $0x20, %rdi
jmp 0x4e424
xorl %r9d, %r9d
testq %r10, %r10
cmovgq %r10, %r9
movq %r9, %r10
shlq $0x5, %r10
orq $0x8, %r10
pushq $-0x1
popq %rdi
testq %r9, %r9
js 0x4e475
movq 0x58(%rbp), %r8
movslq (%r8,%r10), %r8
cmpq %r14, %r8
jle 0x4e472
decq %r9
addq $-0x20, %r10
jmp 0x4e457
movq %r9, %rdi
xorl %r10d, %r10d
testq %rdi, %rdi
cmovgq %rdi, %r10
testq %r11, %r11
je 0x4e487
movq %r10, (%r11)
xorl %r15d, %r15d
testq %r9, %r9
cmovleq %r15, %r9
shlq $0x5, %r9
orq $0x1c, %r9
pushq $-0x1
popq %r13
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
cmpq %rax, %r10
jge 0x4e50d
movq 0x58(%rbp), %r8
movslq -0x14(%r8,%r9), %r11
cmpq %rbx, %r11
jge 0x4e50d
movslq -0x10(%r8,%r9), %rdi
cmpl $-0x1, %edi
je 0x4e504
cmpw $0x7fff, -0x8(%r8,%r9)  # imm = 0x7FFF
jne 0x4e4da
cmpl $0x0, (%r8,%r9)
jne 0x4e4da
cmpw $0x0, -0xa(%r8,%r9)
js 0x4e504
incq %rdi
cmpq %r11, %r14
cmovgq %r14, %r11
cmpq %rdi, %rbx
cmovlq %rbx, %rdi
cmpq %r11, %rdi
jle 0x4e504
cmpq %r12, %r10
cmovlq %r10, %r12
cmpq %r13, %r10
cmovgq %r10, %r13
subq %r11, %r15
addq %rdi, %r15
incq %r10
addq $0x20, %r9
jmp 0x4e4a3
xorl %eax, %eax
testq %r15, %r15
je 0x4e6fa
cmpq $0x7fffffff, %r12       # imm = 0x7FFFFFFF
je 0x4e6fa
testq %r13, %r13
js 0x4e6fa
movq 0x58(%rbp), %rdi
cmpq %r13, %r12
jne 0x4e55b
movq %r12, %rax
shlq $0x5, %rax
cmpw $0x7fff, 0x14(%rdi,%rax) # imm = 0x7FFF
jne 0x4e55b
addq %rdi, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x4e55b
cmpw $0x0, 0x12(%rax)
js 0x4e6f8
movq %rsi, 0x60(%rsp)
movq %rdx, 0x68(%rsp)
movq %rcx, 0x70(%rsp)
movsd %xmm1, 0x48(%rsp)
movq 0x48(%rbp), %rsi
movq %rsi, 0x80(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, (%rsp)
leaq 0xa0(%rsp), %r9
movq %rdi, 0x78(%rsp)
movq %r12, %rdx
movq %r14, %rcx
movq 0x100(%rsp), %r8
movsd %xmm0, 0x88(%rsp)
callq 0x4a2e0
leaq -0x1(%rbx), %rcx
leaq 0xa8(%rsp), %r9
leaq 0x58(%rsp), %rax
movq %rax, (%rsp)
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq %r13, %rdx
movq 0x100(%rsp), %r8
callq 0x4a2e0
leaq 0x50(%rsp), %rcx
movq 0x8(%rcx), %rax
cvtsi2sd %r15, %xmm0
subq (%rcx), %rax
incq %rax
cvtsi2sd %rax, %xmm1
mulsd 0x88(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x4e6f8
incq %r13
movl 0x10(%rbp), %eax
movq 0x120(%rsp), %r15
movq %r15, 0x40(%rsp)
movq 0x118(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq 0x90(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq %r13, (%rsp)
movq 0x98(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq %rbp, %rcx
movq 0x70(%rsp), %r8
movq %r12, %r9
movsd 0x48(%rsp), %xmm0
callq 0x4d70a
movl 0x10(%r15), %eax
cmpq %r14, %rax
jl 0x4e711
movq %r15, %rdi
movl 0x14(%r15), %ecx
cmpq %rbx, %rcx
jg 0x4e711
movl 0x8(%rdi), %edx
cmpq 0x100(%rsp), %rdx
jg 0x4e711
movsd 0x194e99(%rip), %xmm1   # 0x1e3548
movsd 0x48(%rsp), %xmm3
mulsd %xmm3, %xmm1
movsd 0x194e57(%rip), %xmm0   # 0x1e3518
minsd %xmm1, %xmm0
movl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
andl 0x18(%rdi), %esi
cvtsi2sd %esi, %xmm1
subl 0x4(%rdi), %edx
cvtsi2sd %rdx, %xmm2
addsd %xmm3, %xmm0
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jb 0x4e6f8
subl %eax, %ecx
cvtsi2sd %rcx, %xmm2
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jae 0x4e70c
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rax
jmp 0x4e6fa
leaq 0x198dee(%rip), %rdi    # 0x1e7506
leaq 0x194f84(%rip), %rsi    # 0x1e36a3
leaq 0x198e22(%rip), %rcx    # 0x1e7548
movl $0x606d, %edx           # imm = 0x606D
callq 0x2110