// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: generate_cigar(char*, int, window_list*, window_list_alloc*, int*, int*, unsigned int*, char*, int, char*)
inline void generate_cigar(char* path, int path_length, window_list *idx, window_list_alloc *res, int* start, int* end, unsigned int* old_error,
    char* x, int x_len, char* y)
{
    // uint8_t debug_c; uint32_t debug_c_len;
    idx->cidx = res->c.n;
    if ((*old_error) == 0) {
        push_cigar_cell(res, 0, idx->x_end + 1 - idx->x_start);
        idx->clen = res->c.n - idx->cidx;
        // get_cigar_cell(idx, res, idx->clen-1, &debug_c, &debug_c_len);
        // assert(debug_c==0 && debug_c_len==(idx->x_end + 1 - idx->x_start));
        return;
    }

    ///0 is match, 1 is mismatch, 2 is up (more y), 3 is left (more x)
	int32_t i = 0, pre_cl = 0, trem_p = -1; char pre_c = 5;    
    for (i = 0; i < path_length; i++) {
        if(path[i] == 1) {
            path[i] = 3;(*end)--; trem_p = i;
        } else {
            break;
        }
    }

    for (i = path_length - 1; i >= 0; i--) {
        if(path[i] == 1) {
            path[i] = 3; (*start)++;
        }
        else {
            break;
        }
    }


    // for (i = path_length - 1; i >= 0; i--)
    // {

    //     if (pre_ciga != path[i])
    //     {
    //         if (pre_ciga_length != 0)
    //         {
    //             result->cigar.C_L[result->cigar.length] = pre_ciga_length;
    //             result->cigar.C_C[result->cigar.length] = pre_ciga;
    //             result->cigar.length++;
    //         }

    //         pre_ciga = path[i];
    //         pre_ciga_length = 1;
    //     }
    //     else
    //     {
    //         pre_ciga_length++;
    //     }
    // }

    // if (pre_ciga_length != 0)
    // {
    //     result->cigar.C_L[result->cigar.length] = pre_ciga_length;
    //     result->cigar.C_C[result->cigar.length] = pre_ciga;
    //     result->cigar.length++;
    // }

    ///verify_cigar(x, x_len, y + (*start), (*end) - (*start) + 1, &(result->cigar), error);
    
    
    y = y + (*start);
    int32_t x_i = 0, y_i = 0;
    ///terminate_site = -1 in default
    for (i = path_length - 1; i > trem_p; i--) {
        if(path[i] == 0) {
            x_i++; y_i++;
        }
        else if(path[i] == 1) {
            x_i++; y_i++;
        }
        else if(path[i] == 2) {///there are more y
            move_gap_greedy(path, i, path_length, x, x_i, y, y_i, old_error);
            y_i++;
        }
        else if(path[i] == 3) {///there are more x
            move_gap_greedy(path, i, path_length, x, x_i, y, y_i, old_error);
            x_i++;
        }
    }



    pre_c = 5; pre_cl = 0;
    for (i = path_length - 1; i >= 0; i--) {
        if (pre_c != path[i]) {
            if (pre_cl != 0) {
                push_cigar_cell(res, pre_c, pre_cl);
                // get_cigar_cell(idx, res, res->c.n - idx->cidx - 1, &debug_c, &debug_c_len);
                // assert(debug_c==pre_c && debug_c_len==pre_cl);
            }
            pre_c = path[i]; pre_cl = 1;
        }
        else {
            pre_cl++;
        }
    }

    if (pre_cl != 0) {
        push_cigar_cell(res, pre_c, pre_cl);   
        // get_cigar_cell(idx, res, res->c.n - idx->cidx -1, &debug_c, &debug_c_len);
        // assert(debug_c==pre_c && debug_c_len==pre_cl);
    }

    idx->clen = res->c.n - idx->cidx;
    // if(verify_cigar(x, x_len, y, (*end) - (*start) + 1, &(result->cigar), *old_error))
    // {
    //     fprintf(stderr, "error\n");
    // }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r14
movq %rdx, %r12
movq 0x60(%rsp), %rcx
movq 0x18(%r14), %rax
movl %eax, 0x18(%rdx)
cmpl $0x0, (%rcx)
je 0x86801
movl %esi, %edx
movq %rdi, %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %esi, %esi
jle 0x866a3
cmpb $0x1, (%r15)
jne 0x866a3
leal -0x1(%rdx), %eax
movl %edx, %ecx
decq %rcx
xorl %edi, %edi
movb $0x3, (%r15,%rdi)
decl (%r9)
cmpq %rdi, %rcx
je 0x866a3
leaq 0x1(%rdi), %rsi
cmpb $0x1, 0x1(%r15,%rdi)
movq %rsi, %rdi
je 0x86683
decl %esi
movl %esi, %eax
movq %r12, 0x20(%rsp)
leal -0x1(%rdx), %edi
testl %edx, %edx
jle 0x866c7
movl %edx, %ecx
leal -0x1(%rcx), %esi
cmpb $0x1, (%r15,%rsi)
jne 0x866c7
movb $0x3, (%r15,%rsi)
incl (%r8)
decl %ecx
jg 0x866b1
movl %edi, 0xc(%rsp)
cmpl %eax, %edi
jle 0x86779
movslq (%r8), %rcx
addq 0x78(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movslq 0xc(%rsp), %rbx
movslq %eax, %r12
xorl %ebp, %ebp
leaq 0x19ef27(%rip), %rcx    # 0x225618
xorl %r13d, %r13d
movq %rdx, 0x18(%rsp)
movzbl (%r15,%rbx), %eax
cmpq $0x3, %rax
ja 0x86771
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
incl %r13d
jmp 0x8673e
movq %r15, %rdi
movl %ebx, %esi
movq 0x68(%rsp), %rcx
movl %r13d, %r8d
movq 0x10(%rsp), %r9
pushq 0x60(%rsp)
pushq %rbp
callq 0x9898b
leaq 0x19eee3(%rip), %rcx    # 0x225618
movq 0x28(%rsp), %rdx
addq $0x10, %rsp
incl %ebp
jmp 0x86771
movq %r15, %rdi
movl %ebx, %esi
movq 0x68(%rsp), %rcx
movl %r13d, %r8d
movq 0x10(%rsp), %r9
pushq 0x60(%rsp)
pushq %rbp
callq 0x9898b
leaq 0x19eeb3(%rip), %rcx    # 0x225618
movq 0x28(%rsp), %rdx
addq $0x10, %rsp
incl %r13d
decq %rbx
cmpq %r12, %rbx
jg 0x866f9
testl %edx, %edx
jle 0x8684f
movl 0xc(%rsp), %r12d
incq %r12
xorl %ebx, %ebx
movb $0x5, %bpl
movl $0x2, %r13d
cmpb -0x1(%r15,%r12), %bpl
jne 0x8679f
incl %ebx
jmp 0x867f3
testl %ebx, %ebx
je 0x867e9
movq 0x20(%r14), %rax
cmpq %rax, 0x18(%r14)
jne 0x867cc
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0x20(%r14)
movq 0x28(%r14), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0x28(%r14)
movzbl %bpl, %eax
shll $0xe, %eax
addl %ebx, %eax
movq 0x18(%r14), %rcx
movq 0x28(%r14), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x18(%r14)
movw %ax, (%rdx,%rcx,2)
movb -0x1(%r15,%r12), %bpl
movl $0x1, %ebx
decq %r12
jg 0x86794
movzbl %bpl, %ebp
shll $0xe, %ebp
jmp 0x86856
movl 0x4(%r12), %ebx
subl (%r12), %ebx
incl %ebx
movq 0x20(%r14), %rcx
cmpq %rcx, %rax
jne 0x86839
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x20(%r14)
movq 0x28(%r14), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0x28(%r14)
movq 0x18(%r14), %rcx
movq 0x28(%r14), %rdx
leaq 0x1(%rcx), %rax
movq %rax, 0x18(%r14)
movw %bx, (%rdx,%rcx,2)
jmp 0x868a7
movl $0x14000, %ebp          # imm = 0x14000
xorl %ebx, %ebx
testl %ebx, %ebx
movq 0x20(%rsp), %r12
je 0x868a3
movq 0x20(%r14), %rax
cmpq %rax, 0x18(%r14)
jne 0x8688d
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x20(%r14)
movq 0x28(%r14), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0x28(%r14)
addl %ebx, %ebp
movq 0x18(%r14), %rax
movq 0x28(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x18(%r14)
movw %bp, (%rcx,%rax,2)
movq 0x18(%r14), %rax
subl 0x18(%r12), %eax
movl %eax, 0x1c(%r12)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq