// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: calculate_boundary_cigars(overlap_region*, All_reads*, Correct_dumy*, UC_Read*, double)
void calculate_boundary_cigars(overlap_region* z, All_reads* R_INF, Correct_dumy* dumy, UC_Read* g_read, double e_rate)
{
    assert(z->w_list.n > 0);
    int64_t nw = z->w_list.n;
    resize_window_list_alloc(&(z->boundary_cigars), nw - 1);
    int64_t y_id = z->y_id, y_strand = z->y_pos_strand;
    int64_t y_readLen = Get_READ_LENGTH((*R_INF), y_id);
    int64_t i, y_distance, f_err = -1, b_err = -1, m_error;
    int64_t scanLen = 10, boundaryLen = 200;
    int64_t single_sideLen = boundaryLen/2;
    int64_t force_useless_side = single_sideLen/2;
    int64_t L_useless_side, R_useless_side, alpha = 1;
    long long y_start, x_start, x_end, yLen, xLen, leftLen, rightLen, threshold, o_len;
    char *x_string = NULL, *y_string = NULL;
    int end_site, real_y_start, extra_begin, extra_end;
    unsigned int error;
    z->boundary_cigars.n = nw - 1;
    ///the (i)-th boundary between the (i)-th window and the (i+1)-th window
    ///that means it includes (the tail of (i)-th window) and (the header of (i+1)-th window)
    ///note the (i)-th boundary is calculated at the (i)-th window
    for (i = 0; i + 1 < nw; i++) {
        ///if both of the two windows are not aligned
        ///it is not necessary to calculate the boundary
        if(z->w_list.a[i].y_end == -1 || z->w_list.a[i+1].y_end == -1) {
            z->boundary_cigars.a[i].error = -1; z->boundary_cigars.a[i].y_end = -1;
            continue;
        }
        ///y_distance can be less than 0, or larger than 0
        y_distance = (int64_t)z->w_list.a[i+1].y_start - (int64_t)z->w_list.a[i].y_end - 1;

        ///if two windows are aligned
        if(z->w_list.a[i].y_end != -1 && z->w_list.a[i+1].y_end != -1 && y_distance == 0) {
            ///scan backward
            scan_cigar(&(z->w_list.a[i]), &(z->w_list), &b_err, scanLen, 1);
            ///scan forward
            scan_cigar(&(z->w_list.a[i+1]), &(z->w_list), &f_err, scanLen, 0);
            if(b_err == 0 && f_err == 0) {
                z->boundary_cigars.a[i].error = -2; z->boundary_cigars.a[i].y_end = -1;
                continue;
            }
        }

        
        if(z->w_list.a[i].y_end != -1) {
            y_start = z->w_list.a[i].y_end; x_start = z->w_list.a[i].x_end;
        }///if the (i)-th window is not matched, have a look at the (i+1)-th window
        else if(z->w_list.a[i+1].y_end != -1) {
            y_start = z->w_list.a[i+1].y_start; x_start = z->w_list.a[i+1].x_start;
        }///if both of these two windows are not matched, directly skip
        else {
            z->boundary_cigars.a[i].error = -1; z->boundary_cigars.a[i].y_end = -1;
            continue;
        }

        ///it seems we don't need to record x_start and y_start
        z->boundary_cigars.a[i].extra_begin = x_start;
        z->boundary_cigars.a[i].extra_end = y_start;

        ///leftLen and rightLen are used for x        
        ///x should be at [sub_list->w_list[i].x_start, sub_list->w_list[i+1].x_end]
        ///y shouldn't have limitation
        ///note that the x_start and x_end should not be -1 in any case
        ///up to now, x_start and y_start are not -1
        ///leftLen does not include x_start itself, rightLen does
        ///gnerally speaking, rightLen should be always larger than leftLen
        leftLen = MIN(MIN((x_start - (int64_t)z->w_list.a[i].x_start), y_start), single_sideLen);
        rightLen = MIN(MIN(((int64_t)z->w_list.a[i+1].x_end + 1 - x_start), y_readLen - y_start), single_sideLen);

        ///xLen should be the sum length of two windows
        xLen = leftLen + rightLen;
        x_start = x_start - leftLen;
        x_end = x_start + xLen - 1;
        y_start = y_start - leftLen;

        ///if we don't have enough leftLen and rightLen
        // if(leftLen <= useless_side || rightLen <= useless_side)
        // {
        //     sub_list->boundary_cigars.buffer[i].error = -1;
        //     sub_list->boundary_cigars.buffer[i].y_end = -1;
        //     continue;
        // }


        threshold = xLen * e_rate/**asm_opt.max_ov_diff_ec**/;
        threshold = Adjust_Threshold(threshold, xLen);
        threshold = double_error_threshold(threshold, xLen);

        yLen = xLen + (threshold << 1);
        if(!determine_overlap_region(threshold, y_start, y_id, yLen, Get_READ_LENGTH((*R_INF), y_id), 
                    &extra_begin, &extra_end, &y_start, &o_len)) {
            z->boundary_cigars.a[i].error = -1; z->boundary_cigars.a[i].y_end = -1;
            continue;
        }

        if(o_len < xLen) {
            z->boundary_cigars.a[i].error = -1; z->boundary_cigars.a[i].y_end = -1;
            continue;
        }

        fill_subregion(dumy->overlap_region, y_start, o_len, y_strand, R_INF, y_id, extra_begin, extra_end);
        
        x_string = g_read->seq + x_start;
        y_string = dumy->overlap_region;

        end_site = Reserve_Banded_BPM_PATH(y_string, yLen, x_string, xLen, threshold, &error, 
        &real_y_start, &(dumy->path_length), dumy->matrix_bit, dumy->path, -1, -1);

        ///means this window is matched
        if (error!=(unsigned int)-1) {
            z->boundary_cigars.a[i].x_start = x_start;
            z->boundary_cigars.a[i].x_end = x_end;

            generate_cigar(dumy->path, dumy->path_length, &(z->boundary_cigars.a[i]), &(z->boundary_cigars),
                        &real_y_start, &end_site, &error, x_string, xLen, y_string);
            ///should not adjust cigar here, adjust cigar may cause problem
            ///that is not what we want

            ///y_distance can be less than 0, or larger than 0
            ///please if one of the two windows is not matched, 
            ///y_distance may have potential problems 
            if(y_distance < 0) y_distance = y_distance * (-1);
            ///leftLen, rightLen
            // if(leftLen <= useless_side || rightLen <= useless_side)
            // {
            //     sub_list->boundary_cigars.buffer[i].error = -1;
            //     sub_list->boundary_cigars.buffer[i].y_end = -1;
            //     continue;
            // }
            L_useless_side = R_useless_side = force_useless_side;

            ///first window
            if((i == 0) && (x_start == (int64_t)z->w_list.a[0].x_start)) {
                L_useless_side = 0;
            }
            ///last window
            if((i == (int64_t)(z->w_list.n) - 2) && 
                    (x_end == (long long)(z->w_list.a[(int64_t)(z->w_list.n)-1].x_end))) {
                R_useless_side = 0;
            }

            if(leftLen <= L_useless_side || rightLen <= R_useless_side) {
                z->boundary_cigars.a[i].error = -1; z->boundary_cigars.a[i].y_end = -1;
                z->boundary_cigars.c.n = z->boundary_cigars.a[i].cidx;
                continue;
            }



            ///up to now, if we require (i)-th window and (i+1)-th window are matched
            ///boundary_cigars.buffer[i].cigar, w_list[i].cigar and w_list[i+1].cigar are avaiable
            ///get the error excluding the first and the last useless_side bases
            scan_cigar_interval(&(z->boundary_cigars.a[i]), &(z->boundary_cigars), &m_error, L_useless_side, xLen-R_useless_side-1);
            scan_cigar(&(z->w_list.a[i]), &(z->w_list), &b_err, leftLen-L_useless_side, 1);
            scan_cigar(&(z->w_list.a[i+1]), &(z->w_list), &f_err, rightLen-R_useless_side, 0);

            if(f_err + b_err + y_distance + alpha < m_error) {
                z->boundary_cigars.a[i].error = -1; z->boundary_cigars.a[i].y_end = -1;
                z->boundary_cigars.c.n = z->boundary_cigars.a[i].cidx;
                continue;
            }

            z->boundary_cigars.a[i].error = error;
            z->boundary_cigars.a[i].y_start = y_start + real_y_start - extra_begin;
            z->boundary_cigars.a[i].y_end = y_start + end_site - extra_begin;

            z->boundary_cigars.a[i].x_start = x_start;
            z->boundary_cigars.a[i].x_end = x_end;
            ///sub_list->boundary_cigars.buffer[i].error_threshold = useless_side;
            z->boundary_cigars.a[i].extra_begin = L_useless_side;
            z->boundary_cigars.a[i].extra_end = R_useless_side;
        }
        else {
            z->boundary_cigars.a[i].error = -1; z->boundary_cigars.a[i].y_end = -1;
            continue;
        }     
    }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movsd %xmm0, 0xe8(%rsp)
movq %rcx, 0xd0(%rsp)
movq 0x48(%rdi), %rbx
testq %rbx, %rbx
je 0x4aeb5
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rax
addq $0x78, %rdi
leaq -0x1(%rbx), %r15
movq %rdi, 0x98(%rsp)
movq %r15, %rsi
movq %rax, 0x20(%rsp)
callq 0x3fdc3
movq 0x20(%rsp), %r10
movl 0x10(%r10), %ecx
movl 0x1c(%r10), %eax
movl %eax, 0x90(%rsp)
movq 0x18(%r14), %rax
movq %rcx, 0xa8(%rsp)
movq (%rax,%rcx,8), %rax
movq %rax, 0xe0(%rsp)
movq $-0x1, %rax
movq %rax, 0x68(%rsp)
movq %rax, 0x60(%rsp)
movq %r15, 0x78(%r10)
cmpq $0x2, %rbx
movq %r14, 0xb0(%rsp)
jl 0x4aea3
movq %r15, %r9
leaq 0x48(%r10), %rax
movq %rax, 0x78(%rsp)
leaq 0x50(%r12), %rax
movq %rax, 0x50(%rsp)
leaq 0x7d70(%r12), %rbx
leaq 0xff0(%r12), %rax
movq %rax, 0xc0(%rsp)
addq $0xa14, %r12            # imm = 0xA14
movq %r12, 0xa0(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %rax, 0x70(%rsp)
movq %r15, 0x8(%rsp)
movq %rbx, 0xc8(%rsp)
movq 0x58(%r10), %r14
movl 0xc(%r14,%r12), %eax
cmpl $-0x1, %eax
je 0x4a969
cmpl $-0x1, 0x2c(%r14,%r12)
je 0x4a969
leaq (%r14,%r12), %rbx
addq $0x20, %rbx
movslq 0x8(%rbx), %r15
notl %eax
cltq
addq %r15, %rax
movq %rax, 0x88(%rsp)
jne 0x4a975
leaq (%r14,%r12), %rdi
movl $0xa, %ecx
movl $0x1, %r8d
movq 0x78(%rsp), %r13
movq %r13, %rsi
leaq 0x60(%rsp), %rdx
movq %r11, 0x10(%rsp)
callq 0x4a359
movl $0xa, %ecx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x68(%rsp), %rdx
xorl %r8d, %r8d
callq 0x4a359
movq 0x10(%rsp), %r11
movq 0x8(%rsp), %r9
movq 0x20(%rsp), %r10
movq 0x68(%rsp), %rax
orq 0x60(%rsp), %rax
jne 0x4a975
movq 0x88(%r10), %rax
movw $0xfffe, 0x14(%rax,%r12) # imm = 0xFFFE
jmp 0x4ab13
movq 0x88(%r10), %rax
jmp 0x4ab0b
leaq (%r14,%r12), %rcx
addq $0x4, %rcx
movslq 0x8(%rcx), %rax
cmpq $-0x1, %rax
cmovneq %rax, %r15
cmovneq %rcx, %rbx
movslq (%rbx), %rsi
movq 0x88(%r10), %rax
movw %si, 0x10(%rax,%r12)
movw %r15w, 0x12(%rax,%r12)
movslq -0x4(%rcx), %rcx
movq %rsi, %rdx
subq %rcx, %rdx
cmpq %r15, %rdx
cmovgeq %r15, %rdx
movl $0x64, %ecx
movq %rdx, %r13
movq %rdx, 0x80(%rsp)
cmpq %rcx, %rdx
jl 0x4a9cf
movl $0x64, %r13d
movslq 0x24(%r14,%r12), %rdx
movq %rsi, 0x30(%rsp)
subq %rsi, %rdx
incq %rdx
movq 0xe0(%rsp), %r14
subq %r15, %r14
cmpq %r14, %rdx
cmovlq %rdx, %r14
movq %r14, %rdx
cmpq %rcx, %r14
jl 0x4a9fe
movl $0x64, %edx
movq %rdx, 0xb8(%rsp)
leaq (%rdx,%r13), %r8
cvtsi2sd %r8, %xmm0
mulsd 0xe8(%rsp), %xmm0
cvttsd2si %xmm0, %rcx
movl $0x1, %edx
testq %rcx, %rcx
je 0x4aa29
movl %ecx, %edx
cmpq $0x4, %r8
cmovgel %edx, %ecx
leal (%rcx,%rcx), %ebx
movl $0x2, %edx
testl %ecx, %ecx
je 0x4aa3e
movl %ebx, %edx
cmpl $0x4, %r8d
cmovgel %edx, %ebx
movl %ebx, %ecx
cmpl $0x20, %ebx
jge 0x4aa51
movl $0x1f, %ecx
cmpl $0x12c, %r8d            # imm = 0x12C
cmovgel %ecx, %ebx
cmpl $0x1f, %ebx
jl 0x4aa65
movl $0x1f, %ebx
subq %r13, %r15
movslq %ebx, %rsi
leaq (%r8,%rsi,2), %rcx
movq %rcx, 0x38(%rsp)
movb $0x1, %cl
js 0x4ab02
movq 0xb0(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq 0xa8(%rsp), %rdi
movq (%rdx,%rdi,8), %rdx
movq %rdx, %rdi
subq %r15, %rdi
jle 0x4ab02
movq %r13, 0x58(%rsp)
leal (%rbx,%rbx), %r9d
movslq %r9d, %r9
addq %r9, %rdi
addq $0x1f, %rdi
movq 0x38(%rsp), %r9
cmpq %r9, %rdi
jl 0x4aaf8
movq %r9, %rdi
subq %rsi, %r15
subq %r15, %rdx
cmpq %r9, %rdx
cmovgeq %r9, %rdx
xorl %ecx, %ecx
movq %r15, %rsi
testq %r15, %r15
jg 0x4aad5
xorl %esi, %esi
subl %edx, %edi
movl %edi, 0x94(%rsp)
movl %r15d, %edi
negl %edi
sarq $0x3f, %r15
andl %edi, %r15d
movq %r15, 0x70(%rsp)
subq %r15, %rdx
movq %rsi, %r15
movq %rdx, %r11
movq 0x58(%rsp), %r13
movq 0x8(%rsp), %r9
testb %cl, %cl
jne 0x4ab0b
cmpq %r8, %r11
jge 0x4ab31
movw $0xffff, 0x14(%rax,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
incq %rbp
addq $0x20, %r12
cmpq %rbp, %r9
jne 0x4a8c1
jmp 0x4aea3
movq 0x30(%rsp), %rax
movq %r13, 0x58(%rsp)
subq %r13, %rax
movq %rax, 0x48(%rsp)
movslq 0x70(%rsp), %rcx
movq %rcx, 0xd8(%rsp)
movq 0x50(%rsp), %rax
leaq (%rax,%rcx), %r13
movzbl 0x90(%rsp), %ecx
movq %r8, 0x28(%rsp)
movq %r13, %rdi
movq %r15, %rsi
movq %r11, %rdx
movq 0xb0(%rsp), %r8
movq %r11, 0x10(%rsp)
movq 0xa8(%rsp), %r9
callq 0x337f7
movq 0x50(%rsp), %rdi
movl $0x4e, %esi
movq 0xd8(%rsp), %rdx
callq 0x20e0
addq 0x10(%rsp), %r13
movslq 0x94(%rsp), %rdx
movq %r13, %rdi
movl $0x4e, %esi
callq 0x20e0
movq 0xd0(%rsp), %rax
movq (%rax), %r13
addq 0x48(%rsp), %r13
movzwl %bx, %r8d
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %r13, %rdx
movq 0x28(%rsp), %rcx
leaq 0x1c(%rsp), %r9
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
pushq %rax
pushq %rax
pushq 0xb0(%rsp)
pushq 0xd8(%rsp)
movq 0xe8(%rsp), %rbx
pushq %rbx
leaq 0x68(%rsp), %rax
pushq %rax
callq 0x8607d
addq $0x30, %rsp
movl %eax, 0x44(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %eax, 0x1c(%rsp)
je 0x4acf9
movq 0xb8(%rsp), %rax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rax), %rsi
decq %rsi
movq %rbx, %rcx
movq 0x20(%rsp), %rbx
movq 0x88(%rbx), %rax
leaq (%rax,%r12), %rdx
movq 0x48(%rsp), %rdi
movl %edi, (%rax,%r12)
movq %rsi, 0x38(%rsp)
movl %esi, 0x4(%rax,%r12)
movl (%rcx), %esi
movq 0xa0(%rsp), %rdi
movq 0x98(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x44(%rsp), %r9
pushq 0x50(%rsp)
pushq 0x30(%rsp)
pushq %r13
leaq 0x34(%rsp), %rax
pushq %rax
callq 0x8663c
movq %rbx, %r10
addq $0x20, %rsp
movq 0x88(%rsp), %rax
movq %rax, %r8
negq %r8
cmovsq %rax, %r8
movl $0x32, %esi
movl $0x32, %ebx
testq %r12, %r12
jne 0x4acc8
movq 0x58(%r10), %rax
movslq (%rax), %rax
cmpq %rax, 0x48(%rsp)
jne 0x4acc8
xorl %ebx, %ebx
movq 0x78(%rsp), %rax
movq (%rax), %rax
leaq -0x2(%rax), %rdi
cmpq %rdi, %rbp
movq 0x8(%rsp), %r9
jne 0x4ad1d
movq 0x58(%r10), %rsi
shlq $0x5, %rax
movslq -0x1c(%rsi,%rax), %rax
cmpq %rax, 0x38(%rsp)
je 0x4ad1b
movl $0x32, %esi
jmp 0x4ad1d
movq 0x20(%rsp), %r10
movq 0x88(%r10), %rax
movw $0xffff, 0x14(%rax,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
jmp 0x4ae94
xorl %esi, %esi
movq 0x88(%r10), %r13
cmpq %rbx, 0x80(%rsp)
jle 0x4ae20
cmpq %rsi, %r14
jle 0x4ae20
leaq (%r12,%r13), %rdi
movq %rsi, %rax
notq %rax
movq %r8, 0x88(%rsp)
movq 0x28(%rsp), %r8
addq %rax, %r8
movq %rsi, 0x30(%rsp)
movq 0x98(%rsp), %rsi
leaq 0xf0(%rsp), %rdx
movq %rbx, %rcx
movq %r10, %r14
callq 0x4a52a
movq 0x58(%r14), %rax
movq %rax, 0x80(%rsp)
leaq (%rax,%r12), %rdi
movq 0x58(%rsp), %rcx
movq %rbx, 0x28(%rsp)
subq %rbx, %rcx
movl $0x1, %r8d
movq 0x78(%rsp), %r14
movq %r14, %rsi
leaq 0x60(%rsp), %rdx
callq 0x4a359
movq 0x80(%rsp), %rax
leaq (%rax,%r12), %rdi
addq $0x20, %rdi
movq 0xb8(%rsp), %rcx
movq 0x30(%rsp), %rax
subq %rax, %rcx
movq %r14, %rsi
leaq 0x68(%rsp), %rdx
xorl %r8d, %r8d
callq 0x4a359
movq 0x88(%rsp), %rcx
addq 0x68(%rsp), %rcx
movq 0x60(%rsp), %rax
addq %rcx, %rax
incq %rax
cmpq 0xf0(%rsp), %rax
jge 0x4ae3f
movw $0xffff, 0x14(%r13,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%r13,%r12) # imm = 0xFFFFFFFF
movl 0x18(%r13,%r12), %eax
movq 0x20(%rsp), %r10
movq %rax, 0x90(%r10)
jmp 0x4ae94
movw $0xffff, 0x14(%r13,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%r13,%r12) # imm = 0xFFFFFFFF
movl 0x18(%r13,%r12), %eax
movq %rax, 0x90(%r10)
jmp 0x4ae99
movzwl 0x1c(%rsp), %eax
movw %ax, 0x14(%r13,%r12)
subl 0x70(%rsp), %r15d
movl 0x40(%rsp), %eax
addl %r15d, %eax
movl %eax, 0x8(%r13,%r12)
addl 0x44(%rsp), %r15d
movl %r15d, 0xc(%r13,%r12)
movq 0x48(%rsp), %rax
movl %eax, (%r13,%r12)
movq 0x38(%rsp), %rax
movl %eax, 0x4(%r13,%r12)
movq 0x28(%rsp), %rax
movw %ax, 0x10(%r13,%r12)
movq 0x30(%rsp), %rax
movw %ax, 0x12(%r13,%r12)
movq 0x20(%rsp), %r10
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r11
jmp 0x4ab1c
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1da890(%rip), %rdi    # 0x22574c
leaq 0x1da780(%rip), %rsi    # 0x225643
leaq 0x1da892(%rip), %rcx    # 0x22575c
movl $0xa31, %edx            # imm = 0xA31
callq 0x2110