// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/horder.cpp
// Function: insert_sc(osg_t*, lay_t*, unsigned int)
void insert_sc(osg_t *g, lay_t *p, uint32_t uid)
{
    uint32_t i, b, e, vb, ve, max_i = (uint32_t)-1, is_found, ori, max_ori = (uint32_t)-1;
    osg_arc_t *bE = NULL, *eE = NULL;
    double w[2], s_w, max_w = -1;
    for (i = 1; i+1 < p->n; i++)///middle points
    {
        b = p->a[i]; e = p->a[i+1]; w[0] = w[1] = 0; 
        s_w = 0; ori = 0; is_found = 0;

        vb = (uid<<1); ve = (uid<<1)+1;
        bE = get_osg_arc(g, vb, b);
        eE = get_osg_arc(g, ve, e);
        if(bE || eE) ///different with slsa2
        {
            if(bE) w[0] += bE->nw;
            if(eE) w[0] += eE->nw;
            is_found++;
        }


        vb = (uid<<1)+1; ve = (uid<<1);
        bE = get_osg_arc(g, vb, b);
        eE = get_osg_arc(g, ve, e);
        if(bE || eE) ///different with slsa2
        {
            if(bE) w[1] += bE->nw;
            if(eE) w[1] += eE->nw;
            is_found++;
        }

        if(is_found > 0)
        {
            s_w = MAX(w[0], w[1]);
            ori = ((w[0] >= w[1])? 0 : 1);
            
            if(max_i == (uint32_t)-1 || max_w < s_w)
            {
                max_w = s_w;
                max_i = i;
                max_ori = ori;
            }
        }
    }


    ///beg point, ///different with slsa2
    i = 0;
    b = (uint32_t)-1; e = p->a[0]; w[0] = w[1] = 0; 
    s_w = 0; ori = 0; is_found = 0;

    vb = (uint32_t)-1; ve = (uid<<1)+1;
    bE = NULL;
    eE = get_osg_arc(g, ve, e);
    if(bE || eE) ///different with slsa2
    {
        if(bE) w[0] += bE->nw;
        if(eE) w[0] += eE->nw;
        is_found++;
    }

    vb = (uint32_t)-1; ve = (uid<<1);
    bE = NULL;
    eE = get_osg_arc(g, ve, e);
    if(bE || eE) ///different with slsa2
    {
        if(bE) w[1] += bE->nw;
        if(eE) w[1] += eE->nw;
        is_found++;
    }

    if(is_found > 0)
    {
        s_w = MAX(w[0], w[1]);
        ori = ((w[0] >= w[1])? 0 : 1);
        
        if(max_i == (uint32_t)-1 || max_w < s_w)
        {
            max_w = s_w;
            max_i = i;
            max_ori = ori;
        }
    }

    ///end point, ///different with slsa2
    i = p->n - 1;
    b = p->a[p->n - 1]; e = (uint32_t)-1; w[0] = w[1] = 0; 
    s_w = 0; ori = 0; is_found = 0;

    vb = (uid<<1); ve = (uint32_t)-1;
    bE = get_osg_arc(g, vb, b);
    eE = NULL;
    if(bE || eE) ///different with slsa2
    {
        if(bE) w[0] += bE->nw;
        if(eE) w[0] += eE->nw;
        is_found++;
    }

    vb = (uid<<1)+1; ve = (uint32_t)-1;
    bE = get_osg_arc(g, vb, b);
    eE = NULL;
    if(bE || eE) ///different with slsa2
    {
        if(bE) w[1] += bE->nw;
        if(eE) w[1] += eE->nw;
        is_found++;
    }

    if(is_found > 0)
    {
        s_w = MAX(w[0], w[1]);
        ori = ((w[0] >= w[1])? 0 : 1);
        
        if(max_i == (uint32_t)-1 || max_w < s_w)
        {
            max_w = s_w;
            max_i = i;
            max_ori = ori;
        }
    }

    if(max_i != 0 && max_i != (uint32_t)-1) max_i++;

    i = p->n;
    kv_resize(uint32_t, *p, p->n+2);
    p->n += 2; 
    while (i > max_i)
    {
        i--;
        p->a[i+2] = p->a[i];
    }
    
    p->a[max_i] = (uid<<1) + max_ori;
    p->a[max_i+1] = (uid<<1) + 1 - max_ori;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, 0x18(%rsp)
movq (%rsi), %r13
movq %rsi, 0x48(%rsp)
movq 0x8(%rsi), %rbp
leal 0x1(,%rdx,2), %eax
movl %eax, 0x10(%rsp)
movl %edx, %eax
addl %edx, %eax
movl %eax, 0xc(%rsp)
movsd 0xaa0c6(%rip), %xmm0    # 0x1e3580
movsd %xmm0, 0x30(%rsp)
pushq $-0x1
popq %rax
pushq $0x1
popq %rbx
movq %rax, 0x38(%rsp)
movl %eax, 0x14(%rsp)
movq %r13, 0x40(%rsp)
movq %rbp, 0x50(%rsp)
movl %ebx, %r12d
leal 0x1(%r12), %ebx
cmpq %rbx, %rbp
jbe 0x139620
movl %r12d, %eax
movl (%r13,%rax,4), %r15d
movl (%r13,%rbx,4), %r13d
movq 0x18(%rsp), %r14
movq %r14, %rdi
movl 0xc(%rsp), %esi
movl %r15d, %edx
callq 0x13944f
movq %rax, %rbp
movq %r14, %rdi
movl 0x10(%rsp), %esi
movl %r13d, %edx
callq 0x13944f
movq %rbp, %rcx
orq %rax, %rcx
sete 0xb(%rsp)
xorpd %xmm0, %xmm0
je 0x13954a
testq %rbp, %rbp
je 0x139540
movsd 0x18(%rbp), %xmm0
addsd 0xaa028(%rip), %xmm0    # 0x1e3568
testq %rax, %rax
je 0x13954a
addsd 0x18(%rax), %xmm0
movapd %xmm0, 0x20(%rsp)
movq 0x18(%rsp), %r14
movq %r14, %rdi
movl 0x10(%rsp), %esi
movl %r15d, %edx
callq 0x13944f
movq %rax, %r15
movq %r14, %rdi
movl 0xc(%rsp), %esi
movl %r13d, %edx
callq 0x13944f
movq %r15, %rcx
orq %rax, %rcx
je 0x139598
testq %r15, %r15
movq 0x50(%rsp), %rbp
je 0x1395b9
movsd 0x18(%r15), %xmm0
addsd 0xa9fd2(%rip), %xmm0    # 0x1e3568
jmp 0x1395bd
xorpd %xmm0, %xmm0
cmpb $0x0, 0xb(%rsp)
movq 0x40(%rsp), %r13
movq 0x50(%rsp), %rbp
movapd 0x20(%rsp), %xmm3
jne 0x1394d9
jmp 0x1395d2
xorpd %xmm0, %xmm0
movq 0x40(%rsp), %r13
movapd 0x20(%rsp), %xmm3
testq %rax, %rax
je 0x1395d2
addsd 0x18(%rax), %xmm0
movapd %xmm0, %xmm1
cmpnlesd %xmm3, %xmm1
movapd %xmm1, %xmm2
andpd %xmm0, %xmm2
andnpd %xmm3, %xmm1
orpd %xmm2, %xmm1
cmpl $-0x1, 0x38(%rsp)
je 0x1395fe
ucomisd 0x30(%rsp), %xmm1
jbe 0x1394d9
xorl %eax, %eax
ucomisd %xmm0, %xmm3
setb %al
movl %eax, 0x14(%rsp)
leal 0x1(%r12), %ebx
movq %r12, 0x38(%rsp)
movsd %xmm1, 0x30(%rsp)
jmp 0x1394d9
movl (%r13), %ebx
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
movl %ebx, %edx
callq 0x13944f
movq %rax, %r15
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0x139646
addsd 0x18(%r15), %xmm0
movapd %xmm0, 0x20(%rsp)
movl 0x14(%rsp), %r14d
movq 0x38(%rsp), %r12
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
callq 0x13944f
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0x13967c
addsd 0x18(%rax), %xmm0
movapd 0x20(%rsp), %xmm3
jmp 0x139687
testq %r15, %r15
movapd 0x20(%rsp), %xmm3
je 0x1396c2
movapd %xmm0, %xmm1
cmpnlesd %xmm3, %xmm1
movapd %xmm1, %xmm2
andpd %xmm0, %xmm2
andnpd %xmm3, %xmm1
orpd %xmm2, %xmm1
cmpl $-0x1, %r12d
je 0x1396ae
ucomisd 0x30(%rsp), %xmm1
jbe 0x1396c2
xorl %r14d, %r14d
ucomisd %xmm0, %xmm3
setb %r14b
xorl %r12d, %r12d
movsd %xmm1, 0x30(%rsp)
movl -0x4(%r13,%rbp,4), %ebx
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
callq 0x13944f
movq %rax, %r15
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0x1396e9
addsd 0x18(%r15), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
movl %ebx, %edx
callq 0x13944f
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0x13971a
addsd 0x18(%rax), %xmm0
movq 0x48(%rsp), %rbx
movsd 0x20(%rsp), %xmm1
jmp 0x13972a
testq %r15, %r15
movq 0x48(%rsp), %rbx
movsd 0x20(%rsp), %xmm1
je 0x139759
leaq -0x1(%rbp), %rax
cmpl $-0x1, %r12d
sete %dl
xorl %ecx, %ecx
ucomisd %xmm0, %xmm1
setb %cl
jb 0x139744
movapd %xmm1, %xmm0
ucomisd 0x30(%rsp), %xmm0
seta %sil
orb %sil, %dl
cmovnel %eax, %r12d
cmovnel %ecx, %r14d
leal -0x1(%r12), %eax
cmpl $-0x2, %eax
adcl $0x0, %r12d
leaq 0x2(%rbp), %rax
cmpq %rax, 0x10(%rbx)
jae 0x1397ca
leaq 0x1(%rbp), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x10(%rbx)
leaq 0x4(,%rcx,4), %rsi
movq %r13, %rdi
callq 0x2370
movq %rax, %r13
movq %rax, (%rbx)
movq 0x8(%rbx), %rax
addq $0x2, %rax
movq %rax, 0x8(%rbx)
movl %r12d, %eax
cmpq %rax, %rbp
jbe 0x1397eb
leaq -0x1(%rbp), %rcx
movl -0x4(%r13,%rbp,4), %edx
incl %ebp
movl %edx, (%r13,%rbp,4)
movq %rcx, %rbp
jmp 0x1397d1
movl 0xc(%rsp), %ecx
addl %r14d, %ecx
movl %ecx, (%r13,%rax,4)
movl 0x10(%rsp), %eax
subl %r14d, %eax
incl %r12d
movl %eax, (%r13,%r12,4)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq