// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: merge_cigars(Correct_dumy*, Cigar_record*, Round2_alignment*, long long, long long, long long, long long)
int merge_cigars(Correct_dumy* current_dumy, Cigar_record* current_cigar, 
Round2_alignment* second_round, long long total_start_base, long long total_end_base,
long long total_window_start, long long total_window_end)
{
    Cigar_record* new_cigar = &(second_round->tmp_cigar);

    if(new_cigar->length == 1 && Get_Cigar_Type(new_cigar->record[0]) == 0)
    {
        return 1;
    }

    
    long long start_base = total_start_base - total_window_start;
    long long end_base = total_end_base - total_window_start;
    long long x_i, y_i, cigar_i, i;
    x_i = 0;
    y_i = 0;
    int operation;
    int operationLen;

    long long get_x_start, get_x_end, get_y_start, get_y_end;
    get_x_start = get_x_end = get_y_start = get_y_end = -1;

    int start_cigar = -1;
    int end_cigar = -1;

    ///0 is match, 1 is mismatch, 2 is up, 3 is left
    ///obtained x_i may larger than start_base/end_base
    ///when operation == 3
    ///so for operation == 3, we need deal with carefully
    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)
        {
            for (cigar_i = 0; cigar_i < operationLen; cigar_i++)
            {
                if(x_i >= start_base && get_x_start == -1)
                {
                    get_x_start = x_i;
                    get_y_start = y_i;
                    start_cigar = i;
                }



                if(x_i >= end_base && get_x_end == -1)
                {
                    get_x_end = x_i;
                    get_y_end = y_i;
                    end_cigar = i;
                    break;
                }

                

                x_i++;
                y_i++;
            }
        }
        else if (operation == 1)
        {
            for (cigar_i = 0; cigar_i < operationLen; cigar_i++)
            {
                if(x_i >= start_base && get_x_start == -1)
                {
                    get_x_start = x_i;
                    get_y_start = y_i;
                    start_cigar = i;
                }

                if(x_i >= end_base && get_x_end == -1)
                {
                    get_x_end = x_i;
                    get_y_end = y_i;
                    end_cigar = i;
                    break;
                }

                x_i++;
                y_i++;
            }
        }
        else if (operation == 2)
        {
            y_i += operationLen;
        }
        else if (operation == 3)
        {
            ///obtained x_i may larger than start_base/end_base
            ///when operation == 3
            ///so for operation == 3, we need deal with carefully
            x_i += operationLen;
        }
    }

    ///if there are some gap at the end of x, it very likely miscorrection
    if(get_x_end == -1 || get_x_start == -1)
    {
        return 0;
    }
    
    x_i = 0;
    y_i = 0;

    uint32_t single_record = 0;

    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 (i == start_cigar)
        {
            single_record = 0;
            single_record = operationLen - (get_x_start - x_i);
            single_record = single_record << 2;
            single_record = single_record | operation;
            new_cigar->record[i] = single_record;

            if(operation > 1)
            {
                fprintf(stderr, "error\n");
            }

            if (i == end_cigar)
            {
                x_i = get_x_start;
                single_record = 0;
                single_record = get_x_end - x_i + 1;
                single_record = single_record << 2;
                single_record = single_record | operation;
                new_cigar->record[i] = single_record;
                if(operation > 1)
                {
                    fprintf(stderr, "error\n");
                }
                break;
            }
        }
        else if (i == end_cigar)
        {
            single_record = 0;
            single_record = get_x_end - x_i + 1;
            single_record = single_record << 2;
            single_record = single_record | operation;
            new_cigar->record[i] = single_record;
            if(operation > 1)
            {
                fprintf(stderr, "error\n");
            }
            break;
        }


        if (operation == 0 || operation == 1)
        {
            x_i += operationLen;
            y_i += operationLen;
        }
        else if (operation == 2)
        {
            y_i += operationLen;
        }
        else if (operation == 3)
        {
            x_i += operationLen;
        }
    }

    new_cigar->length = end_cigar - start_cigar + 1;
    ///should be improved
    memmove(new_cigar->record, new_cigar->record + start_cigar, new_cigar->length*sizeof(uint32_t));
    
    long long total_x_start = total_window_start + get_x_start;
    long long x_length = get_x_end -get_x_start + 1;
    long long total_y_start = get_y_start;
    long long y_length = get_y_end -get_y_start + 1;

    add_cigar_to_cigar(current_dumy, current_cigar, second_round,
    total_x_start, x_length, total_y_start, y_length);

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r12
movq 0x8dd8(%rdx), %rsi
pushq $0x1
popq %rdx
cmpq $0x1, %rsi
jne 0x1ff31
movq 0x8dc8(%r12), %rax
testb $0x3, (%rax)
je 0x201b6
movq %rdi, 0x48(%rsp)
subq %r9, %rcx
movq %r9, 0x50(%rsp)
subq %r9, %r8
xorl %eax, %eax
testq %rsi, %rsi
movl $0x0, %edx
movq %rsi, 0x40(%rsp)
cmovgq %rsi, %rdx
movq %rdx, 0x38(%rsp)
pushq $-0x1
popq %r14
pushq $-0x1
popq %r15
movq %r15, 0x18(%rsp)
movq %r15, %rbx
movl %r14d, 0x14(%rsp)
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %r12, 0x20(%rsp)
cmpq 0x38(%rsp), %rax
je 0x200c0
movq 0x8dc8(%r12), %rdx
movl (%rdx,%rax,4), %r11d
movl %r11d, %edx
andl $0x3, %edx
shrl $0x2, %r11d
leaq 0x1c3687(%rip), %rsi    # 0x1e3628
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
leaq (%r9,%r11), %rdx
movq %rdx, 0x30(%rsp)
leaq (%r10,%r11), %rdx
movq %rdx, 0x28(%rsp)
subq $0x1, %r11
jb 0x20088
cmpq $-0x1, 0x18(%rsp)
sete %dl
cmpq %rcx, %r10
setge %dil
cmpq $-0x1, %r15
sete %sil
movq %r10, %r12
movq %r9, %r13
movl %eax, %ebp
testb %sil, %dil
jne 0x1fff4
movq %r15, %r12
movq %rbx, %r13
movl %r14d, %ebp
cmpq %r8, %r10
setge %sil
testb %dl, %sil
jne 0x20094
incq %r10
incq %r9
movq %r12, %r15
movq %r13, %rbx
movl %ebp, %r14d
jmp 0x1ffbc
addq %r11, %r9
jmp 0x200b8
addq %r11, %r10
jmp 0x200b8
leaq (%r9,%r11), %rdx
movq %rdx, 0x30(%rsp)
leaq (%r10,%r11), %rdx
movq %rdx, 0x28(%rsp)
subq $0x1, %r11
jb 0x20088
cmpq $-0x1, 0x18(%rsp)
sete %dl
cmpq %rcx, %r10
setge %sil
cmpq $-0x1, %r15
sete %r13b
movq %r10, %r12
movq %r9, %rdi
movl %eax, %ebp
testb %r13b, %sil
jne 0x2006b
movq %r15, %r12
movq %rbx, %rdi
movl %r14d, %ebp
cmpq %r8, %r10
setge %sil
testb %dl, %sil
jne 0x200a1
incq %r10
incq %r9
movq %r12, %r15
movq %rdi, %rbx
movl %ebp, %r14d
jmp 0x20037
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %r10
jmp 0x200b3
movq %r12, %r15
movq %r10, 0x18(%rsp)
movq %r13, %rbx
jmp 0x200ac
movq %r12, %r15
movq %r10, 0x18(%rsp)
movq %rdi, %rbx
movl %ebp, %r14d
movl %eax, 0x14(%rsp)
movq 0x20(%rsp), %r12
incq %rax
jmp 0x1ff79
xorl %eax, %eax
cmpq $-0x1, 0x18(%rsp)
je 0x20236
cmpq $-0x1, %r15
je 0x20236
movslq %r14d, %rsi
movl 0x14(%rsp), %edx
movslq %edx, %r9
xorl %r8d, %r8d
movq 0x232ef4(%rip), %rdi    # 0x252fe0
leaq 0x1c387a(%rip), %r10    # 0x1e396d
xorl %r11d, %r11d
movq 0x40(%rsp), %rax
cmpq %rax, %r8
jge 0x201eb
movq 0x8dc8(%r12), %rax
movl (%rax,%r8,4), %ebp
movl %ebp, %r13d
andl $0x3, %r13d
shrl $0x2, %ebp
cmpq %r8, %rsi
jne 0x2013d
movl %r11d, %ecx
subl %r15d, %ecx
addl %ebp, %ecx
leal (%r13,%rcx,4), %ecx
movl %ecx, (%rax,%r8,4)
cmpl $0x2, %r13d
jae 0x20169
cmpl %r14d, %edx
jne 0x20146
jmp 0x201ba
cmpq %r8, %r9
je 0x201d4
cmpl $0x1, %r13d
ja 0x20151
addq %rbp, %r11
jmp 0x2015c
cmpl $0x2, %r13d
je 0x2015c
movl %ebp, %eax
addq %rax, %r11
incq %r8
movq 0x8dd8(%r12), %rax
jmp 0x200fb
movq (%rdi), %rcx
movq %r10, %rdi
movq %rsi, 0x38(%rsp)
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
movq %r8, 0x20(%rsp)
movq %r9, 0x30(%rsp)
movq %r11, 0x28(%rsp)
callq 0x2410
movq 0x28(%rsp), %r11
leaq 0x1c37d3(%rip), %r10    # 0x1e396d
movq 0x30(%rsp), %r9
movq 0x20(%rsp), %r8
movq 0x232e35(%rip), %rdi    # 0x252fe0
movq 0x38(%rsp), %rsi
movl 0x14(%rsp), %edx
jmp 0x20136
movl %edx, %eax
jmp 0x20236
movq 0x18(%rsp), %rax
subl %r15d, %eax
leal 0x4(%r13,%rax,4), %eax
movq 0x8dc8(%r12), %rcx
movl %eax, (%rcx,%rsi,4)
jmp 0x201e5
movq 0x18(%rsp), %rcx
subl %r11d, %ecx
leal 0x4(%r13,%rcx,4), %ecx
movl %ecx, (%rax,%r8,4)
cmpl $0x1, %r13d
ja 0x20245
subl %r14d, %edx
incl %edx
movslq %edx, %rdx
movq %rdx, 0x8dd8(%r12)
movq 0x8dc8(%r12), %rdi
leaq (%rdi,%rsi,4), %rsi
shlq $0x2, %rdx
callq 0x2360
movq 0x50(%rsp), %rcx
addq %r15, %rcx
movq 0x18(%rsp), %r8
subq %r15, %r8
incq %r8
movq 0x48(%rsp), %rdi
movq %r12, %rdx
movq %rbx, %r9
callq 0x1fdaf
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rdi), %rcx
leaq 0x1c371e(%rip), %rdi    # 0x1e396d
movq %rsi, %r13
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq %r13, %rsi
movl 0x14(%rsp), %edx
jmp 0x201eb