// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
cmpq $0x0, 0x40(%rbp)
jge 0x683ff
movq -0x30(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0x683ff
movq -0x30(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x683ff
movq -0x30(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0xc0(%rbp)
movq $0x1, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jge 0x68202
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x68(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
cmpl $0x0, %eax
jne 0x681ef
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x68(%rbp), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
cmpl $0x0, %eax
jne 0x681ef
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x68(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x68(%rbp), %rdx
subq $0x1, %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x681ef
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x68(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x68(%rbp), %rdx
subq $0x1, %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
movq -0x30(%rbp), %rdx
movq 0x58(%rdx), %rdx
movq -0x68(%rbp), %rsi
shlq $0x5, %rsi
addq %rsi, %rdx
movl 0x4(%rdx), %edx
movq -0x30(%rbp), %rsi
movq 0x58(%rsi), %rsi
movq -0x68(%rbp), %rdi
subq $0x1, %rdi
shlq $0x5, %rdi
addq %rdi, %rsi
subl 0x4(%rsi), %edx
addl %edx, %ecx
cmpl %ecx, %eax
jne 0x681ef
jmp 0x681f1
jmp 0x68202
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x680fc
movq -0x68(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jl 0x683fd
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movslq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x48(%rcx), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x48(%rcx), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0xc(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movl 0xc(%rax), %eax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movl 0x4(%rcx), %ecx
movq -0x30(%rbp), %rdx
movq 0x58(%rdx), %rdx
subl (%rdx), %ecx
subl %ecx, %eax
cltq
movq %rax, -0xa0(%rbp)
movq -0x90(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jg 0x682d1
movq -0x90(%rbp), %rcx
movq -0xa0(%rbp), %rax
subq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
jmp 0x682f4
movq -0xa0(%rbp), %rcx
movq -0x90(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x90(%rbp)
movq $0x0, -0xa0(%rbp)
movq 0x28(%rbp), %rax
subq -0x88(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xa8(%rbp)
movq 0x30(%rbp), %rax
subq -0x98(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jg 0x68356
movq 0x28(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0xa8(%rbp), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x6837a
movq 0x30(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x98(%rbp)
movq -0xb0(%rbp), %rax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x90(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movslq (%rcx), %rcx
cmpq %rcx, %rax
jne 0x683fb
movq -0x88(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x30(%rbp), %rdx
movq 0x48(%rdx), %rdx
subq $0x1, %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
jne 0x683fb
movq 0x18(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
addq $0x1, %rdx
movq -0xa0(%rbp), %rcx
movq -0x98(%rbp), %r8
addq $0x1, %r8
callq 0x61ae0
movq 0x20(%rbp), %rdi
movq 0x18(%rbp), %rsi
callq 0x60af0
jmp 0x68722
jmp 0x683fd
jmp 0x683ff
movq $0x0, -0xc8(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, 0x40(%rbp)
jne 0x68438
movq $0x1, -0xc8(%rbp)
movq -0x40(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x68466
cmpq $0x1, 0x40(%rbp)
jne 0x6844c
movq $0x1, -0xc8(%rbp)
jmp 0x68464
cmpq $0x2, 0x40(%rbp)
jne 0x68462
movq -0x40(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x68464
jmp 0x68466
movq -0xc8(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jg 0x68722
movq $-0x1, -0x78(%rbp)
movq $-0x1, -0x98(%rbp)
movq $-0x1, -0xa0(%rbp)
movq $-0x1, -0x88(%rbp)
movq $-0x1, -0x90(%rbp)
movq $0x0, -0xb8(%rbp)
cmpq $0x0, -0x70(%rbp)
jl 0x6850b
movq -0x38(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq %rax, -0xa0(%rbp)
jmp 0x68516
movq $0x0, -0x90(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x68554
movq -0x38(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq %rax, -0x98(%rbp)
jmp 0x6855f
movq 0x28(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $-0x1, -0xa0(%rbp)
je 0x6857d
cmpq $-0x1, -0x98(%rbp)
je 0x6857d
movq $0x0, -0x78(%rbp)
jmp 0x685c5
cmpq $-0x1, -0xa0(%rbp)
je 0x6859b
cmpq $-0x1, -0x98(%rbp)
jne 0x6859b
movq $0x1, -0x78(%rbp)
jmp 0x685c3
cmpq $-0x1, -0xa0(%rbp)
jne 0x685b9
cmpq $-0x1, -0x98(%rbp)
je 0x685b9
movq $0x2, -0x78(%rbp)
jmp 0x685c1
movq $0x3, -0x78(%rbp)
jmp 0x685c3
jmp 0x685c5
cmpq $0x1, -0x78(%rbp)
je 0x685d3
cmpq $0x2, -0x78(%rbp)
jne 0x68618
leaq -0x90(%rbp), %rdi
leaq -0x90(%rbp), %rsi
addq $0x8, %rsi
leaq -0xa0(%rbp), %rdx
leaq -0xa0(%rbp), %rcx
addq $0x8, %rcx
movq 0x28(%rbp), %r8
movq 0x30(%rbp), %r9
movq -0x78(%rbp), %rax
xorl %r10d, %r10d
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x581e0
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rcx
movq -0x58(%rbp), %r8
movq 0x10(%rbp), %r9
movq -0x90(%rbp), %rax
movq -0x88(%rbp), %r13
movq -0xa0(%rbp), %r12
movq -0x98(%rbp), %r15
movq -0x78(%rbp), %r14
movq -0x48(%rbp), %rbx
movq 0x18(%rbp), %r11
movq 0x28(%rbp), %r10
movsd -0x60(%rbp), %xmm0
movq 0x20(%rbp), %rdx
movq %rdx, -0xd8(%rbp)
xorl %edx, %edx
movq %rdx, %rsi
movq %rax, (%rsp)
movq -0xd8(%rbp), %rax
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq $0x2710, 0x40(%rsp)     # imm = 0x2710
movq $0x7ff, 0x48(%rsp)      # imm = 0x7FF
movq $0x200, 0x50(%rsp)      # imm = 0x200
movq $-0x1, 0x58(%rsp)
movq %rax, 0x60(%rsp)
callq 0x61bf0
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
jne 0x68709
movq 0x20(%rbp), %rdi
movq -0x90(%rbp), %rax
movl %eax, %esi
movq -0x88(%rbp), %rdx
subq $0x1, %rdx
movq -0xa0(%rbp), %rcx
movq -0x98(%rbp), %r8
subq $0x1, %r8
movq -0x78(%rbp), %r9
callq 0x60ce0
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x68480
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)