// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rdx, 0x60(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %r12
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rax, %rbx
movq %r12, (%rax)
movq 0x18(%r12), %r15
callq 0xa0485
movq %rbx, %r10
movq %rax, 0x28(%rbx)
testl $0x7fffffff, 0x14(%r15) # imm = 0x7FFFFFFF
je 0x117def
movq %rax, %r13
xorl %ebx, %ebx
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %ebp, %ebp
movq %r10, 0x10(%rsp)
movq %r15, 0x30(%rsp)
movq 0x20(%r15), %rax
cmpl $0x0, (%rax,%rbp,8)
js 0x117b08
cmpb $0x1, 0x4(%rax,%rbp,8)
jne 0x117b1e
movq %r13, %rdi
movl %ebp, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0xa0887
movb $0x1, %r15b
jmp 0x117b38
movq 0x10(%r12), %rax
movl (%rax,%rbx), %edx
andl %r14d, %edx
xorl %r15d, %r15d
movq %r13, %rdi
movl %ebp, %esi
xorl %ecx, %ecx
callq 0xa0887
movq 0x20(%r13), %rax
movb %r15b, 0x4(%rax,%rbp,8)
incq %rbp
movq 0x30(%rsp), %r15
movl 0x14(%r15), %ecx
movl %ecx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq $0x28, %rbx
cmpq %rax, %rbp
movq 0x10(%rsp), %r10
jb 0x117af7
testl %eax, %eax
je 0x117def
xorl %eax, %eax
xorl %edx, %edx
xorl %ebx, %ebx
movq $0x0, 0x40(%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x48(%rsp)
xorl %r8d, %r8d
xorl %r11d, %r11d
movq %r12, 0x20(%rsp)
movq 0x20(%r15), %rsi
cmpl $0x0, (%rsi,%r11,8)
js 0x117ddb
cmpb $0x1, 0x4(%rsi,%r11,8)
je 0x117ddb
movq 0x10(%r12), %rsi
leaq (%r11,%r11,4), %rdi
movl 0x10(%rsi,%rdi,8), %ecx
testl %ecx, %ecx
je 0x117d71
leaq (%rsi,%rdi,8), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x50(%rsp)
movl $0x1, %ebp
movq %r8, 0x58(%rsp)
movq %r8, %r14
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x38(%rsp), %r9
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rdi
movl %r12d, %r8d
cmpl %ecx, %ebp
je 0x117c48
movq 0xa8(%r9), %rdx
movq 0x10(%rdx), %rdx
movq 0x18(%rsp), %rsi
movq 0x18(%rsi), %rbx
movl %ebp, %esi
movq (%rbx,%rsi,8), %rsi
shrq $0x21, %rsi
movb (%rdx,%rsi), %sil
movq %r8, %r13
movq (%rbx,%r8,8), %r8
shrq $0x21, %r8
cmpb (%rdx,%r8), %sil
movq %r13, %r8
jne 0x117c48
leal -0x1(%rbp), %edx
jmp 0x117d3e
movq 0x48(%rsp), %rdx
cmpq %rdx, %r14
movl %r12d, 0x8(%rsp)
jne 0x117cab
leaq (%rdx,%rdx), %r13
testq %rdx, %rdx
movl $0x2, %eax
cmoveq %rax, %r13
movq %r13, 0x10(%r10)
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rsi
addq %r13, %rsi
movq %r11, %rbx
movq %r8, %r12
callq 0x2370
movq %r12, %r8
movq %rbx, %r11
movq 0x10(%rsp), %r10
movq %rax, 0x18(%r10)
movq 0x18(%rsp), %rdi
movl 0x10(%rdi), %ecx
movq %r13, 0x48(%rsp)
movq 0x38(%rsp), %r9
jmp 0x117cb0
movq 0x18(%rsp), %rdi
leaq 0x1(%r14), %r12
movq %r12, 0x8(%r10)
leaq (%r14,%r14,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %r14, %rsi
movl %r11d, 0x18(%rax,%rsi)
movq 0xa8(%r9), %rdx
movq 0x10(%rdx), %rdx
movq 0x18(%rdi), %rbx
movq (%rbx,%r8,8), %rdi
shrq $0x21, %rdi
movzbl (%rdx,%rdi), %edx
movl %edx, 0x14(%rax,%rsi)
movq 0x50(%rsp), %rdx
movl %edx, (%rax,%rsi)
movq 0x60(%rsp), %rdx
movq 0x20(%rdx), %rdi
leal -0x1(%rbp), %edx
movq (%rbx,%rdx,8), %r14
movq %r14, %r8
shrq $0x21, %r8
movl (%rdi,%r8,8), %edi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edi
addl %r15d, %edi
decl %edi
movl %edi, 0x4(%rax,%rsi)
movl 0x8(%rsp), %edi
movl %edi, 0x8(%rax,%rsi)
movl %edx, 0xc(%rax,%rsi)
addl %r15d, %r14d
movq %r14, 0x50(%rsp)
movq %rax, %rdi
movq %r12, 0x40(%rsp)
movq %r12, %r14
movl %ebp, %r12d
addl (%rbx,%rdx,8), %r15d
incl %ebp
cmpl %ecx, %ebp
jbe 0x117c03
movq %rdi, 0x28(%rsp)
movq %rax, 0x8(%rsp)
movq 0x30(%r10), %rbx
movq 0x38(%r10), %rdx
movq 0x40(%r10), %rax
movq 0x20(%rsp), %r12
movq 0x30(%rsp), %r15
movq 0x58(%rsp), %r8
cmpq %rdx, %rbx
jne 0x117db7
leaq (%rdx,%rdx), %r14
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %r14
movq %r14, 0x38(%r10)
leaq (,%r14,8), %rsi
movq %rax, %rdi
movq %r11, %rbp
movq %r8, %r12
callq 0x2370
movq %r12, %r8
movq 0x20(%rsp), %r12
movq %rbp, %r11
movq 0x10(%rsp), %r10
movq %rax, 0x40(%r10)
movq %r14, %rdx
movq 0x40(%rsp), %rdi
movl %edi, %ecx
subl %r8d, %ecx
leaq 0x1(%rbx), %rsi
movq %rsi, 0x30(%r10)
movl %r8d, (%rax,%rbx,8)
movl %ecx, 0x4(%rax,%rbx,8)
movl 0x14(%r15), %ecx
movq %rsi, %rbx
movq %rdi, %r8
incq %r11
movl %ecx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %rsi, %r11
jb 0x117b9f
movq %r10, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq