// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %r9, %rax
movq %r8, %r10
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r13
movl 0xa0(%rsp), %edx
movq 0x90(%rsp), %r8
movq 0x88(%rsp), %r11
movq 0x80(%rsp), %r9
testq %r10, %r10
je 0x9974a
leaq -0x1(%r11), %rsi
xorq %rax, %rsi
xorl %r15d, %r15d
orq 0x78(%rsp), %rsi
jne 0x99931
movq %rcx, %rdi
subq %r12, %rdi
leaq (%rdi,%rax), %rsi
leaq (%rdi,%rax), %r14
incq %r14
movq 0x18(%r8), %rax
movq (%rax,%r9,8), %rbx
cmpq $-0x1, %rsi
setl %al
cmpq %r14, %rbx
setle %dil
orb %al, %dil
jne 0x99931
leal (,%rbp,2), %eax
cltq
notq %rsi
addq %rax, %rsi
leaq (%rbx,%rsi), %rax
addq $0x1f, %rax
cmpq %r11, %rax
jl 0x99931
movslq %ebp, %rax
jmp 0x99799
xorl %r15d, %r15d
cmpq $0x0, 0x70(%rsp)
jne 0x99931
movq 0x18(%r8), %rax
movq (%rax,%r9,8), %rbx
testq %rcx, %rcx
sets %al
cmpq %rcx, %rbx
setle %sil
orb %al, %sil
jne 0x99931
leal (,%rbp,2), %eax
cltq
subq %rcx, %rax
addq %rbx, %rax
addq $0x1f, %rax
cmpq %r11, %rax
jl 0x99931
movslq %ebp, %rax
movq %rcx, %r14
subq %rax, %r14
xorl %r15d, %r15d
testq %r14, %r14
movl $0x0, %esi
cmovgq %r14, %rsi
cmpq %rcx, %rsi
je 0x99931
movl %r14d, %eax
negl %eax
subq %r14, %rbx
sarq $0x3f, %r14
andl %eax, %r14d
cmpq %r11, %rbx
cmovgeq %r11, %rbx
subq %rbx, %r11
movq %r11, 0x10(%rsp)
subq %r14, %rbx
movq 0x98(%rsp), %rcx
leaq 0xdfc(%rcx), %rax
movq %rax, (%rsp)
movslq %r14d, %rax
movq %rax, 0x18(%rsp)
leaq 0xdfc(%rcx,%rax), %rdi
movq %rdi, 0x20(%rsp)
movzbl %dl, %ecx
movq %rsi, 0x30(%rsp)
movq %rbx, %rdx
callq 0x32ef8
movq (%rsp), %rdi
movl $0x4e, %esi
movq 0x18(%rsp), %rdx
callq 0x20e0
addq 0x20(%rsp), %rbx
movslq 0x10(%rsp), %rdx
movq %rbx, %rdi
movl $0x4e, %esi
callq 0x20e0
movq 0x98(%rsp), %rcx
leaq 0xff0(%rcx), %rax
leaq 0xc08(%rcx), %r9
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
leaq 0x8(%rsp), %r11
movzwl %bp, %r8d
movq (%rsp), %rbp
leaq 0x2c(%rsp), %rbx
movq %rbp, %rdi
movq 0x88(%rsp), %rsi
movq %r13, %rdx
movl %r12d, %ecx
movq %r9, %r12
movq %rbx, %r9
pushq %r10
pushq %r10
pushq %r12
pushq %rax
pushq %r11
leaq 0x34(%rsp), %rax
pushq %rax
callq 0x85726
addq $0x30, %rsp
movl (%rbx), %ecx
cmpl 0xa8(%rsp), %ecx
jae 0x99931
movq 0xd8(%rsp), %rdx
movq 0xd0(%rsp), %rsi
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %r8
movq 0xb8(%rsp), %r9
movq 0xb0(%rsp), %r10
movq 0x30(%rsp), %r11
movq %r11, (%r10)
movl 0xc(%rsp), %r10d
movl %r10d, (%r9)
movl %eax, (%r8)
movl %r14d, (%rdi)
movq 0x10(%rsp), %rax
movl %eax, (%rsi)
movl %ecx, (%rdx)
movslq 0x8(%rsp), %rdx
movq 0x98(%rsp), %rbx
movl %edx, 0x7d70(%rbx)
leaq 0xa14(%rbx), %rdi
movq %r12, %rsi
callq 0x2160
addq $0x50, %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x88(%rsp), %rdx
callq 0x2160
movl $0x1, %r15d
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq