// File: /workspace/github_cpp_makefile_star2_O3/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, 0xd8(%rsp)
movq 0x48(%rdi), %rbx
testq %rbx, %rbx
je 0x4a50d
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rax
addq $0x78, %rdi
leaq -0x1(%rbx), %r12
movq %rdi, 0x78(%rsp)
movq %r12, %rsi
movq %rax, (%rsp)
callq 0x3f264
movq (%rsp), %r10
movl 0x10(%r10), %ecx
movl 0x1c(%r10), %eax
movl %eax, 0x6c(%rsp)
movq 0x18(%r15), %rax
movq %rcx, 0x80(%rsp)
movq (%rax,%rcx,8), %rax
movq %rax, 0xe0(%rsp)
movq $-0x1, %rax
movq %rax, 0x50(%rsp)
movq %rax, 0x48(%rsp)
movq %r12, 0x78(%r10)
cmpq $0x2, %rbx
movq %r15, 0x88(%rsp)
jl 0x4a4fb
movq %r12, %r11
leaq 0x48(%r10), %rax
movq %rax, 0x58(%rsp)
movq %r14, %rdx
leaq 0x50(%r14), %rax
movq %rax, 0x40(%rsp)
leaq 0x7d70(%r14), %rbp
leaq 0xff0(%r14), %rax
movq %rax, 0xb0(%rsp)
addq $0xa14, %rdx            # imm = 0xA14
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %rdx, 0x70(%rsp)
movq %r12, 0x18(%rsp)
movq %rbp, 0xb8(%rsp)
movq 0x58(%r10), %r15
movslq 0xc(%r15,%r14), %r12
cmpq $-0x1, %r12
je 0x49fb8
cmpl $-0x1, 0x2c(%r15,%r14)
je 0x49fb8
movslq 0x28(%r15,%r14), %rax
movl %r12d, %ecx
notl %ecx
movslq %ecx, %rcx
addq %rax, %rcx
movq %rcx, 0x60(%rsp)
je 0x49fe1
movq 0x88(%r10), %rax
jmp 0x4a059
movq 0x88(%r10), %rax
movw $0xffff, 0x14(%rax,%r14) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r14) # imm = 0xFFFFFFFF
incq %r13
addq $0x20, %r14
cmpq %r13, %r11
jne 0x49f7e
jmp 0x4a4fb
leaq (%r15,%r14), %rdi
leaq (%r15,%r14), %rbx
addq $0x20, %rbx
movl $0xa, %ecx
movl $0x1, %r8d
movq 0x58(%rsp), %rbp
movq %rbp, %rsi
leaq 0x48(%rsp), %rdx
callq 0x49a3e
movl $0xa, %ecx
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x50(%rsp), %rdx
xorl %r8d, %r8d
callq 0x49a3e
movq (%rsp), %r10
movq 0x50(%rsp), %rcx
movq 0x88(%r10), %rax
orq 0x48(%rsp), %rcx
jne 0x4a054
movw $0xfffe, 0x14(%rax,%r14) # imm = 0xFFFE
movl $0xffffffff, 0xc(%rax,%r14) # imm = 0xFFFFFFFF
movq 0x18(%rsp), %r11
jmp 0x49fd0
movq 0x18(%rsp), %r11
movslq 0x4(%r15,%r14), %rdi
movw %di, 0x10(%rax,%r14)
movw %r12w, 0x12(%rax,%r14)
movslq (%r15,%r14), %rcx
movq %rdi, %rbx
subq %rcx, %rbx
cmpq %r12, %rbx
cmovgeq %r12, %rbx
movl $0x64, %ecx
movq %rbx, %rsi
cmpq %rcx, %rbx
jl 0x4a08d
movl $0x64, %esi
movslq 0x24(%r15,%r14), %rdx
movq %rdi, %r9
subq %rdi, %rdx
incq %rdx
movq 0xe0(%rsp), %r15
subq %r12, %r15
cmpq %r15, %rdx
cmovlq %rdx, %r15
movq %r15, %rdx
cmpq %rcx, %r15
jl 0x4a0ba
movl $0x64, %edx
movq %rdx, 0x90(%rsp)
leaq (%rdx,%rsi), %rdi
cvtsi2sd %rdi, %xmm0
mulsd 0xe8(%rsp), %xmm0
cvttsd2si %xmm0, %rcx
movl $0x1, %edx
testq %rcx, %rcx
je 0x4a0e5
movl %ecx, %edx
cmpq $0x4, %rdi
cmovgel %edx, %ecx
leal (%rcx,%rcx), %ebp
movl $0x2, %edx
testl %ecx, %ecx
je 0x4a0fa
movl %ebp, %edx
cmpl $0x4, %edi
cmovgel %edx, %ebp
movl %ebp, %ecx
cmpl $0x20, %ebp
jge 0x4a10c
movl $0x1f, %ecx
movq %rdi, 0x8(%rsp)
cmpl $0x12c, %edi            # imm = 0x12C
cmovgel %ecx, %ebp
cmpl $0x1f, %ebp
jl 0x4a124
movl $0x1f, %ebp
movq %rsi, 0x98(%rsp)
subq %rsi, %r12
js 0x49fbf
movq 0x88(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x80(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, %rsi
subq %r12, %rsi
jle 0x49fbf
movslq %ebp, %rdx
movq 0x8(%rsp), %rdi
leaq (%rdi,%rdx,2), %r8
leal (,%rbp,2), %edi
movslq %edi, %rdi
addq %rdi, %rsi
addq $0x1f, %rsi
movq 0x40(%rsp), %rdi
cmpq %r8, %rsi
jl 0x49fbf
subq %rdx, %r12
subq %r12, %rcx
cmpq %r8, %rcx
cmovgeq %r8, %rcx
movq %r12, %rsi
testq %r12, %r12
jg 0x4a19b
xorl %esi, %esi
movl %r12d, %edx
negl %edx
sarq $0x3f, %r12
andl %edx, %r12d
movq %rcx, %rdx
subq %r12, %rdx
cmpq 0x8(%rsp), %rdx
jl 0x49fbf
movq %r9, 0xa8(%rsp)
subq 0x98(%rsp), %r9
movq %r9, 0x38(%rsp)
movq %r8, 0x20(%rsp)
movl %r8d, %eax
subl %ecx, %eax
movl %eax, 0x30(%rsp)
movslq %r12d, %rcx
movq %rcx, 0xc8(%rsp)
addq %rcx, %rdi
movq %rdi, 0xd0(%rsp)
movzbl 0x6c(%rsp), %ecx
movq %rsi, 0xa0(%rsp)
movq %rdx, 0xc0(%rsp)
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %r9
callq 0x32ef8
movq 0x40(%rsp), %rdi
movl $0x4e, %esi
movq 0xc8(%rsp), %rdx
callq 0x20e0
movq 0xd0(%rsp), %rdi
addq 0xc0(%rsp), %rdi
movslq 0x30(%rsp), %rdx
movl $0x4e, %esi
callq 0x20e0
movq 0xd8(%rsp), %rax
movq (%rax), %rdx
addq 0x38(%rsp), %rdx
movzwl %bp, %r8d
movq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rdx, 0x20(%rsp)
movq 0x8(%rsp), %rcx
leaq 0x14(%rsp), %r9
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
pushq %rax
pushq %rax
pushq 0x80(%rsp)
pushq 0xc8(%rsp)
movq 0xd8(%rsp), %rbp
pushq %rbp
leaq 0x50(%rsp), %rax
pushq %rax
callq 0x85726
addq $0x30, %rsp
movl %eax, 0x2c(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %eax, 0x14(%rsp)
je 0x4a387
movq 0x90(%rsp), %rax
movq 0xa8(%rsp), %rcx
addq %rax, %rcx
decq %rcx
movq (%rsp), %rax
movq 0x88(%rax), %rax
leaq (%rax,%r14), %rdx
movq 0x38(%rsp), %rsi
movl %esi, (%rax,%r14)
movq %rcx, 0x30(%rsp)
movl %ecx, 0x4(%rax,%r14)
movl (%rbp), %esi
movq 0x70(%rsp), %rdi
movq 0x78(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x2c(%rsp), %r9
pushq 0x40(%rsp)
pushq 0x10(%rsp)
pushq 0x30(%rsp)
leaq 0x2c(%rsp), %rax
pushq %rax
callq 0x85cd2
movq 0x20(%rsp), %r10
addq $0x20, %rsp
movq 0x60(%rsp), %rax
movq %rax, %rdi
negq %rdi
cmovsq %rax, %rdi
movl $0x32, %edx
movl $0x32, %ecx
testq %r14, %r14
jne 0x4a35b
movq 0x58(%r10), %rax
movslq (%rax), %rax
cmpq %rax, 0x38(%rsp)
jne 0x4a35b
xorl %ecx, %ecx
movq 0x58(%rsp), %rax
movq (%rax), %rax
leaq -0x2(%rax), %rsi
cmpq %rsi, %r13
jne 0x4a3a1
movq 0x58(%r10), %rdx
shlq $0x5, %rax
movslq -0x1c(%rdx,%rax), %rax
cmpq %rax, 0x30(%rsp)
je 0x4a39f
movl $0x32, %edx
jmp 0x4a3a1
movq (%rsp), %r10
movq 0x88(%r10), %rax
movw $0xffff, 0x14(%rax,%r14) # imm = 0xFFFF
jmp 0x4a041
xorl %edx, %edx
movq 0x88(%r10), %rbp
cmpq %rcx, %rbx
jle 0x4a481
cmpq %rdx, %r15
jle 0x4a481
movq %rdi, 0x60(%rsp)
leaq (%r14,%rbp), %rdi
movq %rdx, %rax
notq %rax
movq 0x8(%rsp), %r8
addq %rax, %r8
movq 0x78(%rsp), %rsi
movq %rdx, 0x8(%rsp)
leaq 0xf0(%rsp), %rdx
movq %r10, %rbx
movq %rcx, %r15
callq 0x49c14
movq 0x58(%rbx), %rbx
leaq (%rbx,%r14), %rdi
movq 0x98(%rsp), %rcx
movq %r15, 0x20(%rsp)
subq %r15, %rcx
movl $0x1, %r8d
movq 0x58(%rsp), %r15
movq %r15, %rsi
leaq 0x48(%rsp), %rdx
callq 0x49a3e
leaq (%rbx,%r14), %rdi
addq $0x20, %rdi
movq 0x8(%rsp), %rbx
movq 0x90(%rsp), %rcx
subq %rbx, %rcx
movq %r15, %rsi
leaq 0x50(%rsp), %rdx
xorl %r8d, %r8d
callq 0x49a3e
movq 0x60(%rsp), %rcx
addq 0x50(%rsp), %rcx
movq 0x48(%rsp), %rax
addq %rcx, %rax
incq %rax
cmpq 0xf0(%rsp), %rax
jge 0x4a4a3
movw $0xffff, 0x14(%rbp,%r14) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rbp,%r14) # imm = 0xFFFFFFFF
movl 0x18(%rbp,%r14), %eax
movq (%rsp), %r10
jmp 0x4a497
movw $0xffff, 0x14(%rbp,%r14) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rbp,%r14) # imm = 0xFFFFFFFF
movl 0x18(%rbp,%r14), %eax
movq %rax, 0x90(%r10)
jmp 0x4a04a
movzwl 0x14(%rsp), %eax
movw %ax, 0x14(%rbp,%r14)
movq 0xa0(%rsp), %rcx
subl %r12d, %ecx
movl 0x28(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x8(%rbp,%r14)
addl 0x2c(%rsp), %ecx
movl %ecx, 0xc(%rbp,%r14)
movq 0x38(%rsp), %rax
movl %eax, (%rbp,%r14)
movq 0x30(%rsp), %rax
movl %eax, 0x4(%rbp,%r14)
movq 0x20(%rsp), %rax
movw %ax, 0x10(%rbp,%r14)
movw %bx, 0x12(%rbp,%r14)
movq (%rsp), %r10
jmp 0x4a04a
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e7348(%rip), %rdi    # 0x23185c
leaq 0x1e7238(%rip), %rsi    # 0x231753
leaq 0x1e734a(%rip), %rcx    # 0x23186c
movl $0xa31, %edx            # imm = 0xA31
callq 0x2110