// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %r9, 0x28(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, %rbx
movq 0xa0(%rsp), %r14
andl $0x0, 0x20(%r14)
movq %rdx, %r12
andq $0x0, 0x18(%r14)
movq %rsi, 0x18(%rsp)
andq $0x0, 0x38(%r14)
movq %rdi, %r13
andl $0x0, 0x4(%r14)
movb $0x7f, (%r14)
movq 0x8(%rdx), %rax
leaq 0x232e26(%rip), %rcx    # 0x253758
movsd 0x1c2966(%rip), %xmm0   # 0x1e32a0
divsd 0xc0(%rcx), %xmm0
cvttsd2si %xmm0, %ecx
leaq 0x30(%rsp), %rdi
andl $0x0, 0x30(%rdi)
movslq %ecx, %rcx
movq %rax, (%rdi)
movl $0x177, %esi            # imm = 0x177
movq %rsi, 0x8(%rdi)
movq %rcx, 0x28(%rdi)
andq $0x0, 0x18(%rdi)
cmpq %rsi, %rax
movl $0x177, %ecx            # imm = 0x177
cmovlq %rax, %rcx
decq %rcx
movq %rcx, 0x20(%rdi)
addq $0x176, %rax            # imm = 0x176
cqto
idivq %rsi
movq %rax, 0x10(%rdi)
leaq 0x30(%rbx), %r15
xorl %eax, %eax
xorl %ebp, %ebp
movq %rax, 0x10(%rbx)
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x52721
cmpl $-0x2, %ebp
je 0x20a97
testl %eax, %eax
je 0x20a97
xorpd %xmm0, %xmm0
movapd %xmm0, (%r15)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x530d0
movl %eax, %ebp
cmpq $0x3, 0x30(%rbx)
movq (%r12), %rax
movq 0x10(%rsp), %rcx
jb 0x20a54
movq 0x8(%r12), %rsi
movq 0x8(%rsp), %rdx
movq %rax, %rdi
movq %r13, %r8
movq %rbx, %r9
pushq %r14
pushq 0x30(%rsp)
pushq 0x30(%rsp)
pushq 0x30(%rsp)
callq 0x1fbf6
addq $0x20, %rsp
cmpq $0x0, 0x10(%rbx)
je 0x20a8e
subq $0x8, %rsp
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r8
movq %r14, %r9
pushq 0xb8(%rsp)
pushq 0xb8(%rsp)
pushq 0x20(%rsp)
callq 0x20266
addq $0x20, %rsp
jmp 0x20a8e
movq 0x8(%rsp), %rdx
addq %rdx, %rax
subq %rdx, %rcx
incq %rcx
movq %rbx, %rdi
movq %rax, %rsi
movq %rcx, %rdx
callq 0x531b5
movq 0x8(%rsp), %rax
movq (%r12), %rdi
addq %rax, %rdi
movl 0x10(%rsp), %esi
subl %eax, %esi
incl %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x109b4
movl 0x20(%r14), %eax
jmp 0x20991
movq 0x8(%rsp), %rsi
movq 0x8(%r12), %rdx
subq %rsi, %rdx
jle 0x20acf
addq (%r12), %rsi
movq %rbx, %rdi
callq 0x531b5
movq 0x8(%rsp), %rax
movq (%r12), %rdi
addq %rax, %rdi
movl 0x8(%r12), %esi
subl %eax, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x109b4
movl 0x20(%r14), %esi
movq 0xa8(%rsp), %rdx
movq 0x8e00(%rdx), %rax
cmpq %rsi, %rax
je 0x20afa
movq (%rbx), %rdi
subl %eax, %esi
addq $0x8d80, %rdx           # imm = 0x8D80
xorl %ecx, %ecx
callq 0x109b4
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq