// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: calculate_ul_boundary_cigars(overlap_region*, ul_idx_t const*, Correct_dumy*, UC_Read*, double, long long)
void calculate_ul_boundary_cigars(overlap_region* z, const ul_idx_t *uref, Correct_dumy* dumy, 
UC_Read* g_read, double max_ov_diff_ec, long long blockLen)
{
    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;
    int64_t y_strand = z->y_pos_strand;
    int64_t y_readLen = uref->ug->u.a[y_id].len;
    int64_t i, y_distance;
    int64_t f_err, b_err, m_error, scanLen = 10;
    int64_t boundaryLen = WINDOW_UL_BOUND_RATE*blockLen;
    boundaryLen >>= 2; boundaryLen <<= 2;
    if(boundaryLen < WINDOW_UL_BOUND) boundaryLen = WINDOW_UL_BOUND;
    int64_t single_sideLen = boundaryLen/2;
    int64_t force_useless_side = single_sideLen/2;
    int64_t L_useless_side, R_useless_side;
    int64_t alpha = 1;
    long long y_start, x_start, x_end, yLen, xLen, leftLen, rightLen, threshold, o_len;
    int extra_begin, extra_end, end_site, real_y_start;
    char* x_string;
    char* y_string;
    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(sub_list->w_list[i].y_end == -1 && sub_list->w_list[i+1].y_end == -1)
        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;
        }

        ///note if w_list[i+1].y_start or sub_list->w_list[i].y_end is -1
        ///y_distance might have some problems at the last of this function
        ///we need to deal with it carefully
        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;
            }
        }


        ///y_distance can be less than 0, or larger than 0
        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 - (long long)z->w_list.a[i].x_start), y_start), single_sideLen);
        rightLen = MIN(MIN(((long long)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 * max_ov_diff_ec;
        threshold = Adjust_Threshold(threshold, xLen);
        threshold = double_ul_error_threshold(threshold, xLen);

        yLen = xLen + (threshold << 1);
        if(!determine_overlap_region(threshold, y_start, y_id, yLen, uref->ug->u.a[y_id].len, 
                    &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_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;

        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 == (long long)z->w_list.a[0].x_start)) {
                L_useless_side = 0;
            }
            ///last window
            if((i == (int64_t)(z->w_list.n) - 2) && (x_end == (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, 0xe0(%rsp)
movq 0x48(%rdi), %r14
testq %r14, %r14
je 0x4ac74
movq %r8, %rbx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rax
addq $0x78, %rdi
leaq -0x1(%r14), %r13
movq %rdi, 0xa8(%rsp)
movq %r13, %rsi
movq %rax, 0x30(%rsp)
callq 0x3f264
movq 0x30(%rsp), %r11
movl 0x10(%r11), %eax
movl 0x1c(%r11), %ecx
movl %ecx, 0x94(%rsp)
movq (%r12), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
mulsd 0x1e6fc4(%rip), %xmm0   # 0x231578
cvttsd2si %xmm0, %rax
movq %rdx, 0xb8(%rsp)
movl (%rcx,%rdx,8), %ecx
movq %rcx, 0x78(%rsp)
andq $-0x4, %rax
cmpq $0x31, %rax
movl $0x30, %ecx
cmovgeq %rax, %rcx
movq %r13, 0x78(%r11)
cmpq $0x2, %r14
jl 0x4ac62
movq %r12, %r10
leaq 0x48(%r11), %rax
movq %rax, 0x68(%rsp)
movq 0x78(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x78(%rsp)
movq %rcx, %r8
shrq %r8
shrq $0x2, %rcx
movq %rcx, 0x98(%rsp)
movq %r15, %rdx
leaq 0x50(%r15), %rcx
leaq 0x7d70(%r15), %rax
movq %rax, 0xc8(%rsp)
leaq 0xff0(%r15), %rax
movq %rax, 0xc0(%rsp)
addq $0xa14, %rdx            # imm = 0xA14
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rdx, 0xa0(%rsp)
movq %r10, 0x28(%rsp)
movq %r13, 0x38(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, 0x60(%rsp)
movq 0x58(%r11), %r14
movslq 0xc(%r14,%r12), %rbp
cmpq $-0x1, %rbp
je 0x4a69c
cmpl $-0x1, 0x2c(%r14,%r12)
je 0x4a69c
movslq 0x28(%r14,%r12), %rax
movl %ebp, %ecx
notl %ecx
movslq %ecx, %rcx
addq %rax, %rcx
movq %rcx, 0x70(%rsp)
je 0x4a6b9
movq 0x88(%r11), %rax
jmp 0x4a74a
movq 0x88(%r11), %rax
movw $0xffff, 0x14(%rax,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
jmp 0x4ac52
leaq (%r14,%r12), %rdi
leaq (%r14,%r12), %rax
addq $0x20, %rax
movq %rax, 0x8(%rsp)
movl $0xa, %ecx
movl $0x1, %r8d
movq 0x68(%rsp), %rbx
movq %rbx, %rsi
leaq 0x80(%rsp), %rdx
callq 0x49a3e
movl $0xa, %ecx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
leaq 0x88(%rsp), %rdx
xorl %r8d, %r8d
callq 0x49a3e
movq 0x30(%rsp), %r11
movq 0x88(%rsp), %rcx
movq 0x88(%r11), %rax
orq 0x80(%rsp), %rcx
jne 0x4a740
movw $0xfffe, 0x14(%rax,%r12) # imm = 0xFFFE
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
movq 0x28(%rsp), %r10
jmp 0x4ac4d
movq 0x28(%rsp), %r10
movq 0x10(%rsp), %r8
movslq 0x4(%r14,%r12), %rdx
movw %dx, 0x10(%rax,%r12)
movw %bp, 0x12(%rax,%r12)
movslq (%r14,%r12), %rcx
movq %rdx, %r13
subq %rcx, %r13
cmpq %rbp, %r13
cmovgeq %rbp, %r13
cmpq %r8, %r13
jl 0x4a774
movq %r8, %r13
movslq 0x24(%r14,%r12), %rcx
movq %rdx, 0x40(%rsp)
subq %rdx, %rcx
incq %rcx
movq 0x78(%rsp), %r14
subq %rbp, %r14
cmpq %r14, %rcx
cmovlq %rcx, %r14
cmpq %r8, %r14
jl 0x4a79b
movq %r8, %r14
leaq (%r14,%r13), %rsi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
mulsd 0xe8(%rsp), %xmm0
cvttsd2si %xmm0, %rbx
movl $0x1, %ecx
movl $0x1, %edx
testq %rbx, %rbx
je 0x4a7c6
movl %ebx, %edx
cmpq $0x4, %rsi
cmovgel %edx, %ebx
testl %ebx, %ebx
je 0x4a7d3
movl %ebx, %ecx
cmpl $0x4, %esi
cmovgel %ecx, %ebx
movq %rsi, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
mulsd 0x1e6d93(%rip), %xmm0   # 0x231580
cvttsd2si %xmm0, %ecx
cmpl %ecx, %ebx
cmovlel %ecx, %ebx
cmpl $0x1f, %ebx
jl 0x4a800
movl $0x1f, %ebx
subq %r13, %rbp
movq (%r10), %rcx
movq 0x10(%rcx), %rdx
movq 0xb8(%rsp), %rcx
movl (%rdx,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rcx, %rdi
subq %rbp, %rdi
jbe 0x4a850
movslq %ebx, %rsi
movq 0x8(%rsp), %r8
movq %r10, %r9
leaq (%r8,%rsi,2), %r10
leal (%rbx,%rbx), %r8d
movslq %r8d, %r8
addq %r8, %rdi
addq $0x1f, %rdi
movq 0x10(%rsp), %r8
cmpq %r10, %rdi
movq %r10, %rdi
movq %r9, %r10
jge 0x4a86b
movw $0xffff, 0x14(%rax,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
movq 0x38(%rsp), %r13
jmp 0x4ac52
subq %rsi, %rbp
subq %rbp, %rcx
cmpq %rdi, %rcx
movq %rdi, 0x20(%rsp)
cmovgeq %rdi, %rcx
movq %rbp, %rsi
testq %rbp, %rbp
jg 0x4a887
xorl %esi, %esi
movq %rsi, 0xb0(%rsp)
movl %ebp, %esi
negl %esi
sarq $0x3f, %rbp
andl %esi, %ebp
movq %rcx, %r10
subq %rbp, %r10
movq 0x8(%rsp), %rdi
movq %r10, 0x58(%rsp)
cmpq %rdi, %r10
jge 0x4a8cc
movw $0xffff, 0x14(%rax,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
movq 0x38(%rsp), %r13
movq %r9, %r10
jmp 0x4ac52
movq 0x40(%rsp), %rax
subq %r13, %rax
movq %rax, 0x50(%rsp)
movq 0xb8(%rsp), %rax
leaq (%rdx,%rax,8), %rdx
movq 0x20(%rsp), %rax
subl %ecx, %eax
movl %eax, 0x90(%rsp)
movslq %ebp, %rcx
movq %rcx, 0xd8(%rsp)
movq 0x60(%rsp), %rax
leaq (%rax,%rcx), %rsi
movq %rsi, 0xd0(%rsp)
subq $0x8, %rsp
movzbl 0x9c(%rsp), %ecx
xorl %edi, %edi
movq 0xb8(%rsp), %r8
movq 0x60(%rsp), %r9
pushq $0x0
callq 0x36049
addq $0x10, %rsp
movq 0x60(%rsp), %rdi
movl $0x4e, %esi
movq 0xd8(%rsp), %rdx
callq 0x20e0
movq 0xd0(%rsp), %rdi
addq 0x58(%rsp), %rdi
movslq 0x90(%rsp), %rdx
movl $0x4e, %esi
callq 0x20e0
movq 0xe0(%rsp), %rax
movq (%rax), %rdx
addq 0x50(%rsp), %rdx
movzwl %bx, %r8d
movq 0x60(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rdx, 0x20(%rsp)
movq 0x8(%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 0x70(%rsp), %rax
pushq %rax
callq 0x85726
addq $0x30, %rsp
movl %eax, 0x4c(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %eax, 0x1c(%rsp)
je 0x4aa76
movq 0x40(%rsp), %rax
leaq (%rax,%r14), %rsi
decq %rsi
movq %rbx, %rcx
movq 0x30(%rsp), %rbx
movq 0x88(%rbx), %rax
leaq (%rax,%r12), %rdx
movq 0x50(%rsp), %rdi
movl %edi, (%rax,%r12)
movq %rsi, 0x40(%rsp)
movl %esi, 0x4(%rax,%r12)
movl (%rcx), %esi
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0x4c(%rsp), %r9
pushq 0x60(%rsp)
pushq 0x10(%rsp)
pushq 0x30(%rsp)
leaq 0x34(%rsp), %rax
pushq %rax
callq 0x85cd2
movq %rbx, %r11
addq $0x20, %rsp
movq 0x70(%rsp), %rax
movq %rax, %rdi
negq %rdi
cmovsq %rax, %rdi
movq 0x98(%rsp), %rdx
movq %rdx, %rcx
testq %r12, %r12
jne 0x4aa9a
movq 0x58(%r11), %rax
movslq (%rax), %rax
cmpq %rax, 0x50(%rsp)
je 0x4aa98
movq %rdx, %rcx
jmp 0x4aa9a
movq 0x30(%rsp), %r11
movq 0x88(%r11), %rax
movw $0xffff, 0x14(%rax,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
jmp 0x4abbb
xorl %ecx, %ecx
movq 0x68(%rsp), %rax
movq (%rax), %rax
leaq -0x2(%rax), %rsi
cmpq %rsi, %r15
movq 0x28(%rsp), %r10
movq 0x10(%rsp), %r8
jne 0x4aad5
movq 0x58(%r11), %rdx
shlq $0x5, %rax
movslq -0x1c(%rdx,%rax), %rax
cmpq %rax, 0x40(%rsp)
je 0x4aad3
movq 0x98(%rsp), %rdx
jmp 0x4aad5
xorl %edx, %edx
movq 0x88(%r11), %rax
subq %rcx, %r13
jle 0x4abc5
subq %rdx, %r14
jle 0x4abc5
movq %rdi, 0x20(%rsp)
leaq (%rax,%r12), %rdi
movq %rax, 0x70(%rsp)
movq %rdx, %rax
notq %rax
movq 0x8(%rsp), %r8
addq %rax, %r8
movq 0xa8(%rsp), %rsi
movq %rdx, 0x8(%rsp)
leaq 0xf0(%rsp), %rdx
movq %r11, %rbx
movq %rcx, 0x58(%rsp)
callq 0x49c14
movq 0x58(%rbx), %rbx
leaq (%rbx,%r12), %rdi
movl $0x1, %r8d
movq 0x68(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq %r13, %rcx
callq 0x49a3e
leaq (%rbx,%r12), %rdi
addq $0x20, %rdi
movq 0x68(%rsp), %rsi
leaq 0x88(%rsp), %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x49a3e
movq 0x20(%rsp), %rcx
addq 0x88(%rsp), %rcx
movq 0x80(%rsp), %rax
addq %rcx, %rax
incq %rax
cmpq 0xf0(%rsp), %rax
jge 0x4abe7
movq 0x70(%rsp), %rax
movw $0xffff, 0x14(%rax,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
movl 0x18(%rax,%r12), %eax
movq 0x30(%rsp), %r11
movq %rax, 0x90(%r11)
movq 0x28(%rsp), %r10
jmp 0x4ac48
movw $0xffff, 0x14(%rax,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
movl 0x18(%rax,%r12), %eax
movq %rax, 0x90(%r11)
jmp 0x4a861
movzwl 0x1c(%rsp), %eax
movq 0x70(%rsp), %rdx
movw %ax, 0x14(%rdx,%r12)
movq 0xb0(%rsp), %rcx
subl %ebp, %ecx
movl 0x48(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x8(%rdx,%r12)
addl 0x4c(%rsp), %ecx
movl %ecx, 0xc(%rdx,%r12)
movq 0x50(%rsp), %rax
movl %eax, (%rdx,%r12)
movq 0x40(%rsp), %rax
movl %eax, 0x4(%rdx,%r12)
movq 0x58(%rsp), %rax
movw %ax, 0x10(%rdx,%r12)
movq 0x8(%rsp), %rax
movw %ax, 0x12(%rdx,%r12)
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r11
movq 0x38(%rsp), %r13
movq 0x10(%rsp), %r8
incq %r15
addq $0x20, %r12
cmpq %r15, %r13
jne 0x4a663
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e6be1(%rip), %rdi    # 0x23185c
leaq 0x1e6ad1(%rip), %rsi    # 0x231753
leaq 0x1e6c44(%rip), %rcx    # 0x2318cd
movl $0xae6, %edx            # imm = 0xAE6
callq 0x2110