// File: /workspace/github_cpp_makefile_star2_O2/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, 0x30(%rsp)
movq %rsi, %r14
movq %rdi, 0x20(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x28(%rsp)
movl 0x14(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andq %rcx, %rax
movq %rax, 0x58(%rsp)
pushq $0x1
popq %rdi
movl $0xb0, %esi
callq 0x2120
movq %rax, %r15
movl 0x14(%r14), %ebx
movq %rbx, %rbp
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %rbp
movl %ebp, (%r15)
movq 0x8(%rsp), %rax
movq %rax, 0x18(%r15)
movq %r13, 0x48(%rsp)
movq %r13, 0x20(%r15)
movq %r12, 0x28(%r15)
movl 0xb0(%rsp), %eax
movl %eax, 0x38(%r15)
movl 0xb8(%rsp), %eax
movl %eax, 0x3c(%r15)
movq %r14, 0x30(%r15)
leaq (,%rbp,8), %r13
movq %r13, %rdi
callq 0x2280
movq %rax, 0x10(%r15)
movq %rax, %rdi
movl $0xff, %esi
movq %r13, %rdx
callq 0x20e0
leal (%rbx,%rbx), %eax
movq 0x20(%rsp), %rbx
movslq %eax, %rdi
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, %r13
pushq $0x4
popq %rsi
movq %rbp, 0x40(%rsp)
movq %rbp, %rdi
callq 0x2120
movq %r15, 0x50(%rsp)
movq %rax, 0x68(%rsp)
movq %rax, 0x8(%r15)
xorl %edi, %edi
cmpq 0x58(%rsp), %rdi
je 0x9a68c
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rdx
cmpl $0x0, (%rdx,%rdi,8)
js 0x9a684
movq 0x10(%rbx), %rsi
imulq $0x28, %rdi, %rax
leaq (%rsi,%rax), %rcx
movq %rcx, 0x8(%rsp)
movl 0x10(%rsi,%rax), %ebx
xorl %eax, %eax
cmpq %rax, %rbx
je 0x9a3e4
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x1, (%r13,%rcx)
incq %rax
jmp 0x9a3c3
movq %rsi, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
leaq (%rdi,%rdi), %r10
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rbp
movq 0x28(%rax), %r15
movq %rdi, 0x60(%rsp)
movq %rdi, %rax
shlq $0x4, %rax
movq (%r15,%rax), %rax
movl %eax, %r9d
shrq $0x20, %rax
imulq $0x18, %rax, %r11
addq %rbp, %r11
xorl %eax, %eax
cmpq %r9, %rax
je 0x9a478
imulq $0x18, %rax, %rcx
cmpl $0x0, 0xc(%r11,%rcx)
js 0x9a473
addq %r11, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movq 0x18(%rsp), %rdx
cmpl $0x0, (%rdx,%rcx,8)
js 0x9a473
imulq $0x28, %rcx, %rdx
movq 0x10(%rsp), %rsi
leaq (%rsi,%rdx), %rcx
movl 0x10(%rsi,%rdx), %edx
xorl %r8d, %r8d
cmpq %r8, %rdx
je 0x9a473
movq 0x18(%rcx), %rsi
movq (%rsi,%r8,8), %rsi
shrq $0x21, %rsi
movb $0x1, (%r13,%rsi)
incq %r8
jmp 0x9a457
incq %rax
jmp 0x9a41f
movq 0x8(%r15,%r10,8), %rax
movl %eax, %ecx
movq %rcx, 0x38(%rsp)
shrq $0x20, %rax
imulq $0x18, %rax, %rax
addq %rax, %rbp
xorl %eax, %eax
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r10
cmpq 0x38(%rsp), %rax
je 0x9a4ec
imulq $0x18, %rax, %rcx
cmpl $0x0, 0xc(%rbp,%rcx)
js 0x9a4e7
addq %rbp, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
cmpl $0x0, (%rdi,%rcx,8)
js 0x9a4e7
imulq $0x28, %rcx, %rdx
leaq (%r10,%rdx), %rcx
movl 0x10(%r10,%rdx), %edx
xorl %r8d, %r8d
cmpq %r8, %rdx
je 0x9a4e7
movq 0x18(%rcx), %rsi
movq (%rsi,%r8,8), %rsi
shrq $0x21, %rsi
movb $0x1, (%r13,%rsi)
incq %r8
jmp 0x9a4cb
incq %rax
jmp 0x9a49b
xorl %r15d, %r15d
movq %r11, 0x70(%rsp)
movl %ebx, %eax
cmpq %rax, %r15
jae 0x9a5a6
movq %r9, %r11
movq %rbp, %r9
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%r15,8), %rbx
shrq $0x21, %rbx
imulq $0x18, %rbx, %rax
movq 0x30(%rsp), %rdx
leaq (%rdx,%rax), %rcx
movl 0xc(%rdx,%rax), %eax
imulq $0x28, %rax, %rdx
xorl %eax, %eax
xorl %ebp, %ebp
cmpq %rax, %rdx
je 0x9a57b
movq (%rcx), %r8
movl 0xc(%r8,%rax), %r10d
movq 0x20(%r14), %rsi
cmpl $0x0, (%rsi,%r10,8)
js 0x9a55a
cmpb $0x0, (%r13,%r10)
je 0x9a575
addl 0x8(%r8,%rax), %ebp
subl (%r8,%rax), %ebp
jmp 0x9a575
cmpq %r10, (%r12)
jbe 0x9a575
movq 0x8(%r12), %rdi
movslq (%rdi,%r10,4), %r10
testq %r10, %r10
js 0x9a575
cmpl $0x0, (%rsi,%r10,8)
jns 0x9a547
addq $0x28, %rax
jmp 0x9a52f
movq 0x68(%rsp), %rcx
movl (%rcx,%rbx,4), %eax
cmpl %eax, %ebp
cmoval %ebp, %eax
movl %eax, (%rcx,%rbx,4)
incq %r15
movq 0x8(%rsp), %rax
movl 0x10(%rax), %ebx
movq %r9, %rbp
movq %r11, %r9
movq 0x70(%rsp), %r11
jmp 0x9a4f4
xorl %ecx, %ecx
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r15
cmpq %rcx, %rax
je 0x9a5d8
movq 0x8(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq (%rdx,%rcx,8), %rdx
shrq $0x21, %rdx
movb $0x0, (%r13,%rdx)
incq %rcx
jmp 0x9a5b7
xorl %eax, %eax
cmpq %r9, %rax
je 0x9a62b
imulq $0x18, %rax, %rcx
cmpl $0x0, 0xc(%r11,%rcx)
js 0x9a626
addq %r11, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
cmpl $0x0, (%r10,%rcx,8)
js 0x9a626
imulq $0x28, %rcx, %rdx
leaq (%r15,%rdx), %rcx
movl 0x10(%r15,%rdx), %edx
xorl %r8d, %r8d
cmpq %r8, %rdx
je 0x9a626
movq 0x18(%rcx), %rsi
movq (%rsi,%r8,8), %rsi
shrq $0x21, %rsi
movb $0x0, (%r13,%rsi)
incq %r8
jmp 0x9a60a
incq %rax
jmp 0x9a5da
xorl %eax, %eax
movq 0x60(%rsp), %rdi
cmpq 0x38(%rsp), %rax
je 0x9a684
imulq $0x18, %rax, %rcx
cmpl $0x0, 0xc(%rbp,%rcx)
js 0x9a67f
addq %rbp, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
cmpl $0x0, (%r10,%rcx,8)
js 0x9a67f
imulq $0x28, %rcx, %rdx
leaq (%r15,%rdx), %rcx
movl 0x10(%r15,%rdx), %edx
xorl %r8d, %r8d
cmpq %r8, %rdx
je 0x9a67f
movq 0x18(%rcx), %rsi
movq (%rsi,%r8,8), %rsi
shrq $0x21, %rsi
movb $0x0, (%r13,%rsi)
incq %r8
jmp 0x9a663
incq %rax
jmp 0x9a632
incq %rdi
jmp 0x9a38e
movq %r13, %rdi
callq 0x2220
pushq $0x1
popq %rsi
movq 0x40(%rsp), %rdi
callq 0x2120
movq 0x50(%rsp), %r15
movq %rax, 0xa0(%r15)
movq %rbx, %rdi
movq %r14, %rsi
movq 0x48(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %r12, %r8
movq %rax, %r9
callq 0x83811
cmpl $0x0, 0xc0(%rsp)
je 0x9a6eb
movl 0x14(%r14), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rbx, %rdi
callq 0x83943
movq %rax, 0xa8(%r15)
movq %r15, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq