// File: chhylp123[P]hifiasm/Correct.cpp
// Function: window_consensus(char*, long long, long long, long long, overlap_region_alloc*, Correct_dumy*, All_reads*, Graph*, Graph*, Cigar_record*)
void window_consensus(char* r_string, long long r_total_length, long long window_start, long long window_end, 
overlap_region_alloc* overlap_list, Correct_dumy* dumy, All_reads* R_INF, Graph* g, Graph* DAGCon, Cigar_record* current_cigar)
{
    clear_Graph(g);
    clear_Graph(DAGCon);

    long long x_start;
    long long x_length; 
    char* x_string;
    char* y_string;
    char* backbone;
    long long backbone_length;
    uint64_t i;
    long long y_start, y_length;
    long long windowID;
    long long startNodeID, endNodeID, currentNodeID;
    overlap_region *z;


    backbone = r_string + window_start;
    backbone_length = window_end + 1 - window_start;

    addUnmatchedSeqToGraph(g, backbone, backbone_length, &startNodeID, &endNodeID);

    
    long long correct_x_pos_s;
    for (i = 0; i < dumy->length; i++)
    {
        // assert(dumy->overlapID[i]<overlap_list->length);
        ///this is the overlap ID
        z = &(overlap_list->list[dumy->overlapID[i]]);

        correct_x_pos_s = (z->x_pos_s / WINDOW) * WINDOW;
        windowID = (window_start - correct_x_pos_s) / WINDOW;
        // assert(windowID<(int64_t)z->w_list.n);

        ///if this window is not matched
        if (z->w_list.a[windowID].y_end == -1) continue;
        
        x_start = z->w_list.a[windowID].x_start;
        x_length = z->w_list.a[windowID].x_end + 1 - z->w_list.a[windowID].x_start;


        y_start = z->w_list.a[windowID].y_start;
        y_length = z->w_list.a[windowID].y_end + 1 - z->w_list.a[windowID].y_start;
        // assert(y_start>=0 && y_start<(int64_t)Get_READ_LENGTH((*R_INF), z->y_id));
        // assert((y_start+y_length)>=0 && (y_start+y_length)<=(int64_t)Get_READ_LENGTH((*R_INF), z->y_id));
        recover_UC_Read_sub_region(dumy->overlap_region, y_start, y_length, z->y_pos_strand, R_INF, z->y_id);

        x_string = r_string + x_start;
        y_string = dumy->overlap_region;
        ///here is the offset of the start base, also is the node ID
        currentNodeID = x_start - window_start;

        // if(window_start == 4500) {
        //     fprintf(stderr, "[M::%s] window_start::%lld, ovlp_id::%lu, windowID::%lld, x_start::%lld, x_length::%lld, y_start::%lld, y_length::%lld, currentNodeID::%lld\n", __func__, 
        //     window_start, dumy->overlapID[i], windowID, x_start, x_length, y_start, y_length, currentNodeID);
        // }
        
        ///cigar: overlap_list->list[overlapID].w_list[windowID].cigar;
        addmatchedSeqToGraph(g, currentNodeID, x_string, x_length, 
                    y_string, y_length, &(z->w_list.a[windowID]), &(z->w_list), startNodeID, endNodeID);
    }

    get_seq_from_Graph(g, DAGCon, dumy, current_cigar, backbone, r_string, r_total_length, window_start);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0xc8, %rsp
movq 0x28(%rbp), %rax
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 0x18(%rbp), %rdi
callq 0x2c330
movq 0x20(%rbp), %rdi
callq 0x2c330
movq -0x10(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
subq -0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
leaq -0x90(%rbp), %rcx
leaq -0x98(%rbp), %r8
callq 0x2c3c0
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jae 0x39f24
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x28(%rcx), %rcx
movq -0x70(%rbp), %rdx
imulq $0xa8, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movl 0x4(%rax), %eax
movl $0x177, %ecx            # imm = 0x177
xorl %edx, %edx
divl %ecx
imull $0x177, %eax, %eax      # imm = 0x177
movl %eax, %eax
movq %rax, -0xb0(%rbp)
movq -0x20(%rbp), %rax
subq -0xb0(%rbp), %rax
movl $0x177, %ecx            # imm = 0x177
cqto
idivq %rcx
movq %rax, -0x88(%rbp)
movq -0xa8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $-0x1, 0xc(%rax)
jne 0x39d92
jmp 0x39f13
movq -0xa8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0xa8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movq -0xa8(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x88(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
subl (%rcx), %eax
cltq
movq %rax, -0x48(%rbp)
movq -0xa8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0xa8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
movq -0xa8(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x88(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
subl 0x8(%rcx), %eax
cltq
movq %rax, -0x80(%rbp)
movq -0x38(%rbp), %rdi
addq $0x50, %rdi
movq -0x78(%rbp), %rsi
movq -0x80(%rbp), %rdx
movq -0xa8(%rbp), %rax
movl 0x1c(%rax), %eax
movq 0x10(%rbp), %r8
movq -0xa8(%rbp), %rcx
movl 0x10(%rcx), %ecx
movl %ecx, %r9d
movzbl %al, %ecx
callq 0x9a30
movq -0x10(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
subq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq 0x18(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %r8
movq -0x80(%rbp), %r9
movq -0xa8(%rbp), %rax
movq 0x58(%rax), %rbx
movq -0x88(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rbx
movq -0xa8(%rbp), %r11
addq $0x48, %r11
movq -0x90(%rbp), %r10
movq -0x98(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x2c4e0
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x39cf9
movq 0x18(%rbp), %rdi
movq 0x20(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq 0x28(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0x10(%rbp), %r9
movq -0x18(%rbp), %r10
movq -0x20(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x39860
addq $0xc8, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax)