// File: /workspace/github_cpp_makefile_star2_O3/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 0x4d710
movq %r15, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x58afb
jmp 0x4d731
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 0x4d8d7
xorl %eax, %eax
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x8dc8(%r14), %rcx
movl (%rcx,%rbx,4), %ecx
movl %ecx, %edx
andl $0x3, %edx
movl %ecx, %r13d
shrl $0x2, %r13d
leaq 0x1e3f4a(%rip), %rsi    # 0x2316c8
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
testb %al, %al
je 0x4d891
movq %r15, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x58afb
jmp 0x4d8ad
movq 0x10(%rsp), %rcx
addq %rbp, %rcx
movq %r15, %rdi
movl %r13d, %esi
movl $0x2, %edx
cmpb $0x2, %al
jne 0x4d860
callq 0x58c0f
jmp 0x4d865
movq 0x8(%rsp), %rcx
addq %r12, %rcx
movq %r15, %rdi
movl %r13d, %esi
movl $0x3, %edx
cmpb $0x3, %al
jne 0x4d87e
callq 0x58c0f
jmp 0x4d883
cmpl $0x4, %ecx
jb 0x4d8c7
cmpb $0x1, %al
sete %al
movq 0x10(%rsp), %rcx
movzbl (%rcx,%rbp), %ecx
leaq 0x247e27(%rip), %rsi    # 0x295630
movb (%rsi,%rcx), %cl
shlb $0x3, %cl
movq 0x8(%rsp), %rdx
movzbl (%rdx,%r12), %edx
orb (%rsi,%rdx), %cl
movb %cl, 0x7(%rsp)
movq %r15, %rdi
movl $0x1, %esi
movl $0x1, %edx
leaq 0x7(%rsp), %rcx
testb $0x1, %al
je 0x4d83d
callq 0x58c0f
jmp 0x4d842
callq 0x58b64
incl 0x8da0(%r14)
movb $0x1, 0x8d80(%r14)
incq %r12
incq %rbp
movb $0x1, %al
decq %r13
jne 0x4d7f9
jmp 0x4d8c7
callq 0x58b64
addl %r13d, 0x8da0(%r14)
movb $0x2, 0x8d80(%r14)
movl %r13d, %eax
addq %rax, %rbp
movb $0x2, %al
jmp 0x4d8c7
callq 0x58b64
movb $0x3, (%r15)
movl %r13d, %eax
addq %rax, %r12
movb $0x3, %al
jmp 0x4d8c7
movq 0x8d88(%r14), %rax
movq 0x8d98(%r14), %rdx
andl $-0x4, %ecx
addl -0x4(%rax,%rdx,4), %ecx
andl $-0x4, %ecx
movl %ecx, -0x4(%rax,%rdx,4)
addl %r13d, 0x8da0(%r14)
movb $0x0, 0x8d80(%r14)
movl %r13d, %eax
addq %rax, %r12
addq %rax, %rbp
xorl %eax, %eax
incq %rbx
cmpq 0x8dd8(%r14), %rbx
jl 0x4d761
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq