// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/horder.cpp
// Function: update_scg(horder_t*, trans_col_t*)
void update_scg(horder_t *h, trans_col_t *t_idx)
{
    uint64_t i, k, l, p0s, p0e, p1s, p1e, span_s, span_e, suid, euid, v, w, slen, elen, sd, ed;
    uint64_t t_hits = 0, a_hits = 0;
    double max_div, we;
    h_w_t e; kv_init(e);
    dens_idx_t *idx = NULL;
    hw_aux_t *ep = NULL;
    ma_ug_t *ug = h->ug;
    kvec_pe_hit *hits = &(h->u_hits);
    osg_arc_t *p = NULL;
    osg_destroy(h->sg.g); 
    h->sg.g = osg_init();
    for (i = 0; i < ug->u.n; i++) {
        osg_seq_set(h->sg.g, i, 0);
        h->sg.g->seq[i].mw[0] = h->sg.g->seq[i].mw[1] = 0;
        h->sg.g->seq[i].ez[0] = ug->u.a[i].len>>1;
        h->sg.g->seq[i].ez[1] = ug->u.a[i].len - (ug->u.a[i].len>>1);
    }
    idx = build_interval_idx(hits, ug);///idx is used to get density
    
    for (i = 0, e.n = 0; i < hits->a.n; i++) {
        if(!hits->a.a[i].id) continue;//hom hits
        suid = get_hit_suid(*hits, i);
        euid = get_hit_euid(*hits, i);
        if(suid == euid) continue;//same unitig
        slen = ug->u.a[suid].len;
        elen = ug->u.a[euid].len;

        p0s = get_hit_spos(*hits, i);
        p0e = get_hit_spos_e(*hits, i);
        span_s = MIN(p0s, p0e);
        span_s = MIN(span_s, slen-1);
        span_e = MAX(p0s, p0e);
        span_e =  MIN(span_e, slen-1);
        span_s = ((span_s+span_e)>>1);
        sd = span_s;
        v = suid << 1;
        if(span_s > (slen>>1)) v++;


        p1s = get_hit_epos(*hits, i);
        p1e = get_hit_epos_e(*hits, i);
        span_s = MIN(p1s, p1e);
        span_s = MIN(span_s, elen-1);
        span_e = MAX(p1s, p1e);
        span_e =  MIN(span_e, elen-1);
        span_s = ((span_s+span_e)>>1);
        ed = span_s;
        w = euid << 1;
        if(span_s > (elen>>1)) w++;
        t_hits++;
        
        kv_pushp(hw_aux_t, e, &ep);
        ep->w = 0;
        ep->e = (v<<32)|w; 
        ep->d = (sd<<32)|ed;

        if(v > w) {
            ep->e = (w<<32)|v;
            ep->d = (ed<<32)|sd;
        } 
        
        // div = h->sg.g->seq[v>>1].ez[v&1] + h->sg.g->seq[w>>1].ez[w&1];
        // max_div = MAX(max_div, div);
        a_hits++;
    }

    // fprintf(stderr, "sa-0-sa: occ-%lu\n", get_vw_hits_num(&e, 12, 690));
    // print_specfic_hic_hits(hits, 12, 690, ug);

    
    
    update_h_w(&e, idx, &max_div);

    // fprintf(stderr, "sa-1-sa: occ-%lu\n", get_vw_hits_num(&e, 676, 738));

    radix_sort_hw_e(e.a, e.a+e.n);
    for (k = 1, l = 0; k <= e.n; ++k) 
    {   
        if (k == e.n || e.a[k].e != e.a[l].e) 
        {
            for (i = 0; i < h->avoid.n; i++)
            {
                v = e.a[l].e;
                w = e.a[l].e<<32; w |= (e.a[l].e>>32);
                if(h->avoid.a[i] == v || h->avoid.a[i] == w)
                {
                    break;
                }
            }

            if(i >= h->avoid.n)
            {
                for (i = l, we = 0; i < k; i++)
                {
                    if(e.a[i].w == 0) fprintf(stderr, "ERROR-3\n");
                    we += (max_div/e.a[i].w);
                }
                
                p = osg_arc_pushp(h->sg.g);
                p->u = p->v = p->occ = p->del = p->w = p->nw = 0;
                p->u = e.a[l].e>>32; p->v = (uint32_t)e.a[l].e;
                p->occ = k - l; p->w = we;
                // if(div != 0) p->w = (double)(k - l)*(max_div/div);
                p = osg_arc_pushp(h->sg.g);
                p->u = p->v = p->occ = p->del = p->w = p->nw = 0;
                p->u = (uint32_t)e.a[l].e; p->v = e.a[l].e>>32;
                p->occ = k - l; p->w = we;
                

                h->sg.g->seq[e.a[l].e>>33].mw[(e.a[l].e>>32)&1] 
                                = MAX(h->sg.g->seq[e.a[l].e>>33].mw[(e.a[l].e>>32)&1], p->w);
                h->sg.g->seq[((uint32_t)e.a[l].e)>>1].mw[e.a[l].e&1]
                                = MAX(h->sg.g->seq[((uint32_t)e.a[l].e)>>1].mw[e.a[l].e&1], p->w);
            }

            l = k;
        }
    }

    // fprintf(stderr, "sa-2-sa: occ-%lu\n", get_vw_hits_num(&e, 676, 738));
    osg_cleanup(h->sg.g);
    double bestAlt;
    uint64_t eg_edges = 0;
    for (i = 0; i < h->sg.g->n_arc; i++)///all p->w should be >= 2
    {
        p = &(h->sg.g->arc[i]);
        bestAlt = MAX(h->sg.g->seq[p->u>>1].mw[p->u&1], h->sg.g->seq[p->v>>1].mw[p->v&1]);
        if(p->w >= bestAlt*0.95)///acutally should be p->w == bestAlt
        {
            bestAlt = get_max_weight(p->u, p->v, h->sg.g);
        }

        if(bestAlt == 0) bestAlt = 1;
        ///if there is just one edges between p->u and p->v, then bestAlt -> p->nw <= 1; this is wrong
        p->nw = p->w/bestAlt;
        if(p->nw > 1) eg_edges++;
    }

    fprintf(stderr, "[M::%s::] # Nodes: %u, # Edges: %u, # Best Edges: %lu, t_hits: %lu, a_hits: %lu\n", 
    __func__, h->sg.g->n_seq, h->sg.g->n_arc, eg_edges, t_hits, a_hits);

    /*******************************for debug************************************/
    /**
    for (i = 0; i < h->sg.g->n_arc; i++)
    {
        p = &(h->sg.g->arc[i]);
        fprintf(stderr, "u-stg%.6ul(%c)(div:%f)\tv-stg%.6ul(%c)(div:%f)\tocc:%u\tw:%f\tnw:%f\n", 
        (p->u>>1)+1, "+-"[p->u&1], h->sg.g->seq[p->u>>1].ez[p->u&1],
        (p->v>>1)+1, "+-"[p->v&1], h->sg.g->seq[p->v>>1].ez[p->v&1], p->occ, p->w, p->nw);
    }
    fprintf(stderr, "sbsbsbsb\n\n\n\n\n\n");
    **/
    
    // uint32_t u, nv, f;
    // osg_arc_t *av = NULL;
    // for (k = 0; k < h->sg.g->n_arc; k++)
    // {
    //     p = &(h->sg.g->arc[k]);
    //     u = p->u; v = p->v;
    //     f = 0;

    //     nv = asg_arc_n(h->sg.g, u);
    //     av = asg_arc_a(h->sg.g, u);
    //     for (i = 0; i < nv; i++)
    //     {
    //         if(av[i].v == v) continue;
    //         if(av[i].w > p->w) f = 1;
    //     }

    //     nv = asg_arc_n(h->sg.g, v);
    //     av = asg_arc_a(h->sg.g, v);
    //     for (i = 0; i < nv; i++)
    //     {
    //         if(av[i].v == u) continue;
    //         if(av[i].w > p->w) f = 1;
    //     }

    //     if(p->nw > 1 && f == 1) fprintf(stderr, "ERROR1\n");
    //     if(p->nw <= 1 && f == 0)
    //     {
    //         fprintf(stderr, "\nERROR2, nw-%f, w-%f, u-%u, v-%u\n", p->nw, p->w, p->u, p->v);
    //          nv = asg_arc_n(h->sg.g, u);
    //         av = asg_arc_a(h->sg.g, u);
    //         for (i = 0; i < nv; i++)
    //         {
    //             if(av[i].v == v) continue;
    //             fprintf(stderr, "+u-%u, v-%u, w-%f\n", av[i].u, av[i].v, av[i].w);
    //         }

    //         nv = asg_arc_n(h->sg.g, v);
    //         av = asg_arc_a(h->sg.g, v);
    //         for (i = 0; i < nv; i++)
    //         {
    //             if(av[i].v == u) continue;
    //             fprintf(stderr, "-u-%u, v-%u, w-%f\n", av[i].u, av[i].v, av[i].w);
    //         }
    //     } 
        
    // }
    /*******************************for debug************************************/
    kv_destroy(e); 
    free(idx->pos.a);
    free(idx->a);
    free(idx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %r15
movq 0xc8(%rdi), %r13
movq 0xd8(%rdi), %rdi
callq 0x13782f
callq 0x137824
movq %rax, %rdi
movq %rax, 0xd8(%r15)
xorl %r14d, %r14d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
xorl %ebx, %ebx
cmpq (%r13), %rbx
jae 0x138533
movl %ebx, %esi
xorl %edx, %edx
callq 0x13785d
movq 0xd8(%r15), %rdi
movq 0x18(%rdi), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%r14)
movq 0x10(%r13), %rcx
movl (%rcx,%r14), %edx
shrl %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
cvtsi2sd %edx, %xmm0
movsd %xmm0, 0x10(%rax,%r14)
movl (%rcx,%r14), %ecx
andl %ebp, %ecx
movl %ecx, %edx
shrl %edx
subl %edx, %ecx
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x18(%rax,%r14)
incq %rbx
addq $0x28, %r14
jmp 0x1384d6
leaq 0x70(%r15), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x137afa
movq %rax, 0x48(%rsp)
pushq $0x18
popq %r9
xorl %ebx, %ebx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
xorl %r11d, %r11d
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
movq $0x0, 0x50(%rsp)
xorl %r10d, %r10d
movq %r14, 0x58(%rsp)
movq %r13, 0x60(%rsp)
cmpq (%r14), %r10
jae 0x138789
movq 0x80(%r15), %rsi
cmpq $0x0, -0x8(%rsi,%r9)
je 0x13877d
movq -0x18(%rsi,%r9), %rdi
leaq (%rdi,%rdi), %r8
movb 0xb8(%r15), %dl
xorl %ecx, %ecx
subb %dl, %cl
shrq %cl, %r8
movq -0x10(%rsi,%r9), %rdx
leaq (%rdx,%rdx), %r12
shrq %cl, %r12
movq %r8, 0x20(%rsp)
cmpq %r12, %r8
je 0x13877d
movq 0xc0(%r15), %rcx
movq %rcx, %r8
andq %rdi, %r8
testq %rdi, %rdi
movq %r9, 0x10(%rsp)
movq %r10, 0x38(%rsp)
js 0x1385f3
movq (%rsi,%r9), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
leaq (%r8,%rdi), %r14
decq %r14
jmp 0x13860f
leaq 0x1(%r8), %rdi
movq (%rsi,%r9), %rsi
movq %rsi, %r9
shrq $0x20, %r9
subq %r9, %rdi
movl $0x0, %r14d
cmovaeq %rdi, %r14
movq 0x10(%r13), %rdi
imulq $0x28, 0x20(%rsp), %r9
movl (%rdi,%r9), %r9d
andq %rbp, %r9
cmpq %r14, %r8
movq %r14, %r10
cmovbq %r8, %r10
cmovaq %r8, %r14
leaq -0x1(%r9), %r8
cmpq %r8, %r10
cmovaeq %r8, %r10
cmpq %r8, %r14
cmovaeq %r8, %r14
addq %r10, %r14
shrq %r14
shrl %r9d
xorl %r8d, %r8d
cmpq %r9, %r14
seta 0xf(%rsp)
andq %rdx, %rcx
testq %rdx, %rdx
js 0x138667
movl %esi, %edx
leaq (%rcx,%rdx), %r13
decq %r13
jmp 0x13867a
leaq 0x1(%rcx), %rdx
movl %esi, %esi
subq %rsi, %rdx
movl $0x0, %r13d
cmovaeq %rdx, %r13
imulq $0x28, %r12, %rdx
movl (%rdi,%rdx), %edx
andq %rbp, %rdx
cmpq %r13, %rcx
movq %r13, %rsi
cmovbq %rcx, %rsi
cmovaq %rcx, %r13
leaq -0x1(%rdx), %rcx
cmpq %rcx, %rsi
cmovaeq %rcx, %rsi
cmpq %rcx, %r13
cmovaeq %rcx, %r13
addq %rsi, %r13
shrq %r13
shrl %edx
xorl %ebp, %ebp
cmpq %rdx, %r13
seta %sil
cmpq %r11, 0x18(%rsp)
jne 0x1386f3
movq %rbx, %rdi
leaq (%r11,%r11), %rbx
testq %r11, %r11
pushq $0x2
popq %rcx
cmoveq %rcx, %rbx
movb %sil, 0xe(%rsp)
imulq $0x18, %rbx, %rsi
movq %rax, 0x40(%rsp)
callq 0x2370
movb 0xe(%rsp), %sil
xorl %r8d, %r8d
movq %rbx, %r11
movq %rax, %rbx
movq 0x40(%rsp), %rax
movq 0x10(%rsp), %r9
movq 0x38(%rsp), %r10
movb 0xf(%rsp), %cl
movb %cl, %r8b
movq 0x20(%rsp), %rcx
leaq (%r8,%rcx,2), %rdx
movb %sil, %bpl
leaq (,%r12,2), %rsi
addq %rbp, %rsi
imulq $0x18, 0x18(%rsp), %rcx
andq $0x0, 0x10(%rbx,%rcx)
movq %rdx, %rdi
shlq $0x20, %rdi
orq %rsi, %rdi
movq %rdi, (%rbx,%rcx)
movq %r14, %rdi
shlq $0x20, %rdi
orq %r13, %rdi
movq %rdi, 0x8(%rbx,%rcx)
cmpq %rsi, %rdx
jbe 0x138761
addq %rbx, %rcx
shlq $0x20, %rsi
orq %rdx, %rsi
movq %rsi, (%rcx)
shlq $0x20, %r13
orq %r14, %r13
movq %r13, 0x8(%rcx)
incq %rax
incq 0x18(%rsp)
incq 0x50(%rsp)
movq 0x60(%rsp), %r13
movq 0x58(%rsp), %r14
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
incq %r10
addq $0x20, %r9
jmp 0x138576
movq %rax, 0x40(%rsp)
leaq 0x70(%rsp), %rdi
movq 0x18(%rsp), %r14
movq %r14, 0x8(%rdi)
movq %r11, 0x10(%rdi)
movq %rbx, (%rdi)
leaq 0x68(%rsp), %rdx
movq 0x48(%rsp), %rsi
callq 0x137def
imulq $0x18, %r14, %rsi
addq %rbx, %rsi
movq %rbx, %rdi
callq 0x12f6f8
leaq 0x10(%rbx), %rax
movq %rax, 0x38(%rsp)
pushq $0x1
popq %r13
xorl %r12d, %r12d
xorpd %xmm2, %xmm2
movq %rbx, 0x10(%rsp)
cmpq 0x18(%rsp), %r13
ja 0x13899c
je 0x1387fd
imulq $0x18, %r13, %rax
movq (%rbx,%rax), %rax
imulq $0x18, %r12, %rcx
cmpq (%rbx,%rcx), %rax
je 0x138994
movq (%r15), %rax
imulq $0x18, %r12, %rbp
leaq (%rbx,%rbp), %r14
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x138833
movq (%r14), %rdx
movq 0x10(%r15), %rsi
movq (%rsi,%rcx,8), %rsi
cmpq %rdx, %rsi
je 0x13882b
rolq $0x20, %rdx
incq %rcx
cmpq %rdx, %rsi
jne 0x13880a
movq %r13, %r12
jmp 0x138994
addq 0x38(%rsp), %rbp
xorpd %xmm3, %xmm3
movq %r12, %rbx
cmpq %r13, %rbx
jae 0x13889b
movsd (%rbp), %xmm0
ucomisd %xmm2, %xmm0
jne 0x138851
jnp 0x138868
movsd 0x68(%rsp), %xmm1
divsd %xmm0, %xmm1
addsd %xmm1, %xmm3
incq %rbx
addq $0x18, %rbp
jmp 0x13883f
movq 0x11a771(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xb06fa(%rip), %rdi     # 0x1e8f73
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movapd %xmm3, 0x20(%rsp)
callq 0x2410
movapd 0x20(%rsp), %xmm3
xorpd %xmm2, %xmm2
movsd (%rbp), %xmm0
jmp 0x138851
movq 0xd8(%r15), %rdi
movapd %xmm3, 0x20(%rsp)
callq 0x138b1d
andl $0x0, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq (%r14), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
movl %edx, 0x4(%rax)
movl %ecx, (%rax)
movl %r13d, %ebx
subl %r12d, %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movl %ebx, 0x8(%rax)
movaps 0x20(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
movq 0xd8(%r15), %rdi
callq 0x138b1d
andl $0x0, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq (%r14), %rcx
movl %ecx, 0x4(%rax)
shrq $0x20, %rcx
movl %ecx, (%rax)
movl %ebx, 0x8(%rax)
movapd 0x20(%rsp), %xmm2
movsd %xmm2, 0x10(%rax)
movq 0xd8(%r15), %rcx
movq 0x18(%rcx), %rdx
movq (%r14), %rcx
movq %rcx, %rsi
shrq $0x21, %rsi
imulq $0x28, %rsi, %rsi
addq %rdx, %rsi
movq %rcx, %rdi
shrq $0x20, %rdi
andl $0x1, %edi
movsd (%rsi,%rdi,8), %xmm0
movapd %xmm2, %xmm1
cmplesd %xmm0, %xmm1
andpd %xmm1, %xmm0
andnpd %xmm2, %xmm1
orpd %xmm0, %xmm1
movlpd %xmm1, (%rsi,%rdi,8)
movl %ecx, %esi
shrl %esi
imulq $0x28, %rsi, %rsi
addq %rdx, %rsi
andl $0x1, %ecx
movsd (%rsi,%rcx,8), %xmm0
movsd 0x10(%rax), %xmm1
movapd %xmm1, %xmm2
cmplesd %xmm0, %xmm2
andpd %xmm2, %xmm0
andnpd %xmm1, %xmm2
orpd %xmm0, %xmm2
movlpd %xmm2, (%rsi,%rcx,8)
xorpd %xmm2, %xmm2
movq %r13, %r12
movq 0x10(%rsp), %rbx
incq %r13
jmp 0x1387da
movq 0xd8(%r15), %rdi
callq 0x137a11
movq 0xd8(%r15), %r12
xorl %ebx, %ebx
movsd 0xaab2f(%rip), %xmm3    # 0x1e34e8
xorpd %xmm4, %xmm4
movsd 0xaa8db(%rip), %xmm5    # 0x1e32a0
xorl %r14d, %r14d
xorl %r15d, %r15d
movl 0x4(%r12), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
cmpq %r8, %r14
jae 0x138aac
movq 0x8(%r12), %r13
movq 0x18(%r12), %rax
movl (%r13,%rbx), %esi
movl 0x4(%r13,%rbx), %edi
movl %edi, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
addq %rax, %rcx
movl %edi, %edx
andl $0x1, %edx
movsd (%rcx,%rdx,8), %xmm1
movl %esi, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
addq %rax, %rcx
movl %esi, %eax
andl $0x1, %eax
movsd (%rcx,%rax,8), %xmm2
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
movsd 0x10(%r13,%rbx), %xmm6
movapd %xmm0, %xmm1
mulsd %xmm3, %xmm1
ucomisd %xmm1, %xmm6
jb 0x138a70
movq %r12, %rdx
movsd %xmm6, 0x20(%rsp)
callq 0x137a49
movsd 0x20(%rsp), %xmm6
movsd 0xaa83c(%rip), %xmm5    # 0x1e32a0
xorpd %xmm4, %xmm4
movsd 0xaaa78(%rip), %xmm3    # 0x1e34e8
movapd %xmm0, %xmm1
cmpeqsd %xmm4, %xmm1
movapd %xmm1, %xmm2
andpd %xmm5, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
divsd %xmm1, %xmm6
xorl %eax, %eax
ucomisd %xmm5, %xmm6
seta %al
movsd %xmm6, 0x18(%r13,%rbx)
addq %rax, %r15
incq %r14
addq $0x20, %rbx
jmp 0x1389cb
movq 0x11a52d(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl 0x14(%r12), %ecx
leaq 0xb6dd6(%rip), %rsi     # 0x1ef89d
leaq 0xb6e20(%rip), %rdx     # 0x1ef8ee
movq %r15, %r9
xorl %eax, %eax
pushq 0x50(%rsp)
pushq 0x48(%rsp)
callq 0x22b0
popq %rcx
popq %rdx
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x48(%rsp), %rbx
movq 0x10(%rbx), %rdi
callq 0x2220
movq 0x18(%rbx), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq