// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rdi, %r15
movq 0x58(%rdi), %rax
movl %esi, %r14d
shlq $0x5, %r14
leaq (%rax,%r14), %rsi
cmpw $0x7fff, 0x14(%rax,%r14) # imm = 0x7FFF
jne 0x8447d
cmpl $0x0, 0x1c(%rsi)
jne 0x8447d
cmpw $0x0, 0x12(%rsi)
js 0x84656
movl 0x18(%rax,%r14), %edx
addq %rdx, %rdx
movq %r15, 0x28(%rsp)
addq 0x70(%r15), %rdx
movq %rdx, 0x38(%rsp)
movq %rdx, 0x108(%rsp)
movl 0x1c(%rax,%r14), %r9d
movq %r9, 0x100(%rsp)
movq %r9, 0xf8(%rsp)
movl (%rsi), %r10d
movl %r10d, 0x6c(%rsp)
movl 0x4(%rax,%r14), %r12d
movl %r12d, 0x70(%rsp)
movl 0x8(%rax,%r14), %r15d
movl %r15d, 0x60(%rsp)
movl 0xc(%rax,%r14), %r13d
movl %r13d, 0x64(%rsp)
movswl 0x14(%rax,%r14), %r14d
movl %r14d, 0x7c(%rsp)
movl %r14d, 0x78(%rsp)
movq 0x50(%rbx), %rax
cmpq %rax, 0x48(%rbx)
jne 0x8452f
leaq (%rax,%rax), %rdx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x50(%rbx)
movq 0x58(%rbx), %rdi
shlq $0x5, %rsi
movq %r9, 0x8(%rsp)
movq %rcx, %rbp
movl %r10d, 0x18(%rsp)
callq 0x2370
movl 0x18(%rsp), %r10d
movq %rbp, %rcx
movq 0x8(%rsp), %r9
movq %rax, 0x58(%rbx)
leaq 0x48(%rbx), %rsi
movq 0x48(%rbx), %rax
movq 0x58(%rbx), %r8
leaq 0x1(%rax), %rdx
movq %rdx, 0x48(%rbx)
shlq $0x5, %rax
leaq (%r8,%rax), %rdi
movl %r10d, (%r8,%rax)
movl %r12d, 0x4(%r8,%rax)
movl %r15d, 0x8(%r8,%rax)
movl %r13d, 0xc(%r8,%rax)
movw $0x0, 0x16(%r8,%rax)
movw %r14w, 0x14(%r8,%rax)
movl 0x60(%rbx), %edx
movl %edx, 0x18(%r8,%rax)
movl $0x0, 0x1c(%r8,%rax)
testw %r14w, %r14w
je 0x84602
movq %rsi, 0x18(%rsp)
testq %r9, %r9
setne %al
movq 0x38(%rsp), %r12
je 0x845e0
movq 0x160(%rsp), %r14
movslq %r10d, %r13
movslq %r15d, %rbp
movq %r9, %r11
negq %r11
movl $0x1, %edx
xorl %esi, %esi
cmpw $0x0, -0x2(%r12,%rdx,2)
movl $0x1, %r10d
jns 0x845c7
testq %rsi, %rsi
jne 0x84620
xorl %r10d, %r10d
cmpq %r9, %rdx
setb %al
leaq (%r11,%rdx), %r8
incq %r8
incq %rdx
movq %r10, %rsi
cmpq $0x1, %r8
jne 0x845b0
leaq 0x50(%rsp), %rdx
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
movq %rdi, %r14
callq 0x4bdb5
movq %r14, %rdi
movq %rbx, %rsi
callq 0x84329
jmp 0x8460e
leaq 0x50(%rsp), %rdx
callq 0x4bdb5
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb $0x1, %al
je 0x845e0
cmpq $0x0, 0x30(%rsp)
movq %rdi, 0x10(%rsp)
jne 0x846cb
movq %r14, %r15
movq %r9, %r14
movq 0x28(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
movl 0x10(%rbx), %edx
movq %r15, %rdi
movq %rcx, %rsi
je 0x846b6
callq 0x33dc3
jmp 0x846bb
movq 0x50(%rbx), %rax
cmpq %rax, 0x48(%rbx)
jne 0x84685
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x50(%rbx)
movq 0x58(%rbx), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%rbx)
movq 0x58(%r15), %rax
movq 0x48(%rbx), %rcx
movq 0x58(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x48(%rbx)
shlq $0x5, %rcx
movups (%rax,%r14), %xmm0
movups 0x10(%rax,%r14), %xmm1
movups %xmm1, 0x10(%rdx,%rcx)
movups %xmm0, (%rdx,%rcx)
jmp 0x8460c
callq 0x33cf8
movq (%r15), %rax
movq %rax, 0x30(%rsp)
movq %r14, %r9
movq 0x10(%rsp), %rdi
movq 0x60(%rbx), %rax
addq %r9, %rax
cmpq %rax, 0x68(%rbx)
jae 0x84734
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x68(%rbx)
movq 0x70(%rbx), %rdi
leaq 0x2(,%rcx,2), %rsi
movq %r9, %r14
callq 0x2370
movq 0x10(%rsp), %rdi
movq %r14, %r9
movq %rax, 0x70(%rbx)
cmpq $0x1, %r9
adcq $0x0, %r9
xorl %edx, %edx
xorl %ecx, %ecx
movq $0x0, 0x28(%rsp)
movq %r9, 0x8(%rsp)
movq 0x18(%rsp), %r15
movzwl (%r12,%rcx,2), %eax
movl %eax, %r14d
shrl $0xe, %r14d
movl %eax, %ebx
andl $0x3fff, %ebx           # imm = 0x3FFF
testw %ax, %ax
js 0x847a1
movq %r15, %rsi
movl %r14d, %edx
movq %rcx, %r14
movl %ebx, %ecx
callq 0x83bf5
movq %r14, %rcx
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r9
addq %rbx, %rbp
addq %rbx, %r13
movl $0x1, %edx
incq %rcx
cmpq %r9, %rcx
jne 0x84753
jmp 0x84881
testq %rdx, %rdx
je 0x8484b
movl $0x1, %edx
testl %ebx, %ebx
je 0x84794
movq %rcx, 0x20(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
cmpl $0x2, %r14d
setne %r12b
sete %r15b
movzwl %r14w, %r14d
subq $0x8, %rsp
movq 0x20(%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 0x83d1b
movq 0x30(%rsp), %rdi
addq $0x20, %rsp
testw %ax, %ax
je 0x84827
movq 0x48(%rsp), %rax
subw %ax, 0x14(%rdi)
movq 0x170(%rsp), %rcx
addq %rax, (%rcx)
movl $0x1, %eax
movq %rax, 0x28(%rsp)
addq %r15, %rbp
addq %r12, %r13
decq %rbx
jne 0x847ce
movq 0x38(%rsp), %r12
movq 0x8(%rsp), %r9
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %rcx
jmp 0x8478f
movq %rcx, 0x20(%rsp)
movq %r15, %rsi
movl %r14d, %edx
movl %ebx, %ecx
callq 0x83bf5
cmpl $0x2, %r14d
jne 0x84868
addq %rbx, %rbp
jmp 0x8486b
addq %rbx, %r13
xorl %edx, %edx
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rcx
jmp 0x84794
movq %r15, %rsi
callq 0x84329
testw %ax, %ax
movl $0x1, %eax
cmovel 0x28(%rsp), %eax
jmp 0x8460e