// File: /workspace/github_cpp_makefile_star2_O3/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 0xf0(%rsp), %rbp
movl 0x1c(%rbp), %r12d
movq 0x48(%rbp), %rax
movq %r8, 0x50(%rsp)
movq %r9, 0x58(%rsp)
testq %rax, %rax
jle 0x81bb7
movq %r9, %rbx
movq %r8, %r14
movq 0xf8(%rsp), %r10
testq %r10, %r10
je 0x81888
movq (%r10), %r8
jmp 0x8188b
xorl %r8d, %r8d
xorl %r9d, %r9d
testq %r8, %r8
cmovgq %r8, %r9
leaq -0x1(%rax), %r11
cmpq %rax, %r8
cmovgeq %r11, %r9
cmpq %rax, %r9
jae 0x818d7
movq 0x58(%rbp), %r15
movq %r9, %r8
shlq $0x5, %r8
addq %r8, %r15
addq $0xc, %r15
movslq (%r15), %r8
cmpq %r14, %r8
jge 0x818cd
incq %r9
addq $0x20, %r15
cmpq %r9, %rax
jne 0x818b7
jmp 0x818d7
xorl %r11d, %r11d
testq %r9, %r9
cmovgq %r9, %r11
movq 0x58(%rbp), %r15
movq %r11, %r9
sarq $0x3f, %r9
andq %r11, %r9
decq %r9
movq %r11, %r8
shlq $0x5, %r8
addq %r8, %r15
addq $0x8, %r15
movslq (%r15), %r8
cmpq %r14, %r8
jle 0x81911
leaq -0x1(%r11), %r8
addq $-0x20, %r15
testq %r11, %r11
movq %r8, %r11
jg 0x818f6
movq %r9, %r11
xorl %r8d, %r8d
testq %r11, %r11
cmovgq %r11, %r8
testq %r10, %r10
je 0x81923
movq %r8, (%r10)
cmpq %rax, %r11
jge 0x81bb7
movq %r12, 0x90(%rsp)
movq %rdi, 0x98(%rsp)
movq 0x58(%rbp), %rdi
movq %r8, %r9
shlq $0x5, %r9
addq %rdi, %r9
addq $0x1c, %r9
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq $-0x1, %r13
xorl %r15d, %r15d
movslq -0x14(%r9), %r10
cmpq %rbx, %r10
jge 0x819bd
movslq -0x10(%r9), %rdi
cmpl $-0x1, %edi
je 0x819b1
cmpw $0x7fff, -0x8(%r9)      # imm = 0x7FFF
jne 0x81987
cmpl $0x0, (%r9)
jne 0x81987
cmpw $0x0, -0xa(%r9)
js 0x819b1
incq %rdi
cmpq %r10, %r14
cmovgq %r14, %r10
cmpq %rdi, %rbx
cmovlq %rbx, %rdi
cmpq %r10, %rdi
jle 0x819b1
cmpq %r12, %r8
cmovlq %r8, %r12
cmpq %r13, %r8
cmovgq %r8, %r13
subq %r10, %r15
addq %rdi, %r15
incq %r8
addq $0x20, %r9
cmpq %rax, %r8
jl 0x8195e
xorl %eax, %eax
testq %r15, %r15
je 0x81bb9
cmpq $0x7fffffff, %r12       # imm = 0x7FFFFFFF
je 0x81bb9
testq %r13, %r13
js 0x81bb9
movq 0x58(%rbp), %rdi
cmpq %r13, %r12
jne 0x81a0b
movq %r12, %rax
shlq $0x5, %rax
cmpw $0x7fff, 0x14(%rdi,%rax) # imm = 0x7FFF
jne 0x81a0b
addq %rdi, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x81a0b
cmpw $0x0, 0x12(%rax)
js 0x81bb7
movq %rsi, 0x68(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x78(%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, %rbp
movq %r12, %rdx
movq %r14, %rcx
movq 0x100(%rsp), %r8
movsd %xmm0, 0x88(%rsp)
callq 0x7d4f7
leaq -0x1(%rbx), %rcx
leaq 0xa8(%rsp), %r9
leaq 0x58(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq 0x80(%rsp), %rsi
movq 0x100(%rsp), %rbp
movq %r13, %rdx
movq %rbp, %r8
callq 0x7d4f7
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 0x81bb7
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 %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 0x80aa8
movl 0x10(%r15), %eax
cmpq %r14, %rax
jl 0x81bcb
movl 0x14(%r15), %ecx
cmpq %rbx, %rcx
jg 0x81bcb
movl 0x8(%r15), %edx
cmpq 0x100(%rsp), %rdx
jg 0x81bcb
movsd 0x1afa8f(%rip), %xmm1   # 0x2315f0
movsd 0x48(%rsp), %xmm3
mulsd %xmm3, %xmm1
movsd 0x1afa55(%rip), %xmm0   # 0x2315c8
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 0x81bb7
subl %eax, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
mulsd %xmm2, %xmm0
movl $0x1, %eax
ucomisd %xmm1, %xmm0
jae 0x81bb9
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b39e4(%rip), %rdi    # 0x2355b6
leaq 0x1afb7a(%rip), %rsi    # 0x231753
leaq 0x1b3a18(%rip), %rcx    # 0x2355f8
movl $0x606d, %edx           # imm = 0x606D
callq 0x2110