// File: /workspace/github_cpp_makefile_star2_O3/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 0x4495d
movq %r15, %rdi
callq 0x4495d
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 0x4499c
movq 0x30(%r12), %rsi
testq %rsi, %rsi
je 0x4d67b
leaq 0x50(%r12), %rax
movq %rax, 0x20(%rsp)
xorl %r15d, %r15d
movq %r12, 0x28(%rsp)
movq 0x20(%rsp), %rbp
movq 0x28(%r12), %rax
imulq $0xa8, (%rax,%r15,8), %rdi
movq 0x30(%rsp), %rax
movq (%rax), %r14
movl 0x4(%r14,%rdi), %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(%r14,%rdi), %rcx
shlq $0x5, %r13
movl 0xc(%rcx,%r13), %eax
cmpl $-0x1, %eax
je 0x4d66f
addq %rdi, %r14
addq %r13, %rcx
leaq 0x48(%r14), %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(%r14), %ecx
movl 0x10(%r14), %r9d
movq %rbp, %rdi
movq %rbx, %rdx
movq 0x90(%rsp), %r8
callq 0x32ef8
movq 0x8(%rsp), %rax
leaq (%rax,%r12), %rdx
subq (%rsp), %r12
addq 0x58(%r14), %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 0x44a77
addq $0x20, %rsp
movq 0x30(%r12), %rsi
incq %r15
cmpq %rsi, %r15
jb 0x4d571
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 0x4d0e3
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq