// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movsd %xmm0, 0x58(%rsp)
movq %r9, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x8(%rsp)
movq %rdi, (%rsp)
movq 0xd0(%rsp), %rcx
movq 0xc0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x38(%rsp)
testq %rcx, %rcx
jns 0x6a216
movq (%rsp), %rax
cmpl $0x0, 0x30(%rax)
je 0x6a400
cmpq $0x2, %rcx
je 0x6a23b
cmpq $0x1, %rcx
movq 0x10(%rsp), %r10
je 0x6a24e
testq %rcx, %rcx
jne 0x6a24c
movq 0x8(%rsp), %rax
decq %rax
movl $0x1, %ecx
jmp 0x6a253
movq 0x8(%rsp), %rax
decq %rax
xorl %ecx, %ecx
movq 0x10(%rsp), %r10
jmp 0x6a253
xorl %ecx, %ecx
movq 0x8(%rsp), %rax
cmpq %rax, %rcx
jg 0x6a3f1
leaq -0x1(%rcx), %rdx
shlq $0x4, %rcx
leaq (%rcx,%rsi), %r14
addq $0x8, %r14
movq %rax, 0x30(%rsp)
movq $-0x1, %r12
testq %rdx, %rdx
js 0x6a287
movl -0x14(%r14), %r15d
movl -0x10(%r14), %ebx
jmp 0x6a290
xorl %ebx, %ebx
movq $-0x1, %r15
incq %rdx
movq %r10, %r13
cmpq 0x8(%rsp), %rdx
jge 0x6a2a4
movl -0x4(%r14), %r12d
movl (%r14), %r13d
cmpq $-0x1, %r15
setne %al
cmpq $-0x1, %r12
setne %cl
testb %cl, %al
je 0x6a2bc
xorl %ebp, %ebp
xorl %eax, %eax
jmp 0x6a2ee
cmpq $-0x1, %r15
setne %al
cmpq $-0x1, %r12
sete %cl
testb %cl, %al
je 0x6a2d7
movb $0x1, %al
movl $0x1, %ebp
jmp 0x6a2ee
cmpq $-0x1, %r12
setne %cl
cmpq $-0x1, %r15
sete %al
andb %cl, %al
movzbl %al, %ebp
xorq $0x3, %rbp
testb %al, %al
movq %rdx, 0x60(%rsp)
je 0x6a35d
cmpl $0x2, %ebp
movq 0x18(%rsp), %rax
je 0x6a31f
cmpl $0x1, %ebp
jne 0x6a33c
movq %r10, %r12
subq %rbx, %r12
movq %rax, %r13
subq %r15, %r13
cmpq %r13, %r12
jle 0x6a32c
addq %rbx, %r13
movq %rax, %r12
jmp 0x6a33c
movq %r13, %rbx
subq %r12, %rbx
jle 0x6a334
xorl %r15d, %r15d
jmp 0x6a33c
addq %r15, %r12
movq %r10, %r13
jmp 0x6a33c
movq %r12, %r15
subq %r13, %r15
xorl %ebx, %ebx
testq %rbx, %rbx
movl $0x0, %ecx
cmovleq %rcx, %rbx
testq %r15, %r15
cmovleq %rcx, %r15
cmpq %r10, %r13
cmovgeq %r10, %r13
cmpq %rax, %r12
cmovgeq %rax, %r12
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r8
movq 0x40(%rsp), %r9
movsd 0x60(%rsp), %xmm0
pushq 0x28(%rsp)
pushq $-0x1
pushq $0x200                  # imm = 0x200
pushq $0x7ff                  # imm = 0x7FF
pushq $0x2710                 # imm = 0x2710
pushq %r10
pushq 0x60(%rsp)
pushq 0x80(%rsp)
pushq %rbp
pushq %r12
pushq %r15
pushq %r13
pushq %rbx
callq 0x66813
addq $0x70, %rsp
testq %rax, %rax
jne 0x6a3d5
decq %r13
decq %r12
movq 0x20(%rsp), %rdi
movl %ebx, %esi
movq %r13, %rdx
movq %r15, %rcx
movq %r12, %r8
movq %rbp, %r9
callq 0x65e4a
addq $0x10, %r14
movq 0x30(%rsp), %rax
movq 0x60(%rsp), %rdx
cmpq %rdx, %rax
movq 0x10(%rsp), %r10
jne 0x6a271
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rax
movq 0x48(%rax), %rax
testq %rax, %rax
je 0x6a216
cmpq $0x2, %rax
setge %dl
jl 0x6a436
movq (%rsp), %rdi
movq 0x58(%rdi), %rdi
cmpw $0x0, 0x34(%rdi)
je 0x6a4f7
testb $0x1, %dl
jne 0x6a216
movq (%rsp), %rdx
movq 0x58(%rdx), %rdx
movslq (%rdx), %rdi
shlq $0x5, %rax
movslq -0x1c(%rdx,%rax), %r10
movl %edi, %r9d
subl 0x4(%rdx), %r9d
addl 0xc(%rdx), %r9d
movslq -0x14(%rdx,%rax), %r8
movslq %r9d, %r9
movq %rdi, %rdx
subq %r9, %rdx
xorl %eax, %eax
cmpl %r9d, %edi
cmovgq %rdx, %rax
movq %r10, %r11
notq %r11
movq 0x10(%rsp), %rdx
addq %rdx, %r11
movq %r8, %rbx
notq %rbx
addq 0x18(%rsp), %rbx
leaq (%rbx,%r10), %r14
decq %rdx
cmpq %rbx, %r11
cmovgq %r14, %rdx
cmpq %rdi, %rax
jne 0x6a216
cmpq %r10, %rdx
jne 0x6a216
movq 0x18(%rsp), %rcx
decq %rcx
addq %r11, %r8
cmpq %rbx, %r11
cmovgq %rcx, %r8
movq %r9, %rsi
subq %rdi, %rsi
xorl %ecx, %ecx
cmpl %r9d, %edi
cmovleq %rsi, %rcx
incq %rdx
incq %r8
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x666da
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x65d35
addq $0x4, %rdi
movl $0x2, %r8d
cmpw $0x0, 0x10(%rdi)
jne 0x6a42d
movl (%rdi), %r9d
leal 0x1(%r9), %r10d
cmpl %r10d, 0x1c(%rdi)
jne 0x6a42d
movl 0x8(%rdi), %r10d
subl %r9d, %r10d
addl 0x20(%rdi), %r10d
cmpl %r10d, 0x28(%rdi)
jne 0x6a42d
cmpq %r8, %rax
je 0x6a436
setg %dl
incq %r8
cmpw $0x0, 0x50(%rdi)
leaq 0x20(%rdi), %rdi
je 0x6a501
jmp 0x6a42d