// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, 0x40(%rsp)
movq %rsi, (%rsp)
movq %rdi, %r12
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rax, %rbx
movq %r12, (%rax)
movq 0x18(%r12), %r15
callq 0x9fccc
movq %rbx, %r9
movq %rax, %r13
movq %rax, 0x28(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
xorl %eax, %eax
movq %rax, 0x18(%rbx)
movups %xmm0, 0x30(%rbx)
movq %rax, 0x40(%rbx)
testl $0x7fffffff, 0x14(%r15) # imm = 0x7FFFFFFF
movq %rbx, 0x8(%rsp)
movq %r15, 0x18(%rsp)
je 0x112065
xorl %ebx, %ebx
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %ebp, %ebp
movq 0x20(%r15), %rax
cmpl $0x0, (%rax,%rbp,8)
js 0x11200d
cmpb $0x1, 0x4(%rax,%rbp,8)
jne 0x112023
movq %r13, %rdi
movl %ebp, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0xa0115
movb $0x1, %r15b
jmp 0x11203d
movq 0x10(%r12), %rax
movl (%rax,%rbx), %edx
andl %r14d, %edx
xorl %r15d, %r15d
movq %r13, %rdi
movl %ebp, %esi
xorl %ecx, %ecx
callq 0xa0115
movq 0x20(%r13), %rax
movb %r15b, 0x4(%rax,%rbp,8)
incq %rbp
movq 0x18(%rsp), %r15
movl 0x14(%r15), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq $0x28, %rbx
cmpq %rax, %rbp
movq 0x8(%rsp), %r9
jb 0x111ffc
testl $0x7fffffff, 0x14(%r15) # imm = 0x7FFFFFFF
movq (%rsp), %r8
je 0x112291
movq %r9, %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
xorl %r10d, %r10d
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq %r12, 0x28(%rsp)
movq 0x20(%r15), %rax
cmpl $0x0, (%rax,%r10,8)
js 0x11227c
cmpb $0x1, 0x4(%rax,%r10,8)
je 0x11227c
movq 0x10(%r12), %rcx
leaq (%r10,%r10,4), %rdx
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movl 0x10(%rcx,%rdx,8), %eax
testl %eax, %eax
je 0x112211
leaq (%rcx,%rdx,8), %r11
xorl %ebx, %ebx
movl $0x1, %r15d
xorl %r14d, %r14d
xorl %esi, %esi
movl %esi, %r12d
cmpl %eax, %r15d
je 0x112115
movq 0xa8(%r8), %rax
movq 0x10(%rax), %rax
movq 0x18(%r11), %rcx
movl %r15d, %edx
movq (%rcx,%rdx,8), %rdx
shrq $0x21, %rdx
movb (%rax,%rdx), %dl
movq (%rcx,%r12,8), %rcx
shrq $0x21, %rcx
cmpb (%rax,%rcx), %dl
je 0x1121f5
movl %esi, 0x14(%rsp)
movq 0x8(%r9), %rbp
movq 0x10(%r9), %rax
cmpq %rax, %rbp
jne 0x112177
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%r9)
movq 0x18(%r9), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
movq %r10, %r13
movq %rbx, 0x38(%rsp)
movq %r11, %rbx
callq 0x2370
movq %rbx, %r11
movq 0x38(%rsp), %rbx
movq %r13, %r10
movl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq %rax, 0x18(%r9)
movq 0x18(%r9), %rax
leaq 0x1(%rbp), %rcx
movq %rcx, 0x8(%r9)
leaq (,%rbp,8), %rcx
addq %rbp, %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rbp, %rcx
movl %r10d, 0x18(%rax,%rcx)
movq 0xa8(%r8), %rdx
movq 0x10(%rdx), %rdx
movq 0x18(%r11), %rsi
movq (%rsi,%r12,8), %rdi
shrq $0x21, %rdi
movzbl (%rdx,%rdi), %edx
movl %edx, 0x14(%rax,%rcx)
movl %ebx, (%rax,%rcx)
movq 0x40(%rsp), %rdx
movq 0x20(%rdx), %rdx
leal -0x1(%r15), %edi
movq (%rsi,%rdi,8), %rbx
movq %rbx, %rsi
shrq $0x21, %rsi
movl (%rdx,%rsi,8), %edx
andl %r13d, %edx
addl %r14d, %edx
decl %edx
movl %edx, 0x4(%rax,%rcx)
movl 0x14(%rsp), %edx
movl %edx, 0x8(%rax,%rcx)
movl %edi, 0xc(%rax,%rcx)
addl %r14d, %ebx
movl %r15d, %esi
movq 0x18(%r11), %rax
leal -0x1(%r15), %ecx
addl (%rax,%rcx,8), %r14d
incl %r15d
movl 0x10(%r11), %eax
cmpl %eax, %r15d
jbe 0x1120df
movl 0x8(%r9), %ebx
movq 0x30(%r9), %r14
movq 0x38(%r9), %rax
cmpq %rax, %r14
jne 0x112256
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x38(%r9)
movq 0x40(%r9), %rdi
shlq $0x3, %rsi
movq %r10, %r15
callq 0x2370
movq %r15, %r10
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq %rax, 0x40(%r9)
movq 0x30(%rsp), %rdx
subl %edx, %ebx
movq 0x40(%r9), %rax
leaq 0x1(%r14), %rcx
movq %rcx, 0x30(%r9)
movl %edx, (%rax,%r14,8)
movl %ebx, 0x4(%rax,%r14,8)
movq 0x28(%rsp), %r12
movq 0x18(%rsp), %r15
incq %r10
movl 0x14(%r15), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r10
jb 0x112091
movq %r9, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq