// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq 0xb0(%rsp), %rax
andq $0x0, (%rax)
movzwl 0xa0(%rsp), %ecx
movl 0x1c(%rdi), %ebp
testq %rbp, %rbp
je 0x50212
leal -0x4(%rcx), %eax
cmpw $-0x2, %ax
jb 0x503af
movq %r9, %r12
movq %rdx, 0x30(%rsp)
movq %rcx, %rdx
movq 0xa8(%rsp), %r9
xorl %eax, %eax
xorl %ecx, %ecx
cmpw $0x2, %dx
setne %al
sete %cl
movq %rdi, 0x10(%rsp)
movl 0x18(%rdi), %edx
addq %rdx, %rdx
movq %rsi, 0x18(%rsp)
addq 0x28(%rsi), %rdx
movq %rdx, 0x60(%rsp)
subq %rcx, %r12
andq $0x0, (%r9)
subq %rax, %r8
movq %r8, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
movq %r15, 0x38(%rsp)
testq %rbp, %rbp
jle 0x5021d
movq 0x60(%rsp), %rax
movzwl -0x2(%rax,%rbp,2), %r13d
decq %rbp
testw %r13w, %r13w
js 0x50296
movl %r13d, %edx
andl $0x3fff, %edx           # imm = 0x3FFF
cmpw $0x3fff, %r13w          # imm = 0x3FFF
ja 0x4ff39
movq %r12, %rax
subq %rdx, %rax
movq 0x20(%rsp), %r13
movq %r13, %rcx
subq %rdx, %rcx
movq %rdx, %rbx
movq %rdx, %r14
movq 0x30(%rsp), %rsi
testq %r14, %r14
jle 0x500e1
movb (%rsi,%r13), %dl
cmpb (%r15,%r12), %dl
jne 0x500eb
decq %r13
decq %r12
decq %r14
jmp 0x4ff17
movq %rbp, 0x58(%rsp)
andl $0x4000, %r13d          # imm = 0x4000
leaq (%r15,%r12), %rdi
subq %rdx, %r12
movq %r12, 0x50(%rsp)
movq 0x30(%rsp), %rax
movq 0x20(%rsp), %rcx
leaq (%rax,%rcx), %r15
movzwl %dx, %eax
movq %r13, 0x40(%rsp)
movl %r13d, %ecx
subl %eax, %ecx
movq %rdx, %r13
movzwl %cx, %r14d
movq %rdx, %rbx
negq %rbx
pushq $-0x1
popq %r12
movq %rdx, %rbp
movq %rdx, 0x48(%rsp)
movq %rdi, 0x28(%rsp)
testq %rbp, %rbp
jle 0x5006e
decq %rbp
movb 0x1(%r15,%r12), %al
cmpb 0x1(%rdi,%r12), %al
jne 0x50060
leaq (%rbx,%r13), %rsi
movq (%r9), %rcx
movq 0x8(%r9), %rdx
movq 0x10(%r9), %rax
testq %rsi, %rsi
jle 0x5000f
cmpq %rdx, %rcx
jne 0x4fff4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %rdi
movq 0xa8(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
movq 0x8(%r9), %rdx
addq %r14, %r13
leaq 0x1(%rcx), %rsi
movq %rsi, (%r9)
movw %r13w, (%rax,%rcx,2)
movw $0x1, %cx
movq %rcx, 0x8(%rsp)
movq %rsi, %rcx
cmpq %rdx, %rcx
jne 0x50045
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %rdi
movq 0xa8(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
movw $0x1, (%rax,%rcx,2)
movq 0xb0(%rsp), %rax
incq (%rax)
movq %rbp, %r13
decq %r12
incq %r14
incq %rbx
jmp 0x4ff8a
movq 0x48(%rsp), %rax
subq %rax, 0x20(%rsp)
testq %r13, %r13
movq 0x58(%rsp), %rbp
movq 0x38(%rsp), %r15
movq 0x50(%rsp), %r12
jle 0x4fec9
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x500c9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0xa8(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
movzwl 0x40(%rsp), %edx
addl %r13d, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r9)
movw %dx, (%rax,%rcx,2)
jmp 0x4fec9
movq %rax, %r12
movq %rcx, %r13
xorl %esi, %esi
jmp 0x500ee
movq %r14, %rsi
subq %rsi, %rbx
jle 0x5014a
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x50136
movq %rsi, 0x28(%rsp)
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x28(%rsp), %rsi
movq 0xa8(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r9)
movw %bx, (%rax,%rcx,2)
movw $0x1, %ax
movq %rax, 0x8(%rsp)
movq %r13, 0x20(%rsp)
testq %r14, %r14
jle 0x4fec9
movq %rbp, %r14
movzwl 0xa0(%rsp), %eax
movl %eax, %ebp
shll $0xe, %ebp
incl %ebp
movq (%r9), %rdx
movq 0x8(%r9), %rcx
movq 0x10(%r9), %rax
movq %rsi, %rbx
cmpq %rcx, %rdx
jne 0x501ad
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0xa8(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rdx
movq 0x8(%r9), %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r9)
movw %bp, (%rax,%rdx,2)
cmpq %rcx, %rsi
jne 0x501e9
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0xa8(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rsi
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %r12
movq %r14, %rbp
leaq 0x1(%rsi), %r14
movq %r14, (%r9)
movw %bx, (%rax,%rsi,2)
cmpw $0x0, 0x8(%rsp)
jne 0x50341
jmp 0x50396
movzwl %cx, %edx
pushq $0x1
popq %rcx
jmp 0x503aa
cmpw $0x0, 0x8(%rsp)
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %r12
je 0x50396
andl $0x0, 0x1c(%r12)
movl 0x18(%r12), %eax
movq %rax, 0x18(%r15)
movzwl 0xa0(%rsp), %eax
movzwl %ax, %edx
pushq $0x1
popq %rcx
movq %r12, %rdi
movq %r15, %rsi
movq %r9, %rbx
callq 0x4fdcb
movq (%rbx), %rbx
testq %rbx, %rbx
jle 0x50390
movq 0xa8(%rsp), %rax
movq 0x10(%rax), %rax
movzwl -0x2(%rax,%rbx,2), %ecx
decq %rbx
movl %ecx, %edx
shrl $0xe, %edx
andl $0x3fff, %ecx           # imm = 0x3FFF
movq %r12, %rdi
movq %r15, %rsi
callq 0x4fdcb
jmp 0x50261
movzwl 0xa0(%rsp), %eax
movl %eax, %ebx
shll $0xe, %ebx
incl %ebx
movq (%r9), %rdx
movq 0x8(%r9), %rcx
movq 0x10(%r9), %rax
cmpq %rcx, %rdx
jne 0x502e5
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0xa8(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rdx
movq 0x8(%r9), %rcx
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %r12
leaq 0x1(%rdx), %rsi
movq %rsi, (%r9)
movw %bx, (%rax,%rdx,2)
cmpq %rcx, %rsi
jne 0x5032b
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r9)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0xa8(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rsi
movq 0x8(%rsp), %rcx
leaq 0x1(%rsi), %r14
movq %r14, (%r9)
movw %r13w, (%rax,%rsi,2)
testw %cx, %cx
je 0x50396
movl 0x18(%r12), %eax
movl 0x1c(%r12), %ecx
addl %eax, %ecx
cmpq %rcx, 0x18(%r15)
jne 0x503c0
movl %ebp, 0x1c(%r12)
addl %ebp, %eax
movq %rax, 0x18(%r15)
testq %r14, %r14
jle 0x50390
movq 0x10(%r9), %rax
movzwl -0x2(%rax,%r14,2), %ecx
decq %r14
movl %ecx, %edx
shrl $0xe, %edx
andl $0x3fff, %ecx           # imm = 0x3FFF
movq %r12, %rdi
movq %r15, %rsi
callq 0x4fdcb
movq 0xa8(%rsp), %r9
jmp 0x5035e
movw $0x1, %ax
jmp 0x503b1
movzwl 0xa0(%rsp), %eax
movzwl %ax, %edx
pushq $0x1
popq %rcx
movq %r12, %rdi
movq %r15, %rsi
callq 0x4fdcb
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x197363(%rip), %rdi    # 0x1e772a
leaq 0x1932d5(%rip), %rsi    # 0x1e36a3
leaq 0x197377(%rip), %rcx    # 0x1e774c
movl $0x62a6, %edx           # imm = 0x62A6
callq 0x2110