// File: /workspace/github_cpp_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movsd %xmm0, 0xe0(%rsp)
movq %rcx, 0xd0(%rsp)
movq 0x48(%rdi), %rax
testq %rax, %rax
je 0x1dbcb
movq %rdx, %r14
movq %rsi, %r15
leaq 0x48(%rdi), %rcx
movq %rcx, 0x58(%rsp)
leaq 0x78(%rdi), %rcx
leaq -0x1(%rax), %rbx
movq %rdi, 0x10(%rsp)
movq %rcx, 0x78(%rsp)
movq %rcx, %rdi
movq %rbx, %rsi
movq %rax, 0x18(%rsp)
callq 0x13dc8
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
movl 0x10(%rdx), %esi
movb 0x1c(%rdx), %al
movb %al, 0xf(%rsp)
movq %r15, 0xa0(%rsp)
movq 0x18(%r15), %rax
movq %rsi, 0x98(%rsp)
movq (%rax,%rsi,8), %rax
movq %rax, 0xd8(%rsp)
leaq 0x48(%rsp), %rax
orq $-0x1, (%rax)
leaq 0x40(%rsp), %rax
orq $-0x1, (%rax)
movq %rbx, 0x78(%rdx)
leaq 0x50(%r14), %rax
movq %rax, 0x60(%rsp)
leaq 0x7d70(%r14), %rax
movq %rax, 0xc8(%rsp)
leaq 0xff0(%r14), %rax
movq %rax, 0xc0(%rsp)
addq $0xa14, %r14            # imm = 0xA14
movq %r14, 0x80(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r13, %r12
incq %r13
cmpq %rcx, %r13
jge 0x1dbb9
movq 0x58(%rdx), %r14
movslq 0xc(%r14,%rbp), %r15
cmpq $-0x1, %r15
je 0x1d76b
cmpl $-0x1, 0x2c(%r14,%rbp)
je 0x1d76b
movslq 0x28(%r14,%rbp), %rax
movl %r15d, %ecx
notl %ecx
movslq %ecx, %rcx
addq %rax, %rcx
movq %rcx, 0x68(%rsp)
jne 0x1d782
leaq (%r14,%rbp), %rdi
leaq (%r14,%rbp), %rax
addq $0x20, %rax
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %rbx
movq %rbx, %rsi
leaq 0x40(%rsp), %rdx
pushq $0xa
popq %rcx
pushq $0x1
popq %r8
callq 0x1d1fa
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
leaq 0x48(%rsp), %rdx
pushq $0xa
popq %rcx
xorl %r8d, %r8d
callq 0x1d1fa
movq 0x10(%rsp), %rdx
movq 0x48(%rsp), %rax
orq 0x40(%rsp), %rax
jne 0x1d782
movq 0x88(%rdx), %rax
movw $0xfffe, 0x14(%rax,%rbp) # imm = 0xFFFE
jmp 0x1da2d
movq 0x88(%rdx), %rax
orw $-0x1, 0x14(%rax,%rbp)
orl $-0x1, 0xc(%rax,%rbp)
jmp 0x1da37
movslq 0x4(%r14,%rbp), %rdi
movq 0x88(%rdx), %rax
movw %di, 0x10(%rax,%rbp)
movw %r15w, 0x12(%rax,%rbp)
movslq (%r14,%rbp), %rax
movq %rdi, %rsi
subq %rax, %rsi
cmpq %r15, %rsi
cmovgeq %r15, %rsi
movq %rsi, %rdx
pushq $0x64
popq %rcx
movq %rsi, 0xe8(%rsp)
cmpq %rcx, %rsi
jl 0x1d7c0
movq %rcx, %rdx
movslq 0x24(%r14,%rbp), %rax
movq %rdi, 0x30(%rsp)
subq %rdi, %rax
incq %rax
movq 0xd8(%rsp), %rbx
subq %r15, %rbx
cmpq %rbx, %rax
cmovlq %rax, %rbx
movq %rbx, %rax
cmpq %rcx, %rbx
jl 0x1d7ed
movq %rcx, %rax
movq %rax, 0x88(%rsp)
leaq (%rax,%rdx), %r14
movq %rdx, 0x90(%rsp)
subq %rdx, %r15
movq %r15, 0xa8(%rsp)
cvtsi2sd %r14, %xmm0
mulsd 0xe0(%rsp), %xmm0
cvttsd2si %xmm0, %rdi
pushq $0x1
popq %rax
testq %rdi, %rdi
je 0x1d829
movl %edi, %eax
cmpq $0x4, %r14
cmovgel %eax, %edi
movl %r14d, %esi
callq 0x51fcd
movq %r14, %rcx
movl %eax, %r14d
cltq
movq %rcx, 0x20(%rsp)
leaq (%rcx,%rax,2), %rdx
movq 0xa0(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x98(%rsp), %rcx
movq (%rax,%rcx,8), %r8
subq $0x8, %rsp
movl %r14d, %edi
movq %r15, %rsi
movq %rdx, 0xf8(%rsp)
movq %rdx, %rcx
leaq 0x7c(%rsp), %r9
leaq 0x100(%rsp), %rax
pushq %rax
leaq 0xb8(%rsp), %rax
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
callq 0x1b53a
addq $0x20, %rsp
testl %eax, %eax
je 0x1da1b
movq 0xf8(%rsp), %rdx
cmpq 0x20(%rsp), %rdx
jl 0x1da1b
movq 0x30(%rsp), %r15
subq 0x90(%rsp), %r15
movq 0xa8(%rsp), %rsi
movl 0x74(%rsp), %r10d
movl 0x70(%rsp), %eax
movzbl 0xf(%rsp), %ecx
movq 0x60(%rsp), %rdi
movq %rsi, 0xb8(%rsp)
movq 0xa0(%rsp), %r8
movq 0x98(%rsp), %r9
pushq %rax
movq %r10, 0xb8(%rsp)
pushq %r10
callq 0x1b475
popq %rax
popq %rcx
movq 0xd0(%rsp), %rax
movq (%rax), %rax
movq %r15, 0x50(%rsp)
addq %r15, %rax
movq %rax, %r15
movzwl %r14w, %r8d
movq 0x60(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq %rax, %rdx
movq 0x20(%rsp), %rcx
leaq 0x2c(%rsp), %r9
pushq $-0x1
pushq $-0x1
pushq 0x90(%rsp)
pushq 0xd8(%rsp)
movq 0xe8(%rsp), %r14
pushq %r14
leaq 0x60(%rsp), %rax
pushq %rax
callq 0x51ff9
addq $0x30, %rsp
movl %eax, 0x3c(%rsp)
pushq $-0x1
popq %rax
cmpl %eax, 0x2c(%rsp)
je 0x1da1b
movq 0x88(%rsp), %rax
movq 0x30(%rsp), %rcx
addq %rax, %rcx
decq %rcx
movq 0x10(%rsp), %rax
movq 0x88(%rax), %rax
leaq (%rax,%rbp), %rdx
movq 0x50(%rsp), %rsi
movl %esi, (%rax,%rbp)
movq %rcx, 0x30(%rsp)
movl %ecx, 0x4(%rax,%rbp)
movl (%r14), %esi
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x3c(%rsp), %r9
pushq 0x60(%rsp)
movq 0x28(%rsp), %r14
pushq %r14
pushq %r15
leaq 0x44(%rsp), %rax
pushq %rax
callq 0x52542
movq 0x30(%rsp), %rdx
addq $0x20, %rsp
movq 0x68(%rsp), %rax
movq %rax, %r15
negq %r15
cmovsq %rax, %r15
pushq $0x32
popq %rsi
movq %rsi, %rcx
testq %rbp, %rbp
movq %r14, %r8
jne 0x1da42
movq 0x58(%rdx), %rax
movslq (%rax), %rax
cmpq %rax, 0x50(%rsp)
je 0x1da40
movq %rsi, %rcx
jmp 0x1da42
movq 0x10(%rsp), %rdx
movq 0x88(%rdx), %rax
orw $-0x1, 0x14(%rax,%rbp)
orl $-0x1, 0xc(%rax,%rbp)
movq 0x18(%rsp), %rcx
addq $0x20, %rbp
jmp 0x1d6c4
xorl %ecx, %ecx
movq 0x58(%rsp), %rax
movq (%rax), %rax
leaq -0x2(%rax), %rdi
cmpq %rdi, %r12
jne 0x1da6e
movq 0x58(%rdx), %rsi
shlq $0x5, %rax
movslq -0x1c(%rsi,%rax), %rax
cmpq %rax, 0x30(%rsp)
je 0x1da6c
pushq $0x32
popq %rsi
jmp 0x1da6e
xorl %esi, %esi
movq 0x88(%rdx), %r12
cmpq %rcx, 0xe8(%rsp)
jle 0x1db3f
cmpq %rsi, %rbx
jle 0x1db3f
movq %rdx, %rbx
leaq (%r12,%rbp), %rdi
movq %rsi, %rax
notq %rax
addq %rax, %r8
movq %rsi, 0x20(%rsp)
movq 0x78(%rsp), %rsi
leaq 0x100(%rsp), %rdx
movq %rcx, %r14
callq 0x1d3a1
movq 0x58(%rbx), %rbx
leaq (%rbx,%rbp), %rdi
movq 0x90(%rsp), %rcx
movq %r14, 0x68(%rsp)
subq %r14, %rcx
movq 0x58(%rsp), %r14
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
pushq $0x1
popq %r8
callq 0x1d1fa
leaq (%rbx,%rbp), %rdi
addq $0x20, %rdi
movq 0x20(%rsp), %rbx
movq 0x88(%rsp), %rcx
subq %rbx, %rcx
movq %r14, %rsi
leaq 0x48(%rsp), %rdx
xorl %r8d, %r8d
callq 0x1d1fa
addq 0x48(%rsp), %r15
movq 0x40(%rsp), %rax
addq %r15, %rax
incq %rax
cmpq 0x100(%rsp), %rax
jge 0x1db5d
orw $-0x1, 0x14(%r12,%rbp)
orl $-0x1, 0xc(%r12,%rbp)
movl 0x18(%r12,%rbp), %eax
movq 0x10(%rsp), %rdx
jmp 0x1db51
orw $-0x1, 0x14(%r12,%rbp)
orl $-0x1, 0xc(%r12,%rbp)
movl 0x18(%r12,%rbp), %eax
movq %rax, 0x90(%rdx)
jmp 0x1da32
movzwl 0x2c(%rsp), %eax
movw %ax, 0x14(%r12,%rbp)
movq 0xb8(%rsp), %rcx
subl 0xb0(%rsp), %ecx
movl 0x38(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x8(%r12,%rbp)
addl 0x3c(%rsp), %ecx
movl %ecx, 0xc(%r12,%rbp)
movq 0x50(%rsp), %rax
movl %eax, (%r12,%rbp)
movq 0x30(%rsp), %rax
movl %eax, 0x4(%r12,%rbp)
movq 0x68(%rsp), %rax
movw %ax, 0x10(%r12,%rbp)
movw %bx, 0x12(%r12,%rbp)
movq 0x10(%rsp), %rdx
jmp 0x1da32
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c5bda(%rip), %rdi    # 0x1e37ac
leaq 0x1c5aca(%rip), %rsi    # 0x1e36a3
leaq 0x1c5bdc(%rip), %rcx    # 0x1e37bc
movl $0xa31, %edx            # imm = 0xA31
callq 0x2110