// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %rdx, %r14
movq %rdi, %r15
movq 0x60(%rsp), %rax
movq 0x18(%rcx), %rdi
movl %edi, 0x18(%rdx)
cmpl $0x0, (%rax)
je 0x85d47
movl %esi, %edx
leal -0x1(%rdx), %edi
testl %esi, %esi
movq %r14, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jle 0x85dc1
cmpb $0x1, (%r15)
jne 0x85da5
movl %edx, %ecx
decq %rcx
xorl %esi, %esi
movb $0x3, (%r15,%rsi)
decl (%r9)
cmpq %rsi, %rcx
je 0x85da3
leaq 0x1(%rsi), %rax
cmpb $0x1, 0x1(%r15,%rsi)
movq %rax, %rsi
je 0x85d27
decl %eax
jmp 0x85da5
movl 0x4(%r14), %ebx
subl (%r14), %ebx
incl %ebx
cmpq 0x20(%r12), %rdi
jne 0x85d89
leaq (%rdi,%rdi), %rax
testq %rdi, %rdi
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x20(%r12)
movq 0x28(%r12), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0x28(%r12)
movq 0x18(%r12), %rsi
movl 0x18(%r14), %edi
jmp 0x85d91
movq 0x28(%r12), %rax
movq %rdi, %rsi
leaq 0x1(%rsi), %rcx
movq %rcx, 0x18(%r12)
movw %bx, (%rax,%rsi,2)
jmp 0x85f64
movl %edi, %eax
testl %edx, %edx
jle 0x85dc1
movl %edx, %ecx
leal -0x1(%rcx), %esi
cmpb $0x1, (%r15,%rsi)
jne 0x85dc1
movb $0x3, (%r15,%rsi)
incl (%r8)
decl %ecx
jg 0x85dab
movl %edi, 0x4(%rsp)
cmpl %eax, %edi
jle 0x85e65
movslq (%r8), %rcx
addq 0x78(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movslq 0x4(%rsp), %rbx
movslq %eax, %r12
xorl %ebp, %ebp
leaq 0x1ab92d(%rip), %r14    # 0x231718
xorl %r13d, %r13d
movq %rdx, 0x20(%rsp)
movzbl (%r15,%rbx), %eax
cmpq $0x3, %rax
ja 0x85e5d
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
incl %r13d
jmp 0x85e31
movq %r15, %rdi
movl %ebx, %esi
movq 0x68(%rsp), %rcx
movl %r13d, %r8d
movq 0x18(%rsp), %r9
pushq 0x60(%rsp)
pushq %rbp
callq 0x99505
movq 0x30(%rsp), %rdx
addq $0x10, %rsp
incl %ebp
jmp 0x85e5d
movq %r15, %rdi
movl %ebx, %esi
movq 0x68(%rsp), %rcx
movl %r13d, %r8d
movq 0x18(%rsp), %r9
pushq 0x60(%rsp)
pushq %rbp
callq 0x99505
movq 0x30(%rsp), %rdx
addq $0x10, %rsp
incl %r13d
decq %rbx
cmpq %r12, %rbx
jg 0x85df3
testl %edx, %edx
jle 0x85f3b
movl 0x4(%rsp), %r14d
incq %r14
xorl %ebx, %ebx
movb $0x5, %bpl
movl $0x2, %r12d
movq 0x8(%rsp), %r13
movb -0x1(%r15,%r14), %al
cmpb %al, %bpl
jne 0x85e93
incl %ebx
jmp 0x85ef2
testl %ebx, %ebx
je 0x85ec6
movq 0x18(%r13), %rcx
cmpq 0x20(%r13), %rcx
jne 0x85ecf
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r12, %rsi
movq %rsi, 0x20(%r13)
movq 0x28(%r13), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0x28(%r13)
movq 0x18(%r13), %rcx
jmp 0x85ed3
movl $0x1, %ebx
movl %eax, %ebp
jmp 0x85ef2
movq 0x28(%r13), %rax
movzbl %bpl, %edx
shll $0xe, %edx
addl %ebx, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x18(%r13)
movw %dx, (%rax,%rcx,2)
movb -0x1(%r15,%r14), %bpl
movl $0x1, %ebx
decq %r14
jg 0x85e85
movq 0x18(%r13), %rcx
testl %ebx, %ebx
movq 0x10(%rsp), %r14
je 0x85f60
movzbl %bpl, %ebp
shll $0xe, %ebp
cmpq 0x20(%r13), %rcx
jne 0x85f4b
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x20(%r13)
movq 0x28(%r13), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0x28(%r13)
movq 0x18(%r13), %rcx
jmp 0x85f4f
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rcx
movq 0x10(%rsp), %r14
jmp 0x85f60
movq 0x28(%r13), %rax
addl %ebx, %ebp
leaq 0x1(%rcx), %rdx
movq %rdx, 0x18(%r13)
movw %bp, (%rax,%rcx,2)
movq %rdx, %rcx
movl 0x18(%r14), %edi
subl %edi, %ecx
movl %ecx, 0x1c(%r14)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq