// File: /workspace/github_cpp_makefile_star2_O1/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 0x82346
movq %r9, %rbx
movq %rdi, 0x98(%rsp)
movq 0xf8(%rsp), %r11
testq %r11, %r11
je 0x81fda
movq (%r11), %rdi
jmp 0x81fdc
xorl %edi, %edi
movq %r8, 0x90(%rsp)
xorl %r12d, %r12d
testq %rdi, %rdi
cmovgq %rdi, %r12
leaq -0x1(%rax), %r15
cmpq %rax, %rdi
cmovgeq %r15, %r12
cmpq %rax, %r12
setl %dil
jge 0x82048
movq 0x58(%rbp), %r8
movq %r12, %r9
shlq $0x5, %r9
movslq 0xc(%r8,%r9), %r10
cmpq %r14, %r10
jge 0x82048
incq %r12
addq %r9, %r8
addq $0x2c, %r8
movq %r12, %rdi
cmpq %r12, %rax
je 0x8203e
movslq (%r8), %r9
leaq 0x1(%rdi), %r12
addq $0x20, %r8
cmpq %r14, %r9
jl 0x82021
decq %r12
jmp 0x82041
movq %rax, %r12
cmpq %rax, %rdi
setl %dil
xorl %r8d, %r8d
testq %r12, %r12
cmovgq %r12, %r8
testb %dil, %dil
cmovneq %r8, %r15
movq 0x58(%rbp), %rdi
movq %r15, %r9
sarq $0x3f, %r9
andq %r15, %r9
decq %r9
movq %r15, %r8
shlq $0x5, %r8
addq %r8, %rdi
addq $0x8, %rdi
movslq (%rdi), %r8
cmpq %r14, %r8
jle 0x82093
leaq -0x1(%r15), %r8
addq $-0x20, %rdi
testq %r15, %r15
movq %r8, %r15
jg 0x82078
movq %r9, %r15
xorl %r10d, %r10d
testq %r15, %r15
cmovgq %r15, %r10
testq %r11, %r11
je 0x820a5
movq %r10, (%r11)
cmpq %rax, %r15
jge 0x82131
movq 0x58(%rbp), %rdi
movq %r10, %r8
shlq $0x5, %r8
addq %rdi, %r8
addq $0x1c, %r8
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq $-0x1, %r13
xorl %r15d, %r15d
movslq -0x14(%r8), %r9
cmpq %rbx, %r9
jge 0x82141
movslq -0x10(%r8), %rdi
cmpl $-0x1, %edi
je 0x82123
cmpw $0x7fff, -0x8(%r8)      # imm = 0x7FFF
jne 0x820f9
cmpl $0x0, (%r8)
jne 0x820f9
cmpw $0x0, -0xa(%r8)
js 0x82123
incq %rdi
cmpq %r9, %r14
cmovgq %r14, %r9
cmpq %rdi, %rbx
cmovlq %rbx, %rdi
cmpq %r9, %rdi
jle 0x82123
cmpq %r12, %r10
cmovlq %r10, %r12
cmpq %r13, %r10
cmovgq %r10, %r13
subq %r9, %r15
addq %rdi, %r15
incq %r10
addq $0x20, %r8
cmpq %rax, %r10
jl 0x820d0
jmp 0x82141
movq $-0x1, %r13
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %r15d, %r15d
xorl %eax, %eax
testq %r15, %r15
je 0x82348
cmpq $0x7fffffff, %r12       # imm = 0x7FFFFFFF
je 0x82348
testq %r13, %r13
js 0x82348
cmpq %r13, %r12
jne 0x8218f
movq 0x58(%rbp), %rax
movq %r12, %rdi
shlq $0x5, %rdi
cmpw $0x7fff, 0x14(%rax,%rdi) # imm = 0x7FFF
jne 0x8218f
addq %rdi, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x8218f
cmpw $0x0, 0x12(%rax)
js 0x82346
movq %rsi, 0x68(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x78(%rsp)
movsd %xmm1, 0x48(%rsp)
movq 0x48(%rbp), %rsi
movq %rsi, 0x80(%rsp)
movq 0x58(%rbp), %rbp
leaq 0x50(%rsp), %rax
movq %rax, (%rsp)
leaq 0xa0(%rsp), %r9
movq %rbp, %rdi
movq %r12, %rdx
movq %r14, %rcx
movq 0x100(%rsp), %r8
movsd %xmm0, 0x88(%rsp)
callq 0x7da67
leaq -0x1(%rbx), %rcx
leaq 0xa8(%rsp), %r9
leaq 0x58(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq 0x80(%rsp), %rsi
movq %r13, %rdx
movq 0x100(%rsp), %r8
movq %r9, %rbp
callq 0x7da67
incq (%rbp)
leaq 0x50(%rsp), %rcx
movq 0x8(%rcx), %rax
incq %rax
movq %rax, 0x8(%rcx)
cvtsi2sd %r15, %xmm0
subq (%rcx), %rax
cvtsi2sd %rax, %xmm1
mulsd 0x88(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x82346
incq %r13
movq 0xf0(%rsp), %r8
movl 0x10(%r8), %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), %rbp
movq %rbp, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq %r13, (%rsp)
movq 0x98(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq %r8, %rcx
movq 0x78(%rsp), %r8
movq %r12, %r9
movsd 0x48(%rsp), %xmm0
callq 0x810e9
movl 0x10(%r15), %eax
cmpq %r14, %rax
jl 0x8235a
movl 0x14(%r15), %ecx
cmpq %rbx, %rcx
jg 0x8235a
movl 0x8(%r15), %edx
cmpq %rbp, %rdx
jg 0x8235a
movsd 0x1a3200(%rip), %xmm1   # 0x2254f0
movsd 0x48(%rsp), %xmm3
mulsd %xmm3, %xmm1
movsd 0x1a31c6(%rip), %xmm0   # 0x2254c8
minsd %xmm1, %xmm0
movl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
andl 0x18(%r15), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
subl 0x4(%r15), %edx
cvtsi2sd %rdx, %xmm2
addsd %xmm3, %xmm0
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jb 0x82346
subl %eax, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
mulsd %xmm2, %xmm0
movl $0x1, %eax
ucomisd %xmm1, %xmm0
jae 0x82348
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a7145(%rip), %rdi    # 0x2294a6
leaq 0x1a32db(%rip), %rsi    # 0x225643
leaq 0x1a7179(%rip), %rcx    # 0x2294e8
movl $0x606d, %edx           # imm = 0x606D
callq 0x2110