// File: /workspace/github_cpp_makefile_star2_O1/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, 0xe0(%rsp)
movq %rcx, 0xd0(%rsp)
movq 0x48(%rdi), %r14
testq %r14, %r14
je 0x4b5f0
movq %r8, %rbx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rax
addq $0x78, %rdi
leaq -0x1(%r14), %r13
movq %rdi, 0xa0(%rsp)
movq %r13, %rsi
movq %rax, (%rsp)
callq 0x3fdc3
movq (%rsp), %r9
movl 0x10(%r9), %eax
movl 0x1c(%r9), %ecx
movl %ecx, 0x90(%rsp)
movq (%r12), %rcx
movq 0x10(%rcx), %rcx
leaq (%rax,%rax,4), %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
mulsd 0x1da51e(%rip), %xmm0   # 0x225478
cvttsd2si %xmm0, %rax
movq %rdx, 0xb0(%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(%r9)
cmpq $0x2, %r14
movq %r12, 0xd8(%rsp)
jl 0x4b5de
movq %r15, %rdx
movq %r13, %r15
leaq 0x48(%r9), %rax
movq %rax, 0x68(%rsp)
movq 0x78(%rsp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x78(%rsp)
movq %rcx, %rax
shrq %rax
movq %rax, 0x48(%rsp)
shrq $0x2, %rcx
movq %rcx, 0x98(%rsp)
leaq 0x50(%rdx), %r14
leaq 0x7d70(%rdx), %rbx
leaq 0xff0(%rdx), %rax
movq %rax, 0xb8(%rsp)
addq $0xa14, %rdx            # imm = 0xA14
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %rax, 0x58(%rsp)
movq %rdx, 0xa8(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x38(%rsp)
movq %rbx, 0xc0(%rsp)
movq 0x58(%r9), %rbp
movl 0xc(%rbp,%r12), %eax
cmpl $-0x1, %eax
je 0x4b0bd
cmpl $-0x1, 0x2c(%rbp,%r12)
je 0x4b0bd
leaq (%r12,%rbp), %rbx
addq $0x20, %rbx
movslq 0x8(%rbx), %rsi
notl %eax
cltq
addq %rsi, %rax
movq %rax, 0x70(%rsp)
jne 0x4b0da
leaq (%r12,%rbp), %rdi
movl $0xa, %ecx
movl $0x1, %r8d
movq 0x68(%rsp), %r14
movq %rsi, 0x20(%rsp)
movq %r14, %rsi
leaq 0x80(%rsp), %rdx
callq 0x4a359
movl $0xa, %ecx
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x88(%rsp), %rdx
xorl %r8d, %r8d
callq 0x4a359
movq 0x20(%rsp), %rsi
movq (%rsp), %r9
movq 0x88(%rsp), %rax
orq 0x80(%rsp), %rax
jne 0x4b0da
movq 0x88(%r9), %rax
movw $0xfffe, 0x14(%rax,%r12) # imm = 0xFFFE
jmp 0x4b0cc
movq 0x88(%r9), %rax
movw $0xffff, 0x14(%rax,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
jmp 0x4b433
leaq (%r12,%rbp), %rcx
addq $0x4, %rcx
movslq 0x8(%rcx), %rax
cmpq $-0x1, %rax
cmovneq %rax, %rsi
cmovneq %rcx, %rbx
movslq (%rbx), %rdx
movq 0x88(%r9), %rax
movw %dx, 0x10(%rax,%r12)
movw %si, 0x12(%rax,%r12)
movslq -0x4(%rcx), %rcx
movq %rdx, %r15
subq %rcx, %r15
cmpq %rsi, %r15
cmovgeq %rsi, %r15
cmpq 0x48(%rsp), %r15
jl 0x4b125
movq 0x48(%rsp), %r15
movslq 0x24(%rbp,%r12), %rcx
movq %rdx, %r10
subq %rdx, %rcx
incq %rcx
movq 0x78(%rsp), %rdx
subq %rsi, %rdx
cmpq %rdx, %rcx
cmovlq %rcx, %rdx
cmpq 0x48(%rsp), %rdx
jl 0x4b14e
movq 0x48(%rsp), %rdx
movq %rdx, 0x40(%rsp)
leaq (%rdx,%r15), %r8
cvtsi2sd %r8, %xmm0
mulsd 0xe0(%rsp), %xmm0
cvttsd2si %xmm0, %rbx
movl $0x1, %ecx
movl $0x1, %edx
testq %rbx, %rbx
movq 0xe8(%rsp), %r14
je 0x4b183
movl %ebx, %edx
cmpq $0x4, %r8
cmovgel %edx, %ebx
testl %ebx, %ebx
je 0x4b190
movl %ebx, %ecx
cmpl $0x4, %r8d
cmovgel %ecx, %ebx
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
mulsd 0x1da2d9(%rip), %xmm0   # 0x225480
cvttsd2si %xmm0, %ecx
cmpl %ecx, %ebx
cmovlel %ecx, %ebx
cmpl $0x1f, %ebx
jl 0x4b1ba
movl $0x1f, %ebx
subq %r15, %rsi
movslq %ebx, %rdi
movq %r8, 0x8(%rsp)
leaq (%r8,%rdi,2), %r11
movq 0xd8(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movq 0xb0(%rsp), %rdx
movq %rsi, %rbp
movl (%rcx,%rdx,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movb $0x1, %dl
movq %rsi, %r8
movq %rbp, 0x20(%rsp)
subq %rbp, %r8
movq %r11, 0x28(%rsp)
jbe 0x4b26b
leal (%rbx,%rbx), %r9d
movslq %r9d, %r9
addq %r9, %r8
addq $0x1f, %r8
cmpq %r11, %r8
jge 0x4b219
movq (%rsp), %r9
jmp 0x4b26b
movq 0x20(%rsp), %r8
subq %rdi, %r8
subq %r8, %rsi
cmpq %r11, %rsi
cmovgeq %r11, %rsi
xorl %edx, %edx
movq %r8, %rdi
movq %r8, %rbp
testq %r8, %r8
movq (%rsp), %r9
jg 0x4b23e
xorl %edi, %edi
movq 0x28(%rsp), %r8
subl %esi, %r8d
movl %r8d, 0x94(%rsp)
movl %ebp, %r8d
negl %r8d
sarq $0x3f, %rbp
andl %r8d, %ebp
movq %rbp, 0x58(%rsp)
subq %rbp, %rsi
movq %rdi, 0x20(%rsp)
movq %rsi, %r14
movq 0x8(%rsp), %rsi
testb %dl, %dl
movq %r14, 0xe8(%rsp)
jne 0x4b41d
cmpq %rsi, %r14
jl 0x4b41d
movq %r10, 0xc8(%rsp)
subq %r15, %r10
movq %r10, 0x60(%rsp)
movq 0xb0(%rsp), %rax
leaq (%rcx,%rax,8), %rdx
movslq 0x58(%rsp), %rcx
movq %rcx, 0x50(%rsp)
movq 0x38(%rsp), %rax
leaq (%rax,%rcx), %rbp
subq $0x8, %rsp
movzbl 0x98(%rsp), %ecx
xorl %edi, %edi
movq %rbp, %rsi
movq 0x28(%rsp), %r8
movq %r14, %r9
pushq $0x0
callq 0x36b0d
addq $0x10, %rsp
movq 0x38(%rsp), %rdi
movl $0x4e, %esi
movq 0x50(%rsp), %rdx
callq 0x20e0
addq %r14, %rbp
movslq 0x94(%rsp), %rdx
movq %rbp, %rdi
movl $0x4e, %esi
callq 0x20e0
movq 0xd0(%rsp), %rax
movq (%rax), %rbp
addq 0x60(%rsp), %rbp
movzwl %bx, %r8d
movq 0x38(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %rbp, %rdx
movq 0x8(%rsp), %rcx
leaq 0x14(%rsp), %r9
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
pushq %r14
pushq %r14
pushq 0xb8(%rsp)
pushq 0xd0(%rsp)
movq 0xe0(%rsp), %rbx
pushq %rbx
leaq 0x58(%rsp), %rax
pushq %rax
callq 0x8607d
addq $0x30, %rsp
movl %eax, 0x34(%rsp)
cmpl %r14d, 0x14(%rsp)
je 0x4b412
movq 0x40(%rsp), %rax
movq 0xc8(%rsp), %rcx
leaq (%rcx,%rax), %rsi
decq %rsi
movq %rbx, %rcx
movq (%rsp), %rbx
movq 0x88(%rbx), %rax
leaq (%rax,%r12), %rdx
movq 0x60(%rsp), %r14
movl %r14d, (%rax,%r12)
movq %rsi, 0x28(%rsp)
movl %esi, 0x4(%rax,%r12)
movl (%rcx), %esi
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x34(%rsp), %r9
pushq 0x38(%rsp)
pushq 0x10(%rsp)
pushq %rbp
leaq 0x2c(%rsp), %rax
pushq %rax
callq 0x8663c
movq %rbx, %r9
addq $0x20, %rsp
movq 0x70(%rsp), %rax
movq %rax, %rbx
negq %rbx
cmovsq %rax, %rbx
movq 0x98(%rsp), %rdx
movq %rdx, %rcx
testq %r12, %r12
jne 0x4b44a
movq 0x58(%r9), %rax
movslq (%rax), %rax
cmpq %rax, %r14
je 0x4b448
movq %rdx, %rcx
jmp 0x4b44a
movq (%rsp), %r9
movq 0x88(%r9), %rax
movw $0xffff, 0x14(%rax,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rax,%r12) # imm = 0xFFFFFFFF
movq 0x18(%rsp), %r15
incq %r13
addq $0x20, %r12
cmpq %r13, %r15
jne 0x4b011
jmp 0x4b5de
xorl %ecx, %ecx
movq 0x68(%rsp), %rax
movq (%rax), %rax
leaq -0x2(%rax), %rdi
movq %rdx, %rsi
cmpq %rdi, %r13
movq 0x40(%rsp), %rdx
jne 0x4b483
movq 0x58(%r9), %rdi
shlq $0x5, %rax
movslq -0x1c(%rdi,%rax), %rax
cmpq %rax, 0x28(%rsp)
je 0x4b481
movq 0x98(%rsp), %rsi
jmp 0x4b483
xorl %esi, %esi
movq 0x88(%r9), %rbp
subq %rcx, %r15
jle 0x4b561
subq %rsi, %rdx
jle 0x4b561
leaq (%r12,%rbp), %rdi
movq %rsi, %rax
notq %rax
movq 0x8(%rsp), %r8
addq %rax, %r8
movq %rsi, 0x70(%rsp)
movq 0xa0(%rsp), %rsi
movq %rdx, 0x40(%rsp)
leaq 0xf0(%rsp), %rdx
movq %r9, %r14
movq %rcx, 0x50(%rsp)
callq 0x4a52a
movq 0x58(%r14), %rax
movq %rax, 0x8(%rsp)
leaq (%rax,%r12), %rdi
movl $0x1, %r8d
movq 0x68(%rsp), %r14
movq %r14, %rsi
leaq 0x80(%rsp), %rdx
movq %r15, %rcx
callq 0x4a359
movq 0x8(%rsp), %rax
leaq (%rax,%r12), %rdi
addq $0x20, %rdi
movq %r14, %rsi
leaq 0x88(%rsp), %rdx
movq 0x40(%rsp), %rcx
xorl %r8d, %r8d
callq 0x4a359
addq 0x88(%rsp), %rbx
movq 0x80(%rsp), %rax
addq %rbx, %rax
incq %rax
cmpq 0xf0(%rsp), %rax
jge 0x4b583
movw $0xffff, 0x14(%rbp,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rbp,%r12) # imm = 0xFFFFFFFF
movl 0x18(%rbp,%r12), %eax
movq (%rsp), %r9
jmp 0x4b577
movw $0xffff, 0x14(%rbp,%r12) # imm = 0xFFFF
movl $0xffffffff, 0xc(%rbp,%r12) # imm = 0xFFFFFFFF
movl 0x18(%rbp,%r12), %eax
movq %rax, 0x90(%r9)
jmp 0x4b42e
movzwl 0x14(%rsp), %eax
movw %ax, 0x14(%rbp,%r12)
movq 0x20(%rsp), %rcx
subl 0x58(%rsp), %ecx
movl 0x30(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x8(%rbp,%r12)
addl 0x34(%rsp), %ecx
movl %ecx, 0xc(%rbp,%r12)
movq 0x60(%rsp), %rax
movl %eax, (%rbp,%r12)
movq 0x28(%rsp), %rax
movl %eax, 0x4(%rbp,%r12)
movq 0x50(%rsp), %rax
movw %ax, 0x10(%rbp,%r12)
movq 0x70(%rsp), %rax
movw %ax, 0x12(%rbp,%r12)
movq (%rsp), %r9
jmp 0x4b42e
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1da155(%rip), %rdi    # 0x22574c
leaq 0x1da045(%rip), %rsi    # 0x225643
leaq 0x1da1b8(%rip), %rcx    # 0x2257bd
movl $0xae6, %edx            # imm = 0xAE6
callq 0x2110