// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: aln_wlst(overlap_region*, All_reads*, ul_idx_t const*, UC_Read*, Correct_dumy*, int, int, long, long, long long, long, double, int)
inline uint32_t aln_wlst(overlap_region *z, All_reads *rref, const ul_idx_t *uref, UC_Read* g_read, Correct_dumy* dumy,
int32_t y_strand, int32_t y_id, int64_t x_start, int64_t x_end, long long y_start, int64_t block_s, double e_rate, int32_t is_cigar)
{
    int64_t x_len, Window_Len; window_list *p = NULL; long long o_len;
    int32_t threshold; int real_y_start = 0, end_site, extra_begin, extra_end;
    char *x_string, *y_string; unsigned int error;
    x_len = x_end + 1 - x_start;
    ///there are two potiential reasons for unmatched window:
    ///1. this window has a large number of differences
    ///2. DP does not start from the right offset
    if(rref) {
        threshold = double_error_threshold(get_init_err_thres(x_len, e_rate, block_s, THRESHOLD), x_len);
    } else {
        threshold = double_ul_error_threshold(get_init_err_thres(x_len, e_rate, block_s, THRESHOLD_MAX_SIZE), x_len);
    }
    Window_Len = x_len + (threshold << 1);
    ///y_start might be less than 0
    if(!determine_overlap_region(threshold, y_start, y_id, Window_Len, (rref?(Get_READ_LENGTH((*rref), y_id)):(uref->ug->u.a[y_id].len)), 
    &extra_begin, &extra_end, &y_start, &o_len)) {
        return 0;
    }

    if(o_len + threshold < x_len) return 0;
    if(rref) {
        fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, rref, y_id, extra_begin, extra_end);
    } else {
        fill_subregion_ul(dumy->overlap_region, y_start, o_len, y_strand, uref, y_id, extra_begin, extra_end);
    }
    x_string = g_read->seq + x_start;
    y_string = dumy->overlap_region;

    if(is_cigar) {
        ///note!!! need notification
        end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &error, &real_y_start,
        &(dumy->path_length), dumy->matrix_bit, dumy->path, -1, -1);
    } else {
        ///note!!! need notification
        end_site = Reserve_Banded_BPM(y_string, Window_Len, x_string, x_len, threshold, &error);    
    }
    if(error!=(unsigned int)-1) {
        if(is_cigar) {
            ///this condition is always wrong
            ///in best case, real_y_start = threshold, end_site = Window_Len - threshold - 1
            if (end_site == Window_Len - 1 || real_y_start == 0) {
                if(rref) {
                    fix_boundary(x_string, x_len, threshold, y_start, real_y_start, 
                    end_site, extra_begin, extra_end, y_id, Window_Len, rref, dumy, 
                    y_strand, error, &y_start, &real_y_start, &end_site, &extra_begin, 
                    &extra_end, &error);
                } else {
                    fix_ul_boundary(x_string, x_len, threshold, y_start, real_y_start, 
                    end_site, extra_begin, extra_end, y_id, Window_Len, uref, dumy, 
                    y_strand, error, &y_start, &real_y_start, &end_site, &extra_begin, 
                    &extra_end, &error);
                }
            }
        }

        kv_pushp(window_list, z->w_list, &p);
        p->x_start = x_start; p->x_end = x_end; ///must set x_start/x_end here
        if(is_cigar) {
            generate_cigar(dumy->path, dumy->path_length, p, &(z->w_list), &real_y_start, &end_site, &error, x_string, x_len, y_string);  
        } else {
            p->cidx = p->clen = 0;
        }
        p->y_start = y_start + real_y_start;///difference
        p->y_end = y_start + end_site;
        p->error = error;
        p->extra_begin = extra_begin;
        p->extra_end = extra_end;
        p->error_threshold = threshold;
        z->align_length += x_len; 
        
        return 1;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, 0x80(%rsp)
movq %rsi, %rbx
movq %rdi, 0x40(%rsp)
movq 0xe0(%rsp), %r10
movq 0xd8(%rsp), %rax
movq 0xd0(%rsp), %r13
movl 0xc0(%rsp), %ecx
movl $0x0, 0x10(%rsp)
subq 0xc8(%rsp), %r13
leaq 0x1(%r13), %r14
testq %rsi, %rsi
je 0x9b17a
movl $0xf, %r11d
cmpq %r10, %r14
jge 0x9b146
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %r10
cmpq $0x1f, %r10
movl $0x1f, %esi
cmovlq %r10, %rsi
testq %r10, %r10
movl $0x1, %r11d
cmovnel %esi, %r11d
cmpq $0x4, %r14
cmovll %esi, %r11d
leal (%r11,%r11), %esi
testl %r11d, %r11d
movl $0x2, %r10d
cmovnel %esi, %r10d
cmpl $0x4, %r14d
cmovll %esi, %r10d
cmpl $0x20, %r10d
movl $0x1f, %r11d
cmovgel %r10d, %r11d
cmpl $0x12c, %r14d           # imm = 0x12C
cmovll %r10d, %r11d
jmp 0x9b1e3
movl $0x1f, %esi
cmpq %r10, %r14
jge 0x9b1b4
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rsi
cmpq $0x1f, %rsi
movl $0x1f, %r10d
cmovlq %rsi, %r10
testq %rsi, %rsi
movl $0x1, %esi
cmovnel %r10d, %esi
cmpq $0x4, %r14
cmovll %r10d, %esi
testl %esi, %esi
movl $0x1, %r11d
cmovnel %esi, %r11d
cmpl $0x4, %r14d
cmovll %esi, %r11d
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
mulsd 0x18a2a8(%rip), %xmm0   # 0x225480
cvttsd2si %xmm0, %esi
cmpl %esi, %r11d
cmovlel %esi, %r11d
cmpl $0x1f, %r11d
movl $0x1f, %r12d
cmovll %r11d, %r12d
leal (%r12,%r12), %esi
movslq %esi, %rdi
movslq %ecx, %r11
testq %rbx, %rbx
je 0x9b20a
movq 0x18(%rbx), %rcx
movq (%rcx,%r11,8), %rbp
jmp 0x9b21e
movq (%rdx), %rcx
movq 0x10(%rcx), %rcx
leaq (%r11,%r11,4), %rsi
movl (%rcx,%rsi,8), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
leaq (%r14,%rdi), %r10
movb $0x1, %cl
testq %rax, %rax
js 0x9b2b1
cmpq %rax, %rbp
jle 0x9b2b1
movq %rdi, %rsi
subq %rax, %rsi
addq %rbp, %rsi
addq $0x1f, %rsi
cmpq %r10, %rsi
jl 0x9b2b1
movslq %r12d, %rcx
subq %rcx, %rax
subq %rax, %rbp
cmpq %r10, %rbp
cmovgeq %r10, %rbp
movq %r11, 0x30(%rsp)
movl %r10d, %r11d
subl %ebp, %r11d
movl %eax, %esi
negl %esi
xorl %ecx, %ecx
testq %rax, %rax
movq %r8, %r15
movq %rdx, %r8
movq %r9, %rdx
movq %rdi, %r9
movq %r10, %rdi
movl $0x0, %r10d
cmovgq %rax, %r10
sarq $0x3f, %rax
andl %esi, %eax
subq %rax, %rbp
movl %eax, 0x18(%rsp)
movl %r11d, 0x14(%rsp)
movq 0x30(%rsp), %r11
movq %r10, 0xd8(%rsp)
movq %rdi, %r10
movq %r9, %rdi
movq %rdx, %r9
movq %r8, %rdx
movq %r15, %r8
jmp 0x9b2b1
xorl %r15d, %r15d
testb %cl, %cl
jne 0x9b63d
movslq %r12d, %rax
addq %rbp, %rax
cmpq %r14, %rax
jl 0x9b63d
movq %rdi, 0x78(%rsp)
movq %r10, 0x20(%rsp)
leaq 0x50(%r8), %rax
movq %rax, 0x58(%rsp)
movq 0xd8(%rsp), %r10
movslq 0x18(%rsp), %rcx
movslq 0x14(%rsp), %rax
movq %rax, 0x70(%rsp)
movq %r8, 0x28(%rsp)
movq %rbx, %rax
movq %rcx, 0x68(%rsp)
leaq (%r8,%rcx), %rbx
addq $0x50, %rbx
testq %rax, %rax
movq %r9, 0x48(%rsp)
movq %rdx, 0x60(%rsp)
movq %rax, 0x50(%rsp)
movq %r11, 0x30(%rsp)
movq %r12, 0x38(%rsp)
je 0x9b342
movzbl %r9b, %ecx
movq %rbx, %rdi
movq %r10, %rsi
movq %rbp, %rdx
movq %rax, %r8
movq %r11, %r9
callq 0x337f7
jmp 0x9b36f
movq (%rdx), %rax
leaq (%r11,%r11,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl %r9b, %ecx
xorl %edi, %edi
movq %rbx, %rsi
movq %r10, %r8
movq %rbp, %r9
pushq $0x0
callq 0x36b0d
addq $0x10, %rsp
movq 0x58(%rsp), %r12
movq %r12, %rdi
movl $0x4e, %esi
movq 0x68(%rsp), %rdx
callq 0x20e0
addq %rbp, %rbx
movq %rbx, %rdi
movl $0x4e, %esi
movq 0x70(%rsp), %rdx
callq 0x20e0
movq 0x80(%rsp), %rax
movq (%rax), %rdx
addq 0xc8(%rsp), %rdx
cmpl $0x0, 0xe8(%rsp)
je 0x9b408
movq 0x28(%rsp), %rcx
leaq 0x7d70(%rcx), %rax
leaq 0xff0(%rcx), %r10
leaq 0xa14(%rcx), %r11
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq 0x10(%rsp), %rbp
movzwl 0x38(%rsp), %r8d
leaq 0xc(%rsp), %r9
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %rdx, %r12
movl %r14d, %ecx
pushq %rbx
pushq %rbx
pushq %r11
pushq %r10
pushq %rax
pushq %rbp
callq 0x8607d
addq $0x30, %rsp
jmp 0x9b426
movzwl 0x38(%rsp), %r8d
leaq 0xc(%rsp), %r9
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %rdx, %r12
movl %r14d, %ecx
callq 0x8578f
movq 0x50(%rsp), %rbp
movl %eax, 0x1c(%rsp)
movl 0xc(%rsp), %r10d
cmpl $-0x1, %r10d
movq 0x78(%rsp), %rcx
je 0x9b63d
cmpl $0x0, 0xe8(%rsp)
movq 0x38(%rsp), %r15
je 0x9b53e
movslq %eax, %r9
addq %rcx, %r13
cmpq %r9, %r13
sete %cl
movl 0x10(%rsp), %eax
testl %eax, %eax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x9b53e
movslq %eax, %r8
movq 0xd8(%rsp), %rcx
movslq 0x18(%rsp), %rax
movslq 0x14(%rsp), %r11
testq %rbp, %rbp
movq %r12, %rdi
leaq 0xc(%rsp), %rbx
je 0x9b4e7
leaq 0x14(%rsp), %r13
movq %rdi, %r12
movq %r14, %rsi
movl %r15d, %edx
pushq %rbx
pushq %r13
leaq 0x28(%rsp), %rbx
pushq %rbx
leaq 0x34(%rsp), %rbx
pushq %rbx
leaq 0x30(%rsp), %rbx
pushq %rbx
leaq 0x100(%rsp), %rbx
pushq %rbx
pushq %r10
pushq 0x80(%rsp)
pushq 0x68(%rsp)
pushq %rbp
pushq 0x70(%rsp)
pushq 0x88(%rsp)
pushq %r11
pushq %rax
callq 0x98b36
jmp 0x9b53a
leaq 0x14(%rsp), %rbp
leaq 0x18(%rsp), %r13
movq %rdi, %r12
movq %r14, %rsi
movl %r15d, %edx
pushq %rbx
pushq %rbp
pushq %r13
leaq 0x34(%rsp), %rbx
pushq %rbx
leaq 0x30(%rsp), %rbx
pushq %rbx
leaq 0x100(%rsp), %rbx
pushq %rbx
pushq %r10
pushq 0x80(%rsp)
pushq 0x68(%rsp)
pushq 0xa8(%rsp)
pushq 0x70(%rsp)
pushq 0x88(%rsp)
pushq %r11
pushq %rax
callq 0x98f8f
addq $0x70, %rsp
movq 0x40(%rsp), %rdi
movq 0x50(%rdi), %rax
cmpq %rax, 0x48(%rdi)
jne 0x9b577
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x50(%rdi)
movq 0x58(%rdi), %rdi
shlq $0x5, %rsi
callq 0x2370
movq 0x40(%rsp), %rdi
movq %rax, 0x58(%rdi)
movq 0x48(%rdi), %rax
movq 0x58(%rdi), %rcx
leaq 0x1(%rax), %rdx
shlq $0x5, %rax
leaq (%rcx,%rax), %rbx
movq %rdx, 0x48(%rdi)
movq 0xc8(%rsp), %rdx
movl %edx, (%rcx,%rax)
movq 0xd0(%rsp), %rdx
movl %edx, 0x4(%rcx,%rax)
cmpl $0x0, 0xe8(%rsp)
movq 0x58(%rsp), %r10
je 0x9b5f4
leaq 0x48(%rdi), %rcx
movq 0x28(%rsp), %rdi
movl 0x7d70(%rdi), %esi
addq $0xa14, %rdi            # imm = 0xA14
leaq 0xc(%rsp), %rax
leaq 0x10(%rsp), %r8
leaq 0x1c(%rsp), %r9
movq %rbx, %rdx
pushq %r10
pushq %r14
pushq %r12
pushq %rax
callq 0x8663c
movq 0x60(%rsp), %rdi
addq $0x20, %rsp
jmp 0x9b5fc
movq $0x0, 0x18(%rbx)
movl 0xd8(%rsp), %eax
movl 0x10(%rsp), %ecx
addl %eax, %ecx
movl %ecx, 0x8(%rbx)
addl 0x1c(%rsp), %eax
movl %eax, 0xc(%rbx)
movzwl 0xc(%rsp), %eax
movw %ax, 0x14(%rbx)
movzwl 0x18(%rsp), %eax
movw %ax, 0x10(%rbx)
movzwl 0x14(%rsp), %eax
movw %ax, 0x12(%rbx)
movw %r15w, 0x16(%rbx)
addl %r14d, 0x28(%rdi)
movl $0x1, %r15d
movl %r15d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq