// File: /workspace/github_cpp_makefile_star2_O3/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 0x4d912
movq 0x8dc8(%rdx), %rsi
testb $0x3, (%rsi)
jne 0x4d918
jmp 0x4dc0b
jl 0x4db8d
movq %rdi, 0x40(%rsp)
subq %r9, %rcx
movq %r9, 0x50(%rsp)
subq %r9, %r8
movq %rdx, 0x48(%rsp)
movq 0x8dc8(%rdx), %rdx
movq $-0x1, %rsi
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
xorl %r9d, %r9d
leaq 0x1e3d8b(%rip), %r12    # 0x2316d8
xorl %r10d, %r10d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq $-0x1, 0x18(%rsp)
movq $-0x1, %r13
xorl %edi, %edi
movq %rax, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movl (%rdx,%rdi,4), %ebx
movl %ebx, %r15d
andl $0x3, %r15d
movl %ebx, %r11d
shrl $0x2, %r11d
movslq (%r12,%r15,4), %r15
addq %r12, %r15
jmpq *%r15
cmpl $0x4, %ebx
jb 0x4dacb
leaq (%r10,%r11), %rax
movq %rax, 0x38(%rsp)
leaq (%r9,%r11), %rax
movq %rax, 0x30(%rsp)
movq %rsi, %rax
cmpq $-0x1, 0x18(%rsp)
sete %bpl
cmpq %rcx, %r9
setge %dl
cmpq $-0x1, %r13
sete %sil
movq %r9, %r15
movq %r10, %r12
movl %edi, %ebx
testb %sil, %dl
jne 0x4d9d9
movq %r13, %r15
movq %rax, %r12
movl %r14d, %ebx
cmpq %r8, %r9
setge %dl
testb %bpl, %dl
jne 0x4da98
incq %r9
incq %r10
movl %ebx, %r14d
movq %r12, %rsi
movq %r15, %r13
decq %r11
jne 0x4d9a8
movq %r15, %r13
movq %r12, %rsi
movl %ebx, %r14d
jmp 0x4da8c
addq %r11, %r10
jmp 0x4dacb
addq %r11, %r9
jmp 0x4dacb
cmpl $0x4, %ebx
jb 0x4dacb
leaq (%r10,%r11), %rax
movq %rax, 0x38(%rsp)
leaq (%r9,%r11), %rax
movq %rax, 0x30(%rsp)
movq %rsi, %rax
cmpq $-0x1, 0x18(%rsp)
sete %bl
cmpq %rcx, %r9
setge %dl
cmpq $-0x1, %r13
sete %sil
movq %r9, %r15
movq %r10, %r12
movl %edi, %ebp
testb %sil, %dl
jne 0x4da65
movq %r13, %r15
movq %rax, %r12
movl %r14d, %ebp
cmpq %r8, %r9
setge %dl
testb %bl, %dl
jne 0x4daa8
incq %r9
incq %r10
movl %ebp, %r14d
movq %r12, %rsi
movq %r15, %r13
decq %r11
jne 0x4da35
movq %r15, %r13
movq %r12, %rsi
movl %ebp, %r14d
movq 0x38(%rsp), %r10
movq 0x30(%rsp), %r9
jmp 0x4daba
movq %r15, %r13
movq %r9, 0x18(%rsp)
movq %r12, %rsi
movl %ebx, %r14d
jmp 0x4dab6
movq %r15, %r13
movq %r9, 0x18(%rsp)
movq %r12, %rsi
movl %ebp, %r14d
movl %edi, 0x14(%rsp)
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rdx
leaq 0x1e3c0d(%rip), %r12    # 0x2316d8
incq %rdi
cmpq %rax, %rdi
jne 0x4d972
xorl %eax, %eax
cmpq $-0x1, 0x18(%rsp)
je 0x4dc10
cmpq $-0x1, %r13
je 0x4dc10
movq %rsi, 0x28(%rsp)
movslq %r14d, %rsi
movl 0x14(%rsp), %edx
movslq %edx, %r8
xorl %edi, %edi
xorl %ebp, %ebp
movq 0x48(%rsp), %r15
movq 0x8dc8(%r15), %rax
movl (%rax,%rbp,4), %ebx
movl %ebx, %r12d
andl $0x3, %r12d
shrl $0x2, %ebx
cmpq %rbp, %rsi
jne 0x4db3b
movl %edi, %ecx
subl %r13d, %ecx
addl %ebx, %ecx
leal (%r12,%rcx,4), %ecx
movl %ecx, (%rax,%rbp,4)
cmpl $0x2, %r12d
jae 0x4db64
cmpl %r14d, %edx
jne 0x4db40
jmp 0x4dba4
cmpq %rbp, %r8
je 0x4db91
cmpl $0x1, %r12d
ja 0x4db4b
addq %rbx, %rdi
jmp 0x4db56
cmpl $0x2, %r12d
je 0x4db56
movl %ebx, %eax
addq %rax, %rdi
incq %rbp
cmpq 0x8dd8(%r15), %rbp
jl 0x4db07
jmp 0x4dbc3
movq %rsi, 0x20(%rsp)
movq %rdi, 0x38(%rsp)
movq %r8, 0x30(%rsp)
callq 0x5994
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0x14(%rsp), %edx
jmp 0x4db34
xorl %eax, %eax
jmp 0x4dc10
movq 0x18(%rsp), %rcx
subl %edi, %ecx
leal (%r12,%rcx,4), %ecx
addl $0x4, %ecx
movl %ecx, (%rax,%rbp,4)
jmp 0x4dbbd
movq 0x18(%rsp), %rax
subl %r13d, %eax
leal (%r12,%rax,4), %eax
addl $0x4, %eax
movq 0x8dc8(%r15), %rcx
movl %eax, (%rcx,%rsi,4)
cmpl $0x1, %r12d
ja 0x4dc1f
subl %r14d, %edx
incl %edx
movslq %edx, %rdx
movq %rdx, 0x8dd8(%r15)
movq 0x8dc8(%r15), %rdi
leaq (%rdi,%rsi,4), %rsi
shlq $0x2, %rdx
callq 0x2360
movq 0x50(%rsp), %rcx
addq %r13, %rcx
movq 0x18(%rsp), %r8
subq %r13, %r8
incq %r8
movq 0x40(%rsp), %rdi
movq %r15, %rdx
movq 0x28(%rsp), %r9
callq 0x4d6bb
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rbx
callq 0x59b0
movq %rbx, %rsi
movl 0x14(%rsp), %edx
jmp 0x4dbc3