// File: /workspace/github_cpp_makefile_star2_O1/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, %r10
movq %r8, %rax
movl %edx, %r13d
movq %rsi, %r15
movl 0xa0(%rsp), %edx
movq 0x98(%rsp), %r14
movq 0x90(%rsp), %r8
movq 0x88(%rsp), %r11
movq 0x80(%rsp), %r9
testq %rax, %rax
je 0x98bee
leaq -0x1(%r11), %rax
cmpq %r10, %rax
jne 0x98f7e
xorl %eax, %eax
cmpq $0x0, 0x78(%rsp)
jne 0x98f80
movq %rcx, %rax
subq %r15, %rax
leaq (%rax,%r10), %rsi
addq %rax, %r10
incq %r10
movq 0x18(%r8), %rax
movq (%rax,%r9,8), %rbp
cmpq $-0x1, %rsi
setl %r12b
movq %r10, %rdx
cmpq %r10, %rbp
setle %r10b
movb $0x1, %al
orb %r12b, %r10b
jne 0x98be9
leal (,%r13,2), %r10d
movslq %r10d, %r10
notq %rsi
addq %r10, %rsi
addq %rbp, %rsi
addq $0x1f, %rsi
cmpq %r11, %rsi
jge 0x98dcf
jmp 0x98e08
xorl %eax, %eax
cmpq $0x0, 0x70(%rsp)
jne 0x98f80
movq 0x18(%r8), %rsi
movq (%rsi,%r9,8), %rbp
testq %rcx, %rcx
sets %r12b
cmpq %rcx, %rbp
setle %r10b
movb $0x1, %bl
orb %r12b, %r10b
jne 0x98c33
leal (,%r13,2), %r10d
movslq %r10d, %r10
subq %rcx, %r10
addq %rbp, %r10
addq $0x1f, %r10
cmpq %r11, %r10
jge 0x98c39
movq %r10, (%rsp)
jmp 0x98c77
movslq %r13d, %rsi
movq %rcx, %r12
subq %rsi, %r12
subq %r12, %rbp
cmpq %r11, %rbp
cmovgeq %r11, %rbp
movl %r11d, %esi
subl %ebp, %esi
movl %esi, 0x10(%rsp)
movl %r12d, %r10d
negl %r10d
xorl %ebx, %ebx
testq %r12, %r12
movl $0x0, %esi
cmovgq %r12, %rsi
sarq $0x3f, %r12
andl %r10d, %r12d
movq %r12, (%rsp)
subq %r12, %rbp
cmpq %rcx, %rsi
sete %cl
orb %bl, %cl
jne 0x98f80
movq %r13, 0x18(%rsp)
leaq 0xdfc(%r14), %r13
movslq (%rsp), %rbx
movq %rdi, 0x28(%rsp)
leaq (%r14,%rbx), %r12
addq $0xdfc, %r12            # imm = 0xDFC
movzbl %dl, %ecx
movq %r12, %rdi
movq %rbp, %rdx
movq %rsi, 0x20(%rsp)
callq 0x337f7
movq %r13, %rdi
movl $0x4e, %esi
movq %rbx, %rdx
callq 0x20e0
addq %rbp, %r12
movslq 0x10(%rsp), %rdx
movq %r12, %rdi
movl $0x4e, %esi
callq 0x20e0
leaq 0xff0(%r14), %rax
leaq 0xc08(%r14), %rbp
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
leaq 0x8(%rsp), %r11
leaq 0xc(%rsp), %r12
movzwl 0x18(%rsp), %r8d
leaq 0x34(%rsp), %rbx
movq %r13, %rdi
movq 0x88(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl %r15d, %ecx
movq %rbx, %r9
pushq %r10
pushq %r10
pushq %rbp
pushq %rax
pushq %r11
pushq %r12
movq 0x100(%rsp), %r12
movq 0x108(%rsp), %r15
callq 0x8607d
movq 0x50(%rsp), %r8
movq 0xf0(%rsp), %rdi
movq 0xf8(%rsp), %rsi
addq $0x30, %rsp
movl (%rbx), %ecx
movq 0x88(%rsp), %rbx
cmpl 0xa8(%rsp), %ecx
jae 0x98f7e
movq 0xb0(%rsp), %rdx
movq %r8, (%rdx)
movl 0xc(%rsp), %edx
movq 0xb8(%rsp), %r8
movl %edx, (%r8)
movl %eax, (%rdi)
movq (%rsp), %rax
movl %eax, (%rsi)
movl 0x10(%rsp), %eax
movl %eax, (%r12)
movl %ecx, (%r15)
movslq 0x8(%rsp), %rdx
movl %edx, 0x7d70(%r14)
leaq 0xa14(%r14), %rdi
movq %rbp, %rsi
callq 0x2160
addq $0x50, %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x2160
movl $0x1, %eax
jmp 0x98f80
movslq %r13d, %rax
movq %rdx, %r10
subq %rax, %r10
subq %r10, %rbp
cmpq %r11, %rbp
cmovgeq %r11, %rbp
movl %r11d, %eax
subl %ebp, %eax
movl %eax, (%rsp)
movl %r10d, %esi
negl %esi
xorl %eax, %eax
testq %r10, %r10
movl $0x0, %r12d
cmovgq %r10, %r12
sarq $0x3f, %r10
andl %esi, %r10d
subq %r10, %rbp
movl 0xa0(%rsp), %edx
xorl %esi, %esi
testb %al, %al
jne 0x98f72
movl $0x0, %eax
cmpq %rcx, %r12
je 0x98f79
movq %r12, %rcx
movq %r15, 0x10(%rsp)
leaq 0xdfc(%r14), %r15
movslq %r10d, %rbx
movq %rdi, 0x28(%rsp)
leaq 0xdfc(%r14,%rbx), %r12
movq %rcx, %rsi
movzbl %dl, %ecx
movq %r12, %rdi
movq %rsi, 0x20(%rsp)
movq %rbp, %rdx
movq %r10, 0x18(%rsp)
callq 0x337f7
movq %r15, %rdi
movl $0x4e, %esi
movq %rbx, %rdx
callq 0x20e0
addq %rbp, %r12
movslq (%rsp), %rdx
movq %r12, %rdi
movl $0x4e, %esi
callq 0x20e0
leaq 0xff0(%r14), %rax
leaq 0xc08(%r14), %rbp
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
leaq 0x8(%rsp), %r11
leaq 0xc(%rsp), %r12
movzwl %r13w, %r8d
leaq 0x34(%rsp), %rbx
movq %r15, %r13
movq %r15, %rdi
movq 0x88(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %rbx, %r9
pushq %r10
pushq %r10
pushq %rbp
pushq %rax
pushq %r11
pushq %r12
callq 0x8607d
addq $0x30, %rsp
movl (%rbx), %ecx
cmpl 0xa8(%rsp), %ecx
jae 0x98f76
movq 0xb0(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq %rsi, (%rdx)
movl 0xc(%rsp), %edx
movq 0xb8(%rsp), %rsi
movl %edx, (%rsi)
movq 0xc0(%rsp), %rdx
movl %eax, (%rdx)
movq 0xc8(%rsp), %rax
movq 0x18(%rsp), %rdx
movl %edx, (%rax)
movq 0xd0(%rsp), %rax
movl (%rsp), %edx
movl %edx, (%rax)
movq 0xd8(%rsp), %rax
movl %ecx, (%rax)
movslq 0x8(%rsp), %rdx
movl %edx, 0x7d70(%r14)
leaq 0xa14(%r14), %rdi
movq %rbp, %rsi
callq 0x2160
addq $0x50, %r14
movq %r14, %rdi
movq %r13, %rsi
movq 0x88(%rsp), %rdx
callq 0x2160
movl $0x1, %eax
xorl %esi, %esi
jmp 0x98f79
xorl %eax, %eax
jmp 0x98f79
movb $0x1, %sil
testb %sil, %sil
je 0x98f80
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq