// File: 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
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x9a5bb
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x9a5bb
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x9a5bb
jmp 0x9a4ec
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jne 0x9a55e
movq -0x20(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x9a51d
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rax
shlq %rax
movq %rax, -0x178(%rbp)
jmp 0x9a52b
movl $0x2, %eax
movq %rax, -0x178(%rbp)
jmp 0x9a52b
movq -0x178(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x5, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rcx
movl -0x14(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rcx
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x20(%rbp), %rsi
movq 0x48(%rsi), %rdx
movq %rdx, %rdi
addq $0x1, %rdi
movq %rdi, 0x48(%rsi)
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)
movw $0x0, -0x2(%rbp)
jmp 0x9acae
movq $0x0, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movl -0x14(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movq %rax, -0x78(%rbp)
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x10c(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x108(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x118(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x114(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0xfc(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x14(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x100(%rbp)
jmp 0x9a6bc
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jne 0x9a72e
movq -0x20(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x9a6ed
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rax
shlq %rax
movq %rax, -0x180(%rbp)
jmp 0x9a6fb
movl $0x2, %eax
movq %rax, -0x180(%rbp)
jmp 0x9a6fb
movq -0x180(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x5, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x20(%rbp), %rdx
movq 0x48(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x48(%rdx)
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl -0x10c(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movl -0x108(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x118(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x114(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x40(%rbp), %rax
movw $0x0, 0x16(%rax)
movl -0xfc(%rbp), %eax
movw %ax, %cx
movq -0x40(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x40(%rbp), %rax
movl $0x0, 0x1c(%rax)
cmpl $0x0, -0xfc(%rbp)
jne 0x9a7ea
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x48, %rsi
leaq -0x128(%rbp), %rdx
callq 0x38180
movw $0x0, -0x2(%rbp)
jmp 0x9acae
movslq -0x118(%rbp), %rax
movq %rax, -0x130(%rbp)
movslq -0x10c(%rbp), %rax
movq %rax, -0x138(%rbp)
movq $0x0, -0x158(%rbp)
movq $0x0, -0x168(%rbp)
movq $0x0, -0x170(%rbp)
movq $0x0, -0x170(%rbp)
movq $0x0, -0x168(%rbp)
movq -0x168(%rbp), %rax
cmpq -0x80(%rbp), %rax
jae 0x9a8a0
movq -0x70(%rbp), %rax
movq -0x168(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
cltq
movq %rax, -0x148(%rbp)
cmpq $0x2, -0x148(%rbp)
jge 0x9a87c
movq $0x1, -0x170(%rbp)
jmp 0x9a88a
cmpq $0x0, -0x170(%rbp)
je 0x9a888
jmp 0x9a8a0
jmp 0x9a88a
jmp 0x9a88c
movq -0x168(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x168(%rbp)
jmp 0x9a83d
movq -0x168(%rbp), %rax
cmpq -0x80(%rbp), %rax
jb 0x9a8f7
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x48, %rsi
leaq -0x128(%rbp), %rdx
callq 0x38180
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x48, %rsi
callq 0x9a2b0
cmpw $0x0, %ax
je 0x9a8e7
movq $0x1, -0x158(%rbp)
movq -0x158(%rbp), %rax
movw %ax, -0x2(%rbp)
jmp 0x9acae
cmpq $0x0, -0x50(%rbp)
jne 0x9a941
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x9a920
movq 0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
callq 0xa500
jmp 0x9a936
movq 0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
callq 0xa380
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x9a943
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x60(%rcx), %rcx
addq -0x80(%rbp), %rcx
cmpq %rcx, %rax
jae 0x9aa2e
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rcx
addq -0x80(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rcx
shrq %rcx
movq -0x20(%rbp), %rax
orq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rcx
shrq $0x2, %rcx
movq -0x20(%rbp), %rax
orq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rcx
shrq $0x4, %rcx
movq -0x20(%rbp), %rax
orq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rcx
shrq $0x8, %rcx
movq -0x20(%rbp), %rax
orq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rcx
shrq $0x10, %rcx
movq -0x20(%rbp), %rax
orq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rsi
shlq %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x70(%rax)
jmp 0x9aa30
movq $0x0, -0x170(%rbp)
movq $0x0, -0x168(%rbp)
movq -0x168(%rbp), %rax
cmpq -0x80(%rbp), %rax
jae 0x9ac81
movq -0x70(%rbp), %rax
movq -0x168(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
cltq
movq %rax, -0x148(%rbp)
movq -0x70(%rbp), %rax
movq -0x168(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cltq
movq %rax, -0x140(%rbp)
cmpq $0x2, -0x148(%rbp)
jge 0x9aaf5
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x48, %rsi
movq -0x148(%rbp), %rax
movq -0x140(%rbp), %rcx
movzwl %ax, %edx
callq 0x99790
movq -0x140(%rbp), %rax
addq -0x130(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x140(%rbp), %rax
addq -0x138(%rbp), %rax
movq %rax, -0x138(%rbp)
movq $0x1, -0x170(%rbp)
jmp 0x9ac68
cmpq $0x0, -0x170(%rbp)
jne 0x9ab68
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x48, %rsi
movq -0x148(%rbp), %rax
movq -0x140(%rbp), %rcx
movzwl %ax, %edx
callq 0x99790
cmpq $0x2, -0x148(%rbp)
jne 0x9ab42
movq -0x140(%rbp), %rax
addq -0x130(%rbp), %rax
movq %rax, -0x130(%rbp)
jmp 0x9ab63
cmpq $0x3, -0x148(%rbp)
jne 0x9ab61
movq -0x140(%rbp), %rax
addq -0x138(%rbp), %rax
movq %rax, -0x138(%rbp)
jmp 0x9ab63
jmp 0x9ac66
movq $0x0, -0x150(%rbp)
movq -0x150(%rbp), %rax
cmpq -0x140(%rbp), %rax
jge 0x9ac64
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x48, %rsi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x130(%rbp), %r8
movq -0x138(%rbp), %r9
movq -0x148(%rbp), %rax
movw %ax, %r11w
movq 0x18(%rbp), %r10
leaq -0x160(%rbp), %rax
movzwl %r11w, %r11d
movl %r11d, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x99880
cmpw $0x0, %ax
je 0x9ac0f
movq $0x1, -0x158(%rbp)
movq -0x160(%rbp), %rdx
movq -0x40(%rbp), %rax
movswq 0x14(%rax), %rcx
subq %rdx, %rcx
movw %cx, 0x14(%rax)
movq -0x160(%rbp), %rcx
movq 0x20(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
cmpq $0x2, -0x148(%rbp)
jne 0x9ac2d
movq -0x130(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x130(%rbp)
jmp 0x9ac4b
cmpq $0x3, -0x148(%rbp)
jne 0x9ac49
movq -0x138(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x138(%rbp)
jmp 0x9ac4b
jmp 0x9ac4d
movq -0x150(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x150(%rbp)
jmp 0x9ab73
jmp 0x9ac66
jmp 0x9ac68
jmp 0x9ac6a
movq -0x168(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x168(%rbp)
jmp 0x9aa46
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x48, %rsi
callq 0x9a2b0
cmpw $0x0, %ax
je 0x9aca3
movq $0x1, -0x158(%rbp)
movq -0x158(%rbp), %rax
movw %ax, -0x2(%rbp)
movw -0x2(%rbp), %ax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
nopl (%rax,%rax)