// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: add_cigar_to_cigar(Correct_dumy*, Cigar_record*, Round2_alignment*, long long, long long, long long, long long)
void add_cigar_to_cigar(Correct_dumy* backbone_dumy, Cigar_record* backbone_cigar,
Round2_alignment* second_round,
long long back_bone_start, long long back_bone_length,
long long new_start, long long new_length)
{
    Correct_dumy* new_dumy = &(second_round->dumy);
    Cigar_record* new_cigar = &(second_round->tmp_cigar);
    Cigar_record* result_cigar = &(second_round->cigar);

    char* x_string = backbone_dumy->corrected_read + back_bone_start;
    char* y_string = new_dumy->corrected_read + new_start;
    /**
    if(verify_cigar_2(x_string, back_bone_length, y_string, new_length, new_cigar, -1))
    {
        fprintf(stderr, "error\n");
    }
    **/


   ///if type == 0, x_string here is not useful
   ///output matches to cigar
   add_cigar_record(x_string, back_bone_start - second_round->obtained_cigar_length, result_cigar, 0);
   second_round->obtained_cigar_length = back_bone_start + back_bone_length;

    long long i, cigar_i, x_i, y_i;
    int operation;
    int operationLen;
    x_i = y_i = 0;
    char merge_base;

    for (i = 0; i < (long long)new_cigar->length; i++)
    {
        operation = Get_Cigar_Type(new_cigar->record[i]);
        operationLen = Get_Cigar_Length(new_cigar->record[i]);
        if (operation == 0)
        {
            ///if type == 0, x_string here is not useful
            add_cigar_record(x_string, operationLen, result_cigar, 0);
            x_i += operationLen;
            y_i += operationLen;
        }
        else if (operation == 1)
        {
            for (cigar_i = 0; cigar_i < operationLen; cigar_i++)
            {
                merge_base = 0;
                merge_base = seq_nt6_table[(uint8_t)y_string[y_i]];
                merge_base = merge_base << 3;
                merge_base = merge_base | seq_nt6_table[(uint8_t)x_string[x_i]];

                add_cigar_record(&merge_base, 1, result_cigar, 1);
                x_i++;
                y_i++;
            }
        }
        else if (operation == INSERTION)///2是x缺字符（y多字符）
        {
            add_cigar_record(y_string+y_i, operationLen, result_cigar, INSERTION);
            y_i += operationLen;
        }
        else if (operation == DELETION)
        {
            add_cigar_record(x_string+x_i, operationLen, result_cigar, DELETION);
            x_i += operationLen;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r14
movq %rcx, %rbp
movq %rdx, %r12
addq $0x8d80, %rdx           # imm = 0x8D80
movq (%rdi), %rdi
addq (%r12), %r9
movq %r9, 0x18(%rsp)
addq %rcx, %rdi
movl 0x8e00(%r12), %eax
movl %ebp, %esi
subl %eax, %esi
xorl %r13d, %r13d
movq %rdi, %r15
movq %rdx, %rbx
xorl %ecx, %ecx
callq 0x109b4
movq %r12, %rax
addq %rbp, %r14
movq %r14, 0x8e00(%r12)
pushq $0x1
popq %rbp
xorl %r12d, %r12d
xorl %ecx, %ecx
movq %rax, 0x10(%rsp)
cmpq 0x8dd8(%rax), %rcx
jge 0x1fef0
movq 0x8dc8(%rax), %rax
movq %rcx, 0x20(%rsp)
movl (%rax,%rcx,4), %r14d
movl %r14d, %eax
andl $0x3, %eax
shrl $0x2, %r14d
leaq 0x1c37d7(%rip), %rcx    # 0x1e3618
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movl %r14d, %ecx
jmpq *%rax
movq %r15, %rdi
movl %r14d, %esi
movq %rbx, %rdx
movq %rcx, %r14
xorl %ecx, %ecx
callq 0x109b4
addq %r14, %r13
jmp 0x1fec1
subq $0x1, %r14
jb 0x1fede
movq 0x18(%rsp), %rax
movzbl (%rax,%r12), %eax
leaq 0x2337b4(%rip), %rdx    # 0x253630
movb (%rdx,%rax), %al
shlb $0x3, %al
movzbl (%r15,%r13), %ecx
orb (%rdx,%rcx), %al
movb %al, 0xf(%rsp)
leaq 0xf(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x109b4
incq %r13
incq %r12
jmp 0x1fe65
movq 0x18(%rsp), %rax
leaq (%rax,%r12), %rdi
movl %r14d, %esi
movq %rbx, %rdx
movq %rcx, %r14
pushq $0x2
popq %rcx
callq 0x109b4
addq %r14, %r12
jmp 0x1fede
leaq (%r15,%r13), %rdi
movl %r14d, %esi
movq %rbx, %rdx
movq %rcx, %r14
pushq $0x3
popq %rcx
callq 0x109b4
addq %r14, %r13
movq 0x10(%rsp), %rax
movq 0x20(%rsp), %rcx
incq %rcx
jmp 0x1fe13
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq