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

    if((pre_mode < 0) && (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;
            }
        }
    }

    si = 0; ei = ch_n;
    if(pre_mode == 0) {
        si = 1; ei = ch_n - 1;
    } else if(pre_mode == 1) {
        si = 1;
    } else if(pre_mode == 2) {
        ei = ch_n - 1;
    }

    for (l = si - 1, i = si; i <= ei; 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_adv_hc(z, NULL, NULL, rref, qstr, tu, q[0], q[1], t[0], t[1], mode, wl, exz, ql, 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 $0x98, %rsp
movq %xmm0, 0x80(%rsp)
movq %r9, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdx, (%rsp)
movq %rdi, 0x8(%rsp)
movq 0x100(%rsp), %r14
movq 0xf0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x60(%rsp)
testq %r14, %r14
jns 0x38a04
movq 0x8(%rsp), %rax
cmpl $0x0, 0x30(%rax)
je 0x38bf3
cmpq $0x2, %r14
je 0x38a27
cmpq $0x1, %r14
je 0x38a3b
testq %r14, %r14
jne 0x38a38
movq (%rsp), %rax
decq %rax
movq %rax, 0x10(%rsp)
pushq $0x1
popq %r14
jmp 0x38a44
movq (%rsp), %rax
decq %rax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
jmp 0x38a44
xorl %r14d, %r14d
movq (%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r14, %rax
shlq $0x4, %rax
leaq (%rax,%rsi), %rcx
addq $0x8, %rcx
pushq $-0x1
popq %rbx
cmpq 0x10(%rsp), %r14
jg 0x38be1
movq %r14, %rax
decq %rax
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x30(%rsp)
js 0x38a81
movl -0x14(%rcx), %ebp
movl -0x10(%rcx), %r13d
movq %rbp, 0x30(%rsp)
jmp 0x38a87
movq %rbx, %rbp
xorl %r13d, %r13d
movq %r13, 0x40(%rsp)
movq 0x18(%rsp), %r12
cmpq (%rsp), %r14
jge 0x38aa2
movl -0x4(%rcx), %ebx
movl (%rcx), %r12d
movq %rbx, 0x38(%rsp)
movq %rcx, 0x88(%rsp)
movq %r14, 0x90(%rsp)
movq %r12, 0x48(%rsp)
cmpq $-0x1, %rbp
setne %al
cmpq $-0x1, %rbx
setne %cl
testb %cl, %al
je 0x38ace
xorl %r14d, %r14d
jmp 0x38b41
cmpq $-0x1, %rbp
setne %al
cmpq $-0x1, %rbx
sete %cl
pushq $0x1
popq %r15
testb %cl, %al
jne 0x38b01
cmpq $-0x1, %rbx
setne %al
cmpq $-0x1, %rbp
sete %cl
andb %al, %cl
pushq $0x2
popq %r15
pushq $0x3
popq %r14
cmpb $0x1, %cl
jne 0x38b41
leaq 0x40(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
pushq %r15
pushq $0x0
callq 0x3023a
popq %rax
popq %rcx
movq 0x40(%rsp), %r13
movq 0x48(%rsp), %r12
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %rbx
movq %r15, %r14
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq 0x78(%rsp), %rcx
movq 0x80(%rsp), %r8
movq 0x68(%rsp), %r9
movq 0x88(%rsp), %xmm0
pushq 0x30(%rsp)
pushq $-0x1
pushq $0x200                  # imm = 0x200
pushq $0x7ff                  # imm = 0x7FF
pushq $0x2710                 # imm = 0x2710
pushq 0x48(%rsp)
pushq 0x90(%rsp)
pushq 0xa8(%rsp)
pushq %r14
pushq %rbx
pushq %rbp
pushq %r12
pushq %r13
callq 0x35327
addq $0x70, %rsp
testq %rax, %rax
jne 0x38bc5
decq %r12
decq %rbx
movq 0x28(%rsp), %rdi
movl %r13d, %esi
movq %r12, %rdx
movq %rbp, %rcx
movq %rbx, %r8
movq %r14, %r9
callq 0x34a5b
movq 0x90(%rsp), %r14
incq %r14
movq 0x88(%rsp), %rcx
addq $0x10, %rcx
jmp 0x38a53
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movq 0x48(%rax), %rcx
testq %rcx, %rcx
je 0x38a04
movq 0x8(%rsp), %rax
movq 0x58(%rax), %rdi
leaq 0x34(%rdi), %rdx
xorl %eax, %eax
incq %rax
cmpq %rcx, %rax
jge 0x38c60
cmpw $0x0, (%rdx)
jne 0x38a04
cmpw $0x0, -0x20(%rdx)
jne 0x38a04
movl -0x30(%rdx), %r8d
leal 0x1(%r8), %r9d
cmpl %r9d, -0x14(%rdx)
jne 0x38a04
movl -0x28(%rdx), %r9d
subl %r8d, %r9d
addl -0x10(%rdx), %r9d
leaq 0x20(%rdx), %r8
cmpl %r9d, -0x8(%rdx)
movq %r8, %rdx
je 0x38c14
jmp 0x38a04
movslq (%rdi), %rax
shlq $0x5, %rcx
movslq -0x1c(%rdi,%rcx), %rdx
movl %eax, %r9d
subl 0x4(%rdi), %r9d
addl 0xc(%rdi), %r9d
movslq -0x14(%rdi,%rcx), %r8
movslq %r9d, %rdi
movq %rax, %rcx
subq %rdi, %rcx
xorl %r11d, %r11d
cmpl %edi, %eax
cmovgq %rcx, %r11
movq %rdx, %rcx
notq %rcx
movq 0x18(%rsp), %r10
addq %r10, %rcx
movq %r8, %r9
notq %r9
addq 0x20(%rsp), %r9
leaq (%r9,%rdx), %rbx
decq %r10
cmpq %r9, %rcx
cmovgq %rbx, %r10
cmpq %rax, %r11
jne 0x38a04
cmpq %rdx, %r10
jne 0x38a04
movq 0x20(%rsp), %rsi
decq %rsi
addq %rcx, %r8
cmpq %r9, %rcx
cmovgq %rsi, %r8
movq %rdi, %rsi
subq %rax, %rsi
xorl %ecx, %ecx
cmpl %edi, %eax
cmovleq %rsi, %rcx
incq %rdx
incq %r8
movq 0x58(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x352b9
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x34955