// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r12
movq %r8, 0x30(%rsp)
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
movq 0xa0(%rsp), %r15
movq 0x98(%rsp), %r13
movq %r13, %rdi
callq 0x45328
movq %r15, %rdi
callq 0x45328
movq %r14, 0x8(%rsp)
leaq (%r14,%rbp), %rsi
movq %rbp, (%rsp)
subq %rbp, %rbx
incq %rbx
leaq 0x50(%rsp), %rcx
leaq 0x48(%rsp), %r8
movq %r13, %rdi
movq %rsi, 0x10(%rsp)
movq %rbx, %rdx
callq 0x45367
cmpq $0x0, 0x30(%r12)
je 0x4e427
leaq 0x50(%r12), %rax
movq %rax, 0x20(%rsp)
xorl %r14d, %r14d
movq %r12, 0x28(%rsp)
movq 0x20(%rsp), %rbp
movq 0x28(%r12), %rax
imulq $0xa8, (%rax,%r14,8), %rsi
movq 0x30(%rsp), %rax
movq (%rax), %r15
movl 0x4(%r15,%rsi), %eax
imulq $0x57619f1, %rax, %rax  # imm = 0x57619F1
shrq $0x23, %rax
imull $0x177, %eax, %eax      # imm = 0x177
movq (%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rax
movabsq $-0x513cc1e098ead65b, %rdx # imm = 0xAEC33E1F671529A5
imulq %rdx
movq %rdx, %r13
addq %rcx, %r13
movq %r13, %rax
shrq $0x3f, %rax
sarq $0x8, %r13
addq %rax, %r13
movq 0x58(%r15,%rsi), %rcx
shlq $0x5, %r13
movl 0xc(%rcx,%r13), %eax
cmpl $-0x1, %eax
je 0x4e419
addq %rsi, %r15
addq %r13, %rcx
leaq 0x48(%r15), %rdx
movq %rdx, 0x40(%rsp)
movslq (%rcx), %r12
movl 0x4(%rcx), %edx
subl %r12d, %edx
incl %edx
movslq %edx, %rdx
movq %rdx, 0x38(%rsp)
movslq 0x8(%rcx), %rsi
subl %esi, %eax
incl %eax
movslq %eax, %rbx
movzbl 0x1c(%r15), %ecx
movl 0x10(%r15), %r9d
movq %rbp, %rdi
movq %rbx, %rdx
movq 0x90(%rsp), %r8
callq 0x337f7
movq 0x8(%rsp), %rax
leaq (%rax,%r12), %rdx
subq (%rsp), %r12
addq 0x58(%r15), %r13
movq 0x98(%rsp), %rdi
movq %r12, %rsi
movq 0x28(%rsp), %r12
movq 0x38(%rsp), %rcx
movq %rbp, %r8
movq %rbx, %r9
pushq 0x48(%rsp)
pushq 0x58(%rsp)
pushq 0x50(%rsp)
pushq %r13
callq 0x45442
addq $0x20, %rsp
incq %r14
cmpq 0x30(%r12), %r14
jb 0x4e320
movq 0x98(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq %r12, %rdx
movq 0xa8(%rsp), %rcx
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r9
pushq (%rsp)
pushq 0x20(%rsp)
callq 0x4de85
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq