// File: chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: init_p_g_t(ma_ug_t*, hap_cov_t*, asg_t*)
p_g_t *init_p_g_t(ma_ug_t *ug, hap_cov_t *cov, asg_t *read_g)
{
    uint32_t v, uId, k, l, offset, l_pos, g_beg_idx, occ/**, ovlp, tLen, zLen**/;
    p_g_t *pg = NULL; CALLOC(pg, 1);
    pg->ug = ug;
    asg_t* nsg = pg->ug->g;
    ma_utg_t *u = NULL;
    p_node_t *t = NULL/**, *z = NULL**/;
    ///asg_arc_t *e = NULL;
    p_g_in_t *x = NULL;
    ///pg->pg_het = asg_init();
    pg->pg_h_lev = asg_init();
    kv_init(pg->pg_het_node);
    kv_init(pg->pg_h_lev_idx);

    for (v = 0; v < nsg->n_seq; v++)
    {
        uId = v;
        if(nsg->seq[uId].del || nsg->seq[uId].c == ALTER_LABLE)
        {
            asg_seq_set(pg->pg_h_lev, uId, 0, 1);
            pg->pg_h_lev->seq[uId].c = ALTER_LABLE;
            continue;
        } 
        
        asg_seq_set(pg->pg_h_lev, uId, ug->u.a[uId].len, 0);
        pg->pg_h_lev->seq[uId].c = PRIMARY_LABLE;
    }

    // if(asm_opt.polyploidy <= 2)
    // {
    //     for (v = 0; v < cov->t_ch->r_num; v++)
    //     {
    //         if(cov->t_ch->is_r_het[v]&P_HET) cov->t_ch->is_r_het[v] |= S_HET;
    //     }
    // } 

    for (v = 0; v < nsg->n_seq; v++)
    {
        uId = v;
        if(nsg->seq[uId].del || nsg->seq[uId].c == ALTER_LABLE) continue;

        u = &(ug->u.a[uId]);
        g_beg_idx = pg->pg_het_node.n;
        ///fprintf(stderr, "\n+v: %u, pg->pg_het_node.n: %u\n", v, (uint32_t)pg->pg_het_node.n);
        for (k = 1, l = 0, offset = 0, l_pos = 0; k <= u->n; ++k) 
        {   
            ///if (k == u->n || (!!cov->t_ch->is_r_het[u->a[k]>>33]) != (!!cov->t_ch->is_r_het[u->a[l]>>33]))
            if (k == u->n || cov->t_ch->ir_het[u->a[k]>>33] != cov->t_ch->ir_het[u->a[l]>>33])
            {
                kv_pushp(p_node_t, pg->pg_het_node, &t);
                t->c_ug_id = uId;
                t->h_status = cov->t_ch->ir_het[u->a[l]>>33];
                t->baseBeg = l_pos;
                t->baseEnd = offset + read_g->seq[u->a[k-1]>>33].len - 1;
                t->nodeBeg = l;
                t->nodeEnd = k - 1;
                ///if(t->b_ug_id == (uint32_t)-1) fprintf(stderr, "xxxx\n");
                ///asg_seq_set(pg->pg_het, pg->pg_het_node.n-1, t->baseEnd+1-t->baseBeg, 0);
                ///fprintf(stderr, "l: %u, k: %u, u->n: %u, t->h_status: %u\n", l, k, u->n, t->h_status);
                l = k;
                l_pos = offset + (uint32_t)u->a[k-1];
            }
            offset += (uint32_t)u->a[k-1];
        }

        occ = pg->pg_het_node.n - g_beg_idx;
        kv_pushp(p_g_in_t, pg->pg_h_lev_idx, &x);
        x->beg = g_beg_idx; x->occ = occ;
        ///fprintf(stderr, "-v: %u, pg->pg_het_node.n: %u\n", v, (uint32_t)pg->pg_het_node.n);
        
        // if(occ > 1)
        // {
        //     for (k = g_beg_idx; (k + 1) < pg->pg_het_node.n; ++k) 
        //     {
        //         t = &(pg->pg_het_node.a[k]); tLen = t->baseEnd + 1 - t->baseBeg;
        //         z = &(pg->pg_het_node.a[k+1]); zLen = z->baseEnd + 1 - z->baseBeg;

        //         ovlp = ((MIN(t->baseEnd, z->baseEnd) >= MAX(t->baseBeg, z->baseBeg))? 
        //                         MIN(t->baseEnd, z->baseEnd) - MAX(t->baseBeg, z->baseBeg) + 1 : 0);

        //         e = asg_arc_pushp(pg->pg_het);
        //         e->ol = ovlp;
        //         e->ul = (k<<1); e->ul <<= 32; e->ul += (tLen - ovlp);
        //         e->v = ((k+1)<<1); e->del = 0; e->el = e->no_l_indel = e->strong = 1;

        //         e = asg_arc_pushp(pg->pg_het);
        //         e->ol = ovlp;
        //         e->ul = ((k+1)<<1)+1; e->ul <<= 32; e->ul += (zLen - ovlp);
        //         e->v = (k<<1)+1; e->del = 0; e->el = e->no_l_indel = e->strong = 1;
        //     }
        // }
    }
    ///asg_cleanup(pg->pg_het);
    ///debug_p_g_t(pg, cov, read_g);
    ///print_p_g_t_interval(pg, cov);

    return pg;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x1, %edi
movl $0x48, %esi
callq 0x2140
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
callq 0xcd150
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x40(%rax)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x48(%rbp), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x183854
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x20(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x1837d9
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x20(%rbp), %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x183806
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rdi
movl -0x20(%rbp), %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0xcd8f0
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rax
movl -0x20(%rbp), %ecx
movb $0x1, 0x4(%rax,%rcx,8)
jmp 0x183846
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rdi
movl -0x20(%rbp), %esi
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
callq 0xcd8f0
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x20(%rax), %rax
movl -0x20(%rbp), %ecx
movb $0x0, 0x4(%rax,%rcx,8)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x183790
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x48(%rbp), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x183b7d
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x20(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x1838a4
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x20(%rbp), %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x1838a9
jmp 0x183b6f
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, -0x34(%rbp)
movl $0x1, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x24(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0x10(%rcx), %eax
ja 0x183abc
movl -0x24(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0x10(%rcx), %eax
je 0x183956
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0x24(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
movzbl (%rax,%rcx), %eax
movq -0x10(%rbp), %rcx
movq 0xa8(%rcx), %rcx
movq 0x10(%rcx), %rcx
movq -0x50(%rbp), %rdx
movq 0x18(%rdx), %rdx
movl -0x28(%rbp), %esi
movq (%rdx,%rsi,8), %rdx
shrq $0x21, %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0x183a94
jmp 0x183958
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x1839be
movq -0x40(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x183986
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0x68(%rbp)
jmp 0x183991
movl $0x2, %eax
movq %rax, -0x68(%rbp)
jmp 0x183991
movq -0x68(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rax
imulq $0x1c, 0x10(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x40(%rbp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x8(%rdx)
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movl -0x20(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0x28(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
movzbl (%rax,%rcx), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x14(%rax)
movl -0x30(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rdx
movq 0x18(%rdx), %rdx
movl -0x24(%rbp), %esi
subl $0x1, %esi
movl %esi, %esi
movq (%rdx,%rsi,8), %rdx
shrq $0x21, %rdx
movl (%rax,%rdx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addl %eax, %ecx
subl $0x1, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x28(%rbp), %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movq -0x58(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x2c(%rbp), %eax
movq -0x50(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0x24(%rbp), %edx
subl $0x1, %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
addl %ecx, %eax
movl %eax, -0x30(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1838e6
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
subq %rcx, %rax
movl %eax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x40(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jne 0x183b36
movq -0x40(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x183afb
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rax
shlq %rax
movq %rax, -0x70(%rbp)
jmp 0x183b06
movl $0x2, %eax
movq %rax, -0x70(%rbp)
jmp 0x183b06
movq -0x70(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x40(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x40(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x40(%rbp), %rdx
movq 0x30(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x30(%rdx)
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl -0x34(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
movl -0x38(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x18385b
movq -0x40(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)