// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: generate_consensus(overlap_region_alloc*, All_reads*, UC_Read*, Correct_dumy*, Graph*, Graph*, Cigar_record*, Round2_alignment*, window_list_alloc*)
void generate_consensus(overlap_region_alloc* overlap_list, All_reads* R_INF, 
                        UC_Read* g_read, Correct_dumy* dumy, Graph* g, Graph* DAGCon, Cigar_record* current_cigar,
                        Round2_alignment* second_round, window_list_alloc* win_ciagr_buf)
{
    clear_Cigar_record(current_cigar);
    
    long long window_start, window_end;

    long long num_availiable_win = 0;
    
    


    Window_Pool w_inf;
    init_Window_Pool(&w_inf, g_read->length, WINDOW, (int)(1.0/asm_opt.max_ov_diff_ec));

    int flag = 0;
    ///for last window
    dumy->last_boundary_length = 0;
    while(get_Window(&w_inf, &window_start, &window_end) && flag != -2)
    {

        

        dumy->length = 0;
        dumy->lengthNT = 0;

        ///return overlaps that are overlaped with [window_start, window_end]
        flag = get_available_interval(window_start, window_end, overlap_list, dumy);
        switch (flag)
        {
            case 1:    ///match
                break;
            case 0:    ///unmatch
                break;
            case -2: ///unmatch, and cannot match for next window
                break;
        }

        num_availiable_win = num_availiable_win + dumy->length;

        // fprintf(stderr, "[M::%s] window_start::%lld, window_end::%lld, g_read->length::%lld, dumy->length::%lu\n", __func__, 
        // window_start, window_end, g_read->length, dumy->length);
        
        ///number of overlaps, also be the coverage
        if(dumy->length >= MIN_COVERAGE_THRESHOLD)
        {
            window_consensus(g_read->seq, g_read->length, window_start, window_end, overlap_list, 
            dumy, R_INF, g, DAGCon, current_cigar);

            if(dumy->last_boundary_length != 0)
            {
                process_boundary(overlap_list, R_INF, dumy, g, DAGCon, current_cigar, 
                window_start, second_round, win_ciagr_buf);
            }
            
        }
        else
        {
            add_segment_to_correct_read(dumy, g_read->seq + window_start, window_end - window_start + 1);
            add_cigar_record(g_read->seq + window_start, window_end - window_start + 1, current_cigar, 0);
        }


        dumy->last_boundary_length = current_cigar->new_read_length;
    }

    if (window_start < g_read->length)
    {
        add_segment_to_correct_read(dumy, g_read->seq + window_start, g_read->length - window_start);
        add_cigar_record(g_read->seq + window_start, g_read->length - window_start, current_cigar, 0);        
    }

    ///if type == 0, x_string here is not useful
    ///output matches to cigar
    if (current_cigar->new_read_length != second_round->obtained_cigar_length)
    {
        add_cigar_record(dumy->corrected_read, current_cigar->new_read_length - second_round->obtained_cigar_length, 
        &(second_round->cigar), 0);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x38(%rsp)
movq %r8, 0x50(%rsp)
movq %rcx, %r15
movq %rsi, 0x48(%rsp)
movq %rdi, 0x28(%rsp)
movq 0x90(%rsp), %rbx
xorl %r14d, %r14d
movl %r14d, 0x20(%rbx)
movq %r14, 0x18(%rbx)
movq %r14, 0x38(%rbx)
movl %r14d, 0x4(%rbx)
movb $0x7f, (%rbx)
movq 0x8(%rdx), %rax
movl $0x177, %ecx            # imm = 0x177
cmpq %rcx, %rax
movl $0x177, %r13d           # imm = 0x177
cmovlq %rax, %r13
decq %r13
movq %rax, 0x40(%rsp)
decq %rax
leaq 0x30(%r15), %rsi
movq %rsi, 0x30(%rsp)
xorps %xmm0, %xmm0
xorl %r12d, %r12d
movl $0x0, 0x1c(%rsp)
xorl %esi, %esi
movq %rdx, 0x10(%rsp)
movq %rax, 0x20(%rsp)
movq %r14, 0x10(%r15)
cmpq %rax, %r13
jne 0x4e435
cmpq $0x0, 0x40(%rsp)
je 0x4e668
cmpl $0x1, 0x1c(%rsp)
movl $0x1, 0x1c(%rsp)
je 0x4e668
leaq (%rcx,%r13), %rbp
cmpq 0x40(%rsp), %rbp
cmovgeq %rax, %rbp
cmpl $-0x2, %esi
je 0x4e668
movq 0x30(%rsp), %rax
movaps %xmm0, (%rax)
testq %r12, %r12
je 0x4e45e
movq 0x48(%r15), %rax
jmp 0x4e468
movq $0x0, 0x48(%r15)
xorl %eax, %eax
movq 0x28(%rsp), %r8
movq 0x10(%r8), %rcx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
cmpq %rcx, %rax
jae 0x4e496
movq (%r8), %rcx
imulq $0xa8, %rax, %rdi
movl 0x4(%rcx,%rdi), %esi
cmpq %rsi, %r13
jge 0x4e55e
xorl %ecx, %ecx
xorl %esi, %esi
movl %esi, 0xc(%rsp)
movq %rcx, 0x48(%r15)
movq 0x30(%rsp), %rax
movaps %xmm0, (%rax)
movq (%rdx), %r14
addq %r12, %r14
subq %r12, %r13
movq (%r15), %rax
movq 0x8(%r15), %rdi
leaq (%rdi,%r13), %rsi
addq $0x3, %rsi
cmpq 0x18(%r15), %rsi
jle 0x4e4d7
movq %rsi, 0x18(%r15)
movq %rax, %rdi
callq 0x2370
movq %rax, (%r15)
movq 0x8(%r15), %rdi
addq %rax, %rdi
leaq 0x1(%r13), %rbx
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2160
addq 0x8(%r15), %rbx
movq %rbx, 0x8(%r15)
movq (%r15), %rax
movb $0x0, (%rax,%rbx)
incl %r13d
movq 0x90(%rsp), %rbx
cmpb $0x0, (%rbx)
je 0x4e517
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x58afb
jmp 0x4e532
movq 0x8(%rbx), %rax
movq 0x18(%rbx), %rcx
leal (,%r13,4), %edx
addl -0x4(%rax,%rcx,4), %edx
andl $-0x4, %edx
movl %edx, -0x4(%rax,%rcx,4)
movq 0x10(%rsp), %rdx
xorps %xmm0, %xmm0
movl 0xc(%rsp), %esi
addl %r13d, 0x20(%rbx)
movb $0x0, (%rbx)
movl $0x177, %ecx            # imm = 0x177
movq 0x20(%rsp), %rax
addq %rcx, %r12
movl 0x20(%rbx), %r14d
movq %rbp, %r13
jmp 0x4e40d
movq 0x30(%rsp), %rsi
movaps %xmm0, (%rsi)
leaq (%rcx,%rdi), %r11
addq $0x4, %r11
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edi, %edi
movl (%r11), %r14d
movl 0x4(%r11), %r9d
cmpq %r13, %r9
cmovaeq %r13, %r9
cmpq %r12, %r14
movq %r12, %r10
cmovgq %r14, %r10
cmpq %r10, %r9
jb 0x4e5bc
cmpb $0x1, 0x28(%r11)
jne 0x4e5ab
movq 0x28(%r15), %r9
movq %rax, (%r9,%rcx,8)
movq 0x30(%r15), %rcx
incq %rcx
movq %rcx, 0x30(%r15)
incq %rsi
testq %rdi, %rdi
jne 0x4e5b7
movq %rax, 0x48(%r15)
movl $0x1, %edi
cmpq %r14, %r13
jl 0x4e5d1
incq %rax
addq $0xa8, %r11
cmpq 0x10(%r8), %rax
jb 0x4e574
xorl %eax, %eax
testq %rsi, %rsi
setne %al
cmpq $0x3, %rcx
movl %eax, 0xc(%rsp)
jb 0x4e4a6
movq (%rdx), %rdi
movq 0x8(%rdx), %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %r15, %r9
pushq %rbx
pushq 0x40(%rsp)
movq 0x60(%rsp), %r13
pushq %r13
movq 0x60(%rsp), %r14
pushq %r14
callq 0x4d4db
addq $0x20, %rsp
cmpq $0x0, 0x10(%r15)
je 0x4e64d
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq 0x40(%rsp), %r8
movq %rbx, %r9
pushq 0xa8(%rsp)
pushq 0xa8(%rsp)
pushq %r12
callq 0x4dc30
addq $0x20, %rsp
movq 0x10(%rsp), %rdx
movl $0x177, %ecx            # imm = 0x177
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
movl 0xc(%rsp), %esi
jmp 0x4e54f
movq 0x8(%rdx), %r13
subq %r12, %r13
jle 0x4e707
movq (%rdx), %r14
addq %r12, %r14
movq (%r15), %rax
movq 0x8(%r15), %rdi
leaq (%rdi,%r13), %rsi
addq $0x2, %rsi
cmpq 0x18(%r15), %rsi
jle 0x4e6a3
movq %rsi, 0x18(%r15)
movq %rax, %rdi
callq 0x2370
movq %rax, (%r15)
movq 0x8(%r15), %rdi
addq %rax, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2160
addq 0x8(%r15), %r13
movq %r13, 0x8(%r15)
movq (%r15), %rax
movb $0x0, (%rax,%r13)
movq 0x10(%rsp), %rax
movl 0x8(%rax), %r14d
subl %r12d, %r14d
cmpb $0x0, (%rbx)
je 0x4e6e1
movq %rbx, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x58afb
jmp 0x4e6fc
movq 0x8(%rbx), %rax
movq 0x18(%rbx), %rcx
leal (,%r14,4), %edx
addl -0x4(%rax,%rcx,4), %edx
andl $-0x4, %edx
movl %edx, -0x4(%rax,%rcx,4)
addl 0x20(%rbx), %r14d
movl %r14d, 0x20(%rbx)
movb $0x0, (%rbx)
movl %r14d, %ecx
movq 0x98(%rsp), %rbx
movq 0x8e00(%rbx), %rax
cmpq %rcx, %rax
je 0x4e76c
subl %eax, %r14d
cmpb $0x0, 0x8d80(%rbx)
je 0x4e73d
leaq 0x8d80(%rbx), %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x58afb
jmp 0x4e75e
movq 0x8d88(%rbx), %rax
movq 0x8d98(%rbx), %rcx
leal (,%r14,4), %edx
addl -0x4(%rax,%rcx,4), %edx
andl $-0x4, %edx
movl %edx, -0x4(%rax,%rcx,4)
addl %r14d, 0x8da0(%rbx)
movb $0x0, 0x8d80(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq