// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x38, %rsp
movq 0xb0(%rsp), %rax
movq (%rax), %rbp
leaq -0x1(%rbp), %rax
cmpq $0x1, %rax
ja 0x30648
movq %rdi, %rbx
movq 0xa8(%rsp), %r13
movq 0xa0(%rsp), %r9
movq 0x98(%rsp), %r8
movq 0x90(%rsp), %rax
movq 0x88(%rsp), %rcx
movq 0x80(%rsp), %rdx
movq 0x78(%rsp), %rsi
movq (%rsi), %r14
leaq 0x30(%rsp), %rdi
movq %r14, (%rdi)
movq (%rdx), %r12
leaq 0x28(%rsp), %rsi
movq %r12, (%rsi)
movq (%rcx), %r15
leaq 0x20(%rsp), %rdx
movq %r15, (%rdx)
movq (%rax), %rax
leaq 0x18(%rsp), %rcx
movq %rax, 0x10(%rsp)
movq %rax, (%rcx)
pushq %rbp
pushq $0x0
callq 0x3023a
popq %rax
popq %rcx
cmpq $0x1, %rbp
jne 0x30489
movq 0x28(%rsp), %r8
cmpq %r12, %r8
jle 0x305d7
movq %r12, %rax
cqto
idivq %r13
movq %rdx, %rcx
addq %r13, %r12
notq %rcx
addq %r12, %rcx
pushq $-0x1
popq %rax
movq %rax, 0x8(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x10(%rsp)
xorl %r10d, %r10d
cmpq %r8, %rcx
jge 0x30590
cmpq $0x5, %r10
jg 0x30590
movl 0x4(%rbx), %esi
movq %rsi, %rax
xorl %edx, %edx
idivq %r13
subq %rsi, %rdx
leaq (%rdx,%rcx), %rax
cqto
idivq %r13
movq 0x58(%rbx), %r12
movq %rax, %rdx
shlq $0x5, %rdx
movslq 0xc(%r12,%rdx), %rdi
cmpq $-0x1, %rdi
je 0x30481
movq %rax, %rsi
addq %r12, %rdx
movslq 0x4(%rdx), %rax
movq %rax, %rbp
subq %r14, %rbp
jl 0x30481
subq %r15, %rdi
jl 0x30481
movq %r15, %r9
subl (%rdx), %eax
incl %eax
movswl 0x14(%rdx), %r15d
cltd
pushq $0x10
popq %r11
idivl %r11d
cmpl %r15d, %eax
jge 0x30425
movq %r9, %r15
jmp 0x30481
movq %rbp, %r11
subq %rdi, %r11
cmovlq %rbp, %rdi
movq %rdi, %rax
cqto
pushq $0x10
popq %rdi
idivq %rdi
movq %rax, %rdi
cmpq $0x2, %rax
jge 0x30446
pushq $0x1
popq %rdi
movq %r11, %rax
negq %rax
cmovsq %r11, %rax
xorl %edx, %edx
divq %rdi
cmpq %rax, 0x10(%rsp)
jle 0x30468
movq %rax, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
jmp 0x3047b
jne 0x3047b
movq 0x8(%rsp), %rax
shlq $0x5, %rax
cmpw %r15w, 0x14(%r12,%rax)
jg 0x30461
movq %r9, %r15
incq %r10
addq %r13, %rcx
jmp 0x303ac
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
jge 0x30630
decq %r14
movq %r14, %rax
cqto
idivq %r13
subq %rdx, %r14
pushq $-0x1
popq %r8
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x8(%rsp)
xorl %r9d, %r9d
cmpq %rdi, %r14
jl 0x305f1
cmpq $0x5, %r9
jg 0x305f1
movl 0x4(%rbx), %ecx
movq %rcx, %rax
xorl %edx, %edx
idivq %r13
subq %rcx, %rdx
leaq (%rdx,%r14), %rax
cqto
idivq %r13
movq 0x58(%rbx), %rbp
movq %rax, %rdx
shlq $0x5, %rdx
cmpl $-0x1, 0xc(%rbp,%rdx)
je 0x30588
addq %rbp, %rdx
movslq (%rdx), %r10
movq %r12, %r15
subq %r10, %r15
jle 0x30588
movq %rax, %rcx
movslq 0x8(%rdx), %rax
movq 0x10(%rsp), %rsi
subq %rax, %rsi
jle 0x30588
movl 0x4(%rdx), %eax
subl %r10d, %eax
incl %eax
movswl 0x14(%rdx), %r11d
cltd
pushq $0x10
popq %r10
idivl %r10d
cmpl %r11d, %eax
jl 0x30588
movq %r15, %r10
subq %rsi, %r10
cmovlq %r15, %rsi
movq %rsi, %rax
cqto
pushq $0x10
popq %rsi
idivq %rsi
movq %rax, %rsi
cmpq $0x2, %rax
jge 0x30554
pushq $0x1
popq %rsi
movq %r10, %rax
negq %rax
cmovsq %r10, %rax
xorl %edx, %edx
divq %rsi
cmpq %rax, 0x8(%rsp)
jle 0x30574
movq %rax, 0x8(%rsp)
movq %rcx, %r8
jmp 0x30585
jne 0x30585
movq %r8, %rax
shlq $0x5, %rax
cmpw %r11w, 0x14(%rbp,%rax)
jg 0x3056f
incq %r9
subq %r13, %r14
jmp 0x304b6
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
js 0x305d7
movq 0x58(%rbx), %rax
shlq $0x5, %rdi
movslq 0x4(%rax,%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq 0x80(%rsp), %rsi
movq %rdx, (%rsi)
addq %r13, %rcx
incq %rcx
cmpq %r8, %rcx
jge 0x30657
movslq 0xc(%rax,%rdi), %rax
incq %rax
movq 0x90(%rsp), %rcx
jmp 0x3061f
movq 0x80(%rsp), %rax
movq %r8, (%rax)
movq 0x18(%rsp), %rax
movq 0x90(%rsp), %rcx
jmp 0x30645
testq %r8, %r8
js 0x30630
movq 0x58(%rbx), %rax
shlq $0x5, %r8
movslq (%rax,%r8), %rcx
movq 0x78(%rsp), %rdx
movq %rcx, (%rdx)
addq %rdi, %r13
cmpq %rcx, %r13
jge 0x3065c
movslq 0x8(%rax,%r8), %rax
movq 0x88(%rsp), %rcx
movq %rax, (%rcx)
movq 0xb0(%rsp), %rax
andq $0x0, (%rax)
jmp 0x30648
movq 0x78(%rsp), %rax
movq %rdi, (%rax)
movq 0x20(%rsp), %rax
movq 0x88(%rsp), %rcx
movq %rax, (%rcx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r8, (%rsi)
jmp 0x305e2
movq %rdi, (%rdx)
jmp 0x30638