// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: move_wins(overlap_region*, unsigned int, overlap_region*, All_reads*, char*, char*, UC_Read*, asg16_v*, long*)
uint16_t move_wins(overlap_region *z, uint32_t wid, overlap_region *aux, All_reads *rref, char *tstr, char *pstr, UC_Read *pu, asg16_v* buf, int64_t *tot_re)
{
    // if(z->y_id == 3199 && z->x_id == 3196) {
    //     fprintf(stderr, "\n[M::%s] x_id::%u, y_id::%u, x::[%u, %u), y::[%u, %u)\n", __func__, z->x_id, z->y_id, z->x_pos_s, z->x_pos_e + 1, z->y_pos_s, z->y_pos_e + 1);
    //     fprintf(stderr, "[M::%s] wid::%u, x::[%d, %d), y::[%d, %d), err::%d\n", __func__, wid, z->w_list.a[wid].x_start, z->w_list.a[wid].x_end + 1, z->w_list.a[wid].y_start, z->w_list.a[wid].y_end + 1, z->w_list.a[wid].error);
    // }
    if(is_ualn_win((z->w_list.a[wid]))) {
        kv_push(window_list, aux->w_list, (z->w_list.a[wid]));
        return 0;///no move
    }

    window_list *p = NULL; char *tseq = tstr, *pseq = pstr;
    bit_extz_t ez; set_bit_extz_t(ez, (*z), wid);
    kv_pushp(window_list, aux->w_list, &p);
    p->x_start = ez.ts; p->x_end = ez.te;
    p->y_start = ez.ps; p->y_end = ez.pe;
    p->error_threshold = 0; p->error = ez.err;///single round of alignment cannot have INT16_MAX errors
    p->cidx = aux->w_list.c.n; p->clen = 0; ///aux->w_list.c.n += ez.cigar.n;

    if(ez.err == 0) {
        push_wcigar(p, &(aux->w_list), &ez);
        return 0;///no move
    }

    int64_t pi = ez.ps, ti = ez.ts/**, err = 0**/, cl, op, k, rr = 0, re; uint64_t ci = 0, mm = 0; 
    // char cm[4]; cm[0] = 'M'; cm[1] = 'S'; cm[2] = 'I'; cm[3] = 'D'; 
    // if(z->y_id == 3199 && z->x_id == 3196) {
    //     fprintf(stderr, "******\n");
    //     for (ci = 0; ci < ez.cigar.n; ci++) {
    //         op = ez.cigar.a[ci]>>14; cl = (ez.cigar.a[ci]&(0x3fff)); 
    //         fprintf(stderr, "%ld%c(q::[%ld,%ld))(t::[%ld,%ld))(ck::%ld)\n", cl, cm[op], ti, ti + (((op<2)||(op==3))?(cl):(0)), pi, pi + (((op<2)||(op==2))?(cl):(0)), ci);

    //         if(op < 2) {
    //             pi+=cl; ti+=cl;
    //         } else {
    //             if(op == 2) {///more p -> y
    //                 pi+=cl;
    //             } else if(op == 3) {///more t -> x
    //                 ti+=cl;
    //             }
    //         }
    //     }
    //     pi = ez.ps; ti = ez.ts;
    //     fprintf(stderr, "******\n");
    // }

    for (ci = mm = 0; ci < ez.cigar.n; ci++) {
        op = ez.cigar.a[ci]>>14; ///cl = (ez.cigar.a[ci]&(0x3fff)); 
        if(op < 2) {
            mm = 1;
        } else if(mm) {
            break;
        }
        // if(op == 2 || op == 3) break;
    }
    if(ci >= ez.cigar.n) {
        push_wcigar(p, &(aux->w_list), &ez);
        if(ajust_end_cigar(p, &(aux->w_list))) rr = 1;
        return rr;
    }

    if(!pseq) {
        if(z->y_pos_strand) {
            recover_UC_Read_RC(pu, rref, aux->y_id);
        } else {
            recover_UC_Read(pu, rref, aux->y_id);
        }
        pseq = pu->seq;
    }
    
    kv_resize(uint16_t, aux->w_list.c, (aux->w_list.c.n + ez.cigar.n));

    for (ci = mm = 0; ci < ez.cigar.n; ci++) {
        op = ez.cigar.a[ci]>>14; cl = (ez.cigar.a[ci]&(0x3fff)); 
        // if(z->y_id == 24 && z->x_id == 25) {
        //     fprintf(stderr, "[M::%s] op::%ld, cl::%ld, pi::%ld, ti::%ld\n", __func__, op, cl, pi, ti);
        //     fprintf(stderr, "[M::%s] p->cidx::%lu, p->clen::%lu, cc->n::%lu\n", __func__, (uint64_t)p->cidx, (uint64_t)p->clen, (uint64_t)aux->w_list.c.n);
        // }
        // if(z->y_id == 3199 && z->x_id == 3196) {
        //     fprintf(stderr, "%ld%c(q::[%ld,%ld))(t::[%ld,%ld))(ck::%ld)\tmm::%lu\n", cl, cm[op], ti, ti + (((op<2)||(op==3))?(cl):(0)), pi, pi + (((op<2)||(op==2))?(cl):(0)), ci, mm);
        // }

        if(op < 2) {
            append_cigar(p, &(aux->w_list), op, cl);
            pi+=cl; ti+=cl; mm = 1;
        } else {
            if(mm == 0) {
                append_cigar(p, &(aux->w_list), op, cl);
                if(op == 2) {///more p -> y
                    pi+=cl;
                } else if(op == 3) {///more t -> x
                    ti+=cl;
                }
            } else {
                for (k = 0; k < cl; k++) {
                    // if(z->y_id == 3199 && z->x_id == 3196) {
                    //     fprintf(stderr, "+++k::%ld\tqi::%ld\tti::%ld\n", k, ti, pi);
                    // }
                    if(adjust_gap(p, &(aux->w_list), pseq, tseq, pi, ti, op, buf, &re)) {
                        rr = 1; p->error -= re; (*tot_re) += re;
                    }
                    if(op == 2) {///more p -> y
                        pi++;
                    } else if(op == 3) {///more t -> x
                        ti++;
                    }
                }
            }
        }
    }

    // if(rr) fprintf(stderr, "[M::%s] rr::%ld\n", __func__, rr);

    if(ajust_end_cigar(p, &(aux->w_list))) rr = 1;
    return rr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %r9, 0x30(%rsp)
movq %r8, 0x40(%rsp)
movq %rdx, %r13
movq %rdi, %r15
movq 0x58(%rdi), %rdi
movl %esi, %r14d
shlq $0x5, %r14
leaq (%rdi,%r14), %rax
movswl 0x14(%rdi,%r14), %ebx
cmpl $0x7fff, %ebx           # imm = 0x7FFF
jne 0x83bed
movl 0x1c(%rax), %edx
testl %edx, %edx
jne 0x83bf2
xorl %edx, %edx
cmpw $0x0, 0x12(%rax)
jns 0x83bf2
movq 0x48(%r13), %rdx
cmpq 0x50(%r13), %rdx
jne 0x83d9e
leaq (%rdx,%rdx), %rax
testq %rdx, %rdx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x50(%r13)
movq 0x58(%r13), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%r13)
movq 0x58(%r15), %rdi
movq 0x48(%r13), %rdx
jmp 0x83da2
movl 0x1c(%rdi,%r14), %edx
movl 0x18(%rdi,%r14), %esi
addq %rsi, %rsi
movq %r15, 0x20(%rsp)
addq 0x70(%r15), %rsi
movq %rsi, 0x38(%rsp)
movq %rsi, 0x108(%rsp)
movl %edx, %r8d
movq %r8, 0x100(%rsp)
movq %r8, 0xf8(%rsp)
movl (%rax), %r15d
movl %r15d, 0x6c(%rsp)
movl 0x4(%rdi,%r14), %r12d
movl %r12d, 0x70(%rsp)
movl 0x8(%rdi,%r14), %r11d
movl %r11d, 0x60(%rsp)
movl 0xc(%rdi,%r14), %r14d
movl %r14d, 0x64(%rsp)
movl %ebx, 0x7c(%rsp)
movl %ebx, 0x78(%rsp)
movq 0x48(%r13), %rdx
cmpq 0x50(%r13), %rdx
movq %r8, 0x10(%rsp)
jne 0x83ca0
leaq (%rdx,%rdx), %rax
testq %rdx, %rdx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x50(%r13)
movq 0x58(%r13), %rdi
shlq $0x5, %rsi
movq %rcx, %rbp
movl %r11d, 0x8(%rsp)
callq 0x2370
movl 0x8(%rsp), %r11d
movq %rbp, %rcx
movq 0x10(%rsp), %r8
movq %rax, 0x58(%r13)
movq 0x48(%r13), %rdx
jmp 0x83ca4
movq 0x58(%r13), %rax
leaq 0x48(%r13), %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, 0x48(%r13)
shlq $0x5, %rdx
leaq (%rax,%rdx), %rdi
movl %r15d, (%rax,%rdx)
movl %r12d, 0x4(%rax,%rdx)
movl %r11d, 0x8(%rax,%rdx)
movl %r14d, 0xc(%rax,%rdx)
movw $0x0, 0x16(%rax,%rdx)
movw %bx, 0x14(%rax,%rdx)
movq 0x60(%r13), %r9
movl %r9d, 0x18(%rax,%rdx)
movl $0x0, 0x1c(%rax,%rdx)
testw %bx, %bx
je 0x83d52
movq %rsi, 0x28(%rsp)
testq %r8, %r8
movq 0x38(%rsp), %r12
je 0x83d30
movq %r13, %r14
movq 0x160(%rsp), %rbx
movslq %r15d, %r13
movslq %r11d, %rbp
xorl %edx, %edx
xorl %eax, %eax
movl $0x1, %esi
cmpw $0x0, (%r12,%rax,2)
jns 0x83d25
testq %rdx, %rdx
jne 0x83d70
xorl %esi, %esi
incq %rax
movq %rsi, %rdx
cmpq %rax, %r8
jne 0x83d11
leaq 0x50(%rsp), %rdx
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq %rdi, %r14
callq 0x4b41c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x83a56
jmp 0x83d5e
leaq 0x50(%rsp), %rdx
callq 0x4b41c
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x30(%rsp)
movq 0x28(%rsp), %r15
movq %rdi, 0x8(%rsp)
jne 0x83ddf
movq 0x20(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
movl 0x10(%r14), %edx
movq %rbx, %rdi
movq %rcx, %rsi
je 0x83dc4
callq 0x334a6
jmp 0x83dc9
movq 0x58(%r13), %rax
leaq 0x1(%rdx), %rsi
movq %rsi, 0x48(%r13)
shlq $0x5, %rdx
movups (%rdi,%r14), %xmm0
movups 0x10(%rdi,%r14), %xmm1
movups %xmm1, 0x10(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
jmp 0x83d5c
callq 0x333e0
movq (%rbx), %rax
movq %rax, 0x30(%rsp)
movq 0x60(%r14), %r9
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
addq %r8, %r9
cmpq %r9, 0x68(%r14)
jae 0x83e43
decq %r9
movq %r9, %rax
shrq %rax
orq %r9, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x68(%r14)
movq 0x70(%r14), %rdi
leaq 0x2(,%rax,2), %rsi
callq 0x2370
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r8
movq %rax, 0x70(%r14)
xorl %edx, %edx
xorl %ecx, %ecx
movq $0x0, 0x20(%rsp)
movzwl (%r12,%rcx,2), %eax
movl %eax, %r14d
shrl $0xe, %r14d
movl %eax, %ebx
andl $0x3fff, %ebx           # imm = 0x3FFF
testw %ax, %ax
js 0x83e9e
movq %r15, %rsi
movl %r14d, %edx
movq %rcx, %r14
movl %ebx, %ecx
callq 0x833c8
movq %r14, %rcx
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r8
addq %rbx, %rbp
addq %rbx, %r13
movl $0x1, %edx
incq %rcx
cmpq %r8, %rcx
jne 0x83e50
jmp 0x83f7d
testq %rdx, %rdx
je 0x83f47
movl $0x1, %edx
testl %ebx, %ebx
je 0x83e91
movq %rcx, 0x18(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
cmpl $0x2, %r14d
setne %r12b
sete %r15b
movzwl %r14w, %r14d
subq $0x8, %rsp
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq %rbp, %r8
movq %r13, %r9
leaq 0x50(%rsp), %rax
pushq %rax
pushq 0x178(%rsp)
pushq %r14
callq 0x834fb
movq 0x28(%rsp), %rdi
addq $0x20, %rsp
testw %ax, %ax
je 0x83f23
movq 0x48(%rsp), %rax
subw %ax, 0x14(%rdi)
movq 0x170(%rsp), %rcx
addq %rax, (%rcx)
movw $0x1, %ax
movq %rax, 0x20(%rsp)
addq %r15, %rbp
addq %r12, %r13
decq %rbx
jne 0x83ecb
movq 0x38(%rsp), %r12
movq 0x10(%rsp), %r8
movq 0x28(%rsp), %r15
movq 0x18(%rsp), %rcx
jmp 0x83e8c
movq %r15, %rsi
movq %rcx, 0x18(%rsp)
movl %r14d, %edx
movl %ebx, %ecx
callq 0x833c8
cmpl $0x2, %r14d
jne 0x83f64
addq %rbx, %rbp
jmp 0x83f67
addq %rbx, %r13
xorl %edx, %edx
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rcx
jmp 0x83e91
movq %r15, %rsi
callq 0x83a56
testw %ax, %ax
movl $0x1, %eax
cmovel 0x20(%rsp), %eax
jmp 0x83d5e