// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %r9, 0x30(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, %r13
movq %rdi, 0x68(%rsp)
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rbp
movq 0xc0(%rsp), %rbx
andl $0x0, 0x4(%rsp)
subq 0xb8(%rsp), %rbx
leaq 0x1(%rbx), %r14
pushq $0x1
popq %rdx
testq %rsi, %rsi
je 0x64f2c
cmpq %rax, %r14
jge 0x64f5c
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
pushq $0x1f
popq %rcx
cmovlq %rax, %rcx
testq %rax, %rax
movl %ecx, %edi
cmovel %edx, %edi
cmpq $0x4, %r14
cmovll %ecx, %edi
jmp 0x64f5f
cmpq %rax, %r14
jge 0x64f69
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
pushq $0x1f
popq %rcx
cmovlq %rax, %rcx
testq %rax, %rax
pushq $0x1
popq %rdi
cmovnel %ecx, %edi
cmpq $0x4, %r14
cmovll %ecx, %edi
jmp 0x64f6c
pushq $0xf
popq %rdi
movl %r14d, %esi
callq 0x51fcd
jmp 0x64f74
pushq $0x1f
popq %rdi
movl %r14d, %esi
callq 0x526f4
movl %eax, %r12d
leal (%r12,%r12), %eax
movslq %eax, %r15
leaq (%r14,%r15), %rcx
movslq 0xb0(%rsp), %rdx
testq %r13, %r13
movq %rdx, 0x28(%rsp)
je 0x64f9e
movq 0x18(%r13), %rax
movq (%rax,%rdx,8), %r8
jmp 0x64fb9
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rdx, %rdx
movl (%rax,%rdx), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
subq $0x8, %rsp
leaq 0x78(%rsp), %rax
leaq 0xd0(%rsp), %rdx
leaq 0x18(%rsp), %r10
leaq 0x1c(%rsp), %r9
movl %r12d, %edi
movq %rbp, %rsi
movq %rcx, 0x48(%rsp)
pushq %rax
pushq %rdx
pushq %r10
callq 0x1b53a
addq $0x20, %rsp
xorl %ebp, %ebp
testl %eax, %eax
je 0x65307
movq 0x70(%rsp), %rdx
movslq %r12d, %rax
addq %rdx, %rax
cmpq %r14, %rax
jl 0x65307
movq 0x20(%rsp), %rax
leaq 0x50(%rax), %rdi
movq 0xc8(%rsp), %rsi
movslq 0x14(%rsp), %rax
movslq 0x10(%rsp), %r10
testq %r13, %r13
movq %rdi, 0x38(%rsp)
movq %rax, 0x60(%rsp)
movq %r10, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
je 0x65055
movzbl 0x30(%rsp), %ecx
movq %r13, %r8
movq 0x28(%rsp), %r9
pushq %r10
pushq %rax
callq 0x1b475
jmp 0x6506c
movzbl 0x30(%rsp), %ecx
movq 0x48(%rsp), %r8
movq 0x28(%rsp), %r9
pushq %r10
pushq %rax
callq 0x1b4ca
popq %rax
popq %rcx
movq 0x18(%rsp), %rax
movq (%rax), %rdx
addq 0xb8(%rsp), %rdx
cmpl $0x0, 0xd8(%rsp)
movq 0x40(%rsp), %rsi
movq %rdx, 0x18(%rsp)
je 0x65184
movq 0x20(%rsp), %rcx
leaq 0x7d70(%rcx), %rax
leaq 0xff0(%rcx), %r10
leaq 0xa14(%rcx), %r11
movzwl %r12w, %r8d
leaq 0xc(%rsp), %r9
movq 0x38(%rsp), %rdi
movl %r14d, %ecx
pushq $-0x1
pushq $-0x1
pushq %r11
pushq %r10
pushq %rax
leaq 0x2c(%rsp), %rax
pushq %rax
callq 0x51ff9
leaq 0x3c(%rsp), %r11
addq $0x30, %rsp
movl %eax, 0x8(%rsp)
movl (%r11), %r10d
pushq $-0x1
popq %rcx
cmpl %ecx, %r10d
je 0x65307
movslq %eax, %r9
addq %r15, %rbx
cmpq %r9, %rbx
sete %cl
movl 0x4(%rsp), %eax
testl %eax, %eax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x65212
movslq %eax, %r8
leaq 0x8(%rsp), %rax
testq %r13, %r13
je 0x651ac
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movl %r12d, %edx
movq 0x50(%rsp), %rcx
pushq %r11
leaq 0x18(%rsp), %r11
pushq %r11
leaq 0x24(%rsp), %r11
pushq %r11
pushq %rax
leaq 0x24(%rsp), %rax
pushq %rax
leaq 0xf0(%rsp), %rax
pushq %rax
pushq %r10
pushq 0x68(%rsp)
pushq 0x60(%rsp)
pushq %r13
pushq 0x90(%rsp)
pushq 0x80(%rsp)
pushq 0xb8(%rsp)
pushq 0xc8(%rsp)
callq 0x6380f
jmp 0x6520e
movzwl %r12w, %r8d
leaq 0xc(%rsp), %r15
movq 0x38(%rsp), %rdi
movl %r14d, %ecx
movq %r15, %r9
callq 0x516e4
movl %eax, 0x8(%rsp)
cmpl $-0x1, (%r15)
jne 0x65212
jmp 0x65307
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movl %r12d, %edx
movq 0x50(%rsp), %rcx
pushq %r11
leaq 0x18(%rsp), %r11
pushq %r11
leaq 0x24(%rsp), %r11
pushq %r11
pushq %rax
leaq 0x24(%rsp), %rax
pushq %rax
leaq 0xf0(%rsp), %rax
pushq %rax
pushq %r10
pushq 0x68(%rsp)
pushq 0x60(%rsp)
pushq 0x90(%rsp)
pushq 0x90(%rsp)
pushq 0x80(%rsp)
pushq 0xb8(%rsp)
pushq 0xc8(%rsp)
callq 0x63a35
addq $0x70, %rsp
movq 0x68(%rsp), %r13
leaq 0x48(%r13), %rbx
movq 0x48(%r13), %rcx
cmpq 0x50(%r13), %rcx
jne 0x6524e
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
pushq $0x2
popq %rsi
cmovneq %rax, %rsi
movq %rsi, 0x50(%r13)
movq 0x58(%r13), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%r13)
movq 0x48(%r13), %rcx
jmp 0x65252
movq 0x58(%r13), %rax
leaq 0x1(%rcx), %rdx
shlq $0x5, %rcx
leaq (%rax,%rcx), %r15
movq %rdx, (%rbx)
movq 0xb8(%rsp), %rdx
movl %edx, (%rax,%rcx)
movq 0xc0(%rsp), %rdx
movl %edx, 0x4(%rax,%rcx)
cmpl $0x0, 0xd8(%rsp)
je 0x652bf
movq 0x20(%rsp), %rdi
movl 0x7d70(%rdi), %esi
addq $0xa14, %rdi            # imm = 0xA14
leaq 0xc(%rsp), %rax
leaq 0x4(%rsp), %r8
leaq 0x8(%rsp), %r9
movq %r15, %rdx
movq %rbx, %rcx
pushq 0x38(%rsp)
pushq %r14
pushq 0x28(%rsp)
pushq %rax
callq 0x52542
addq $0x20, %rsp
jmp 0x652c4
andq $0x0, 0x18(%r15)
movl 0xc8(%rsp), %eax
movl 0x4(%rsp), %ecx
addl %eax, %ecx
movl %ecx, 0x8(%r15)
addl 0x8(%rsp), %eax
movl %eax, 0xc(%r15)
movzwl 0xc(%rsp), %eax
movw %ax, 0x14(%r15)
movzwl 0x14(%rsp), %eax
movw %ax, 0x10(%r15)
movzwl 0x10(%rsp), %eax
movw %ax, 0x12(%r15)
movw %r12w, 0x16(%r15)
addl %r14d, 0x28(%r13)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq