// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %r9, -0x60(%rbp)
movsd %xmm0, -0x68(%rbp)
movsd %xmm1, -0x70(%rbp)
movq 0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0x78(%rbp)
movq $0x0, -0xa8(%rbp)
movq 0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0xd8(%rbp)
movq $0x0, -0xf8(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0xd8(%rbp)
jg 0x9645f
movq $0x0, -0x30(%rbp)
jmp 0x96b79
cmpq $0x0, 0x18(%rbp)
je 0x96474
movq 0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jge 0x96489
movq $0x0, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0xd8(%rbp), %rax
jl 0x964ab
movq -0xd8(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x964ad
movq -0xa8(%rbp), %rcx
xorl %eax, %eax
cmpq -0xd8(%rbp), %rcx
movb %al, -0x101(%rbp)
jge 0x964f2
movq -0x90(%rbp), %rax
movq 0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0xa8(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq 0xc(%rcx), %rcx
cmpq %rcx, %rax
setg %al
movb %al, -0x101(%rbp)
movb -0x101(%rbp), %al
testb $0x1, %al
jne 0x964fe
jmp 0x96514
jmp 0x96500
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x964ad
cmpq $0x0, -0xa8(%rbp)
jge 0x96529
movq $0x0, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0xd8(%rbp), %rax
jl 0x9654b
movq -0xd8(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x9654d
xorl %eax, %eax
cmpq $0x0, -0xa8(%rbp)
movb %al, -0x102(%rbp)
jl 0x9658c
movq -0x90(%rbp), %rax
movq 0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0xa8(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq 0x8(%rcx), %rcx
cmpq %rcx, %rax
setl %al
movb %al, -0x102(%rbp)
movb -0x102(%rbp), %al
testb $0x1, %al
jne 0x96598
jmp 0x965ae
jmp 0x9659a
movq -0xa8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x9654d
cmpq $0x0, -0xa8(%rbp)
jge 0x965c3
movq $0x0, -0xa8(%rbp)
cmpq $0x0, 0x18(%rbp)
je 0x965d8
movq -0xa8(%rbp), %rcx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
movq $0x7fffffff, -0xd0(%rbp) # imm = 0x7FFFFFFF
movq $-0x1, -0xc8(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0xd8(%rbp), %rax
jge 0x96806
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xa8(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rax
movq %rax, -0xb0(%rbp)
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xa8(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0x88(%rbp), %rax
jl 0x9665d
jmp 0x96806
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xa8(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $-0x1, 0xc(%rax)
je 0x966d5
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xa8(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x966da
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xa8(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x966da
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xa8(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x966da
jmp 0x967ef
movq -0x90(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jl 0x966fa
movq -0x90(%rbp), %rax
movq %rax, -0x110(%rbp)
jmp 0x96708
movq -0xb0(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x88(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jg 0x96736
movq -0x88(%rbp), %rax
movq %rax, -0x118(%rbp)
jmp 0x96744
movq -0xb8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
cmpq -0xe0(%rbp), %rax
jle 0x96779
movq -0xe8(%rbp), %rax
subq -0xe0(%rbp), %rax
movq %rax, -0x120(%rbp)
jmp 0x96784
xorl %eax, %eax
movq %rax, -0x120(%rbp)
jmp 0x96784
movq -0x120(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
jne 0x9679e
jmp 0x967ef
movq -0xa8(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jge 0x967bc
movq -0xa8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jle 0x967da
movq -0xa8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xf0(%rbp), %rax
addq -0xf8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x965ee
cmpq $0x0, -0xf8(%rbp)
je 0x96827
cmpq $0x7fffffff, -0xd0(%rbp) # imm = 0x7FFFFFFF
je 0x96827
cmpq $0x0, -0xc8(%rbp)
jge 0x96834
movq $0x0, -0x30(%rbp)
jmp 0x96b79
movq -0xd0(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jne 0x968ad
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xd0(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x968ad
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xd0(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x968ad
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xd0(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x968ad
movq $0x0, -0x30(%rbp)
jmp 0x96b79
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq 0x10(%rbp), %rax
movq 0x48(%rax), %rsi
movq -0xd0(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq 0x20(%rbp), %r8
leaq -0xa0(%rbp), %r9
leaq -0x90(%rbp), %rax
movq %rax, (%rsp)
callq 0x8e260
movq 0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq 0x10(%rbp), %rax
movq 0x48(%rax), %rsi
movq -0xc8(%rbp), %rdx
movq -0x60(%rbp), %rcx
subq $0x1, %rcx
movq 0x20(%rbp), %r8
leaq -0xa0(%rbp), %r9
addq $0x8, %r9
leaq -0x90(%rbp), %rax
addq $0x8, %rax
movq %rax, (%rsp)
callq 0x8e260
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
cvtsi2sdq -0xf8(%rbp), %xmm1
movq -0x88(%rbp), %rax
subq -0x90(%rbp), %rax
cvtsi2sd %rax, %xmm0
mulsd -0x68(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x9697d
movq $0x0, -0x30(%rbp)
jmp 0x96b79
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq -0x50(%rbp), %r8
movq -0xd0(%rbp), %r9
movq -0xc8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x138(%rbp)
movq -0x58(%rbp), %r13
movq -0x60(%rbp), %r12
movq 0x20(%rbp), %r15
movq 0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %r14d
movq -0x78(%rbp), %rbx
movsd -0x70(%rbp), %xmm0
movq 0x28(%rbp), %r11
movq 0x38(%rbp), %r10
movq 0x40(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x138(%rbp), %rax
movq %rax, (%rsp)
movq -0x130(%rbp), %rax
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
callq 0x94c90
movq 0x40(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq -0x58(%rbp), %rcx
movb %al, -0x121(%rbp)
jl 0x96a6f
movq 0x40(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq -0x60(%rbp), %rcx
movb %al, -0x121(%rbp)
jg 0x96a6f
movq 0x40(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x4(%rcx)
movb %al, -0x121(%rbp)
jb 0x96a6f
movq 0x40(%rbp), %rax
movl 0x8(%rax), %eax
cmpq 0x20(%rbp), %rax
setle %al
movb %al, -0x121(%rbp)
movb -0x121(%rbp), %al
testb $0x1, %al
jne 0x96a7b
jmp 0x96a7d
jmp 0x96a9c
leaq 0x2ef6fa(%rip), %rdi    # 0x38617e
leaq 0x2eb890(%rip), %rsi    # 0x38231b
movl $0x606d, %edx           # imm = 0x606D
leaq 0x2ef729(%rip), %rcx    # 0x3861c0
callq 0x2130
movsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x140(%rbp)
movsd -0x70(%rbp), %xmm0
movsd 0x2eb832(%rip), %xmm1   # 0x3822e8
divsd %xmm1, %xmm0
movsd 0x2eb77e(%rip), %xmm1   # 0x382240
ucomisd %xmm1, %xmm0
jb 0x96ada
movsd 0x2eb770(%rip), %xmm0   # 0x382240
movsd %xmm0, -0x148(%rbp)
jmp 0x96af3
movsd -0x70(%rbp), %xmm0
movsd 0x2eb801(%rip), %xmm1   # 0x3822e8
divsd %xmm1, %xmm0
movsd %xmm0, -0x148(%rbp)
movsd -0x140(%rbp), %xmm0
movsd -0x148(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x100(%rbp)
movq 0x40(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
cvtsi2sd %ecx, %xmm1
movl 0x4(%rax), %ecx
movl 0x8(%rax), %eax
subl %ecx, %eax
cvtsi2sd %rax, %xmm0
mulsd -0x100(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x96b71
movq 0x40(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
cvtsi2sd %ecx, %xmm1
movl 0x10(%rax), %ecx
movl 0x14(%rax), %eax
subl %ecx, %eax
cvtsi2sd %rax, %xmm0
mulsd -0x100(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x96b71
movq $0x1, -0x30(%rbp)
jmp 0x96b79
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop