// File: /workspace/github_cpp_makefile_star2_O1/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 0x8dd8(%rdx), %rax
cmpq $0x1, %rax
jne 0x4e6e6
movq 0x8dc8(%rdx), %rsi
testb $0x3, (%rsi)
je 0x4ea0f
movq %rdi, 0x40(%rsp)
testq %rax, %rax
movq %r9, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
jle 0x4e8b6
subq %r9, %rcx
subq %r9, %r8
movq 0x8dc8(%rdx), %rdx
movq $-0x1, %rsi
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
xorl %r9d, %r9d
leaq 0x1d6eb4(%rip), %r13    # 0x2255d8
xorl %r10d, %r10d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq $-0x1, 0x18(%rsp)
movq $-0x1, %r12
xorl %edi, %edi
movq %rax, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movl (%rdx,%rdi,4), %r11d
movl %r11d, %r15d
andl $0x3, %r15d
movl %r11d, %ebx
shrl $0x2, %ebx
movslq (%r13,%r15,4), %r15
addq %r13, %r15
jmpq *%r15
cmpl $0x4, %r11d
jb 0x4e8a8
leaq (%r10,%rbx), %rax
movq %rax, 0x30(%rsp)
leaq (%r9,%rbx), %rax
movq %rax, 0x38(%rsp)
movq %rsi, %rax
cmpq $-0x1, 0x18(%rsp)
sete %bpl
cmpq %rcx, %r9
setge %dl
cmpq $-0x1, %r12
sete %sil
movq %r9, %r15
movq %r10, %r13
movl %edi, %r11d
testb %sil, %dl
jne 0x4e7b3
movq %r12, %r15
movq %rax, %r13
movl %r14d, %r11d
cmpq %r8, %r9
setge %dl
testb %bpl, %dl
jne 0x4e875
incq %r9
incq %r10
movl %r11d, %r14d
movq %r13, %rsi
movq %r15, %r12
decq %rbx
jne 0x4e781
movq %r15, %r12
movq %r13, %rsi
movl %r11d, %r14d
jmp 0x4e869
addq %rbx, %r10
jmp 0x4e8a8
addq %rbx, %r9
jmp 0x4e8a8
cmpl $0x4, %r11d
jb 0x4e8a8
leaq (%r10,%rbx), %rax
movq %rax, 0x30(%rsp)
leaq (%r9,%rbx), %rax
movq %rax, 0x38(%rsp)
movq %rsi, %rax
cmpq $-0x1, 0x18(%rsp)
sete %r11b
cmpq %rcx, %r9
setge %dl
cmpq $-0x1, %r12
sete %sil
movq %r9, %r15
movq %r10, %r13
movl %edi, %ebp
testb %sil, %dl
jne 0x4e841
movq %r12, %r15
movq %rax, %r13
movl %r14d, %ebp
cmpq %r8, %r9
setge %dl
testb %r11b, %dl
jne 0x4e885
incq %r9
incq %r10
movl %ebp, %r14d
movq %r13, %rsi
movq %r15, %r12
decq %rbx
jne 0x4e810
movq %r15, %r12
movq %r13, %rsi
movl %ebp, %r14d
movq 0x30(%rsp), %r10
movq 0x38(%rsp), %r9
jmp 0x4e897
movq %r15, %r12
movq %r9, 0x18(%rsp)
movq %r13, %rsi
movl %r11d, %r14d
jmp 0x4e893
movq %r15, %r12
movq %r9, 0x18(%rsp)
movq %r13, %rsi
movl %ebp, %r14d
movl %edi, 0x14(%rsp)
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rdx
leaq 0x1d6d30(%rip), %r13    # 0x2255d8
incq %rdi
cmpq %rax, %rdi
jne 0x4e749
jmp 0x4e8db
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq $-0x1, %r12
movq $-0x1, 0x18(%rsp)
movq $-0x1, %rsi
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
xorl %eax, %eax
cmpq $-0x1, 0x18(%rsp)
je 0x4ea14
cmpq $-0x1, %r12
je 0x4ea14
movq %rsi, 0x28(%rsp)
movq 0x48(%rsp), %r15
cmpq $0x0, 0x8dd8(%r15)
movslq %r14d, %rsi
jle 0x4e9c4
movslq 0x14(%rsp), %rdi
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x8dc8(%r15), %rax
movl (%rax,%rbp,4), %ebx
movl %ebx, %r14d
andl $0x3, %r14d
shrl $0x2, %ebx
cmpq %rbp, %rsi
jne 0x4e94d
movl %r13d, %ecx
subl %r12d, %ecx
addl %ebx, %ecx
leal (%r14,%rcx,4), %ecx
movl %ecx, (%rax,%rbp,4)
cmpl $0x2, %r14d
jae 0x4e976
cmpq %rbp, %rdi
jne 0x4e952
jmp 0x4e9a5
cmpq %rbp, %rdi
je 0x4e991
cmpl $0x1, %r14d
ja 0x4e95d
addq %rbx, %r13
jmp 0x4e968
cmpl $0x2, %r14d
je 0x4e968
movl %ebx, %eax
addq %rax, %r13
incq %rbp
cmpq 0x8dd8(%r15), %rbp
jl 0x4e918
jmp 0x4e9c4
movq %rsi, 0x20(%rsp)
movq %rdi, 0x30(%rsp)
callq 0x58d0
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
jmp 0x4e946
movq 0x18(%rsp), %rcx
subl %r13d, %ecx
leal (%r14,%rcx,4), %ecx
addl $0x4, %ecx
movl %ecx, (%rax,%rbp,4)
jmp 0x4e9be
movq 0x18(%rsp), %rax
subl %r12d, %eax
leal (%r14,%rax,4), %eax
addl $0x4, %eax
movq 0x8dc8(%r15), %rcx
movl %eax, (%rcx,%rbp,4)
cmpl $0x1, %r14d
ja 0x4ea23
movl 0x14(%rsp), %eax
subl %esi, %eax
incl %eax
movslq %eax, %rdx
movq %rdx, 0x8dd8(%r15)
movq 0x8dc8(%r15), %rdi
leaq (%rdi,%rsi,4), %rsi
shlq $0x2, %rdx
callq 0x2360
movq 0x50(%rsp), %rcx
addq %r12, %rcx
movq 0x18(%rsp), %r8
subq %r12, %r8
incq %r8
movq 0x40(%rsp), %rdi
movq %r15, %rdx
movq 0x28(%rsp), %r9
callq 0x4e467
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rbx
callq 0x58ec
movq %rbx, %rsi
jmp 0x4e9c4