// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: adjust_specific_ext_offset(overlap_region*, ul_idx_t const*, hpc_t*, All_reads*, bit_extz_t*, char*, UC_Read*, long*, long*, long*, long*, long, long, long, long*)
void adjust_specific_ext_offset(overlap_region *z, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, bit_extz_t *exz, char* qstr, UC_Read *tu, 
int64_t *qs0, int64_t *qe0, int64_t *ts0, int64_t *te0, int64_t ql, int64_t tl, int64_t wl, int64_t *mode)
{   
    ///[qs, qe)
    if((*mode) != 1 && (*mode) != 2) return;
    int64_t qs = *qs0, qe = *qe0, ts = *ts0, te = *te0, k, update, wid, we, ws, sl, gq, gt, gg, min_g = INT32_MAX, min_id = -1, gl;
    adjust_ext_offset(&qs, &qe, &ts, &te, ql, tl, 0, *mode);
    if((*mode) == 1) {///forward extension
        //qs0 and ts0 are fixed; te0 = -1, qe0 is unreliable
        if(qe > (*qe0)) {
            we = (*qe0); we/=wl; we *= wl; we +=wl; we--; ///next window
            for (k = 0; we < qe && k < ext_w; we+=wl) {//[ws, we]; [qs, qe)
                wid = get_win_id_by_e(z, we, wl, NULL);
                if(z->w_list.a[wid].y_end == -1) continue;//unmapped
                if(z->w_list.a[wid].x_end < (*qs0)) continue;
                if(z->w_list.a[wid].y_end < (*ts0)) continue;
                sl = z->w_list.a[wid].x_end+1-z->w_list.a[wid].x_start;
                if(z->w_list.a[wid].error > (sl/A_L)) continue;
                gq = z->w_list.a[wid].x_end-(*qs0); 
                gt = z->w_list.a[wid].y_end-(*ts0); 
                gl = MIN(gq, gt); gl/=16; if(gl <= 0) gl = 1;
                gg = (gq>=gt)?(gq-gt):(gt-gq); gg /= gl;
                update = 0;
                if(min_g>gg) {
                    update = 1;
                } else if((min_g==gg)&&(z->w_list.a[min_id].error>z->w_list.a[wid].error)) {
                    update = 1;
                } 
                if(update) {
                    min_g = gg; min_id = wid;
                }
                k++;
            }
        }
        if(min_id >= 0) {
            (*qe0) = z->w_list.a[min_id].x_end+1; 
            if(((*qe0)+wl) >= qe) {
                (*qe0) = qe; (*te0) = te; ///still extension
                return;
            }
            (*te0) = z->w_list.a[min_id].y_end+1; (*mode) = 0;///global
        } else {
            (*qe0) = qe; (*te0) = te; ///still extension
        }
    } else if((*mode) == 2) {///backward extension
        //qe0 and te0 are fixed; ts0 = -1, qs0 is unreliable
        if(qs < (*qs0)) {
            ws = (*qs0)-1; ws/=wl; ws*=wl; 
            for (k = 0; ws >= qs && k < ext_w; ws-=wl) {//[ws, we]; [qs, qe)
                wid = get_win_id_by_s(z, ws, wl, NULL);
                if(z->w_list.a[wid].y_end == -1) continue;//unmapped
                if(z->w_list.a[wid].x_start >= (*qe0)) continue;
                if(z->w_list.a[wid].y_start >= (*te0)) continue;
                sl = z->w_list.a[wid].x_end+1-z->w_list.a[wid].x_start;
                if(z->w_list.a[wid].error > (sl/A_L)) continue;
                gq = (*qe0) - z->w_list.a[wid].x_start; 
                gt = (*te0) - z->w_list.a[wid].y_start; 
                gl = MIN(gq, gt); gl/=16; if(gl <= 0) gl = 1;
                gg = (gq>=gt)?(gq-gt):(gt-gq); gg /= gl;
                update = 0;
                if(min_g>gg) {
                    update = 1;
                } else if((min_g==gg)&&(z->w_list.a[min_id].error>z->w_list.a[wid].error)) {
                    update = 1;
                } 
                if(update) {
                    min_g = gg; min_id = wid;
                }
                k++;
            }
        }
        if(min_id >= 0) {
            (*qs0) = z->w_list.a[min_id].x_start; 
            if((qs+wl) >= (*qs0)) {
                (*qs0) = qs; (*ts0) = ts; ///still extension
                return;
            }
            (*ts0) = z->w_list.a[min_id].y_start; (*mode) = 0;///global
        } else {
            (*qs0) = qs; (*ts0) = ts; ///still extension
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq 0x78(%rsp), %rax
movq (%rax), %rax
leaq -0x1(%rax), %rcx
cmpq $0x1, %rcx
ja 0x609c6
movq 0x70(%rsp), %r11
movq 0x68(%rsp), %r13
movq 0x60(%rsp), %rbp
movq 0x58(%rsp), %rdx
movq 0x50(%rsp), %r9
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rcx
movq (%rcx), %r10
movq (%rsi), %rcx
movq (%r9), %r14
movq (%rdx), %r15
cmpq $0x1, %rax
jne 0x60639
movq %rbp, %rdx
subq %r10, %rdx
movq %r13, %rsi
subq %r14, %rsi
cmpq %rsi, %rdx
jle 0x60650
addq %r10, %rsi
movq %r10, %rbx
movq %r14, %r8
movq %r13, %rdx
jmp 0x6065c
movq %rcx, %rbx
subq %r15, %rbx
jle 0x60822
xorl %r8d, %r8d
movq %rcx, %rsi
movq %r15, %rdx
jmp 0x6065c
addq %r14, %rdx
movq %r10, %rbx
movq %rbp, %rsi
movq %r14, %r8
xorl %r12d, %r12d
testq %rbx, %rbx
cmovleq %r12, %rbx
testq %r8, %r8
cmovgq %r8, %r12
cmpq %rbp, %rsi
cmovlq %rsi, %rbp
cmpq %r13, %rdx
cmovlq %rdx, %r13
cmpq $0x1, %rax
jne 0x60831
cmpq %rcx, %rbp
jle 0x6080d
movq %rcx, %rax
cqto
idivq %r11
movq %rdx, %r8
leaq (%r11,%rcx), %rax
notq %r8
addq %r8, %rax
cmpq %rbp, %rax
jge 0x6080d
movl 0x4(%rdi), %r9d
xorl %ebx, %ebx
movq %r9, %rax
xorl %edx, %edx
idivq %r11
movq %rdx, %rsi
movq %rdi, -0x8(%rsp)
movq 0x58(%rdi), %r15
leaq (%rcx,%r11,2), %r12
addq %r8, %r12
addq %r11, %rsi
addq %rcx, %rsi
addq %r8, %rsi
subq %r9, %rsi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x10(%rsp)
movq $-0x1, -0x18(%rsp)
movq %rsi, %rax
cqto
idivq %r11
movq %rax, %rcx
movq %rax, %r9
shlq $0x5, %r9
movslq 0xc(%r15,%r9), %rax
cmpq $-0x1, %rax
je 0x607ac
addq %r15, %r9
movslq 0x4(%r9), %rdx
movq %rdx, %r8
subq %r10, %r8
jl 0x607ac
subq %r14, %rax
jl 0x607ac
subl (%r9), %edx
leal 0x1(%rdx), %edi
movswl 0x14(%r9), %r9d
addl $0x10, %edx
testl %edi, %edi
cmovnsl %edi, %edx
sarl $0x4, %edx
cmpl %r9d, %edx
jl 0x607ac
movq %r8, %rdx
subq %rax, %rdx
cmovlq %r8, %rax
leaq 0xf(%rax), %rdi
testq %rax, %rax
cmovnsq %rax, %rdi
sarq $0x4, %rdi
cmpq $0x2, %rdi
movl $0x1, %eax
cmovlq %rax, %rdi
movq %rdx, %rax
negq %rax
cmovsq %rdx, %rax
xorl %edx, %edx
divq %rdi
cmpq %rax, -0x10(%rsp)
jle 0x6078c
movq %rax, -0x10(%rsp)
movq %rcx, -0x18(%rsp)
jmp 0x607a9
jne 0x607a9
movq -0x18(%rsp), %rdx
movq %rdx, %rax
shlq $0x5, %rax
cmpw %r9w, 0x14(%r15,%rax)
cmovgq %rcx, %rdx
movq %rdx, -0x18(%rsp)
incq %rbx
cmpq %rbp, %r12
jge 0x607c1
addq %r11, %r12
addq %r11, %rsi
cmpq $0x6, %rbx
jl 0x606ec
movq -0x18(%rsp), %rdi
testq %rdi, %rdi
movq -0x8(%rsp), %rax
js 0x6080d
movq 0x58(%rax), %rax
shlq $0x5, %rdi
movslq 0x4(%rax,%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq 0x48(%rsp), %rsi
movq %rdx, (%rsi)
addq %r11, %rcx
incq %rcx
cmpq %rbp, %rcx
jge 0x609d6
movslq 0xc(%rax,%rdi), %rax
incq %rax
movq 0x58(%rsp), %rcx
movq %rax, (%rcx)
jmp 0x609ad
movq 0x48(%rsp), %rax
movq %rbp, (%rax)
movq 0x58(%rsp), %rax
movq %r13, (%rax)
jmp 0x609c6
movq %r15, %r12
subq %rcx, %r12
xorl %ebx, %ebx
testq %r12, %r12
cmovleq %rbx, %r12
cmpq %r10, %rbx
jge 0x609bb
leaq -0x1(%r10), %rsi
movq %rsi, %rax
cqto
idivq %r11
subq %rdx, %rsi
cmpq %rbx, %rsi
jl 0x609bb
movq %rdx, %r8
movl 0x4(%rdi), %r9d
xorl %r14d, %r14d
movq %r9, %rax
xorl %edx, %edx
idivq %r11
movq %rdx, %rsi
movq %rdi, -0x8(%rsp)
movq 0x58(%rdi), %r13
notq %r8
leaq (%r8,%r10), %rbp
subq %r11, %rbp
addq %r10, %rsi
addq %r8, %rsi
subq %r9, %rsi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x10(%rsp)
movq $-0x1, -0x18(%rsp)
movq %rsi, %rax
cqto
idivq %r11
movq %rax, %r10
shlq $0x5, %r10
cmpl $-0x1, 0xc(%r13,%r10)
je 0x60960
movq %rax, %r8
addq %r13, %r10
movslq (%r10), %r9
movq %rcx, %rax
subq %r9, %rax
jle 0x60960
movslq 0x8(%r10), %rdi
movq %r15, %rdx
subq %rdi, %rdx
jle 0x60960
movl 0x4(%r10), %edi
subl %r9d, %edi
leal 0x1(%rdi), %r9d
movswl 0x14(%r10), %r10d
addl $0x10, %edi
testl %r9d, %r9d
cmovnsl %r9d, %edi
sarl $0x4, %edi
cmpl %r10d, %edi
jl 0x60960
movq %rax, %rdi
subq %rdx, %rdi
cmovlq %rax, %rdx
leaq 0xf(%rdx), %r9
testq %rdx, %rdx
cmovnsq %rdx, %r9
sarq $0x4, %r9
cmpq $0x2, %r9
movl $0x1, %eax
cmovlq %rax, %r9
movq %rdi, %rax
negq %rax
cmovsq %rdi, %rax
xorl %edx, %edx
divq %r9
cmpq %rax, -0x10(%rsp)
jle 0x60940
movq %rax, -0x10(%rsp)
movq %r8, -0x18(%rsp)
jmp 0x6095d
jne 0x6095d
movq -0x18(%rsp), %rdx
movq %rdx, %rax
shlq $0x5, %rax
cmpw %r10w, 0x14(%r13,%rax)
cmovgq %r8, %rdx
movq %rdx, -0x18(%rsp)
incq %r14
cmpq %rbx, %rbp
jl 0x60975
subq %r11, %rbp
subq %r11, %rsi
cmpq $0x6, %r14
jl 0x60896
movq -0x18(%rsp), %rsi
testq %rsi, %rsi
movq -0x8(%rsp), %rax
movq 0x50(%rsp), %r9
js 0x609bb
movq 0x58(%rax), %rax
shlq $0x5, %rsi
movslq (%rax,%rsi), %rcx
movq 0x40(%rsp), %rdx
movq %rcx, (%rdx)
addq %rbx, %r11
cmpq %rcx, %r11
jge 0x609d1
movslq 0x8(%rax,%rsi), %rax
movq %rax, (%r9)
movq 0x78(%rsp), %rax
movq $0x0, (%rax)
jmp 0x609c6
movq 0x40(%rsp), %rax
movq %rbx, (%rax)
movq %r12, (%r9)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, (%rdx)
jmp 0x609c3
movq %rbp, (%rsi)
jmp 0x60815