// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: hc_ovlp_base_non_retrieve_direct(overlap_region*, k_mer_hit*, long, long, char*, char*, bit_extz_t*, overlap_region*, double, long, long, unsigned long)
void hc_ovlp_base_non_retrieve_direct(overlap_region *z, k_mer_hit *ch_a, int64_t ch_n, int64_t wl, char* qstr, char* tstr, bit_extz_t *exz, overlap_region *aux_o, double e_rate, int64_t ql, int64_t tl, uint64_t rid)
{
    int64_t i, l, mode, q[2], t[2], qr, tr, is_done, zn;

    if(z->non_homopolymer_errors == 0 && z->w_list.n) {
        zn = z->w_list.n;
        for (i = 1; i < zn; i++) {
            if((z->w_list.a[i].error == 0 && z->w_list.a[i-1].error == 0) && (z->w_list.a[i].x_start == z->w_list.a[i-1].x_end + 1) && 
                (z->w_list.a[i].y_end == (z->w_list.a[i-1].y_end + (z->w_list.a[i].x_end-z->w_list.a[i-1].x_end)))) {
                    continue;
            }
            break;
        }
        if(i >= zn) {
            q[0] = z->w_list.a[0].x_start; q[1] = z->w_list.a[z->w_list.n-1].x_end;  
            t[1] = z->w_list.a[z->w_list.n-1].y_end; t[0] = z->w_list.a[0].y_end - (z->w_list.a[0].x_end-z->w_list.a[0].x_start); 

            if(q[0] <= t[0]) {
                t[0] -= q[0]; q[0] = 0;
            } else {
                q[0] -= t[0]; t[0] = 0;
            }

            qr = ql-q[1]-1; tr = tl-t[1]-1;
            if(qr <= tr) {
                q[1] = ql-1; t[1] += qr;        
            } else {
                t[1] = tl-1; q[1] += tr; 
            }

            if(q[0] == z->w_list.a[0].x_start && q[1] == z->w_list.a[z->w_list.n-1].x_end) {
                // fprintf(stderr, "[M::%s::%u->%u::%c] ovlp::%u, w_list.n::%u\n", __func__, z->x_id, z->y_id+1, "+-"[z->y_pos_strand], z->x_pos_e+1-z->x_pos_s, (uint32_t)z->w_list.n);
                set_exact_exz(exz, q[0], q[1] + 1, t[0], t[1] + 1); push_alnw(aux_o, exz);
                return;
            }
        }
    }

    for (l = -1, i = 0; i <= ch_n; i++) {
        q[0] = q[1] = t[0] = t[1] = mode = -1; is_done = 0;
        if(l >= 0) {
            q[0] = ch_a[l].self_offset; t[0] = ch_a[l].offset;
        } else {
            q[0] = 0;
        }

        if(i < ch_n) {
            q[1] = ch_a[i].self_offset; t[1] = ch_a[i].offset;
        } else {
            q[1] = ql;
        }

        if((t[0] != -1) && (t[1] != -1)) {
            mode = 0;//global
        } else if((t[0] != -1) && (t[1] == -1)) {
            mode = 1;///forward extension
        } else if((t[0] == -1) && (t[1] != -1)) {
            mode = 2;///backward extension
        } else {
            mode = 3;///no primary hit within [ibeg, iend] 
        }

        // if(z->x_id == 57 && z->y_id == 2175) {
        if(mode == 1 || mode == 2) adjust_ext_offset(&(q[0]), &(q[1]), &(t[0]), &(t[1]), ql, tl, 0, mode);
        //     fprintf(stderr, "#[M::%s::] utg%.6dl(%c), q::[%ld, %ld), t::[%ld, %ld), mode::%ld\n", 
        //             __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], q[0], q[1], t[0], t[1], mode);
        // }
        is_done = hc_aln_exz_non_retrieve_adv_hc(z, qstr, tstr, q[0], q[1], t[0], t[1], mode, wl, exz, ql, tl, e_rate, MAX_SIN_L, MAX_SIN_E, FORCE_SIN_L, -1, aux_o);

        // if(z->x_id == 57 && z->y_id == 2175) {
            //     fprintf(stderr, "-is_done::%ld[M::%s::] utg%.6dl(%c), q::[%ld, %ld), t::[%ld, %ld), mode::%ld, ch_n::%ld\n", 
            //         is_done, __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], q[0], q[1], t[0], t[1], mode, ch_n);
        // }
                
        if(!is_done) {///postprocess
            push_unmap_alnw(aux_o, q[0], q[1]-1, t[0], t[1]-1, mode);
        }
        l = i;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm0, 0x48(%rsp)
movq %r9, 0x40(%rsp)
movq %r8, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, %r12
movq %rsi, %rbx
movq 0xa8(%rsp), %r11
movq 0xa0(%rsp), %r10
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %rdi, 0x8(%rsp)
cmpl $0x0, 0x30(%rdi)
movq %r11, 0x20(%rsp)
movq %r10, (%rsp)
je 0x6a376
testq %r12, %r12
js 0x6a367
addq $0x8, %rbx
movq $-0x1, %rax
movq %r12, 0x28(%rsp)
leaq 0x1(%rax), %rcx
testq %rax, %rax
movq %rcx, 0x50(%rsp)
js 0x6a239
movl -0x14(%rbx), %r14d
movl -0x10(%rbx), %r15d
cmpq %r12, %rcx
jge 0x6a266
movl -0x4(%rbx), %r12d
movl (%rbx), %r13d
xorl %ebp, %ebp
jmp 0x6a2d4
cmpq %r12, %rcx
jge 0x6a284
movl -0x4(%rbx), %r12d
movl (%rbx), %r13d
cmpl %r12d, %r13d
jbe 0x6a2ac
movq %r13, %r15
subq %r12, %r15
testq %r15, %r15
movl $0x0, %eax
cmovleq %rax, %r15
movl $0x2, %ebp
xorl %r14d, %r14d
jmp 0x6a2c6
movq %r10, %r12
subq %r15, %r12
movq %r11, %r13
subq %r14, %r13
cmpq %r13, %r12
jle 0x6a29f
addq %r15, %r13
movl $0x1, %ebp
movq %r11, %r12
jmp 0x6a2ba
movq $-0x1, %r12
movl $0x3, %ebp
movq %r10, %r13
xorl %r15d, %r15d
movq $-0x1, %r14
jmp 0x6a2d4
addq %r14, %r12
movl $0x1, %ebp
movq %r10, %r13
jmp 0x6a2ba
movq %r12, %r14
subq %r13, %r14
movl $0x2, %ebp
xorl %r15d, %r15d
testq %r14, %r14
movl $0x0, %eax
cmovleq %rax, %r14
cmpq %r10, %r13
cmovgeq %r10, %r13
cmpq %r11, %r12
cmovgeq %r11, %r12
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %r15, %rcx
movq %r13, %r8
movq %r14, %r9
movsd 0x50(%rsp), %xmm0
pushq 0x18(%rsp)
pushq $-0x1
pushq $0x200                  # imm = 0x200
pushq $0x7ff                  # imm = 0x7FF
pushq $0x2710                 # imm = 0x2710
pushq %r11
pushq %r10
pushq 0x58(%rsp)
pushq 0x78(%rsp)
pushq %rbp
pushq %r12
callq 0x66abe
addq $0x60, %rsp
testq %rax, %rax
jne 0x6a347
decq %r13
decq %r12
movq 0x10(%rsp), %rdi
movl %r15d, %esi
movq %r13, %rdx
movq %r14, %rcx
movq %r12, %r8
movq %rbp, %r9
callq 0x65b7a
addq $0x10, %rbx
movq 0x28(%rsp), %r12
movq 0x50(%rsp), %rax
cmpq %rax, %r12
movq 0x20(%rsp), %r11
movq (%rsp), %r10
jne 0x6a210
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movq 0x48(%rax), %rax
testq %rax, %rax
je 0x6a1f7
movq 0x8(%rsp), %rcx
movq 0x58(%rcx), %rcx
cmpq $0x2, %rax
jl 0x6a3e3
leaq -0x1(%rax), %rdx
leaq 0x34(%rcx), %rsi
cmpw $0x0, (%rsi)
jne 0x6a1f7
cmpw $0x0, -0x20(%rsi)
jne 0x6a1f7
movl -0x30(%rsi), %edi
leal 0x1(%rdi), %r8d
cmpl %r8d, -0x14(%rsi)
jne 0x6a1f7
movl -0x28(%rsi), %r8d
subl %edi, %r8d
addl -0x10(%rsi), %r8d
cmpl %r8d, -0x8(%rsi)
jne 0x6a1f7
addq $0x20, %rsi
decq %rdx
jne 0x6a39f
movslq (%rcx), %rsi
shlq $0x5, %rax
movslq -0x1c(%rcx,%rax), %rdx
movl %esi, %edi
subl 0x4(%rcx), %edi
addl 0xc(%rcx), %edi
movslq -0x14(%rcx,%rax), %r8
movslq %edi, %rax
movq %rsi, %rcx
subq %rax, %rcx
xorl %r10d, %r10d
cmpl %eax, %esi
cmovgq %rcx, %r10
movq %rdx, %rcx
notq %rcx
addq (%rsp), %rcx
movq %r8, %rdi
notq %rdi
addq %r11, %rdi
leaq (%rdi,%rdx), %r11
movq (%rsp), %r9
decq %r9
cmpq %rdi, %rcx
cmovgq %r11, %r9
movq 0x20(%rsp), %r11
cmpq %rsi, %r10
movq (%rsp), %r10
jne 0x6a1f7
cmpq %rdx, %r9
jne 0x6a1f7
decq %r11
addq %rcx, %r8
cmpq %rdi, %rcx
cmovgq %r11, %r8
movq %rax, %rdi
subq %rsi, %rdi
xorl %ecx, %ecx
cmpl %eax, %esi
cmovleq %rdi, %rcx
incq %rdx
incq %r8
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x663fa
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x65a71