// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xf0, %rsp
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)
movq -0x30(%rbp), %rax
movslq 0x24(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rcx
movq -0x38(%rbp), %rdx
xorl %eax, %eax
cmpq 0x8(%rdx), %rcx
movb %al, -0xc1(%rbp)
jge 0x6a2ad
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x68(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
sete %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x6a2b9
jmp 0x6a2c9
jmp 0x6a2bb
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x6a259
movq -0x78(%rbp), %rax
subq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jg 0x6a2e9
movq $0x0, -0x28(%rbp)
jmp 0x6a7ce
movq 0x18(%rbp), %rax
movq $0x0, 0x60(%rax)
movq 0x18(%rbp), %rax
movq $0x0, 0x48(%rax)
movq -0x30(%rbp), %rax
movl 0x10(%rax), %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x30(%rbp), %rax
movl 0x1c(%rax), %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x30(%rbp), %rax
movl 0x4(%rax), %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x30(%rbp), %rax
movl 0x14(%rax), %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x30(%rbp), %rax
movl 0x18(%rax), %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x30(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %r9
movq -0x58(%rbp), %r15
movq 0x10(%rbp), %r14
movq 0x18(%rbp), %rbx
movsd -0x60(%rbp), %xmm0
movq 0x20(%rbp), %r11
movq -0x80(%rbp), %r10
movq 0x28(%rbp), %rax
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq $-0x1, 0x30(%rsp)
callq 0x68070
movq 0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0xc0(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jge 0x6a4a6
movq 0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x6a433
movq 0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x6a433
movq 0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jl 0x6a435
jmp 0x6a495
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq 0x18(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
movq -0x50(%rbp), %r12
movq -0x58(%rbp), %r15
movq 0x10(%rbp), %r14
movsd -0x60(%rbp), %xmm0
movq 0x20(%rbp), %rbx
movq -0x80(%rbp), %r11
movq 0x30(%rbp), %r10
movq 0x28(%rbp), %rax
movq %r12, (%rsp)
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x692f0
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x6a3cf
movq 0x18(%rbp), %rax
movq 0x48(%rax), %rax
cmpq -0xc0(%rbp), %rax
jle 0x6a5db
movq $0x0, -0x90(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq 0x18(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jge 0x6a5a8
movq -0x78(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jge 0x6a542
movq 0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x6a542
movq 0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x6a542
movq 0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x6a542
jmp 0x6a597
movq 0x18(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x78(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rcx
movq 0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rdx
movq %rdx, %rsi
addq $0x1, %rsi
movq %rsi, -0x90(%rbp)
shlq $0x5, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movq 0x18(%rcx), %rcx
movq %rcx, 0x18(%rax)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x6a4ce
movq -0x90(%rbp), %rcx
movq 0x18(%rbp), %rax
movq %rcx, 0x48(%rax)
movq 0x18(%rbp), %rax
movq 0x58(%rax), %rdi
movq 0x18(%rbp), %rax
movq 0x58(%rax), %rsi
movq 0x18(%rbp), %rax
movq 0x48(%rax), %rax
shlq $0x5, %rax
addq %rax, %rsi
callq 0x2f3b0
movq -0x30(%rbp), %rdi
movq 0x18(%rbp), %rsi
movq 0x20(%rbp), %rdx
movq -0x80(%rbp), %rcx
callq 0x673f0
movq -0x30(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0xc0(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jge 0x6a7b8
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x6a741
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x6a741
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x6a741
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x78(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
subl (%rcx), %eax
cltq
movq %rax, -0xa8(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x78(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
subl 0x8(%rcx), %eax
cltq
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jl 0x6a71c
movq -0xa8(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x6a72a
movq -0xb0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x6a7a5
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswq 0x14(%rax), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x78(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
subl (%rcx), %eax
cltq
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x6a7a7
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x6a61d
movq -0x98(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
movq $0x1, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0xf0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)