// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_hc_fast_cigar0(overlap_region*, Candidates_list*, unsigned long, All_reads*, char*, UC_Read*, bit_extz_t*, overlap_region*, double, long, unsigned long, long, long*)
uint64_t gen_hc_fast_cigar0(overlap_region *z, Candidates_list *cl, uint64_t wl, All_reads *rref, char* qstr, UC_Read *tu, bit_extz_t *exz, overlap_region *aux_o, double e_rate, int64_t ql, uint64_t rid, int64_t h_khit, int64_t *re)
{
    int64_t ch_idx = z->shared_seed, ch_n;
    int64_t i, tl, id = z->y_id, m, tot_e, aln, xe, ye; 
    k_mer_hit *ch_a = cl->list + ch_idx;
    tl = Get_READ_LENGTH((*rref), id);
    for (i = ch_idx; i < cl->length && cl->list[i].readID == cl->list[ch_idx].readID; i++); ch_n = i-ch_idx;
    if(ch_n <= 0) return 0;

    ///debug for memory
    // snprintf(NULL, 0, "dwn::%u\tdcn::%u", (uint32_t)aux_o->w_list.n, (uint32_t)aux_o->w_list.c.n);
    
    // fprintf(stderr, "[M::%s::rid->%ld] utg%.6dl(%c), z::[%u, %u)\n", 
    // __func__, rid, (int32_t)z->y_id+1, "+-"[z->y_pos_strand],  z->x_pos_s, z->x_pos_e+1);
    aux_o->w_list.n = aux_o->w_list.c.n = 0; 
    aux_o->y_id = z->y_id; aux_o->y_pos_strand = z->y_pos_strand;
    aux_o->x_pos_s = z->x_pos_s; aux_o->x_pos_e = z->x_pos_e;
    aux_o->y_pos_s = z->y_pos_s; aux_o->y_pos_e = z->y_pos_e;

    hc_ovlp_base_direct(z, ch_a, ch_n, wl, rref, qstr, tu, exz, aux_o, e_rate, ql, tl, rid, -1);

    int64_t aux_n = aux_o->w_list.n;
    for (i = 0; i < aux_n; i++) {
        // if(z->y_id == 30129) {
        //     fprintf(stderr, "[aln::-i->%ld::ql->%d] q::[%d, %d), t::[%d, %d), err::%d, clen::%u, mode::%d\n", i, 
        //         aux_o->w_list.a[i].x_end+1-aux_o->w_list.a[i].x_start,
        //         aux_o->w_list.a[i].x_start, aux_o->w_list.a[i].x_end+1, 
        //         aux_o->w_list.a[i].y_start, aux_o->w_list.a[i].y_end+1, 
        //         aux_o->w_list.a[i].error, aux_o->w_list.a[i].clen, aux_o->w_list.a[i].error_threshold);
        // }
        if(!(is_ualn_win(aux_o->w_list.a[i]))) continue;
        // if((aux_o->w_list.a[i].x_end+1-aux_o->w_list.a[i].x_start) <= FORCE_CNS_L) {
            // fprintf(stderr, "[aln::-i->%ld::ql->%d] q::[%d, %d), t::[%d, %d), err::%d, clen::%u, mode::%d\n", i, 
            //             aux_o->w_list.a[i].x_end+1-aux_o->w_list.a[i].x_start,
            //             aux_o->w_list.a[i].x_start, aux_o->w_list.a[i].x_end+1, 
            //             aux_o->w_list.a[i].y_start, aux_o->w_list.a[i].y_end+1, 
            //             aux_o->w_list.a[i].error, aux_o->w_list.a[i].clen, aux_o->w_list.a[i].error_threshold);
        // }
        //will overwrite ch_a; does not matter
        rechain_aln_hc(z, cl, aux_o, i, wl, rref, qstr, tu, exz, e_rate, ql, tl, h_khit, rid);
    }

    if(((int64_t)aux_o->w_list.n) > aux_n) {
        for (i = m = 0; i < ((int64_t)aux_o->w_list.n); i++) {
            if((i < aux_n) && (is_ualn_win(aux_o->w_list.a[i]))) continue;
            aux_o->w_list.a[m++] = aux_o->w_list.a[i];
        }
        aux_o->w_list.n = m;
        radix_sort_window_list_xs_srt(aux_o->w_list.a, aux_o->w_list.a+aux_o->w_list.n);
    }

    ///update z by aux_o
    update_overlap_region(z, aux_o, ql, tl);

    aux_n = z->w_list.n;
    for (i = tot_e = aln = 0; i < aux_n; i++) {
        if(is_ualn_win(z->w_list.a[i])) {
            xe = z->w_list.a[i].x_end + 1 - z->w_list.a[i].x_start; 
            ye = z->w_list.a[i].y_end + 1 - z->w_list.a[i].y_start;
            tot_e += ((xe >= ye)?(xe):(ye));
        } else {
            tot_e += z->w_list.a[i].error; aln += z->w_list.a[i].x_end + 1 - z->w_list.a[i].x_start;
        }
    }
    *re = tot_e;
    // fprintf(stderr, "[M::%s::%u->%u::%c] ovlp::%u, aln::%ld, tot_e::%ld, w_list.n::%u, ch_n::%ld\n", 
    // __func__, z->x_id, z->y_id+1, "+-"[z->y_pos_strand], z->x_pos_e+1-z->x_pos_s, aln, tot_e, (uint32_t)z->w_list.n, ch_n);

    // debug_overlap_region(aux_o, qstr, tu, NULL, NULL, rref);


    // ch_a = cl->list + ch_idx; //update
    // for (i = 0; i < wn; i++) z->w_list.a[i].clen = 0;///clean cigar
    // if(on > 1) {
    //     fprintf(stderr, "[M::%s::] rid::%lu, on::%ld\n", __func__, rid, on);
    // }
    // if(z->y_id == 126) prt_k_mer_hit(ch_a, ch_n);
    // for (i = ch_i = 0; i < on; i++) {
    //     assert((i<=0)||(ov[i].qs > ov[i-1].qe));
    //     ov[i].sec = 16;///do not know the aln type
    //     ch_i = sub_base_aln(z, dp, ch_a, ch_n, pe, ov[i].qs, ov[i].qe, wl, uref, hpc_g, rref, qstr, tu, exz, e_rate, ql, tl, ch_i, rid);
    //     pe = ov[i].qe;
    // }
    
   return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm0, 0x28(%rsp)
movq %r9, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r8
movq %rdi, %rbx
movslq 0x24(%rdi), %rax
movq %rax, %rsi
shlq $0x4, %rsi
addq (%r8), %rsi
movl 0x10(%rdi), %r11d
movq %rcx, 0x10(%rsp)
movq 0x18(%rcx), %rdx
movq (%rdx,%r11,8), %rdx
movq %rdx, (%rsp)
movq %r8, 0x30(%rsp)
movq 0x8(%r8), %rdi
cmpq %rax, %rdi
movq %rax, %rdx
cmovgq %rdi, %rdx
movq 0x78(%rsp), %r12
movq %rsi, %r9
movq %rax, %r8
cmpq %rdi, %r8
jge 0x39b05
movl (%rsi), %r10d
xorl (%r9), %r10d
testl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jne 0x39b02
incq %r8
addq $0x10, %r9
jmp 0x39ae5
movq %r8, %rdx
subq %rax, %rdx
jle 0x39c8a
andq $0x0, 0x60(%r12)
andq $0x0, 0x48(%r12)
movl %r11d, 0x10(%r12)
movl 0x1c(%rbx), %eax
movl %eax, 0x1c(%r12)
movq 0x4(%rbx), %rax
movq %rax, 0x4(%r12)
movq 0x14(%rbx), %rax
movq %rax, 0x14(%r12)
subq $0x40, %rsp
movq 0x40(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r12, 0x10(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x8(%rsp)
orq $-0x1, 0x30(%rsp)
movq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x58(%rsp), %r9
movsd 0x68(%rsp), %xmm0
callq 0x38975
addq $0x40, %rsp
movq 0x48(%r12), %r15
xorl %ebp, %ebp
testq %r15, %r15
movl $0x0, %r14d
cmovgq %r15, %r14
pushq $0x12
popq %r13
cmpq %rbp, %r14
je 0x39c27
movq 0x58(%r12), %rax
cmpw $0x7fff, 0x2(%rax,%r13) # imm = 0x7FFF
jne 0x39c1e
cmpl $0x0, 0xa(%rax,%r13)
jne 0x39c1e
cmpw $0x0, (%rax,%r13)
jns 0x39c1e
subq $0x10, %rsp
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
movq %r12, %rdx
movq %rbp, %rcx
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
movsd 0x38(%rsp), %xmm0
pushq 0xa0(%rsp)
pushq 0x18(%rsp)
pushq 0xa0(%rsp)
pushq 0x98(%rsp)
pushq 0x50(%rsp)
pushq 0x50(%rsp)
callq 0x3937f
addq $0x40, %rsp
incq %rbp
addq $0x20, %r13
jmp 0x39bae
movq 0x48(%r12), %rcx
cmpq %r15, %rcx
jle 0x39ca7
pushq $0x1c
popq %rax
xorl %esi, %esi
xorl %edx, %edx
cmpq %rcx, %rdx
jge 0x39c91
movq 0x58(%r12), %rdi
cmpq %r15, %rdx
jge 0x39c5e
cmpw $0x7fff, -0x8(%rdi,%rax) # imm = 0x7FFF
jne 0x39c5e
cmpl $0x0, (%rdi,%rax)
jne 0x39c5e
cmpw $0x0, -0xa(%rdi,%rax)
js 0x39c81
leaq 0x1(%rsi), %r8
shlq $0x5, %rsi
movups -0x1c(%rdi,%rax), %xmm0
movups -0xc(%rdi,%rax), %xmm1
movups %xmm1, 0x10(%rdi,%rsi)
movups %xmm0, (%rdi,%rsi)
movq 0x48(%r12), %rcx
movq %r8, %rsi
incq %rdx
addq $0x20, %rax
jmp 0x39c38
xorl %eax, %eax
jmp 0x39d2c
movq %rsi, 0x48(%r12)
movq 0x58(%r12), %rdi
shlq $0x5, %rsi
addq %rdi, %rsi
callq 0x19dc0
movq %rbx, %rdi
movq %r12, %rsi
movq 0x80(%rsp), %rdx
movq (%rsp), %rcx
callq 0x382a0
movq 0x48(%rbx), %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
xorl %edx, %edx
movq 0x98(%rsp), %r9
subq $0x1, %rax
jb 0x39d26
movq 0x58(%rbx), %rsi
movswq 0x14(%rsi,%rcx), %rdi
cmpq $0x7fff, %rdi           # imm = 0x7FFF
jne 0x39d1d
cmpl $0x0, 0x1c(%rsi,%rcx)
jne 0x39d1d
cmpw $0x0, 0x12(%rsi,%rcx)
jns 0x39d1d
movl 0x4(%rsi,%rcx), %edi
movl 0xc(%rsi,%rcx), %r8d
subl (%rsi,%rcx), %edi
incl %edi
subl 0x8(%rsi,%rcx), %r8d
incl %r8d
cmpl %r8d, %edi
cmovgl %edi, %r8d
movslq %r8d, %rdi
addq %rdi, %rdx
addq $0x20, %rcx
jmp 0x39cd5
movq %rdx, (%r9)
pushq $0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq