// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %r8, %r13
movq %rcx, %r12
movq %rdx, 0x40(%rsp)
movq %rsi, %r15
movq %rdi, 0x18(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x20(%rsp)
movl 0x14(%rax), %eax
movq %rax, 0x8(%rsp)
movl $0x1, %edi
movl $0xb0, %esi
callq 0x2120
movq %rax, %rbp
movl 0x14(%r15), %r14d
movq %r14, (%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r14
movl %r14d, (%rbp)
movq %r13, 0x18(%rbp)
movq %rbx, 0x50(%rsp)
movq %rbx, 0x20(%rbp)
movq %r12, 0x28(%rbp)
movl 0xb0(%rsp), %eax
movl %eax, 0x38(%rbp)
movl 0xb8(%rsp), %eax
movl %eax, 0x3c(%rbp)
movq %r15, 0x38(%rsp)
movq %r15, 0x30(%rbp)
xorl %r15d, %r15d
movq %r15, 0xa8(%rbp)
leaq (,%r14,8), %r13
movq %r13, %rdi
callq 0x2280
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rbp)
movq %r15, 0x50(%rbp)
movups %xmm0, 0x60(%rbp)
movq %r15, 0x70(%rbp)
movups %xmm0, 0x80(%rbp)
movq %r15, 0x90(%rbp)
movq %rax, 0x10(%rbp)
movq %rax, %rdi
movl $0xff, %esi
movq %r13, %rdx
callq 0x20e0
movq (%rsp), %rax
addl %eax, %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x2120
movq %rax, %r13
movl $0x4, %esi
movq %r14, 0x48(%rsp)
movq %r14, %rdi
movq 0x8(%rsp), %r14
callq 0x2120
movq 0x18(%rsp), %r11
movq %rbp, 0x58(%rsp)
movq %rax, 0x70(%rsp)
movq %rax, 0x8(%rbp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r14
je 0xd66c1
movq 0x20(%rsp), %rax
movq 0x20(%rax), %r15
xorl %ebp, %ebp
movq %r14, 0x8(%rsp)
movq %r15, 0x60(%rsp)
cmpl $0x0, (%r15,%rbp,8)
js 0xd66b5
movq %r11, %r14
movq 0x10(%r11), %rcx
leaq (,%rbp,4), %rax
addq %rbp, %rax
movq %rcx, 0x10(%rsp)
leaq (%rcx,%rax,8), %rax
movq %rax, (%rsp)
movl 0x10(%rax), %eax
testq %rax, %rax
je 0xd63a8
movq (%rsp), %rcx
movq 0x18(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rdi
shrq $0x21, %rdi
movb $0x1, (%r13,%rdi)
incq %rdx
cmpq %rdx, %rax
jne 0xd6392
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdx
movq 0x28(%rax), %r8
movq %rbp, %rax
shlq $0x4, %rax
movq (%r8,%rax), %rcx
movq %rcx, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rdi
movq %rcx, 0x30(%rsp)
testl %ecx, %ecx
je 0xd643b
movl 0x30(%rsp), %r9d
xorl %r10d, %r10d
leaq (%r10,%r10,2), %rax
cmpl $0x0, 0xc(%rdi,%rax,8)
js 0xd6433
leaq (%rdi,%rax,8), %rax
movl 0x8(%rax), %eax
shrl %eax
cmpl $0x0, (%r15,%rax,8)
js 0xd6433
leaq (%rax,%rax,4), %rcx
movq 0x10(%rsp), %rax
movl 0x10(%rax,%rcx,8), %eax
testq %rax, %rax
je 0xd6433
movq 0x10(%rsp), %rsi
leaq (%rsi,%rcx,8), %rcx
movq 0x18(%rcx), %rcx
xorl %r11d, %r11d
movq (%rcx,%r11,8), %rbx
shrq $0x21, %rbx
movb $0x1, (%r13,%rbx)
incq %r11
cmpq %r11, %rax
jne 0xd641d
incq %r10
cmpq %r9, %r10
jne 0xd63e0
leaq (,%rbp,2), %rax
movq 0x8(%r8,%rax,8), %rcx
movq %rcx, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %r8
movq %rcx, 0x28(%rsp)
testl %ecx, %ecx
movq 0x10(%rsp), %rbx
je 0xd64be
movl 0x28(%rsp), %edx
xorl %r9d, %r9d
leaq (%r9,%r9,2), %rax
cmpl $0x0, 0xc(%r8,%rax,8)
js 0xd64b6
leaq (%r8,%rax,8), %rax
movl 0x8(%rax), %eax
shrl %eax
cmpl $0x0, (%r15,%rax,8)
js 0xd64b6
leaq (%rax,%rax,4), %rcx
movl 0x10(%rbx,%rcx,8), %eax
testq %rax, %rax
je 0xd64b6
leaq (%rbx,%rcx,8), %rcx
movq 0x18(%rcx), %rcx
xorl %r10d, %r10d
movq (%rcx,%r10,8), %r11
shrq $0x21, %r11
movb $0x1, (%r13,%r11)
incq %r10
cmpq %r10, %rax
jne 0xd64a0
incq %r9
cmpq %rdx, %r9
jne 0xd646c
movq (%rsp), %rax
movl 0x10(%rax), %eax
testl %eax, %eax
movq %r14, %r11
movq 0x8(%rsp), %r14
je 0xd65d1
movq %rbp, 0x68(%rsp)
movq (%rsp), %rax
movq 0x18(%rax), %r14
xorl %ebx, %ebx
movq (%r14,%rbx,8), %r10
shrq $0x21, %r10
leaq (%r10,%r10,2), %rcx
movq 0x40(%rsp), %rdx
movl 0xc(%rdx,%rcx,8), %eax
testq %rax, %rax
je 0xd658e
leaq (%rdx,%rcx,8), %rcx
movq (%rcx), %r11
movq 0x38(%rsp), %rcx
movq 0x20(%rcx), %rdx
shlq $0x3, %rax
leaq (%rax,%rax,4), %rbp
xorl %r9d, %r9d
xorl %r15d, %r15d
movl 0xc(%r11,%r9), %eax
cmpl $0x0, (%rdx,%rax,8)
movl %eax, %ecx
js 0xd6548
movl %ecx, %eax
cmpl $0x0, (%rdx,%rax,8)
js 0xd6583
cmpb $0x0, (%r13,%rax)
je 0xd6583
addl 0x8(%r11,%r9), %r15d
subl (%r11,%r9), %r15d
jmp 0xd6583
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rax, (%r12)
jbe 0xd656d
movq 0x8(%r12), %rsi
movl (%rsi,%rax,4), %eax
cmpl $-0x1, %eax
je 0xd656d
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
shrl $0x1f, %eax
jmp 0xd656f
xorl %eax, %eax
cmpl $-0x1, %ecx
sete %sil
orb %al, %sil
jne 0xd6583
movl %ecx, %eax
cmpl $0x0, (%rdx,%rax,8)
jns 0xd652d
addq $0x28, %r9
cmpq %r9, %rbp
jne 0xd6520
jmp 0xd6591
xorl %r15d, %r15d
movq 0x70(%rsp), %rcx
movl (%rcx,%r10,4), %eax
cmpl %eax, %r15d
cmoval %r15d, %eax
movl %eax, (%rcx,%r10,4)
incq %rbx
movq (%rsp), %rax
movl 0x10(%rax), %eax
cmpq %rax, %rbx
jb 0xd64e4
movq 0x18(%rsp), %r11
movq 0x8(%rsp), %r14
movq 0x60(%rsp), %r15
movq 0x68(%rsp), %rbp
movq 0x10(%rsp), %rbx
testl %eax, %eax
je 0xd65f7
movq (%rsp), %rcx
movq 0x18(%rcx), %rcx
movl %eax, %eax
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
shrq $0x21, %rsi
movb $0x0, (%r13,%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0xd65e1
movq 0x30(%rsp), %rax
testl %eax, %eax
je 0xd6656
movl %eax, %edx
xorl %r9d, %r9d
leaq (%r9,%r9,2), %rax
cmpl $0x0, 0xc(%rdi,%rax,8)
js 0xd664e
leaq (%rdi,%rax,8), %rax
movl 0x8(%rax), %eax
shrl %eax
cmpl $0x0, (%r15,%rax,8)
js 0xd664e
leaq (%rax,%rax,4), %rcx
movl 0x10(%rbx,%rcx,8), %eax
testq %rax, %rax
je 0xd664e
leaq (%rbx,%rcx,8), %rcx
movq 0x18(%rcx), %rcx
xorl %r10d, %r10d
movq (%rcx,%r10,8), %rsi
shrq $0x21, %rsi
movb $0x0, (%r13,%rsi)
incq %r10
cmpq %r10, %rax
jne 0xd6638
incq %r9
cmpq %rdx, %r9
jne 0xd6605
movq 0x28(%rsp), %rax
testl %eax, %eax
je 0xd66b5
movl %eax, %edx
xorl %edi, %edi
leaq (%rdi,%rdi,2), %rax
cmpl $0x0, 0xc(%r8,%rax,8)
js 0xd66ad
leaq (%r8,%rax,8), %rax
movl 0x8(%rax), %eax
shrl %eax
cmpl $0x0, (%r15,%rax,8)
js 0xd66ad
leaq (%rax,%rax,4), %rcx
movl 0x10(%rbx,%rcx,8), %eax
testq %rax, %rax
je 0xd66ad
leaq (%rbx,%rcx,8), %rcx
movq 0x18(%rcx), %rcx
xorl %r9d, %r9d
movq (%rcx,%r9,8), %rsi
shrq $0x21, %rsi
movb $0x0, (%r13,%rsi)
incq %r9
cmpq %r9, %rax
jne 0xd6697
incq %rdi
cmpq %rdx, %rdi
jne 0xd6663
incq %rbp
cmpq %r14, %rbp
jne 0xd6356
movq %r11, %r14
movl 0xc0(%rsp), %ebx
testq %r13, %r13
je 0xd66d8
movq %r13, %rdi
callq 0x2220
movl $0x1, %esi
movq 0x48(%rsp), %rdi
callq 0x2120
movq 0x58(%rsp), %r15
movq %rax, 0xa0(%r15)
movq %r14, %rdi
movq 0x38(%rsp), %r13
movq %r13, %rsi
movq 0x50(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq %r12, %r8
movq %rax, %r9
callq 0xbe271
movq $0x0, 0xa8(%r15)
testl %ebx, %ebx
je 0xd673b
movl 0x14(%r13), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %r14, %rdi
callq 0xbe3b4
movq %rax, 0xa8(%r15)
movq %r15, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq