// File: /workspace/github_cpp_makefile_star2_O2/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 $0x138, %rsp            # imm = 0x138
movq %r9, 0x30(%rsp)
movq %rdi, %r15
movq 0x58(%rdi), %r9
movl %esi, %r14d
shlq $0x5, %r14
leaq (%r9,%r14), %rdi
movswl 0x14(%r9,%r14), %ebx
pushq $0x2
popq %rax
cmpl $0x7fff, %ebx           # imm = 0x7FFF
jne 0x50506
movl 0x1c(%rdi), %esi
testl %esi, %esi
jne 0x5050b
xorl %esi, %esi
cmpw $0x0, 0x12(%rdi)
jns 0x5050b
movq 0x48(%rdx), %rdi
cmpq 0x50(%rdx), %rdi
jne 0x506a5
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
cmoveq %rax, %rsi
movq %rsi, 0x50(%rdx)
movq 0x58(%rdx), %rdi
shlq $0x5, %rsi
movq %rdx, %rbx
callq 0x2370
movq %rbx, %rdx
movq %rax, 0x58(%rbx)
movq 0x58(%r15), %r9
movq 0x48(%rbx), %rdi
jmp 0x506a9
movl 0x1c(%r9,%r14), %esi
movq %r8, 0x50(%rsp)
movl 0x18(%r9,%r14), %r13d
addq %r13, %r13
movq %r15, 0x28(%rsp)
addq 0x70(%r15), %r13
movq %r13, 0x118(%rsp)
movl %esi, %esi
movq %rsi, 0x110(%rsp)
movq %rsi, 0x38(%rsp)
movq %rsi, 0x108(%rsp)
movl (%rdi), %ebp
movl %ebp, 0x7c(%rsp)
movl 0x4(%r9,%r14), %r12d
movl %r12d, 0x80(%rsp)
movl 0x8(%r9,%r14), %r15d
movl %r15d, 0x70(%rsp)
movl 0xc(%r9,%r14), %r14d
movl %r14d, 0x74(%rsp)
movl %ebx, 0x8c(%rsp)
movl %ebx, 0x88(%rsp)
movq 0x48(%rdx), %r8
cmpq 0x50(%rdx), %r8
jne 0x505b9
leaq (%r8,%r8), %rsi
testq %r8, %r8
cmoveq %rax, %rsi
movq %rsi, 0x50(%rdx)
movq 0x58(%rdx), %rdi
shlq $0x5, %rsi
movq %rdx, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
callq 0x2370
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdx
movq %rax, 0x58(%rdx)
movq 0x48(%rdx), %r8
jmp 0x505bd
movq 0x58(%rdx), %rax
leaq 0x48(%rdx), %rsi
leaq 0x1(%r8), %rdi
movq %rdi, 0x48(%rdx)
shlq $0x5, %r8
movl %ebp, (%rax,%r8)
movl %r12d, 0x4(%rax,%r8)
movl %r15d, 0x8(%rax,%r8)
movl %r14d, 0xc(%rax,%r8)
andw $0x0, 0x16(%rax,%r8)
leaq (%rax,%r8), %rdi
movw %bx, 0x14(%rax,%r8)
movq 0x60(%rdx), %r10
movl %r10d, 0x18(%rax,%r8)
andl $0x0, 0x1c(%rax,%r8)
testw %bx, %bx
je 0x50669
movq 0x170(%rsp), %rbx
movslq %ebp, %rax
movq %rax, 0x18(%rsp)
movslq %r15d, %rbp
xorl %eax, %eax
pushq $0x1
popq %r9
xorl %r8d, %r8d
movq 0x38(%rsp), %r12
movq %rsi, %r14
cmpq %rax, %r12
je 0x50649
cmpw $0x0, (%r13,%rax,2)
movq %r9, %rsi
jns 0x50641
testq %r8, %r8
jne 0x5067a
xorl %esi, %esi
incq %rax
movq %rsi, %r8
jmp 0x50629
leaq 0x60(%rsp), %rdx
movq %r14, %rsi
movq %rdi, %rbx
callq 0x1e842
movq %rbx, %rdi
movq %r14, %rsi
callq 0x503df
jmp 0x508b6
leaq 0x60(%rsp), %rdx
callq 0x1e842
xorl %eax, %eax
jmp 0x508b6
cmpq $0x0, 0x30(%rsp)
movq %rdi, 0x8(%rsp)
jne 0x506e4
movq 0x28(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
movq %rdx, %r15
movl 0x10(%rdx), %edx
movq %rbx, %rdi
movq %rcx, %rsi
je 0x506cb
callq 0x7215
jmp 0x506d0
movq 0x58(%rdx), %rax
leaq 0x1(%rdi), %rsi
movq %rsi, 0x48(%rdx)
shlq $0x5, %rdi
movups (%r9,%r14), %xmm0
movups 0x10(%r9,%r14), %xmm1
movups %xmm1, 0x10(%rax,%rdi)
movups %xmm0, (%rax,%rdi)
jmp 0x50673
callq 0x715e
movq (%rbx), %rax
movq %rax, 0x30(%rsp)
movq %r15, %rdx
movq 0x60(%r15), %r10
movq 0x8(%rsp), %rdi
addq %r12, %r10
cmpq %r10, 0x68(%rdx)
jae 0x50746
decq %r10
movq %r10, %rax
shrq %rax
orq %r10, %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(%rdx)
movq 0x70(%rdx), %rdi
leaq 0x2(,%rax,2), %rsi
movq %rdx, %rbx
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x70(%rbx)
movq $0x0, 0x40(%rsp)
xorl %r8d, %r8d
xorl %eax, %eax
movq %r13, 0x48(%rsp)
movq %r14, 0x10(%rsp)
cmpq %r12, %r8
je 0x508a3
movzwl (%r13,%r8,2), %ecx
movl %ecx, %r15d
shrl $0xe, %r15d
movl %ecx, %ebx
andl $0x3fff, %ebx           # imm = 0x3FFF
testw %cx, %cx
js 0x507a9
movq %r14, %rsi
movl %r15d, %edx
movl %ebx, %ecx
movq %r8, %r15
callq 0x4fdcb
movq %r15, %r8
movq 0x8(%rsp), %rdi
addq %rbx, %rbp
addq %rbx, 0x18(%rsp)
pushq $0x1
popq %rax
jmp 0x5089b
movq %r8, 0x20(%rsp)
movzwl %r15w, %r10d
testq %rax, %rax
je 0x5086b
xorl %eax, %eax
xorl %r13d, %r13d
cmpl $0x2, %r15d
setne %al
movq %rax, 0x28(%rsp)
sete %r13b
movq 0x50(%rsp), %r15
movq 0x18(%rsp), %r9
movq %rbp, %r8
subq $0x1, %rbx
jb 0x5084f
subq $0x8, %rsp
movq 0x18(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r15, %rcx
movq %r8, %r14
movq %r9, %rbp
leaq 0x60(%rsp), %rax
pushq %rax
pushq 0x188(%rsp)
movq %r10, %r12
pushq %r10
callq 0x4fe34
movq 0x28(%rsp), %rdi
addq $0x20, %rsp
testw %ax, %ax
je 0x5083c
movq 0x58(%rsp), %rax
subw %ax, 0x14(%rdi)
movq 0x180(%rsp), %rcx
addq %rax, (%rcx)
movw $0x1, %ax
movq %rax, 0x40(%rsp)
movq %r14, %r8
addq %r13, %r8
movq %rbp, %r9
addq 0x28(%rsp), %r9
movq %r12, %r10
jmp 0x507dd
movq %r8, %rbp
movq %r9, 0x18(%rsp)
pushq $0x1
popq %rax
movq 0x48(%rsp), %r13
movq 0x38(%rsp), %r12
movq 0x10(%rsp), %r14
jmp 0x50896
movq 0x10(%rsp), %rsi
movl %r10d, %edx
movl %ebx, %ecx
callq 0x4fdcb
cmpl $0x2, %r15d
jne 0x50885
addq %rbx, %rbp
jmp 0x5088a
addq %rbx, 0x18(%rsp)
xorl %eax, %eax
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %r8
incq %r8
jmp 0x5075e
movq %r14, %rsi
callq 0x503df
testw %ax, %ax
pushq $0x1
popq %rax
cmovel 0x40(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq