// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %r15
movq %rdi, %r13
movq 0xc0(%rsp), %rax
movzwl 0xb0(%rsp), %ecx
movq $0x0, (%rax)
movl 0x1c(%rdi), %ebp
testq %rbp, %rbp
je 0x84277
leal -0x4(%rcx), %eax
cmpw $-0x2, %ax
jb 0x8428a
movq %r8, (%rsp)
movq 0xb8(%rsp), %r8
movzwl %cx, %eax
cmpw $0x3, %cx
movl %eax, 0x2c(%rsp)
je 0x83d8c
cmpl $0x2, %eax
jne 0x83d90
decq %r9
jmp 0x83d90
decq (%rsp)
movl 0x18(%r13), %eax
addq %rax, %rax
addq 0x28(%r15), %rax
movq %rax, 0x70(%rsp)
movq $0x0, (%r8)
movzwl 0xb0(%rsp), %eax
shll $0xe, %eax
incl %eax
movl %eax, 0x30(%rsp)
movq $0x0, 0x20(%rsp)
movq %r15, 0x40(%rsp)
movq %r13, 0x8(%rsp)
testq %rbp, %rbp
movq %rbp, 0x68(%rsp)
jle 0x84153
decq %rbp
movq 0x70(%rsp), %rax
movzwl (%rax,%rbp,2), %edx
testw %dx, %dx
js 0x8415f
movl %edx, %r12d
andl $0x3fff, %r12d          # imm = 0x3FFF
movzwl %dx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
setb %cl
movl %ecx, 0x34(%rsp)
jae 0x83e81
testw %r12w, %r12w
setne %bl
je 0x83fe4
movq 0x10(%rsp), %rax
movq (%rsp), %rdx
movb (%rax,%rdx), %al
movq 0x18(%rsp), %rcx
cmpb (%rcx,%r9), %al
jne 0x83fe4
movq %r9, %rsi
movq %r9, %rax
subq %r12, %rax
movq %rdx, %rcx
subq %r12, %rcx
movq %r12, %r9
cmpq $0x2, %r12
setge %bl
jl 0x84144
movq %rsi, %rdi
leaq -0x1(%rsi), %r14
decq %r12
movq 0x10(%rsp), %rdx
movq (%rsp), %rsi
movb -0x1(%rdx,%rsi), %dl
decq %rsi
movq %rsi, (%rsp)
movq 0x18(%rsp), %rsi
cmpb -0x1(%rsi,%rdi), %dl
movq %r14, %rsi
je 0x83e43
jmp 0x83fea
testw %r12w, %r12w
je 0x840cb
movq %rbp, 0x60(%rsp)
movq %rdx, 0x48(%rsp)
movl %edx, %eax
andl $0x4000, %eax           # imm = 0x4000
leaq 0x1(%r12), %r14
movzwl %r12w, %ecx
subl %ecx, %eax
movswq %ax, %r15
movq %r12, %r13
negq %r13
movq 0x10(%rsp), %rax
movq (%rsp), %rcx
leaq (%rax,%rcx), %rsi
subq %r12, %rcx
movq %rcx, (%rsp)
movq 0x18(%rsp), %rax
movq %r9, 0x58(%rsp)
leaq (%rax,%r9), %rbp
xorl %ebx, %ebx
movq %r12, 0x50(%rsp)
movq %rsi, 0x38(%rsp)
movb (%rsi,%rbx), %al
cmpb (%rbp,%rbx), %al
jne 0x83fab
leaq (%r12,%r13), %rax
testq %rax, %rax
jle 0x83f4e
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x83f2f
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
addq %rsi, %rsi
callq 0x2370
movq 0x38(%rsp), %rsi
movq 0xb8(%rsp), %r8
movq %rax, 0x10(%r8)
addq %r15, %r12
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
movw %r12w, (%rcx,%rax,2)
movw $0x1, %ax
movq %rax, 0x20(%rsp)
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x83f88
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
addq %rsi, %rsi
callq 0x2370
movq 0x38(%rsp), %rsi
movq 0xb8(%rsp), %r8
movq %rax, 0x10(%r8)
leaq -0x2(%r14), %r12
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
movw $0x1, (%rcx,%rax,2)
movq 0xc0(%rsp), %rax
incq (%rax)
decq %r14
incq %r15
incq %r13
decq %rbx
cmpq $0x1, %r14
jg 0x83edf
movq 0x58(%rsp), %r14
subq 0x50(%rsp), %r14
movq 0x40(%rsp), %r15
movq 0x8(%rsp), %r13
movq 0x60(%rsp), %rbp
movq 0x48(%rsp), %rdx
jmp 0x840ce
movq %r9, %r14
movq %r12, %r9
movq %r15, %r13
subq %r12, %r9
testq %r9, %r9
jle 0x84049
movq %r9, %r15
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x8402d
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
addq %rsi, %rsi
callq 0x2370
movq 0xb8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
movw %r15w, (%rcx,%rax,2)
movw $0x1, %ax
movq %rax, 0x20(%rsp)
testb %bl, %bl
je 0x840bf
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
movq %r13, %r15
jne 0x84085
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
addq %rsi, %rsi
callq 0x2370
movq 0xb8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r8)
movl 0x30(%rsp), %edx
movw %dx, (%rdi,%rax,2)
movq 0x8(%r8), %rax
cmpq %rax, %rcx
movq 0x8(%rsp), %r13
jne 0x8411d
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
jmp 0x840fd
xorl %esi, %esi
movq %r13, %r15
movq 0x8(%rsp), %r13
jmp 0x84133
movq %r9, %r14
testq %r12, %r12
jle 0x84115
andl $0x4000, %edx           # imm = 0x4000
addl %edx, %r12d
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x84119
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
movq %r8, %rbx
addq %rsi, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq %rbx, %r8
movl 0x34(%rsp), %esi
jmp 0x84120
xorl %esi, %esi
jmp 0x84133
xorl %esi, %esi
jmp 0x84120
movb $0x1, %sil
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
movw %r12w, (%rcx,%rax,2)
movq %r14, %r9
testb %sil, %sil
je 0x83dcb
jmp 0x841fc
movq %rax, %r14
movq %rcx, (%rsp)
xorl %r12d, %r12d
jmp 0x83fea
movq $-0x1, %rbp
jmp 0x841fc
movq %rdx, %r14
movzwl 0xb0(%rsp), %eax
movl %eax, %ebx
shll $0xe, %ebx
incl %ebx
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x841a6
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
addq %rsi, %rsi
callq 0x2370
movq 0xb8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r8)
movw %bx, (%rdi,%rax,2)
movq 0x8(%r8), %rax
cmpq %rax, %rcx
jne 0x841e9
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r8)
addq %rsi, %rsi
callq 0x2370
movq 0xb8(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r8)
movw %r14w, (%rcx,%rax,2)
cmpw $0x0, 0x20(%rsp)
movzwl 0xb0(%rsp), %ecx
je 0x84277
cmpq $0x0, 0x68(%rsp)
jle 0x8429d
movl 0x18(%r13), %eax
movl 0x1c(%r13), %ecx
addl %eax, %ecx
cmpq %rcx, 0x18(%r15)
jne 0x8430a
movl %ebp, 0x1c(%r13)
addl %ebp, %eax
movq %rax, 0x18(%r15)
movq (%r8), %rbx
movw $0x1, %bp
testq %rbx, %rbx
jle 0x8428c
incq %rbx
movq 0x10(%r8), %rax
movzwl -0x4(%rax,%rbx,2), %ecx
movl %ecx, %edx
shrl $0xe, %edx
andl $0x3fff, %ecx           # imm = 0x3FFF
movq %r13, %rdi
movq %r15, %rsi
callq 0x83bf5
movq 0xb8(%rsp), %r8
decq %rbx
cmpq $0x1, %rbx
jg 0x84245
jmp 0x8428c
movzwl %cx, %edx
movq %r13, %rdi
movq %r15, %rsi
movl $0x1, %ecx
callq 0x83bf5
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0x1c(%r13)
movl 0x18(%r13), %eax
movq %rax, 0x18(%r15)
movq %r13, %rdi
movq %r15, %rsi
movl 0x2c(%rsp), %edx
movl $0x1, %ecx
callq 0x83bf5
movq 0xb8(%rsp), %rax
movq (%rax), %rbx
movw $0x1, %bp
testq %rbx, %rbx
jle 0x8428c
incq %rbx
movq 0x10(%rax), %rax
movzwl -0x4(%rax,%rbx,2), %ecx
movl %ecx, %edx
shrl $0xe, %edx
andl $0x3fff, %ecx           # imm = 0x3FFF
movq %r13, %rdi
movq %r15, %rsi
callq 0x83bf5
movq 0xb8(%rsp), %rax
decq %rbx
cmpq $0x1, %rbx
jg 0x842d8
jmp 0x8428c
leaq 0x1a53b9(%rip), %rdi    # 0x2296ca
leaq 0x1a132b(%rip), %rsi    # 0x225643
leaq 0x1a53cd(%rip), %rcx    # 0x2296ec
movl $0x62a6, %edx           # imm = 0x62A6
callq 0x2110