// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rdx, 0x50(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
pushq $0x1
popq %rdi
pushq $0x48
popq %rsi
callq 0x2120
movq %rax, %r14
movq %rbx, (%rax)
movq %rbx, 0x38(%rsp)
movq 0x18(%rbx), %r15
callq 0x68f5d
movq %r14, %r9
movq %rax, %r13
movq %rax, 0x28(%r14)
xorl %ebx, %ebx
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %ebp, %ebp
movq %r9, 0x18(%rsp)
movq %r15, 0x8(%rsp)
movl 0x14(%r15), %eax
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbp
jae 0xd291f
movq 0x20(%r15), %rax
cmpl $0x0, (%rax,%rbp,8)
js 0xd28d1
cmpb $0x1, 0x4(%rax,%rbp,8)
jne 0xd28e5
movq %r13, %rdi
movl %ebp, %esi
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x69316
movb $0x1, %r15b
jmp 0xd2903
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax,%rbx), %edx
andl %r14d, %edx
xorl %r15d, %r15d
movq %r13, %rdi
movl %ebp, %esi
xorl %ecx, %ecx
callq 0x69316
movq 0x20(%r13), %rax
movb %r15b, 0x4(%rax,%rbp,8)
incq %rbp
addq $0x28, %rbx
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r15
jmp 0xd28af
xorl %edi, %edi
xorl %r13d, %r13d
movq $0x0, 0x40(%rsp)
xorl %r11d, %r11d
xorl %edx, %edx
xorl %r10d, %r10d
movq 0x10(%rsp), %r8
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r10
jae 0xd2b2f
movq 0x20(%r15), %rcx
cmpl $0x0, (%rcx,%r10,8)
js 0xd2b27
cmpb $0x1, 0x4(%rcx,%r10,8)
je 0xd2b27
imulq $0x28, %r10, %rcx
movq 0x38(%rsp), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x20(%rsp)
xorl %r12d, %r12d
movq %rdx, 0x30(%rsp)
movq %rdx, %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
pushq $0x1
popq %rax
movl %eax, %ebp
movq 0x20(%rsp), %rax
cmpl 0x10(%rax), %ebp
ja 0xd2ab7
movq %rbx, 0x48(%rsp)
movl %r12d, %ebx
je 0xd29e0
movq 0xa8(%r8), %rax
movq 0x10(%rax), %rcx
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movl %ebp, %edx
movq (%rax,%rdx,8), %rdx
shrq $0x21, %rdx
movb (%rcx,%rdx), %dl
movq (%rax,%rbx,8), %rsi
shrq $0x21, %rsi
cmpb (%rcx,%rsi), %dl
jne 0xd29e0
leal -0x1(%rbp), %ecx
movq 0x48(%rsp), %rbx
jmp 0xd2aad
movl %r12d, 0x2c(%rsp)
cmpq %r11, %r15
jne 0xd2a24
leaq (%r11,%r11), %r12
testq %r11, %r11
pushq $0x2
popq %rax
cmoveq %rax, %r12
movq %r12, 0x10(%r9)
imulq $0x1c, %r12, %rsi
movq 0x40(%rsp), %rdi
movq %r10, %r13
callq 0x2370
movq %r13, %r10
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r8
movq %rax, %r13
movq %rax, 0x18(%r9)
movq %r12, %r11
leaq 0x1(%r15), %r12
movq %r12, 0x8(%r9)
imulq $0x1c, %r15, %rdx
movl %r10d, 0x18(%r13,%rdx)
movq 0xa8(%r8), %rax
movq 0x10(%rax), %rcx
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%rbx,8), %rsi
shrq $0x21, %rsi
movzbl (%rcx,%rsi), %ecx
movl %ecx, 0x14(%r13,%rdx)
movl %r14d, (%r13,%rdx)
movq 0x50(%rsp), %rcx
movq 0x20(%rcx), %rsi
leal -0x1(%rbp), %ecx
movq (%rax,%rcx,8), %r14
movq %r14, %rdi
shrq $0x21, %rdi
movl (%rsi,%rdi,8), %esi
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %esi
movq %r12, %rdi
movq 0x48(%rsp), %rbx
addl %ebx, %esi
decl %esi
movl %esi, 0x4(%r13,%rdx)
movl 0x2c(%rsp), %esi
movl %esi, 0x8(%r13,%rdx)
movl %ecx, 0xc(%r13,%rdx)
addl %ebx, %r14d
movq %r13, 0x40(%rsp)
movq %r12, %r15
movl %ebp, %r12d
addl (%rax,%rcx,8), %ebx
incl %ebp
jmp 0xd298d
movq 0x30(%r9), %rbx
movq 0x40(%r9), %rax
cmpq 0x38(%r9), %rbx
jne 0xd2b03
leaq (%rbx,%rbx), %rsi
testq %rbx, %rbx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x38(%r9)
shlq $0x3, %rsi
movq %rdi, %rbp
movq %rax, %rdi
movq %r10, %r14
movq %r11, %r15
callq 0x2370
movq %rbp, %rdi
movq %r15, %r11
movq %r14, %r10
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r8
movq %rax, 0x40(%r9)
movq 0x8(%rsp), %r15
movq 0x30(%rsp), %rsi
movl %edi, %ecx
subl %esi, %ecx
leaq 0x1(%rbx), %rdx
movq %rdx, 0x30(%r9)
movl %esi, (%rax,%rbx,8)
movl %ecx, 0x4(%rax,%rbx,8)
movl 0x14(%r15), %eax
movq %rdi, %rdx
incq %r10
jmp 0xd293a
movq %r9, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq