// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: adjust_gap(window_list*, window_list_alloc*, char*, char*, long, long, unsigned short, asg16_v*, long*)
uint16_t adjust_gap(window_list *idx, window_list_alloc *res, char *pstr, char *tstr, int64_t pi, int64_t ti, uint16_t op0, asg16_v* buf, int64_t *rd_err)
{
    (*rd_err) = 0;

    if(idx->clen == 0) {
        append_cigar(idx, res, op0, 1);
        return 0;///no move
    }
    if(op0 != 2 && op0 != 3) return 0;///no move
    if(op0 == 2) {///more p -> y
        ti--;
    } else if(op0 == 3) {///more t -> x
        pi--;
    }

    // if(z->y_id == 3199 && z->x_id == 3196) {
    //     fprintf(stderr, "\n[M::%s]\tqi::%ld\tti::%ld\n", __func__, ti, pi);
    // }

    uint16_t *ca = res->c.a+idx->cidx; 
    int64_t ci = idx->clen; int64_t op, cl, k, l[2]; uint16_t p, ff;
    for (ci--, buf->n = ff = 0; ci >= 0; ci--) {
        op = ca[ci]>>14; cl = (ca[ci]&(0x3fff)); 
        // if(z->y_id == 3199 && z->x_id == 3196) {
        //     fprintf(stderr, "[M::%s]\tqi::%ld\tti::%ld\tci::%ld\tcl::%ld\top::%ld\n", __func__, ti, pi, ci, cl, op);
        // }
        if(op == 2 || op == 3) {
            p = op0; p <<= 14; p += 1; kv_push(uint16_t, *buf, p);
            l[0] = cl;
            p = op; p <<= 14; p += l[0]; kv_push(uint16_t, *buf, p);
            break;
        } else if(op == 0) {        
            for (k = cl-1, l[0] = l[1] = 0; k >= 0; k--, pi--, ti--) {
                if(pstr[pi] != (tstr[ti])) break;
                // if(op == 1) {
                //     if(!(pstr[pi] != (tstr[ti]))) {
                //         fprintf(stderr, "[M::%s] xid::%u, yid::%u, qi::%ld, ti::%ld\n", __func__, z->x_id, z->y_id, ti, pi);
                //     }
                //     assert(pstr[pi] != (tstr[ti]));
                // }
            }
            l[1] = cl - k - 1; l[0] = k + 1;
            if(l[1] > 0) {
                p = op; p <<= 14; p += l[1]; kv_push(uint16_t, *buf, p); ff = 1;
            }

            if(l[0] > 0) {
                p = op0; p <<= 14; p += 1; kv_push(uint16_t, *buf, p);
            }

            if(l[0] > 0) {
                p = op; p <<= 14; p += l[0]; kv_push(uint16_t, *buf, p);
            }
        } else {///op == 1; it is possible since cigar is not optimal
            for (k = cl-1, l[0] = cl, l[1] = 0; k >= 0; k--, pi--, ti--) {
                if(pstr[pi] == (tstr[ti])) {
                    l[1] = l[0] - k - 1;
                    l[0] = k;

                    if(l[1] > 0) {
                        p = op; p <<= 14; p += l[1]; kv_push(uint16_t, *buf, p); ff = 1;///push unmatch
                    }
                    p = 0; p <<= 14; p += 1; kv_push(uint16_t, *buf, p);///push match
                    (*rd_err)++;
                }
            }

            if(l[0] > 0) {
                p = op; p <<= 14; p += l[0]; kv_push(uint16_t, *buf, p);
            }
            l[0] = 0;
        }

        // fprintf(stderr, "[M::%s] ci::%ld, l[0]::%ld, l[1]::%ld\n", __func__, ci, l[0], l[1]);

        if(l[0] > 0) break;
    }

    // fprintf(stderr, "[M::%s] ff::%u, ci::%ld, buf->n::%lu\n", __func__, ff, ci, (uint64_t)buf->n);

    if(!ff) {///no move
        append_cigar(idx, res, op0, 1);
        return 0;///no move
    } else if(ci >= 0) {
        // if(!(idx->cidx + idx->clen == res->c.n)) {
        //     fprintf(stderr, "[M::%s] idx->cidx::%lu, idx->clen::%lu, res->c.n::%lu\n", __func__, (uint64_t)idx->cidx, (uint64_t)idx->clen, (uint64_t)res->c.n);
        // }
        assert(idx->cidx + idx->clen == res->c.n);
        idx->clen = ci; res->c.n = idx->cidx + idx->clen;
        for (k = ((int64_t)buf->n)-1; k >= 0; k--) {
            op = buf->a[k]>>14; cl = (buf->a[k]&(0x3fff)); 
            append_cigar(idx, res, op, cl);
        }
    } else {
        idx->clen = 0; res->c.n = idx->cidx + idx->clen;
        append_cigar(idx, res, op0, 1);
        for (k = ((int64_t)buf->n)-1; k >= 0; k--) {
            op = buf->a[k]>>14; cl = (buf->a[k]&(0x3fff)); 
            append_cigar(idx, res, op, cl);
        }
    }
    return 1;///no move
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, (%rsp)
movq %rdi, %r14
movq 0xb0(%rsp), %rax
movzwl 0xa0(%rsp), %ecx
movq $0x0, (%rax)
movl 0x1c(%rdi), %r12d
testq %r12, %r12
je 0x838f8
leal -0x4(%rcx), %eax
cmpw $-0x2, %ax
jb 0x83a24
movq %r8, %rbp
movq %rcx, %rdx
movq 0xa8(%rsp), %rdi
xorl %eax, %eax
xorl %ecx, %ecx
cmpw $0x2, %dx
setne %al
sete %cl
subq %rcx, %r9
movl 0x18(%r14), %ecx
addq %rcx, %rcx
movq (%rsp), %rdx
addq 0x28(%rdx), %rcx
movq %rcx, 0x60(%rsp)
subq %rax, %rbp
movq $0x0, (%rdi)
movq $0x0, 0x8(%rsp)
movq %r14, 0x18(%rsp)
movq %r12, %r13
decq %r12
movq 0x60(%rsp), %rax
movzwl -0x2(%rax,%r13,2), %ecx
testw %cx, %cx
js 0x83900
movl %ecx, %edx
andl $0x3fff, %edx           # imm = 0x3FFF
movzwl %cx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jae 0x8361d
movq %r13, 0x10(%rsp)
testw %dx, %dx
je 0x837ea
movq %r9, %rax
subq %rdx, %rax
movq %rbp, %rcx
subq %rdx, %rcx
movq %rdx, %r8
movq %rdx, %r13
movq 0x40(%rsp), %rdx
movb (%rdx,%rbp), %dl
movq 0x48(%rsp), %rsi
cmpb (%rsi,%r9), %dl
setne %bl
jne 0x837f2
leaq -0x1(%r13), %rdx
decq %rbp
decq %r9
cmpq $0x1, %r13
movq %rdx, %r13
jg 0x835e2
movq %rax, %r9
movq %rcx, %rbp
xorl %r13d, %r13d
jmp 0x837f2
testw %dx, %dx
je 0x83865
movq %r13, 0x10(%rsp)
movq %r12, 0x20(%rsp)
movq %rcx, 0x50(%rsp)
movl %ecx, %eax
andl $0x4000, %eax           # imm = 0x4000
leaq 0x1(%rdx), %r14
movzwl %dx, %ecx
subl %ecx, %eax
movswq %ax, %r13
movq %rdx, %r15
negq %r15
movq 0x40(%rsp), %rax
leaq (%rax,%rbp), %r8
subq %rdx, %rbp
movq 0x48(%rsp), %rax
leaq (%rax,%r9), %r10
movq %rdx, %r12
xorl %ebx, %ebx
movq %rdx, 0x58(%rsp)
movq %r9, 0x38(%rsp)
movq %r8, 0x30(%rsp)
movq %r10, 0x28(%rsp)
movb (%r8,%rbx), %al
cmpb (%r10,%rbx), %al
jne 0x8375e
leaq (%r12,%r15), %rsi
movq (%rdi), %rcx
movq 0x8(%rdi), %rdx
movq 0x10(%rdi), %rax
testq %rsi, %rsi
jle 0x83700
cmpq %rdx, %rcx
jne 0x836e5
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %r9
movq 0xa8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movq 0x8(%rdi), %rdx
addq %r13, %r12
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
movw %r12w, (%rax,%rcx,2)
movw $0x1, %cx
movq %rcx, 0x8(%rsp)
movq %rsi, %rcx
cmpq %rdx, %rcx
jne 0x83742
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %r9
movq 0xa8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
leaq -0x2(%r14), %r12
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movw $0x1, (%rax,%rcx,2)
movq 0xb0(%rsp), %rax
incq (%rax)
decq %r14
incq %r13
incq %r15
decq %rbx
cmpq $0x1, %r14
ja 0x8367d
subq 0x58(%rsp), %r9
testq %r12, %r12
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r13
jle 0x837e3
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x837c9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
addq %rsi, %rsi
movq %rax, %rdi
movq %r9, %rbx
callq 0x2370
movq %rbx, %r9
movq 0xa8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movq 0x50(%rsp), %rdx
andl $0x4000, %edx           # imm = 0x4000
addl %edx, %r12d
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movw %r12w, (%rax,%rcx,2)
movq 0x20(%rsp), %r12
jmp 0x83865
movq %rdx, %r8
xorl %r13d, %r13d
xorl %ebx, %ebx
subq %r13, %r8
testq %r8, %r8
jle 0x83858
movq %r8, %r15
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x83843
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
addq %rsi, %rsi
movq %rax, %rdi
movq %r9, %r14
callq 0x2370
movq %r14, %r9
movq 0x18(%rsp), %r14
movq 0xa8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movw %r15w, (%rax,%rcx,2)
movw $0x1, %ax
movq %rax, 0x8(%rsp)
testb %bl, %bl
jne 0x83903
movq 0x10(%rsp), %r13
cmpq $0x1, %r13
jg 0x83596
cmpw $0x0, 0x8(%rsp)
je 0x83a08
movl $0x0, 0x1c(%r14)
movl 0x18(%r14), %eax
movq (%rsp), %rsi
movq %rax, 0x18(%rsi)
movzwl 0xa0(%rsp), %eax
movzwl %ax, %edx
movq %r14, %rdi
movl $0x1, %ecx
callq 0x833c8
movq 0xa8(%rsp), %rax
movq (%rax), %rbx
movw $0x1, %bp
testq %rbx, %rbx
jle 0x83a26
incq %rbx
movq 0x10(%rax), %rax
movzwl -0x4(%rax,%rbx,2), %ecx
movl %ecx, %edx
shrl $0xe, %edx
andl $0x3fff, %ecx           # imm = 0x3FFF
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x833c8
movq 0xa8(%rsp), %rax
decq %rbx
cmpq $0x1, %rbx
ja 0x838c2
jmp 0x83a26
movzwl %cx, %edx
jmp 0x83a13
movq %rcx, %r13
movzwl 0xa0(%rsp), %eax
movl %eax, %ebx
shll $0xe, %ebx
incl %ebx
movq (%rdi), %rdx
movq 0x8(%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq %rcx, %rdx
jne 0x83954
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%rdi)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0xa8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rdx
movq 0x8(%rdi), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rdi)
movw %bx, (%rax,%rdx,2)
cmpq %rcx, %rsi
jne 0x83992
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%rdi)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0xa8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rsi
leaq 0x1(%rsi), %rbx
movq %rbx, (%rdi)
movw %r13w, (%rax,%rsi,2)
cmpw $0x0, 0x8(%rsp)
je 0x83a08
movl 0x18(%r14), %eax
movl 0x1c(%r14), %ecx
addl %eax, %ecx
movq (%rsp), %rdx
cmpq %rcx, 0x18(%rdx)
jne 0x83a37
movl %r12d, 0x1c(%r14)
addl %r12d, %eax
movq (%rsp), %rcx
movq %rax, 0x18(%rcx)
movw $0x1, %bp
testq %rbx, %rbx
jle 0x83a26
incq %rbx
movq 0x10(%rdi), %rax
movzwl -0x4(%rax,%rbx,2), %ecx
movl %ecx, %edx
shrl $0xe, %edx
andl $0x3fff, %ecx           # imm = 0x3FFF
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x833c8
movq 0xa8(%rsp), %rdi
decq %rbx
cmpq $0x1, %rbx
ja 0x839d5
jmp 0x83a26
movzwl 0xa0(%rsp), %eax
movzwl %ax, %edx
movq %r14, %rdi
movq (%rsp), %rsi
movl $0x1, %ecx
callq 0x833c8
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b1d9c(%rip), %rdi    # 0x2357da
leaq 0x1add0e(%rip), %rsi    # 0x231753
leaq 0x1b1db0(%rip), %rcx    # 0x2357fc
movl $0x62a6, %edx           # imm = 0x62A6
callq 0x2110