// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: init_hap_cov_t(ma_ug_t*, asg_t*, ma_hit_t_alloc*, R_to_U*, ma_hit_t_alloc*, ma_sub_t*, int, int, unsigned int)
hap_cov_t* init_hap_cov_t(ma_ug_t *ug, asg_t* read_g, ma_hit_t_alloc* sources, R_to_U* ruIndex, 
ma_hit_t_alloc* reverse_sources, ma_sub_t *coverage_cut, int max_hang, int min_ovlp, 
uint32_t is_collect_trans)
{
    uint32_t n_ux = ug->g->n_seq, i, k, j, v, rId, tn, is_Unitig, r_i, nv, w, C_bases;
    uint8_t *set = NULL; 
    hap_cov_t *x = NULL; CALLOC(x, 1);
    x->n = read_g->n_seq; 
    x->reverse_sources = reverse_sources;
    x->coverage_cut = coverage_cut;
    x->ruIndex = ruIndex;
    x->max_hang = max_hang;
    x->min_ovlp = min_ovlp;
    x->read_g = read_g;
    x->t_ch = NULL;
    kv_init(x->u_buffer.a);
    kv_init(x->tailIndex.a);
    kv_init(x->prevIndex.a);
    ma_utg_t* u = NULL;
    asg_arc_t *av = NULL;
    ma_hit_t *h = NULL;
    MALLOC(x->pos_idx, x->n); memset(x->pos_idx, -1, x->n*sizeof(uint64_t));
    CALLOC(set, read_g->n_seq<<1);
    CALLOC(x->cov, x->n); 
    for (i = 0; i < n_ux; i++)//get the coverage for each read
    {    
        if(ug->g->seq[i].del) continue;
        u = &(ug->u.a[i]);
        for (r_i = 0; r_i < u->n; r_i++) set[u->a[r_i]>>33] = 1;

        v = i<<1; 
        nv = asg_arc_n(ug->g, v);
        av = asg_arc_a(ug->g, v);
        for (k = 0; k < nv; k++)
        {
            w = av[k].v;
            if(av[k].del) continue;
            if(ug->g->seq[w>>1].del) continue;
            u = &(ug->u.a[w>>1]);
            for (r_i = 0; r_i < u->n; r_i++) set[u->a[r_i]>>33] = 1;
        }
        
        v = (i<<1)+1; 
        nv = asg_arc_n(ug->g, v);
        av = asg_arc_a(ug->g, v);
        for (k = 0; k < nv; k++)
        {
            w = av[k].v;
            if(av[k].del) continue;
            if(ug->g->seq[w>>1].del) continue;
            u = &(ug->u.a[w>>1]);
            for (r_i = 0; r_i < u->n; r_i++) set[u->a[r_i]>>33] = 1;
        }



        u = &(ug->u.a[i]); 
        for (k = 0; k < u->n; k++)
        {
            C_bases = 0;
            rId = u->a[k]>>33;
            for (j = 0; j < (uint64_t)(sources[rId].length); j++)
            {
                h = &(sources[rId].buffer[j]);
                tn = Get_tn((*h));
                if(read_g->seq[tn].del == 1)
                {
                    ///get the id of read that contains it 
                    // uint32_t tn0 = tn;
                    get_R_to_U(ruIndex, tn, &tn, &is_Unitig);
                    // if(tn != (uint32_t)-1 && is_Unitig != 1) {
                    //     fprintf(stderr, "[M::%s]\ttn::%u\tn_seq::%u\tis_Unitig::%u\ttn0::%u\n", __func__, tn, read_g->n_seq, is_Unitig, tn0); 
                    // }
                    if(tn == (uint32_t)-1 || is_Unitig == 1 || read_g->seq[tn].del == 1) continue;
                }
                if(read_g->seq[tn].del == 1) continue;
                if(!set[tn]) continue;
                C_bases += (Get_qe((*h)) - Get_qs((*h)));
            }
            x->cov[rId] = MAX(C_bases, x->cov[rId]);
        }
        

        
        u = &(ug->u.a[i]);
        for (r_i = 0; r_i < u->n; r_i++) set[u->a[r_i]>>33] = 0;

        v = i<<1; 
        nv = asg_arc_n(ug->g, v);
        av = asg_arc_a(ug->g, v);
        for (k = 0; k < nv; k++)
        {
            w = av[k].v;
            if(av[k].del) continue;
            if(ug->g->seq[w>>1].del) continue;
            u = &(ug->u.a[w>>1]);
            for (r_i = 0; r_i < u->n; r_i++) set[u->a[r_i]>>33] = 0;
        }
        
        v = (i<<1)+1; 
        nv = asg_arc_n(ug->g, v);
        av = asg_arc_a(ug->g, v);
        for (k = 0; k < nv; k++)
        {
            w = av[k].v;
            if(av[k].del) continue;
            if(ug->g->seq[w>>1].del) continue;
            u = &(ug->u.a[w>>1]);
            for (r_i = 0; r_i < u->n; r_i++) set[u->a[r_i]>>33] = 0;
        }
    }

    if(set) free(set);
    CALLOC(x->is_r_het, read_g->n_seq);
    set_r_het_flag(ug, read_g, coverage_cut, sources, ruIndex, x->is_r_het);

    x->t_ch = NULL;
    if(is_collect_trans) x->t_ch = init_trans_chain(ug, read_g->n_seq);
    
    return x;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %r13
movq %r8, 0x8(%rsp)
movq %rcx, %r12
movq %rdx, 0x48(%rsp)
movq %rsi, %rbx
movq %rdi, %rbp
movq 0x18(%rdi), %rax
movq %rax, 0x20(%rsp)
movl 0x14(%rax), %eax
movq %rax, 0x28(%rsp)
movl $0x1, %edi
movl $0xb0, %esi
callq 0x2120
movq %rax, %r14
movl 0x14(%rbx), %r15d
movq %r15, 0x18(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r15
movl %r15d, (%r14)
movq 0x8(%rsp), %rax
movq %rax, 0x18(%r14)
movq %r13, 0x58(%rsp)
movq %r13, 0x20(%r14)
movq %r12, 0x28(%r14)
movl 0xb0(%rsp), %eax
movl %eax, 0x38(%r14)
movl 0xb8(%rsp), %eax
movl %eax, 0x3c(%r14)
movq %rbx, 0x40(%rsp)
movq %rbx, 0x30(%r14)
leaq (,%r15,8), %r13
movq %r13, %rdi
callq 0x2280
movq %rax, 0x10(%r14)
movq %rax, %rdi
movl $0xff, %esi
movq %r13, %rdx
callq 0x20e0
movq 0x18(%rsp), %rax
addl %eax, %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x2120
movq %rax, %r13
movl $0x4, %esi
movq %r15, 0x50(%rsp)
movq %r15, %rdi
callq 0x2120
movq %r14, 0x60(%rsp)
movq %rax, 0x18(%rsp)
movq %rax, 0x8(%r14)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, 0x28(%rsp)
je 0xd7915
movq 0x20(%rsp), %rax
movq 0x20(%rax), %r15
xorl %r11d, %r11d
movq %rbp, 0x68(%rsp)
movq %r15, 0x10(%rsp)
cmpl $0x0, (%r15,%r11,8)
js 0xd7907
movq 0x10(%rbp), %rsi
leaq (%r11,%r11,4), %rcx
leaq (%rsi,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movl 0x10(%rax), %ebx
testq %rbx, %rbx
je 0xd7641
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rdi
shrq $0x21, %rdi
movb $0x1, (%r13,%rdi)
incq %rdx
cmpq %rdx, %rbx
jne 0xd762b
movq 0x20(%rsp), %rax
movq 0x8(%rax), %r8
movq 0x28(%rax), %r10
movq %r11, 0x38(%rsp)
movq %r11, %rcx
shlq $0x4, %rcx
movq (%r10,%rcx), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%r8,%rcx,8), %rdi
movq %rax, 0x30(%rsp)
testl %eax, %eax
je 0xd76d3
movl 0x30(%rsp), %edx
xorl %r9d, %r9d
leaq (%r9,%r9,2), %rcx
cmpl $0x0, 0xc(%rdi,%rcx,8)
js 0xd76cb
leaq (%rdi,%rcx,8), %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movq 0x10(%rsp), %rax
cmpl $0x0, (%rax,%rcx,8)
js 0xd76cb
leaq (%rcx,%rcx,4), %rcx
movl 0x10(%rsi,%rcx,8), %r11d
testq %r11, %r11
je 0xd76cb
leaq (%rsi,%rcx,8), %rcx
movq 0x18(%rcx), %rcx
xorl %r14d, %r14d
movq (%rcx,%r14,8), %r15
shrq $0x21, %r15
movb $0x1, (%r13,%r15)
incq %r14
cmpq %r14, %r11
jne 0xd76b5
incq %r9
cmpq %rdx, %r9
jne 0xd767d
movq 0x38(%rsp), %rax
leaq (%rax,%rax), %rcx
movq 0x8(%r10,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%r8,%rcx,8), %r8
testl %eax, %eax
movq 0x10(%rsp), %r15
je 0xd7751
movl %eax, %edx
xorl %r9d, %r9d
leaq (%r9,%r9,2), %rcx
cmpl $0x0, 0xc(%r8,%rcx,8)
js 0xd7749
leaq (%r8,%rcx,8), %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
cmpl $0x0, (%r15,%rcx,8)
js 0xd7749
leaq (%rcx,%rcx,4), %rcx
movl 0x10(%rsi,%rcx,8), %r10d
testq %r10, %r10
je 0xd7749
leaq (%rsi,%rcx,8), %rcx
movq 0x18(%rcx), %rcx
xorl %r11d, %r11d
movq (%rcx,%r11,8), %r14
shrq $0x21, %r14
movb $0x1, (%r13,%r14)
incq %r11
cmpq %r11, %r10
jne 0xd7733
incq %r9
cmpq %rdx, %r9
jne 0xd76fe
movq %rax, 0x70(%rsp)
testq %rbx, %rbx
je 0xd7842
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rbx
xorl %r14d, %r14d
movq (%rbx,%r14,8), %r10
shrq $0x21, %r10
leaq (%r10,%r10,2), %rdx
movq 0x48(%rsp), %rax
movl 0xc(%rax,%rdx,8), %ecx
testq %rcx, %rcx
je 0xd77e6
leaq (%rax,%rdx,8), %rdx
movq (%rdx), %r11
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rdx
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rbp
xorl %r9d, %r9d
xorl %r15d, %r15d
movl 0xc(%r11,%r9), %ecx
cmpl $0x0, (%rdx,%rcx,8)
js 0xd77c1
cmpb $0x0, (%r13,%rcx)
je 0xd77db
addl 0x8(%r11,%r9), %r15d
subl (%r11,%r9), %r15d
jmp 0xd77db
cmpq %rcx, (%r12)
jbe 0xd77db
movq 0x8(%r12), %rax
movslq (%rax,%rcx,4), %rcx
testq %rcx, %rcx
js 0xd77db
cmpl $0x0, (%rdx,%rcx,8)
jns 0xd77ae
addq $0x28, %r9
cmpq %r9, %rbp
jne 0xd77a3
jmp 0xd77e9
xorl %r15d, %r15d
movq 0x18(%rsp), %rcx
movl (%rcx,%r10,4), %eax
cmpl %eax, %r15d
cmoval %r15d, %eax
movl %eax, (%rcx,%r10,4)
incq %r14
movq 0x8(%rsp), %rax
movl 0x10(%rax), %edx
cmpq %rdx, %r14
jb 0xd776b
testq %rdx, %rdx
movq 0x68(%rsp), %rbp
movq 0x10(%rsp), %r15
je 0xd7842
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rcx
xorl %r9d, %r9d
movq (%rcx,%r9,8), %rax
shrq $0x21, %rax
movb $0x0, (%r13,%rax)
incq %r9
cmpq %r9, %rdx
jne 0xd782c
movq 0x30(%rsp), %rax
testl %eax, %eax
je 0xd78a2
movl %eax, %edx
xorl %r9d, %r9d
leaq (%r9,%r9,2), %rcx
cmpl $0x0, 0xc(%rdi,%rcx,8)
js 0xd789a
leaq (%rdi,%rcx,8), %rax
movl 0x8(%rax), %ecx
shrl %ecx
cmpl $0x0, (%r15,%rcx,8)
js 0xd789a
leaq (%rcx,%rcx,4), %rcx
movl 0x10(%rsi,%rcx,8), %r10d
testq %r10, %r10
je 0xd789a
leaq (%rsi,%rcx,8), %rax
movq 0x18(%rax), %rcx
xorl %r11d, %r11d
movq (%rcx,%r11,8), %rax
shrq $0x21, %rax
movb $0x0, (%r13,%rax)
incq %r11
cmpq %r11, %r10
jne 0xd7884
incq %r9
cmpq %rdx, %r9
jne 0xd7850
movq 0x70(%rsp), %rax
testl %eax, %eax
movq 0x38(%rsp), %r11
je 0xd7907
movl %eax, %edx
xorl %edi, %edi
leaq (%rdi,%rdi,2), %rcx
cmpl $0x0, 0xc(%r8,%rcx,8)
js 0xd78ff
leaq (%r8,%rcx,8), %rax
movl 0x8(%rax), %ecx
shrl %ecx
cmpl $0x0, (%r15,%rcx,8)
js 0xd78ff
leaq (%rcx,%rcx,4), %rcx
movl 0x10(%rsi,%rcx,8), %r9d
testq %r9, %r9
je 0xd78ff
leaq (%rsi,%rcx,8), %rax
movq 0x18(%rax), %rcx
xorl %r10d, %r10d
movq (%rcx,%r10,8), %rax
shrq $0x21, %rax
movb $0x0, (%r13,%rax)
incq %r10
cmpq %r10, %r9
jne 0xd78e9
incq %rdi
cmpq %rdx, %rdi
jne 0xd78b4
incq %r11
cmpq 0x28(%rsp), %r11
jne 0xd75fc
movl 0xc0(%rsp), %ebx
testq %r13, %r13
je 0xd7929
movq %r13, %rdi
callq 0x2220
movl $0x1, %esi
movq 0x50(%rsp), %rdi
callq 0x2120
movq 0x60(%rsp), %r14
movq %rax, 0xa0(%r14)
movq %rbp, %rdi
movq 0x40(%rsp), %r15
movq %r15, %rsi
movq 0x58(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq %r12, %r8
movq %rax, %r9
callq 0xbf230
testl %ebx, %ebx
je 0xd7981
movl 0x14(%r15), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rbp, %rdi
callq 0xbf373
movq %rax, 0xa8(%r14)
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq