// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0xb8, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq 0x10(%rbp), %rdi
callq 0x31d60
movq $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
movsd 0x346d2f(%rip), %xmm0   # 0x382000
leaq 0x389520(%rip), %rax    # 0x3c47f8
divsd 0xc0(%rax), %xmm0
cvttsd2si %xmm0, %eax
movslq %eax, %rcx
leaq -0x88(%rbp), %rdi
movl $0x177, %edx            # imm = 0x177
callq 0x9f470
movl $0x0, -0x8c(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, 0x10(%rax)
leaq -0x88(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0x9f520
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x8d(%rbp)
je 0x3b341
cmpl $-0x2, -0x8c(%rbp)
setne %al
movb %al, -0x8d(%rbp)
movb -0x8d(%rbp), %al
testb $0x1, %al
jne 0x3b350
jmp 0x3b4b9
movq -0x28(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0xa08e0
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0x94(%rbp)
subl $-0x2, %eax
je 0x3b3b3
jmp 0x3b396
movl -0x94(%rbp), %eax
testl %eax, %eax
je 0x3b3b1
jmp 0x3b3a2
movl -0x94(%rbp), %eax
subl $0x1, %eax
jne 0x3b3b5
jmp 0x3b3af
jmp 0x3b3b5
jmp 0x3b3b5
jmp 0x3b3b5
movq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
addq 0x30(%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x3, 0x30(%rax)
jb 0x3b45f
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %r8
movq -0x28(%rbp), %r9
movq -0x18(%rbp), %rbx
movq -0x30(%rbp), %r11
movq -0x38(%rbp), %r10
movq 0x10(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x39c70
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x3b45d
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r8
movq 0x10(%rbp), %r9
movq -0x40(%rbp), %r11
movq 0x18(%rbp), %r10
movq 0x20(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x3a7d0
jmp 0x3b4a3
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
addq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
subq -0x40(%rbp), %rdx
addq $0x1, %rdx
callq 0xa0b60
movq -0x20(%rbp), %rax
movq (%rax), %rdi
addq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rax
subq -0x40(%rbp), %rax
addq $0x1, %rax
movl %eax, %esi
movq 0x10(%rbp), %rdx
xorl %ecx, %ecx
callq 0x1c6a0
movq 0x10(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x3b30e
movq -0x40(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jge 0x3b50b
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
addq -0x40(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdx
subq -0x40(%rbp), %rdx
callq 0xa0b60
movq -0x20(%rbp), %rax
movq (%rax), %rdi
addq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x40(%rbp), %rax
movl %eax, %esi
movq 0x10(%rbp), %rdx
xorl %ecx, %ecx
callq 0x1c6a0
movq 0x10(%rbp), %rax
movl 0x20(%rax), %eax
movq 0x18(%rbp), %rcx
cmpq 0x8e00(%rcx), %rax
je 0x3b54c
movq -0x28(%rbp), %rax
movq (%rax), %rdi
movq 0x10(%rbp), %rax
movl 0x20(%rax), %eax
movq 0x18(%rbp), %rcx
subq 0x8e00(%rcx), %rax
movl %eax, %esi
movq 0x18(%rbp), %rdx
addq $0x8d80, %rdx           # imm = 0x8D80
xorl %ecx, %ecx
callq 0x1c6a0
addq $0xb8, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)