// File: /workspace/github_cpp_makefile_star2_O1/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), %rbp
movq (%rbp), %rax
leaq -0x1(%rax), %rcx
cmpq $0x1, %rcx
ja 0x610aa
movq 0x68(%rsp), %r15
movq 0x60(%rsp), %r12
movq 0x58(%rsp), %rcx
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %r8
movq (%r8), %r14
movq (%rdx), %r13
movq (%rsi), %r8
movq (%rcx), %rsi
cmpq $0x2, %rax
movq %r8, -0x18(%rsp)
movq %rsi, -0x20(%rsp)
je 0x60c94
cmpq $0x1, %rax
jne 0x60ca1
movq %r12, %rsi
subq %r14, %rsi
movq %r15, %rdx
subq %r8, %rdx
cmpq %rdx, %rsi
jle 0x60ca6
addq %r14, %rdx
movq %r14, %rbx
movq %r15, %rsi
jmp 0x60cbc
movq %r13, %rbx
subq %rsi, %rbx
jle 0x60cb1
xorl %r8d, %r8d
jmp 0x60cb9
movq %r14, %rbx
jmp 0x60cb9
addq %r8, %rsi
movq %r14, %rbx
movq %r12, %rdx
jmp 0x60cbc
movq %rsi, %r8
subq %r13, %r8
xorl %ebx, %ebx
movq %r13, %rdx
xorl %r10d, %r10d
testq %rbx, %rbx
cmovleq %r10, %rbx
testq %r8, %r8
cmovgq %r8, %r10
movq 0x70(%rsp), %r11
cmpq %r12, %rdx
cmovlq %rdx, %r12
cmpq %r15, %rsi
cmovlq %rsi, %r15
cmpq $0x2, %rax
movq %rdi, -0x10(%rsp)
je 0x60ebf
cmpq $0x1, %rax
jne 0x610aa
movq $-0x1, -0x20(%rsp)
cmpq %r13, %r12
jle 0x60e67
movq %r13, %rax
cqto
idivq %r11
movq %rdx, %r8
leaq (%r11,%r13), %rax
notq %r8
addq %r8, %rax
cmpq %r12, %rax
jge 0x60e67
movq -0x10(%rsp), %rcx
movl 0x4(%rcx), %r9d
xorl %r10d, %r10d
movq %r9, %rax
xorl %edx, %edx
idivq %r11
movq %rdx, %rsi
movq 0x58(%rcx), %rbx
movq %r13, %rax
leaq (%r13,%r11,2), %r13
addq %r8, %r13
addq %r11, %rsi
addq %rax, %rsi
addq %r8, %rsi
subq %r9, %rsi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x28(%rsp)
movq $-0x1, -0x20(%rsp)
movq %rsi, %rax
cqto
idivq %r11
movq %rax, %r8
movq %rax, %rcx
shlq $0x5, %rcx
movslq 0xc(%rbx,%rcx), %rax
cmpq $-0x1, %rax
je 0x60e52
leaq (%rbx,%rcx), %rdi
movslq 0x4(%rdi), %rdx
movq %rdx, %r9
subq %r14, %r9
jl 0x60e52
subq -0x18(%rsp), %rax
jl 0x60e52
subl (%rdi), %edx
leal 0x1(%rdx), %ebp
movswl 0x14(%rdi), %edi
addl $0x10, %edx
testl %ebp, %ebp
cmovnsl %ebp, %edx
sarl $0x4, %edx
cmpl %edi, %edx
jge 0x60dd0
movq 0x78(%rsp), %rbp
jmp 0x60e52
movq %r9, %rdx
subq %rax, %rdx
cmovlq %r9, %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 %r9d, %r9d
xorl %edx, %edx
divq %rdi
cmpq %rax, -0x28(%rsp)
movq 0x78(%rsp), %rbp
jg 0x60e30
movb $0x1, %r9b
jne 0x60e30
movq -0x20(%rsp), %rdx
shlq $0x5, %rdx
movzwl 0x14(%rbx,%rdx), %edx
cmpw 0x14(%rbx,%rcx), %dx
setle %r9b
testb %r9b, %r9b
movq -0x28(%rsp), %rcx
cmoveq %rax, %rcx
movq %rcx, -0x28(%rsp)
movq -0x20(%rsp), %rax
cmoveq %r8, %rax
movq %rax, -0x20(%rsp)
incq %r10
cmpq %r12, %r13
jge 0x60e67
addq %r11, %r13
addq %r11, %rsi
cmpq $0x6, %r10
jl 0x60d6e
movq -0x20(%rsp), %rdi
testq %rdi, %rdi
js 0x61078
movq -0x10(%rsp), %rax
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 %r12, %rcx
movq 0x58(%rsp), %rcx
jge 0x6109c
movslq 0xc(%rax,%rdi), %rax
incq %rax
movq %rax, (%rcx)
movq $0x0, (%rbp)
jmp 0x610aa
movq $-0x1, -0x18(%rsp)
cmpq %r14, %rbx
jge 0x61037
leaq -0x1(%r14), %rsi
movq %rsi, %rax
cqto
idivq %r11
subq %rdx, %rsi
cmpq %rbx, %rsi
jl 0x61037
movq %rdx, %r8
movl 0x4(%rdi), %r9d
xorl %r15d, %r15d
movq %r9, %rax
xorl %edx, %edx
idivq %r11
movq %rdx, %rsi
movq 0x58(%rdi), %r12
notq %r8
leaq (%r8,%r14), %rbp
subq %r11, %rbp
addq %r14, %rsi
addq %r8, %rsi
subq %r9, %rsi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x8(%rsp)
movq $-0x1, -0x18(%rsp)
movq %r13, -0x28(%rsp)
movq %rsi, %rax
cqto
idivq %r11
movq %rax, %r8
shlq $0x5, %r8
cmpl $-0x1, 0xc(%r12,%r8)
je 0x61022
movq %rax, %r14
leaq (%r12,%r8), %rcx
movslq (%rcx), %r9
movq %r13, %rax
subq %r9, %rax
jle 0x61022
movslq 0x8(%rcx), %r13
movq -0x20(%rsp), %rdx
subq %r13, %rdx
jle 0x60f8f
movl 0x4(%rcx), %r13d
subl %r9d, %r13d
leal 0x1(%r13), %r9d
movswl 0x14(%rcx), %edi
addl $0x10, %r13d
testl %r9d, %r9d
cmovnsl %r9d, %r13d
sarl $0x4, %r13d
cmpl %edi, %r13d
jge 0x60f99
movq -0x10(%rsp), %rdi
jmp 0x6101d
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 %r13d, %r13d
xorl %edx, %edx
divq %r9
cmpq %rax, -0x8(%rsp)
movq -0x10(%rsp), %rdi
jg 0x60ffb
movb $0x1, %r13b
jne 0x60ffb
movq -0x18(%rsp), %rdx
shlq $0x5, %rdx
movzwl 0x14(%r12,%rdx), %edx
cmpw 0x14(%r12,%r8), %dx
setle %r13b
testb %r13b, %r13b
movq -0x8(%rsp), %rcx
cmoveq %rax, %rcx
movq %rcx, -0x8(%rsp)
movq -0x18(%rsp), %rax
cmoveq %r14, %rax
movq %rax, -0x18(%rsp)
incq %r15
movq -0x28(%rsp), %r13
cmpq %rbx, %rbp
jl 0x61037
subq %r11, %rbp
subq %r11, %rsi
cmpq $0x6, %r15
jl 0x60f2d
movq -0x18(%rsp), %rsi
testq %rsi, %rsi
js 0x6108a
movq 0x58(%rdi), %rax
shlq $0x5, %rsi
movslq (%rax,%rsi), %rcx
movq 0x40(%rsp), %rdx
movq %rcx, (%rdx)
addq %rbx, %r11
cmpq %rcx, %r11
movq 0x50(%rsp), %rcx
jge 0x610a4
movslq 0x8(%rax,%rsi), %rax
movq %rax, (%rcx)
movq 0x78(%rsp), %rax
movq $0x0, (%rax)
jmp 0x610aa
movq 0x48(%rsp), %rax
movq %r12, (%rax)
movq 0x58(%rsp), %rax
movq %r15, (%rax)
jmp 0x610aa
movq 0x40(%rsp), %rax
movq %rbx, (%rax)
movq 0x50(%rsp), %rax
movq %r10, (%rax)
jmp 0x610aa
movq %r12, (%rsi)
movq %r15, (%rcx)
jmp 0x610aa
movq %rbx, (%rdx)
movq %r10, (%rcx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq