// File: /workspace/github_cpp_makefile_star2_O2/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, %r13
movq %rdx, %rbx
movq 0x60(%rsp), %rcx
movl 0x18(%r13), %eax
movl %eax, 0x18(%rdx)
cmpl $0x0, (%rcx)
je 0x5259e
movl %esi, %edx
movq %rdi, %r15
movq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
xorl %ecx, %ecx
testl %esi, %esi
movl $0x0, %esi
cmovgl %edx, %esi
leal -0x1(%rsi), %eax
cmpq %rcx, %rsi
je 0x525b3
cmpb $0x1, (%r15,%rcx)
jne 0x525af
movb $0x3, (%r15,%rcx)
decl (%r9)
incq %rcx
jmp 0x52585
movl 0x4(%rbx), %edx
subl (%rbx), %edx
incl %edx
movq %r13, %rdi
xorl %esi, %esi
jmp 0x526d6
decl %ecx
movl %ecx, %eax
leal -0x1(%rdx), %r14d
movl %r14d, %ecx
testl %ecx, %ecx
js 0x525d3
movl %ecx, %esi
cmpb $0x1, (%r15,%rsi)
jne 0x525d3
movb $0x3, (%r15,%rsi)
incl (%r8)
decl %ecx
jmp 0x525ba
movq %r13, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movslq (%r8), %rcx
addq %rcx, 0x8(%rsp)
movslq %r14d, %rbx
movslq %eax, %r13
xorl %ebp, %ebp
leaq 0x191074(%rip), %rcx    # 0x1e3668
xorl %r12d, %r12d
movq %rdx, 0x10(%rsp)
cmpq %r13, %rbx
jle 0x52686
movzbl (%r15,%rbx), %eax
cmpq $0x3, %rax
ja 0x5267e
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
incl %ebp
jmp 0x5264a
movq %r15, %rdi
movl %ebx, %esi
movq 0x68(%rsp), %rcx
movl %ebp, %r8d
movq 0x8(%rsp), %r9
pushq 0x60(%rsp)
pushq %r12
callq 0x63639
leaq 0x191027(%rip), %rcx    # 0x1e3668
movq 0x20(%rsp), %rdx
addq $0x10, %rsp
incl %r12d
jmp 0x5267e
movq %r15, %rdi
movl %ebx, %esi
movq 0x68(%rsp), %rcx
movl %ebp, %r8d
movq 0x8(%rsp), %r9
pushq 0x60(%rsp)
pushq %r12
callq 0x63639
leaq 0x190ff5(%rip), %rcx    # 0x1e3668
movq 0x20(%rsp), %rdx
addq $0x10, %rsp
incl %ebp
decq %rbx
jmp 0x525fc
movb $0x5, %al
xorl %edx, %edx
pushq $0x1
popq %rbx
movq 0x18(%rsp), %r13
movzbl %al, %esi
testl %r14d, %r14d
js 0x526ca
movl %r14d, %r12d
movb (%r15,%r12), %cl
cmpb %cl, %al
jne 0x526ad
incl %edx
movl %edx, %edi
movl %eax, %ecx
jmp 0x526c1
movl %ebx, %edi
testl %edx, %edx
je 0x526c1
movq %r13, %rdi
callq 0x635dd
movb (%r15,%r12), %cl
movl %ebx, %edi
decl %r14d
movl %edi, %edx
movl %ecx, %eax
jmp 0x52692
testl %edx, %edx
movq 0x20(%rsp), %rbx
je 0x526db
movq %r13, %rdi
callq 0x635dd
movl 0x18(%r13), %eax
subl 0x18(%rbx), %eax
movl %eax, 0x1c(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq