// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: fix_boundary(char*, long long, int, long long, long long, long long, long long, long long, long long, long long, All_reads*, Correct_dumy*, int, unsigned int, long long*, int*, int*, int*, int*, unsigned int*)
inline int fix_boundary(char* x_string, long long x_len, int threshold,
long long total_y_start, long long local_y_start, long long local_y_end,
long long old_extra_begin, long long old_extra_end,
long long y_ID, long long Window_Len, All_reads* R_INF,
Correct_dumy* dumy, int y_strand, unsigned int old_error,
long long* r_total_y_start, int* r_start_site, int* r_end_site,
int* r_extra_begin, int* r_extra_end, unsigned int* r_error)
{

    
    int new_extra_begin, new_extra_end;
    long long new_y_start, new_y_length;
    int new_end_site, new_start_site;
    unsigned int new_error;
    char* y_string;


    int path_length;

    ///if the start pos at the left boundary 
    if(local_y_start == 0)
    {
        total_y_start = total_y_start + local_y_start;
        ///if local_y_start == 0 and old_extra_begin != 0
        ///this means total_y_start == 0, so shift to the left cannot get a new start pos
        if(old_extra_begin != 0)
        {
            return 0;
        }

        ///if the begining of alignment is 0, we should try to shift the window to find a better result
        ///shift to the left by threshold-1 bases
        if(!determine_overlap_region(threshold, total_y_start, y_ID, Window_Len, Get_READ_LENGTH((*R_INF), y_ID),
        &new_extra_begin, &new_extra_end, &new_y_start, &new_y_length))
        {
            return 0;
        }

        ///if new_y_start is equal to total_y_start, recalculate makes no sense
        if(new_y_start == total_y_start)
        {
            return 0;
        }

        fill_subregion(dumy->overlap_region_fix, new_y_start, new_y_length, y_strand, R_INF, y_ID, 
        new_extra_begin, new_extra_end);

        y_string = dumy->overlap_region_fix;

        new_end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &new_error, &new_start_site,
                    &path_length, dumy->matrix_bit, dumy->path_fix, -1, -1);
        
        if (new_error != (unsigned int)-1 && new_error < old_error)
        {
            (*r_total_y_start) = new_y_start;
            (*r_start_site) = new_start_site;
            (*r_end_site) = new_end_site;
            (*r_extra_begin) = new_extra_begin;
            (*r_extra_end) = new_extra_end;
            (*r_error) = new_error;

            dumy->path_length = path_length;
            memcpy(dumy->path, dumy->path_fix, path_length);
            memcpy(dumy->overlap_region, dumy->overlap_region_fix, Window_Len);
            return 1;
        }
    }
    else if(local_y_end == Window_Len - 1)
    {
        ///if local_y_end == Window_Len - 1 and old_extra_end > 0
        ///this means local_y_end is the end of the y
        ///so shit to the right makes no sense
        if(old_extra_end != 0)
        {
            return 0;
        }
        long long total_y_end = total_y_start + local_y_end;

        total_y_start = total_y_end - x_len + 1;

        if(!determine_overlap_region(threshold, total_y_start, y_ID, Window_Len, Get_READ_LENGTH((*R_INF), y_ID),
        &new_extra_begin, &new_extra_end, &new_y_start, &new_y_length))
        {
            return 0;
        }

        if(new_y_start == total_y_end - local_y_end)
        {
            return 0;
        }

        fill_subregion(dumy->overlap_region_fix, new_y_start, new_y_length, y_strand, R_INF, y_ID, 
        new_extra_begin, new_extra_end);

        y_string = dumy->overlap_region_fix;

        new_end_site = Reserve_Banded_BPM_PATH(y_string, Window_Len, x_string, x_len, threshold, &new_error, &new_start_site,
                    &path_length, dumy->matrix_bit, dumy->path_fix, -1, -1);

        if (new_error != (unsigned int)-1 && new_error < old_error)
        {
            (*r_total_y_start) = new_y_start;
            (*r_start_site) = new_start_site;
            (*r_end_site) = new_end_site;
            (*r_extra_begin) = new_extra_begin;
            (*r_extra_end) = new_extra_end;
            (*r_error) = new_error;

            dumy->path_length = path_length;
            memcpy(dumy->path, dumy->path_fix, path_length);
            memcpy(dumy->overlap_region, dumy->overlap_region_fix, Window_Len);
            return 1;
        }


    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r12
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %r15
movq 0xa8(%rsp), %r14
movq 0xa0(%rsp), %rdi
movq 0x98(%rsp), %rcx
movq 0x90(%rsp), %rdx
testq %r8, %r8
je 0x63895
leaq -0x1(%rcx), %rax
xorq %r9, %rax
xorl %ebp, %ebp
orq 0x88(%rsp), %rax
jne 0x63a24
movq %r12, %rax
subq %r13, %rax
leaq (%rax,%r9), %rsi
incq %rsi
movq 0x18(%rdi), %rax
movq (%rax,%rdx,8), %r8
subq $0x8, %rsp
leaq 0x18(%rsp), %rax
leaq 0x20(%rsp), %rdx
leaq 0x8(%rsp), %r10
leaq 0xc(%rsp), %r9
movl %ebx, %edi
jmp 0x638cb
xorl %ebp, %ebp
cmpq $0x0, 0x80(%rsp)
jne 0x63a24
movq 0x18(%rdi), %rax
movq (%rax,%rdx,8), %r8
subq $0x8, %rsp
leaq 0x18(%rsp), %rax
leaq 0x20(%rsp), %rdx
leaq 0x8(%rsp), %r10
leaq 0xc(%rsp), %r9
movl %ebx, %edi
movq %r12, %rsi
pushq %rax
pushq %rdx
pushq %r10
callq 0x1b53a
addq $0x20, %rsp
testl %eax, %eax
je 0x63a24
movq 0x18(%rsp), %rsi
cmpq %r12, %rsi
je 0x63a24
leaq 0xdfc(%r14), %r12
movq 0x10(%rsp), %rdx
movl 0x4(%rsp), %eax
movl (%rsp), %r10d
movl 0xb0(%rsp), %ecx
movzbl %cl, %ecx
movq %r12, %rdi
movq %rsi, 0x38(%rsp)
movq 0xa0(%rsp), %r8
movq 0x90(%rsp), %r9
movq %r10, 0x28(%rsp)
pushq %r10
movq %rax, 0x38(%rsp)
pushq %rax
callq 0x1b475
addq $0x10, %rsp
leaq 0xff0(%r14), %rax
leaq 0xc08(%r14), %rcx
leaq 0x8(%rsp), %r10
leaq 0xc(%rsp), %r11
movzwl %bx, %r8d
leaq 0x24(%rsp), %rbx
movq %r12, 0x40(%rsp)
movq %r12, %rdi
movq 0x98(%rsp), %rsi
movq %r15, %rdx
movq %rcx, %r15
movl %r13d, %ecx
movq %rbx, %r9
pushq $-0x1
pushq $-0x1
pushq %r15
pushq %rax
pushq %r10
pushq %r11
callq 0x51ff9
addq $0x30, %rsp
movl (%rbx), %ecx
cmpl 0xb8(%rsp), %ecx
jae 0x63a24
movq 0xe8(%rsp), %rdx
movq 0xe0(%rsp), %rsi
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %r8
movq 0xc8(%rsp), %r9
movq 0xc0(%rsp), %r10
movq 0x38(%rsp), %r11
movq %r11, (%r10)
movl 0xc(%rsp), %r10d
movl %r10d, (%r9)
movl %eax, (%r8)
movq 0x30(%rsp), %rax
movl %eax, (%rdi)
movq 0x28(%rsp), %rax
movl %eax, (%rsi)
movl %ecx, (%rdx)
movslq 0x8(%rsp), %rdx
movl %edx, 0x7d70(%r14)
leaq 0xa14(%r14), %rdi
movq %r15, %rsi
callq 0x2160
addq $0x50, %r14
movq %r14, %rdi
movq 0x40(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x2160
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq