// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movq %r8, %rbx
movq %rcx, %r12
movq %rdx, %r14
leaq 0x8d80(%rdx), %r15
movq (%rdi), %rax
addq %rcx, %rax
movq %rax, 0x8(%rsp)
movl 0x8e00(%rdx), %eax
movl %r12d, %r13d
subl %eax, %r13d
addq (%rdx), %r9
movq %r9, 0x10(%rsp)
cmpb $0x0, 0x8d80(%rdx)
je 0x4e4bc
movq %r15, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x5965e
jmp 0x4e4dd
movq 0x8d88(%r14), %rax
movq 0x8d98(%r14), %rcx
leal (,%r13,4), %edx
addl -0x4(%rax,%rcx,4), %edx
andl $-0x4, %edx
movl %edx, -0x4(%rax,%rcx,4)
addl %r13d, 0x8da0(%r14)
movb $0x0, 0x8d80(%r14)
addq %r12, %rbx
movq %rbx, 0x8e00(%r14)
cmpq $0x0, 0x8dd8(%r14)
jle 0x4e6ac
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %ebx, %ebx
movq 0x8dc8(%r14), %rax
movl (%rax,%r12,4), %eax
movl %eax, %ecx
andl $0x3, %ecx
movl %eax, %r13d
shrl $0x2, %r13d
leaq 0x1d709f(%rip), %rdx    # 0x2255c8
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpb $0x0, (%r15)
je 0x4e668
movq %r15, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x5965e
jmp 0x4e684
movq 0x10(%rsp), %rax
leaq (%rax,%rbp), %rcx
cmpb $0x2, (%r15)
jne 0x4e628
movq %r15, %rdi
movl %r13d, %esi
movl $0x2, %edx
callq 0x59767
jmp 0x4e638
movq 0x8(%rsp), %rax
leaq (%rax,%rbx), %rcx
cmpb $0x3, (%r15)
jne 0x4e64c
movq %r15, %rdi
movl %r13d, %esi
movl $0x3, %edx
callq 0x59767
jmp 0x4e65c
cmpl $0x4, %eax
jb 0x4e69c
movb $0x0, 0x7(%rsp)
movq 0x10(%rsp), %rax
movzbl (%rax,%rbp), %eax
leaq 0x23c074(%rip), %rdx    # 0x28a630
movb (%rdx,%rax), %al
shlb $0x3, %al
movb %al, 0x7(%rsp)
movq 0x8(%rsp), %rcx
movzbl (%rcx,%rbx), %ecx
orb (%rdx,%rcx), %al
movb %al, 0x7(%rsp)
cmpb $0x1, (%r15)
jne 0x4e5f5
movq %r15, %rdi
movl $0x1, %esi
movl $0x1, %edx
leaq 0x7(%rsp), %rcx
callq 0x59767
jmp 0x4e60c
movq %r15, %rdi
movl $0x1, %esi
movl $0x1, %edx
leaq 0x7(%rsp), %rcx
callq 0x596c5
incl 0x8da0(%r14)
movb $0x1, 0x8d80(%r14)
incq %rbx
incq %rbp
decq %r13
jne 0x4e5a7
jmp 0x4e69c
movq %r15, %rdi
movl %r13d, %esi
movl $0x2, %edx
callq 0x596c5
addl %r13d, 0x8da0(%r14)
movb $0x2, 0x8d80(%r14)
movl %r13d, %eax
jmp 0x4e699
movq %r15, %rdi
movl %r13d, %esi
movl $0x3, %edx
callq 0x596c5
movb $0x3, (%r15)
movl %r13d, %eax
addq %rax, %rbx
jmp 0x4e69c
movq 0x8d88(%r14), %rcx
movq 0x8d98(%r14), %rdx
andl $-0x4, %eax
addl -0x4(%rcx,%rdx,4), %eax
andl $-0x4, %eax
movl %eax, -0x4(%rcx,%rdx,4)
addl %r13d, 0x8da0(%r14)
movb $0x0, 0x8d80(%r14)
movl %r13d, %eax
addq %rax, %rbx
addq %rax, %rbp
incq %r12
cmpq 0x8dd8(%r14), %r12
jl 0x4e50b
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq