// File: /workspace/github_cpp_makefile_star2_O2/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 $0x118, %rsp            # imm = 0x118
movsd %xmm0, 0x100(%rsp)
movq %rcx, 0xe0(%rsp)
movq 0x48(%rdi), %rax
testq %rax, %rax
je 0x1e240
movq %r8, %r14
movq %rdx, %r15
movq %rsi, %r12
leaq 0x48(%rdi), %rcx
movq %rcx, 0x50(%rsp)
leaq 0x78(%rdi), %rcx
leaq -0x1(%rax), %rbx
movq %rdi, 0x10(%rsp)
movq %rcx, 0x88(%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 %r12, 0xa8(%rsp)
movq (%r12), %rax
movq 0x10(%rax), %rax
movq %rsi, 0xd8(%rsp)
imulq $0x28, %rsi, %rsi
movq %rsi, 0xf8(%rsp)
movl (%rax,%rsi), %eax
cvtsi2sd %r14, %xmm0
mulsd 0x1c5830(%rip), %xmm0   # 0x1e34c8
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0xf0(%rsp)
cvttsd2si %xmm0, %rax
andq $-0x4, %rax
cmpq $0x31, %rax
pushq $0x30
popq %rsi
cmovgeq %rax, %rsi
movq %rsi, %rax
shrq %rax
movq %rax, 0xe8(%rsp)
shrq $0x2, %rsi
movq %rsi, 0x80(%rsp)
movq %rbx, 0x78(%rdx)
leaq 0x50(%r15), %rsi
leaq 0x7d70(%r15), %rdi
leaq 0xff0(%r15), %rax
movq %rax, 0xc8(%rsp)
addq $0xa14, %r15            # imm = 0xA14
movq %r15, 0x98(%rsp)
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %rsi, 0x90(%rsp)
movq %rdi, 0xd0(%rsp)
movq %rbp, %r14
incq %rbp
cmpq %rcx, %rbp
jge 0x1e22e
movq 0x58(%rdx), %r12
movslq 0xc(%r12,%r15), %r13
cmpq $-0x1, %r13
je 0x1ddc5
cmpl $-0x1, 0x2c(%r12,%r15)
je 0x1ddc5
movslq 0x28(%r12,%r15), %rax
movl %r13d, %ecx
notl %ecx
movslq %ecx, %rcx
addq %rax, %rcx
movq %rcx, 0x60(%rsp)
jne 0x1ddde
leaq (%r12,%r15), %rdi
leaq (%r12,%r15), %rax
addq $0x20, %rax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rbx
movq %rbx, %rsi
leaq 0x68(%rsp), %rdx
pushq $0xa
popq %rcx
pushq $0x1
popq %r8
callq 0x1d1fa
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
leaq 0x70(%rsp), %rdx
pushq $0xa
popq %rcx
xorl %r8d, %r8d
callq 0x1d1fa
movq 0x10(%rsp), %rdx
movq 0x70(%rsp), %rax
orq 0x68(%rsp), %rax
jne 0x1ddde
movq 0x88(%rdx), %rax
movw $0xfffe, 0x14(%rax,%r15) # imm = 0xFFFE
jmp 0x1e091
movq 0x88(%rdx), %rax
orw $-0x1, 0x14(%rax,%r15)
orl $-0x1, 0xc(%rax,%r15)
jmp 0x1e09c
movslq 0x4(%r12,%r15), %rdi
movq 0x88(%rdx), %rax
movw %di, 0x10(%rax,%r15)
movw %r13w, 0x12(%rax,%r15)
movslq (%r12,%r15), %rax
movq %rdi, %rsi
subq %rax, %rsi
cmpq %r13, %rsi
cmovgeq %r13, %rsi
movq 0xe8(%rsp), %rcx
cmpq %rcx, %rsi
jl 0x1de17
movq %rcx, %rsi
movslq 0x24(%r12,%r15), %rax
movq %rdi, 0x30(%rsp)
subq %rdi, %rax
incq %rax
movq 0xf0(%rsp), %rdx
subq %r13, %rdx
cmpq %rdx, %rax
cmovlq %rax, %rdx
cmpq %rcx, %rdx
jl 0x1de41
movq %rcx, %rdx
movq %rdx, 0x40(%rsp)
leaq (%rdx,%rsi), %rbx
movq %rsi, 0x28(%rsp)
subq %rsi, %r13
movq %r13, 0xb0(%rsp)
cvtsi2sd %rbx, %xmm0
mulsd 0x100(%rsp), %xmm0
cvttsd2si %xmm0, %rdi
pushq $0x1
popq %rax
testq %rdi, %rdi
je 0x1de77
movl %edi, %eax
cmpq $0x4, %rbx
cmovgel %eax, %edi
movl %ebx, %esi
callq 0x526f4
movl %eax, %r12d
cltq
leaq (%rbx,%rax,2), %rdx
movq 0xa8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0xf8(%rsp), %rcx
movl (%rax,%rcx), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
subq $0x8, %rsp
movl %r12d, %edi
movq %r13, %rsi
movq %rdx, 0x60(%rsp)
movq %rdx, %rcx
leaq 0x84(%rsp), %r9
leaq 0x110(%rsp), %rax
pushq %rax
leaq 0xc0(%rsp), %rax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
callq 0x1b53a
addq $0x20, %rsp
testl %eax, %eax
je 0x1e07e
movq 0x108(%rsp), %rdx
cmpq %rbx, %rdx
jl 0x1e07e
movq 0x30(%rsp), %r8
subq 0x28(%rsp), %r8
movq 0xb0(%rsp), %rsi
movl 0x7c(%rsp), %r10d
movl 0x78(%rsp), %eax
movzbl 0xf(%rsp), %ecx
movq 0x90(%rsp), %r13
movq %r13, %rdi
movq %rbx, 0xa0(%rsp)
movq %r8, %rbx
movq %rsi, 0xc0(%rsp)
movq 0xa8(%rsp), %r8
movq 0xd8(%rsp), %r9
pushq %rax
movq %r10, 0xc0(%rsp)
pushq %r10
callq 0x1b4ca
popq %rax
popq %rcx
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movq %rbx, 0x48(%rsp)
addq %rbx, %rax
movq %rax, %rbx
movzwl %r12w, %r8d
movq %r13, %rdi
movq 0xa0(%rsp), %r13
movq 0x58(%rsp), %rsi
movq %rax, %rdx
movl %r13d, %ecx
leaq 0x24(%rsp), %r9
pushq $-0x1
pushq $-0x1
pushq 0xa8(%rsp)
pushq 0xe0(%rsp)
movq 0xf0(%rsp), %r12
pushq %r12
leaq 0x60(%rsp), %rax
pushq %rax
callq 0x51ff9
addq $0x30, %rsp
movl %eax, 0x3c(%rsp)
pushq $-0x1
popq %rax
cmpl %eax, 0x24(%rsp)
je 0x1e07e
movq 0x40(%rsp), %rax
movq 0x30(%rsp), %rcx
addq %rax, %rcx
decq %rcx
movq 0x10(%rsp), %rax
movq 0x88(%rax), %rax
leaq (%rax,%r15), %rdx
movq 0x48(%rsp), %rsi
movl %esi, (%rax,%r15)
movq %rcx, 0x30(%rsp)
movl %ecx, 0x4(%rax,%r15)
movl (%r12), %esi
movq 0x98(%rsp), %rdi
movq 0x88(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x3c(%rsp), %r9
pushq 0x90(%rsp)
pushq %r13
pushq %rbx
leaq 0x3c(%rsp), %rax
pushq %rax
callq 0x52542
movq 0x30(%rsp), %rdx
addq $0x20, %rsp
movq 0x60(%rsp), %rax
movq %rax, %r13
negq %r13
cmovsq %rax, %r13
movq 0x80(%rsp), %rsi
movq %rsi, %rcx
testq %r15, %r15
movq 0x28(%rsp), %rbx
jne 0x1e0a7
movq 0x58(%rdx), %rax
movslq (%rax), %rax
cmpq %rax, 0x48(%rsp)
je 0x1e0a5
movq %rsi, %rcx
jmp 0x1e0a7
movq 0x10(%rsp), %rdx
movq 0x88(%rdx), %rax
orw $-0x1, 0x14(%rax,%r15)
orl $-0x1, 0xc(%rax,%r15)
movq 0x18(%rsp), %rcx
addq $0x20, %r15
jmp 0x1dd15
xorl %ecx, %ecx
movq 0x50(%rsp), %rax
movq (%rax), %rax
leaq -0x2(%rax), %rdi
movq %rsi, %r8
cmpq %rdi, %r14
movq 0x40(%rsp), %rsi
jne 0x1e0e1
movq 0x58(%rdx), %rdi
shlq $0x5, %rax
movslq -0x1c(%rdi,%rax), %rax
cmpq %rax, 0x30(%rsp)
je 0x1e0de
movq 0x80(%rsp), %r8
jmp 0x1e0e1
xorl %r8d, %r8d
movq 0x88(%rdx), %r14
subq %rcx, %rbx
jle 0x1e1af
subq %r8, %rsi
jle 0x1e1af
movq %rdx, %r12
movq %rbx, 0x28(%rsp)
leaq (%r14,%r15), %rdi
movq %r8, %rax
notq %rax
movq %r8, 0x60(%rsp)
movq 0xa0(%rsp), %r8
addq %rax, %r8
movq %rsi, 0x40(%rsp)
movq 0x88(%rsp), %rsi
leaq 0x110(%rsp), %rdx
movq %rcx, 0x58(%rsp)
callq 0x1d3a1
movq 0x58(%r12), %r12
leaq (%r12,%r15), %rdi
movq 0x50(%rsp), %rbx
movq %rbx, %rsi
leaq 0x68(%rsp), %rdx
movq 0x28(%rsp), %rcx
pushq $0x1
popq %r8
callq 0x1d1fa
leaq (%r12,%r15), %rdi
addq $0x20, %rdi
movq %rbx, %rsi
leaq 0x70(%rsp), %rdx
movq 0x40(%rsp), %rcx
xorl %r8d, %r8d
callq 0x1d1fa
addq 0x70(%rsp), %r13
movq 0x68(%rsp), %rax
addq %r13, %rax
incq %rax
cmpq 0x110(%rsp), %rax
jge 0x1e1cd
orw $-0x1, 0x14(%r14,%r15)
orl $-0x1, 0xc(%r14,%r15)
movl 0x18(%r14,%r15), %eax
movq 0x10(%rsp), %rdx
jmp 0x1e1c1
orw $-0x1, 0x14(%r14,%r15)
orl $-0x1, 0xc(%r14,%r15)
movl 0x18(%r14,%r15), %eax
movq %rax, 0x90(%rdx)
jmp 0x1e097
movzwl 0x24(%rsp), %eax
movw %ax, 0x14(%r14,%r15)
movq 0xc0(%rsp), %rcx
subl 0xb8(%rsp), %ecx
movl 0x38(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x8(%r14,%r15)
addl 0x3c(%rsp), %ecx
movl %ecx, 0xc(%r14,%r15)
movq 0x48(%rsp), %rax
movl %eax, (%r14,%r15)
movq 0x30(%rsp), %rax
movl %eax, 0x4(%r14,%r15)
movq 0x58(%rsp), %rax
movw %ax, 0x10(%r14,%r15)
movq 0x60(%rsp), %rax
movw %ax, 0x12(%r14,%r15)
movq 0x10(%rsp), %rdx
jmp 0x1e097
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c5565(%rip), %rdi    # 0x1e37ac
leaq 0x1c5455(%rip), %rsi    # 0x1e36a3
leaq 0x1c55c8(%rip), %rcx    # 0x1e381d
movl $0xae6, %edx            # imm = 0xAE6
callq 0x2110